資源簡介
一個用C++寫的簡單的逆波蘭式的生成程序,用于對編譯原理里面的中綴表達式的處理!
代碼片段和文件信息
#include?
using?namespace?std;
#include?
#include?
#include?
#define?maxbuffer?64?
int?main()?
{?
char?display_out(char?out_ch[maxbuffer]?char?ch[32]);???????????//定義顯示轉換函數?
static?int?i=0;?
static?int?j=0;?
char?ch[maxbuffer]s[maxbuffer]out[maxbuffer];?
begin:?
cout<<“請輸入中綴表達式:?“;???????????????//輸入中綴表達式并存放在?數組ch[maxbuffer]中?
cin>>ch;?
for(i=0;i ??{out[i]=ch[i];}?
cout<<“請確認您輸入的表達式:?“;??????????//確認所輸入的式子是否為您所想的??
while(out[j]!=‘#‘)???????????//判斷是不是以#結束?
?{?
????cout< ????j++;?
?}?
cout<<‘#‘< char?m;
cout<<“Yes:Enter?Y?or?y!?No:Enter?N?or?n!“< cin>>m;
if((m==‘Y‘)||(m==‘y‘))
display_out(sout);????//調用函數進行處理?
else?if?((m==‘N‘)||(m==‘n‘))
??{cout<<“請重新輸入中綴表達式“;?goto?begin;??}
system(“pause“);
}?
char?display_out(char?out_ch[32]char?ch[])?
{?
int?top=-1;?
int?i=0data[maxbuffer]n;?
int?j=0;?
char?sta[20];?
while(ch[i]!=‘#‘)?
{?
??if(isalnum(ch[i]))?
????{?
??????while(isalnum(ch[i]))?
???????{?
?????????out_ch[j]=ch[i];?
?????????j++;?
?????????i++;?
????????}
??????out_ch[j]=‘?‘;
??????j++;?
????}?
???else
????{?
?????switch(ch[i])?
??????{?
????????case?‘+‘:??????????????//對”+、-“號的處理?
????????case?‘-‘:??
??????????if(sta[top]==‘(‘||top==-1)?
???????????{?
?????????????top++;?
?????????????sta[top]=ch[i];?
?????????????i++;?
????????????}?
??????????else?
????????????{???????????
?????????????out_ch[j]=sta[top];?
?????????????j++;?
?????????????to
評論
共有 條評論