全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1406
推到 Plurk!
推到 Facebook!

中序式轉成前序式的程式

尚未結案
v0830
一般會員


發表:4
回覆:3
積分:1
註冊:2003-05-28

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-06-12 19:07:53 IP:218.165.xxx.xxx 未訂閱
之前做了個中序式轉成後序式的題目...    卻不曉得要怎麼改成中序式轉式前序式...    請大家幫我看看..哪裡能夠修改..   謝謝...    ************************************************************** #include  void main(void) { char poly[30]={0}; //多項式 char temp[10]={0}; //運算元 char b_poly[30]={0};//後置式結果 int result[10]={0}; int oper=0; int p=0,b=0,t=0,i; cout << "Please input poly: "; cin >> poly; //將中置式轉換成後置式 do { if(poly[p]==' ' ||poly[p]=='-'||poly[p]=='*' ||poly[p]=='/' ||poly[p]=='(') { switch(poly[p]) { case '*': case '(': temp[t]=poly[p]; t ; p ; break; case '/': if(temp[t-1]!='*') { temp[t]=poly[p]; t ; p ; break; } else { b_poly[b]=temp[t-1]; temp[t-1]=0; b ; t--; continue; } case ' ': if(temp[t-1]!='*'&&temp[t-1]!='/') { temp[t]=poly[p]; t ; p ; break; } else { b_poly[b]=temp[t-1]; temp[t-1]=0; b ; t--; break; } case '-': if(temp[t-1]!='*'&&temp[t-1]!='/'&&temp[t-1]!=' ') { temp[t]=poly[p]; t ; p ; break; } else { b_poly[b]=temp[t-1]; temp[t-1]=0; b ; t--; break; } } } else if(poly[p] == ')') //處理刮號的部分如下 { while(temp[t-1]!='(') { b_poly[b]=temp[t-1]; temp[t-1]=0; b ; t--; } temp[t-1]=0; t--; p ; } else { b_poly[b]=poly[p]; b ; p ; } }while(poly[p]!='\0'); for(i=t-1;i>=0;i--) { b_poly[b 1]=b_poly[b]; b_poly[b]=temp[i]; b ; } b_poly[b]='\0'; cout << b_poly <<"\n"; //印出後置式結果 b=0,t=0; //利用後置式運算出最後答案 while(b_poly[b]!='\0') { switch(b_poly[b]) { case '*': oper=result[t-2]*result[t-1]; result[t-2]=0; result[t-1]=0; result[t-2]=oper; t--; b ; break; case '/': oper=result[t-2]/result[t-1]; result[t-2]=0; result[t-1]=0; result[t-2]=oper; t--; b ; break; case ' ': oper=result[t-2] result[t-1]; result[t-2]=0; result[t-1]=0; result[t-2]=oper; t--; b ; break; case '-': oper=result[t-2]-result[t-1]; result[t-2]=0; result[t-1]=0; result[t-2]=oper; t--; b ; break; default: result[t]=int(b_poly[b])-48; t ,b ; break; } } cout << result[0] << endl; cin.get(); } **************************************************************** From:小蘋果咬一口...
------




From:小蘋果咬一口...
turboted
版主


發表:95
回覆:754
積分:452
註冊:2002-07-23

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-06-12 19:27:46 IP:61.66.xxx.xxx 未訂閱
(1)這個問題,很類似功課 (2)這種東西,很多書上都有寫 (3)請您寫出,您在轉換時的問題所在,而不是把問題給大家
系統時間:2024-05-18 5:50:18
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!