View Code of Problem 16

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

int fun(char a[],char c[][10]){
	for(int i=0;i<10;i++){
		if(strcmp(a,c[i])==0){
			return i;
		}
	}
	return -1;
}

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

Double click to view unformatted code.


Back to problem 16