思路:
- 读入字符串,分别求出aa和bb两个加数;
- 使用一个temp字符串,读取每一个英文单词,在number函数中,将temp转成相应的数字,返回值为该数字
- aa求法:在a[i] != '+' 之前的每一个英文单词都依次存入temp,转成数字后,"存入"aa;bb同理。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <math.h>
#include <string>
#include <stdlib.h>
using namespace std;
#define N 81
char num[10][6] = {{"zero"},{"one"},{"two"},{"three"},{"four"},{"five"},{"six"},{"seven"},{"eight"},{"nine"}};
int number(char temp[]){
for(int i = 0; i < 10; i++){
if (strcmp(temp,num[i])==0) return i;
}
return 0;
}
int main(){
string a;
while(getline(cin ,a)){
//cout<< a<<endl;
int aa= 0, bb = 0;
int i = 0, j = 0;
char temp[7],ch = '#';
//处理加数aa
while (a[i] != '+'){
if (a[i]!=' ')
temp[j++] = a[i++];
else
{
temp[j] = '\0'; //给temp字符串赋字符串结束符 便于匹配number数组
aa = aa*10+number(temp); //将temp转成的数字"存入"aa
memset(temp,'0',7); //将temp全部置'0',没有这一步也可以,但是第29行的'\0'一定要有(这是第31行)
j = 0; //j置0,便于字符存入temp
i++;
}
}
i+=2; //跳过 + 和空格
j = 0;
//处理加数bb 方法同处理aa
while (a[i] != '='){
if (a[i]!=' ')
temp[j++] = a[i++];
else
{
temp[j] = '\0';
bb = bb*10+number(temp);
memset(temp,'0',7);
j = 0;
i++;
}
}
if (!(aa== 0 && bb == 0))
cout <<aa+bb<<endl;
}
}

京公网安备 11010502036488号