[問題] postfix轉infix運算

看板C_and_CPP (C/C++)作者 (小馬非馬)時間16年前 (2009/07/30 13:51), 編輯推噓5(504)
留言9則, 6人參與, 最新討論串1/1
我知道我寫的很不完整,有很多漏洞 不過現在我想先試出最基本的例子 輸入 12+ 可以得到3的結果 請問為什麼以下程式的輸出結果有誤?請高手指正,謝謝 > <" #include<iostream.h> #include <string.h> #define space 20 char str[space]; int s=0; void push(int); int pop(void); int main() { printf("輸入後序運算式:"); scanf("%s",str); int l=strlen(str)-1; for(int s=0; s<l ;s++) { if(str[s]>='0' && str[s]<='9') //若陣列中是1~9則轉換成數字 { str[s]=str[s]-'0'; } else //若是運算子則做以下 { if(str[s]=='+') {push(pop()+pop());} else if(str[s]=='-') {push(pop()-pop());} else if(str[s]=='+') {push(pop()*pop());} else if(str[s]=='/') {push(pop()/pop());} else {printf("error");} } } printf("ans= %d",str[s]); //輸出最後結果 system("pause"); } ============================ //push、pop 副程式 void push(int n) { if(top>=space-1) {printf("stack is full!");} else {str[s]=n;} } int pop(void) { if(s==0) {printf("stack is empty!");} else {return str[--s];} } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.229.129.195

07/30 14:21, , 1F
錯誤太多,數字沒push進去,-和/運算子順序不對,請參考K&R
07/30 14:21, 1F

07/30 14:29, , 2F
把VC6先丟掉吧...
07/30 14:29, 2F

07/30 14:37, , 3F
沒錯 先把VC6砍掉
07/30 14:37, 3F

07/30 14:42, , 4F
從哪邊看出VC6的...iostream.h嗎?
07/30 14:42, 4F

07/30 14:42, , 5F
yes
07/30 14:42, 5F

07/30 14:44, , 6F
原來如此
07/30 14:44, 6F

07/30 16:29, , 7F
DevC++不是也可以寫 #include <iostream.h> 的嗎XD
07/30 16:29, 7F

07/30 16:50, , 8F
下午腦袋不清醒寫的果然慘不人睹,現在看才知道有多白
07/30 16:50, 8F

07/30 16:51, , 9F
癡,謝謝樓上幾位...還有...我的確是用dev...
07/30 16:51, 9F
文章代碼(AID): #1ASJJamc (C_and_CPP)
文章代碼(AID): #1ASJJamc (C_and_CPP)