标签:style blog io color ar os 使用 for sp
描述
人们的日常习惯是把算术表达式写成中缀式,但对于机器来说更“习惯于”后缀式,关于算术表达式的中缀式和后缀式的论述一般的数据结构书都有相关内容可供参看,这里不再赘述,现在你的任务是将中缀式变为后缀式。
2 1.000+2/4= ((1+2)*5+1)/4=
1.000 2 4 / + = 1 2 + 5 * 1 + 4 / =
#include"stdio.h" #include"string.h" int youxianji(char a) { switch(a) { case ‘+‘: case ‘-‘:return 1;break; case ‘*‘: case ‘/‘:return 2;break; case ‘(‘:return 0;break; default:return -1;break; } } int main() { char caozuoshu[1000+10],houzui[1000+10],j[1000+10]; int i,k,n,t,top; scanf("%d",&n); while(n--) { scanf("%s",j); t=strlen(j)-1; //去掉=号 caozuoshu[0]=‘#‘; for(i=0,k=0,top=1;i<=t;i++) { if((j[i]>=‘0‘&&j[i]<=‘9‘)||j[i]==‘.‘) houzui[k++]=j[i]; else if(j[i]==‘(‘) caozuoshu[top++]=j[i]; else if(j[i]==‘)‘) { top=top-1; while(caozuoshu[top]!=‘(‘) { houzui[k++]=‘ ‘; houzui[k++]=caozuoshu[top--]; } } else if(j[i]==‘/‘||j[i]==‘*‘||j[i]==‘-‘||j[i]==‘+‘) { houzui[k++]=‘ ‘; while(youxianji(j[i])<=youxianji(caozuoshu[top-1])) { houzui[k++]=caozuoshu[--top]; houzui[k++]=‘ ‘; } caozuoshu[top]=j[i]; top++; } } top=top-1; while(caozuoshu[top]!=‘#‘) { houzui[k++]=‘ ‘; houzui[k++]=caozuoshu[top--]; } houzui[k]=‘ ‘; houzui[k+1]=‘=‘; houzui[k+2]=‘\0‘; printf("%s\n",houzui); } return 0; } //转
标签:style blog io color ar os 使用 for sp
原文地址:http://www.cnblogs.com/imwtr/p/4069598.html