View Code of Problem 16

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

int main(){
	int flag;
	int n;
	int sum;
	char temp[10][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
	char s[100];
	int t[100];
	scanf("%d",&n);
	int a=0;
	int b=0;
	int count;
	while(n--){
		scanf("%d",&flag);
		a=0;
		b=0;
		count=0;
		while(scanf("%s",&s)!=EOF&&s[0]!='='){
			if(s[0]=='+'){
				count=1;
			}
			if(s[0]>='1'&&s[0]<='9'){
				for(int i=0;i<strlen(s);i++){
					if(count==0){
						a=(a*10)+(s[i]-'0');
					}else{
						b=(b*10)+(s[i]-'0');
					}
				}
			}else{
				for(int i=0;i<10;i++){
					if(strcmp(s,temp[i])==0){
						if(count==0){
							a=a*10+i;
							break;
						}else{
							b=b*10+i;
							break;
						}
					}
				}
			}
			
		}
		if(flag==0){
				printf("%d\n",a+b);
			}else{
				int pos=0;
				sum=a+b;
				while(sum>9){
					t[pos]=sum%10;
					pos++;
					sum/=10;
				}
				t[pos]=sum;
				for(int i=pos;i>0;i--){
					printf("%s ",temp[t[i]]);
				}
				printf("%s\n",temp[t[0]]);
			}
	}
}
				

Double click to view unformatted code.


Back to problem 16