解题思路:
直接将计算表达式当作字符串输入,读取出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语言程序设计教程(第三版)课后习题6.3 (Java代码)浏览:655 |
WU-格式化数据输出 (C++代码)浏览:1199 |
WU-格式化数据输出 (C语言代码)浏览:1748 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:507 |
1012题解浏览:866 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:510 |
母牛的故事 (C语言代码)浏览:562 |
数列问题 (C语言代码)浏览:1020 |
C语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:502 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:447 |