H2230621088


私信TA

用户名:dotcpp0609001

访问量:129

签 名:

不想打螺丝啦

等  级
排  名 2143
经  验 2353
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校 Hezhou University
专  业

  自我简介:

TA的其他文章

最简单的做法
浏览:20

解题思路:
直接将计算表达式当作字符串输入,读取出a,b,c的值。判断即可。


在字符串中a的分界是+或者-;使用string中的find_first_of函数,找到第一个出现+、-的位置

string各个函数的用法,参考:string - C++ Reference (cplusplus.com)


找到了分隔位置,那么就需要分割字符串然后将字符串转换成整数

substr函数用来分割


我们自定义一个字符串转换成整型的函数,具体参考代码。

注意包含头文件#include <sstream>



参考代码:

#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string.h>
#include <sstream>

int toint(string str)
{
	int ret;
	stringstream stream(str);
	stream >> ret;
	return ret;
}

void test_1073()
{
	string str;
	int sum=0;
	while(cin>>str)
	{
		int a,b,c,pos1,pos2;
		pos1=str.find_first_of("+-");
		pos2=str.find_first_of("=");
		a=toint(str.substr(0,pos1));
		b=toint(str.substr(pos1+1,pos2));
		c=toint(str.substr(pos2+1));
		if(str[pos1]=='+')
		{
			if(a+b==c)
			{
				sum++;
			}
		}
		else
		{
			if(a-b==c)
			{
				sum++;
			}
		}
	}
	cout<<sum<<endl;
}


int main()
{

	
	test_1073();
	


	return 0;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区