解题思路:
直接将计算表达式当作字符串输入,读取出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 人评分
奖学金 (C++代码)浏览:2005 |
wu-淘淘的名单 (C++代码)浏览:1330 |
用筛法求之N内的素数。 (C语言代码)浏览:802 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:536 |
星期判断机 (C语言代码)浏览:858 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:398 |
模拟计算器 (C语言代码)浏览:2292 |
输入输出格式练习 (C语言代码)浏览:743 |
数列问题 (C语言代码)浏览:1019 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:800 |