考点:字符串的分割
方法一:使用strtok(,)函数
#include<bits/stdc++.h>
using namespace std;
const int MAXSIZE=10000;
int s2i(char *p);
int add(char s[]);
int main() {
char s[100];
while(gets(s)) {
printf("%d\n",add(s));
}
return 0;
}
int s2i(char p[]) {
int r;
if(strcmp(p,"one")==0) {
r=1;
} else if(strcmp(p,"two")==0) {
r=2;
} else if(strcmp(p,"three")==0) {
r=3;
} else if(strcmp(p,"four")==0) {
r=4;
} else if(strcmp(p,"five")==0) {
r=5;
} else if(strcmp(p,"six")==0) {
r=6;
} else if(strcmp(p,"seven")==0) {
r=7;
} else if(strcmp(p,"eight")==0) {
r=8;
} else if(strcmp(p,"nine")==0) {
r=9;
} else {
r=0;
}
return r;
}
int add(char s[]) {
int ret,a=0,b=0,p1;
vector<char *> tmp;
char *p;
p=strtok(s," ");
while(p) {
if(*p=='+') {
p1=tmp.size();
}
tmp.push_back(p);
p=strtok(NULL," ");
}
int len=tmp.size();
for(int i=0; i<len; i++) {
if(i<p1) {
//printf("%s,%d\n",tmp[i],s2i(tmp[i]));
a=a*10+s2i(tmp[i]);
} else if(i<len-1&&i>p1) {
//printf("%s,%d\n",tmp[i],s2i(tmp[i]));
b=b*10+s2i(tmp[i]);
}
}
// printf("a=%d,b=%d\n",a,b);
ret=a+b;
return ret;
}