Samuel


私信TA

用户名:zhaohaibo

访问量:3617

签 名:

等  级
排  名 25202
经  验 548
参赛次数 0
文章发表 1
年  龄 0
在职情况 学生
学  校 华北理工大学
专  业

  自我简介:

TA的其他文章

解题思路:
c++ STL的string类包含了多种对字符串的处理方式。本题可用string.find()找到运算符号的位置,得到位置后用string.substr()划分数字与运算符。
注意事项:
string转换成int的方式有多种,C++中多用stringstream
参考代码:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int toInt(string s){
    int ret;
    stringstream stream(s);
    stream>>ret;
    return ret;
}
int main(){
    string str;
    int sum = 0;
    while(cin>>str){
        int a=0,b=0,c=0;
        string::size_type pos1,pos2;
        pos1 = str.find_first_of("+-");//找到加号或减号的位置
        pos2 = str.find('=');//找到等号的位置
        a = toInt(str.substr(0,pos1));
        b = toInt(str.substr(pos1,pos2));
        c = toInt(str.substr(pos2+1));//从位置pos2+1到最后
        if(a+b==c)
            sum += 1;
    }
    cout<<sum<<endl;
}


 

0.0分

38 人评分

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

编程语言转换

万能编程问答

代码解释器

  评论区

厉害了 感谢
2021-04-28 20:09:02
少了减的情况了
2020-02-19 23:48:24
  • «
  • 1
  • »