View Code of Problem 16

#include <bits/stdc++.h>
using namespace std;
string str[10]={"zero","one","two","three","four","five","six","seven","eight","nine"}; 
map<string,string>m={
{"zero","0"} ,
{"one","1"},
{"two","2"},
{"three","3"},
{"four","4"},
{"five","5"},
{"six","6"},
{"seven","7"},
{"eight","8"},
{"nine","9"}
};
int main(){
	int t;
	cin>>t;
	while(t--){
		int flag,k=1;
		cin>>flag;
		string s;
		getchar();
		getline(cin,s);
		int l=s.size();
		while(k){
			k=0;
			for(int i=0;i<10;i++){
				if(s.find(str[i])!=string::npos){
					int index=s.find(str[i]);
					s.replace(index,str[i].size(),m[str[i]]);
					k=1;
				}
			}
		}
		k=1;
		while(k){
			k=0;
			if(s.find(" ")!=string::npos){
				int index=s.find(" ");
				s.erase(index,1);
				k=1;
			}	
		}
		int index1=s.find("+");
		int index2=s.find("=");
		int a=stoi(s.substr(0,index1));
		int b=stoi(s.substr(index1+1,index2));
		if(flag==0) cout<<a+b<<endl;
		else{
			vector<int> v;
			int sum=a+b;
			while(sum){
				v.push_back(sum%10);
				sum/=10;
			}
			for(int i=v.size()-1;i>=0;i--){
				if(i!=v.size()-1) cout<<" "<<str[v[i]];
				else cout<<str[v[i]];
			}
			cout<<endl;
		}
	}
} 

Double click to view unformatted code.


Back to problem 16