user-0 提问: 我不是伸手党。只是问一下这个程序的设计思路。

回复:
user: user-1
content: 引用 14 楼 u014444402 的回复:#include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> char numStr[][16] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; char operateStr[][2] = { "+", "-", "*", "/"}; char expssioin[100]; char result[100]; char buf[100]; char *pBuf = expssioin; int main() 	{ 	int len, i, operate = -1; 	int a = 0, b = 0, c = 0; 	gets( expssioin );  	while ( 1 ) 		{ 		sscanf( pBuf,"%s",buf);  //截取单个单词或者操作符 		if ( strstr( buf, "=" ) ) break;          for ( i=0; i<4; i++ )             if( !strcmp( buf, operateStr[i]) ) break;  		if ( i < 4 ) operate = i; 		else 			{ 			for( i=0; i<10; i++ ) 				if ( !strcmp( buf, numStr[i]) ) break; 			if( i >= 10 ) exit( 0 ); 			if( operate == -1 ) a = a * 10 + i; 			else b = b * 10 + i; 			}  		pBuf = strstr( pBuf, buf ); 		len = strlen( buf ); 		pBuf += len; 		if( pBuf >= expssioin + strlen( expssioin ) ) break; 		}  	printf( "你输入的表达式是：%d %s %d =\n", a, operateStr[operate],b );  	switch ( operate ) 	{ 	    case 0: c = a + b; break; 	    case 1: c = a - b;break; 	    case 2: c = a * b;break; 	    case 3: c = a / b; break; 	    default:exit( 0 ); 	}  	sprintf( result, "%s %d \n", expssioin, c ); 	len = strlen( result ); 	for( i=0; i<len; i++ )     {         if ( isdigit( result[i] )) printf( " %s", numStr[result[i]-0x30] );         else printf( "%c", result[i] );     }   	getchar();  	return 0; 	} 为毛用二维数组，数字表，和字符表，一位数组不行么，，

user: user-0
content: 还有，第一次玩这个论坛。。不知道怎么结贴。。。

user: user-0
content: 不好意思，最近比较忙就没有怎么关注。。谢了。。

user: user-2
content: 一个星期了,即不发表意见,也不结贴 这种姿态,全没有半点讨论学习的态度 应该进黑名单了,真后悔回答他的问题

user: user-3
content: #include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> char numStr[][16] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; char operateStr[][2] = { "+", "-", "*", "/"}; char expssioin[100]; char result[100]; char buf[100]; char *pBuf = expssioin; int main() 	{ 	int len, i, operate = -1; 	int a = 0, b = 0, c = 0; 	gets( expssioin );  	while ( 1 ) 		{ 		sscanf( pBuf,"%s",buf);  //截取单个单词或者操作符 		if ( strstr( buf, "=" ) ) break;          for ( i=0; i<4; i++ )             if( !strcmp( buf, operateStr[i]) ) break;  		if ( i < 4 ) operate = i; 		else 			{ 			for( i=0; i<10; i++ ) 				if ( !strcmp( buf, numStr[i]) ) break; 			if( i >= 10 ) exit( 0 ); 			if( operate == -1 ) a = a * 10 + i; 			else b = b * 10 + i; 			}  		pBuf = strstr( pBuf, buf ); 		len = strlen( buf ); 		pBuf += len; 		if( pBuf >= expssioin + strlen( expssioin ) ) break; 		}  	printf( "你输入的表达式是：%d %s %d =\n", a, operateStr[operate],b );  	switch ( operate ) 	{ 	    case 0: c = a + b; break; 	    case 1: c = a - b;break; 	    case 2: c = a * b;break; 	    case 3: c = a / b; break; 	    default:exit( 0 ); 	}  	sprintf( result, "%s %d \n", expssioin, c ); 	len = strlen( result ); 	for( i=0; i<len; i++ )     {         if ( isdigit( result[i] )) printf( " %s", numStr[result[i]-0x30] );         else printf( "%c", result[i] );     }   	getchar();  	return 0; 	}

user: user-2
content: 哥做了一个,不知道合心不  char  *NumToStr(int n); int    StrToNum(char *s); bool   GetExpression(int *a,int *b,char *type);  int main(int argc, char* argv[]) { 	 	int  A=0;     int  B=0; 	int  C=0; 	char TYPE=0;  	printf("\n请输入表达式:\n");  	if(GetExpression(&A,&B,&TYPE)) 	{ 		printf("\n你要计算的内容为:\n%d %c %d",A,TYPE,B); 		switch(TYPE) 		{ 		case '+': 			C=A+B; 			break; 		case '-': 			C=A-B; 			break; 		case '*': 			C=A*B; 			break; 		case '/': 			if(B==0) C=0; 			else C=A+B; 			break; 		default: 			C=0; 			break; 		} 		printf(" = %d\n",C); 		printf("\n英文表达式为:\n"); 		printf(NumToStr(A)); 		printf(" %c ",TYPE); 		printf(NumToStr(B)); 		printf(" = "); 		printf(NumToStr(C)); 		printf("\n\n"); 	} 	else 	{ 		printf("\n错误的表达式!\n",A,B,C); 	} 	return 0; }  bool  GetExpression(int *a,int *b,char *type) { 	char buf[128]={0}; 	int  aa=0,bb=0,ta=0,tb=0,sum=0; 	char ch=0;  	char *pa1=NULL; 	char *pa2=NULL; 	char *pb1=NULL; 	char *pb2=NULL; 	char *p=NULL;  	*a=0; 	*b=0; 	*type=0; 	if(gets(buf)==NULL)  		return false;  	p=buf; 	pa1=buf;     while(*p) 	{ 	   if((*p=='+')||(*p=='-')||(*p=='*')||(*p=='/')) 	   { 		   ch=*p; 		   *p=0x20; 		   while(*p==0x20)  		   { 			   *p=0; 			   p++; 		   } 		   pb1=p; 	   } 	   if(*p=='=') *p=0; 	   p++;	    	} 	p=pa1; 	while(*p) 	{ 	   if(*p==0x20) 	   { 			while(*p==0x20)  			{ 				*p='\0'; 				p++; 			} 			if(*p>0)  			{ 				pa2=p; 				break; 			} 	   } 	   p++; 	}  	if(pb1) 	{ 		p=pb1; 		while(*p) 		{ 		   if(*p==0x20) 		   { 				while(*p==0x20)  				{ 					*p='\0'; 					p++; 				} 				if(*p>0)  				{ 					pb2=p; 					break; 				} 		   } 		   p++; 		} 	} 	aa=StrToNum(pa1); 	ta=StrToNum(pa2); 	bb=StrToNum(pb1); 	tb=StrToNum(pb2);      if(ta>10||tb>10)  		return false; 	if(aa<20&&ta>0) 		return false; 	if(bb<20&&tb>0)  		return false;  	aa+=ta; 	bb+=tb; 	if(aa==0||bb==0||ch==0) 		return false;  	*a=aa; 	*b=bb; 	*type=ch; 	return true; }  int  StrToNum(char *s) { 	char *txt[28]={"zero","one","two","three","four","five","six","seven","eight","nine","ten", 				   "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen", 	               "twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; 	int  i; 	char *p1,*p2; 	if(NULL==s)  		return 0; 	for(i=1;i<28;i++) 	{ 		p1=s; 		p2=txt[i]; 		while(1) 		{    			if(*p1==0x20) 		        while(*p1==0x20) p1++; 			if(*p1!=*p2)  				break; 		    if(*p1=='\0')  			{ 				if(i>=20) 					return (i-18)*10; 				else 					return i; 			} 			p1++; 			p2++; 		} 	} 	return 0; }  char *NumToStr(int  n) { 	char   buf[256]={0}; 	char   *p; 	int    num,temp; 	bool   hlay=false; 	char   *txt[28]={"zero","one","two","three","four","five","six","seven","eight","nine","ten", 				   "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen", 	               "twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; 	 	p=buf; 	num=n;     if(num<0)  	{ 		*p='-'; 		*(p+1)=0x20; 		p+=2; 		num*=-1; 	} 	temp=num/1000; 	if(temp>0) 	{ 		sprintf(p,"%s thousand",txt[temp]); 		hlay=true; 		p=buf; 		while(*p) p++; 	} 	num%=1000; 	temp=num/100; 	if(temp>0) 	{ 		if(hlay) sprintf(p," and %s hundred",txt[temp]); 		else sprintf(p,"%s hundred",txt[temp]); 		hlay=true; 		p=buf; 		while(*p) p++; 	} 	num%=100; 	if(num>=20) 	{ 		temp=num/10+18; 		num%=10; 		if(num>0) 		{ 			if(hlay) sprintf(p," and %s %s",txt[temp],txt[num]); 			else sprintf(p,"%s %s",txt[temp],txt[num]); 		} 		else 		{ 			if(hlay) sprintf(p," and %s",txt[temp]); 			else sprintf(p,"%s",txt[temp]); 		} 	} 	else if(num>0) 	{ 		if(hlay) sprintf(p," and %s",txt[num]); 		else sprintf(p,"%s",txt[num]); 	}      return (char*)buf; }

user: user-4
content: 字符串处理呗

user: user-5
content: 引用 10 楼 gz_qmc 的回复:于是,问题就变成一个数据怎么转换成字串了 char *NumToStr(int  n) {        char   buf[256]=" ";        char   *s1[20]={"zero","one","two","three","four","five","six","seven","eight","nine", 		               "ten","11","12","13","14","15","16","17","18","nineteen"};        char   *s2[10]={"zero","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};        char   *s3="hundred";        char   *s4="thousand";        if(n<0)             return   "操你妈,坑爹啊!";        if(n<20)           sprintf(buf,"%s",s1[n]);        else if(n<100)           sprintf(buf,"%s %s",s2[n/10],s1[n%10]);        else if(n<1000)           sprintf(buf,"%s hundred and %s %s",s1[1+n/100],s2[(n%100)/10],s1[(n%100)%10]); 	   else 		  sprintf(buf,"自己想办法");        return buf; }输入不是英文吗？好吧，你这个是输出。。。

user: user-2
content: 于是,问题就变成一个数据怎么转换成字串了 char *NumToStr(int  n) {        char   buf[256]=" ";        char   *s1[20]={"zero","one","two","three","four","five","six","seven","eight","nine", 		               "ten","11","12","13","14","15","16","17","18","nineteen"};        char   *s2[10]={"zero","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};        char   *s3="hundred";        char   *s4="thousand";        if(n<0)             return   "操你妈,坑爹啊!";        if(n<20)           sprintf(buf,"%s",s1[n]);        else if(n<100)           sprintf(buf,"%s %s",s2[n/10],s1[n%10]);        else if(n<1000)           sprintf(buf,"%s hundred and %s %s",s1[1+n/100],s2[(n%100)/10],s1[(n%100)%10]); 	   else 		  sprintf(buf,"自己想办法");        return buf; }

user: user-2
content: 定义一个函数将数据转换成字串  char *NumToStr(int  n);  输出的时候  int  A=100; int  B=78;  printf("%s+%s=%s",NumToStr(A),NumToStr(B),NumToStr(A+B));

user: user-6
content: 建立一个数字和词素的一一映射即可！ three four + five six =它和实际的表达式34+5=只相差一个映射！，1楼，6楼结合使用就很不错的解决方案

user: user-0
content: 谢了，楼上的各位，很有帮助

user: user-5
content: 仅供参考 /*  * zero one two three four five six seven eight nine ten  */ int str_to_num(char *str) { 	switch(str[0]) 	{ 		case 'z':return 0; 		case 'o':return 1; 		case 't': 			switch(str[1]) 			{ 				case 'w':return 2; 				case 'h':return 3; 			} 		case 'f': 			switch(str[1]) 			{ 				case 'o':return 4; 				case 'i':return 5; 			} 		case 's': 			switch(str[1]) 			{ 				case 'i':return 6; 				case 'e':return 7; 			} 		case 'e':return 8; 		case 'n':return 9; 	} }

user: user-7
content: 总的流程肯定是  ①把单词转换为整数 ②整数加和 ③把和转换为单词  而①和③怎么做，就有很多办法了 推荐1楼说的查表

user: user-8
content: 用map做字典，查询之！定义 map<string, int> 然后输入查询到对应的数值，相加即可！

user: user-9
content: 按照你的说法.90不要写成ninty而是写成nine zero。这样很好处理啊。 不管怎么样，反正加起来是0--9的数字，像楼上说的，先计算出真实的结果，再根据那个表“翻译”过来

user: user-10
content: char [][16] = { “zero", "one", "two", ..., "nine" }; 忘了0了

user: user-10
content: char [][16] = { "one", "two", ... } 把输入的字符串按单词拆分，然后查这个表