?? 逆波蘭.txt
字號(hào):
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
int newtop;
char exp[MAX];
void operand()
{ float opnd[MAX],d;
char chi;
int t=1,newnewtop=0;
chi=exp[t];
t++;
while(chi!='#')
{ switch(chi)
{ case'+':
opnd[newtop-1]=opnd[newtop-1]+opnd[newtop];
newtop--;
break;
case'-':
opnd[newtop-1]=opnd[newtop-1]-opnd[newtop];
newtop--;
break;
case'*':
opnd[newtop-1]=opnd[newtop-1]*opnd[newtop];
newtop--;
break;
case'/':
if(opnd[newtop]!=0)
opnd[newtop-1]=opnd[newtop-1]/opnd[newtop];
else
{ printf("Error!\n");
}
newtop--;
break;
default:
d=0;
while(chi>='0'&&chi<='9')
{ d=10*d+chi-'0';
chi=exp[t];
t++;
}
newtop++;
while(chi>='a'&&chi<='z')
{chi=exp[t];
t++;
}
newtop++;
opnd[newtop]=d;
}
chi=exp[t];
t++;
}
}
void express()
{ char opmt[MAX];
char opst[MAX];
char chi;
int i,k,t,j,newtop=0;
printf("請(qǐng)輸入一個(gè)對(duì)應(yīng)上述LL(1)輸入串的算術(shù)表達(dá)式(以#號(hào)結(jié)束):");
i=0;
do
{ i++;
scanf("%chi",&opmt[i]);
}
while(opmt[i]!='#'&&i!=MAX);
printf("\n\n");
k=i;
t=1;
i=1;
chi=opmt[i];
i++;
while(chi!='#')
{ switch(chi)
{ case'(':
newtop++;
opst[newtop]=chi;
break;
case')':
while (opst[newtop]!='(')
{ exp[t]=opst[newtop];
newtop--;
t++;
}
newtop--;
break;
case'+':
case'-':
while(newtop!=0&&opst[newtop]!='(')
{ exp[t]=opst[newtop];
newtop--;
t++;
}
newtop++;
opst[newtop]=chi;
break;
case'*':
case'/':
while(opst[newtop]=='*'||opst[newtop]=='/')
{ exp[t]=opst[newtop];
newtop--;
t++;
}
newtop++;
opst[newtop]=chi;
break;
default:
while(chi>='0'&&chi<='9'||chi>='a'&&chi<='z')
{ exp[t]=chi;
t++;
chi=opmt[i];
i++;
}
i--;
t++;
}
chi=opmt[i];
i++;
}
while(newtop!=0)
{ exp[t]=opst[newtop];
t++;
newtop--;
}
printf("當(dāng)前的算術(shù)表達(dá)式為:");
for(j=1;j<k;j++) printf("%chi",opmt[j]);
printf("\n\n");
printf("\n此算術(shù)表達(dá)式的逆波蘭式中間代碼:");
for(j=1;j<t;j++)printf("%chi",exp[j]);
printf("\n\n\n");
system("pause");
}
void main()
{ express();
operand();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -