要求:
https://www.dotcpp.com/oj/problem1073.html
这一题深深受到了C的伤害!!!!
正则简直就是为这种题而生的!!!but,python都快忘光了。。
手动造轮子可还行。。
两点说明:
1、C++对于识别是否读到文件末尾(EOF)其实很方便,while(cin>>str[signal])即可~~
2、几个stop节点,要注意算c的时候和前面两个有细微差别
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
string str[110];
int main(){
int signal = 0;
int ans = 0;
while(cin>>str[signal]){
int t = 1; //第二个数的符号记录
int a = 0,b = 0,c = 0;
int stop1 = 0,stop2 = 0; //分界点
for(int i = 0;i <str[signal].size();i++){
if(str[signal][i] == '+' || str[signal][i] == '-'){
for(int j = i-1;j>=0;j--){
a = a + pow(10.0,i-j-1)*(str[signal][j] - '0');
}
stop1 = i;
if(str[signal][i] == '-'){
t = -1; //正负号记录
}
//printf("%d ",a);
}
if(str[signal][i] == '='){
for(int j = i-1;j>=stop1 + 1;j--){
b = b + pow(10.0,i-j-1)*(str[signal][j] - '0');
}
stop2 = i;
b = b*t;
//printf("%d ",b);
}
if(i == str[signal].size() - 1){
if(str[signal][i] == '?'){
c = -1; //因为不可能为负数
//printf("%d ",c);
}
else{
for(int j = i;j>=stop2 + 1;j--){
c = c + pow(10.0,i-j)*(str[signal][j] - '0');
}
//printf("%d ",c);
}
}
}
if(a + b == c){
ans++;
}
signal++;
//cin>>str[signal];
}
printf("%d",ans);
}