Re: [問題] 資料結構-用c語言array寫stack,push和 …

看板C_and_CPP作者 (最愛)時間15年前 (2009/04/10 00:28), 編輯推噓0(008)
留言8則, 1人參與, 最新討論串4/5 (看更多)
#include<stdio.h> #include<string.h> #include<ctype.h> //只能夠計算加減乘除和左右括號的四則運算 //infix先轉postfix再作計算 //使用方法array的stack typedef struct STACK { char item [1000]; int top; }STACK; STACK S; void push(char ); void pop(int *); int main(){ char infix[1000]; char postfix[1000]; int i=0,j=0; S.top=0; printf("請輸入中序式, 例如 (a+b)*c-d/e :\n"); scanf("%s",infix); char token; int y; //----開始進行轉換為postfix的動作 //think:將掃的字元分為符號和字母,依括號法字母直接掃入新的array //運算子-符號則分為( ) + - * / ,左括號一律掃入,直到有右括號對應 " * / " 的優先權大於" + - " while(infix[i] != '\0'){// 進行掃入的工作 token=infix[i++]; if( (token) >='a' && (token)<='z'){postfix[j++]=token;}//判斷為字母 else{//為符號-分為6種情況 if(token==')'){//右括號,遇到要把到左括號之前的全部POP出 並除去左括號 pop(&(int)y); while(y != '('){ postfix[j++]=y; pop(&(int)y);}//y不等於左括號,印出y break; } //end ')' else if( token=='(' ) push(token);//case '(' 在stack外優先權最高,stack內優先權最低 else if( token=='+'){//case '+' 優先權等於 + - 小於 * / 大於( if( (S.top==0) || (S.item[S.top]=='(')) push(token); else {pop(&(int)y); while(y == '*' || y == '/' || y=='+' || y=='-'){//y是優先權<= 的情況 postfix[j++]=y; pop(&(int)y);}//end while break; }}//end case '+' else if( token=='-'){//case '-' if( S.top==0 || S.item[S.top]=='(') push(token); else {pop(&(int)y); while(y == '*' || y == '/' || y=='+' || y=='-'){//y是優先權<= 的情況 postfix[j++]=y; pop(&(int)y);}//end while break; }}//end case '-' else if( token=='*'){//優先權等於 * / 大於( + - if( S.top==0 || S.item[S.top]=='(' || S.item[S.top]=='+' || S.item[S.top]=='-') push(token); else{pop(&(int)y); while(y == '*' || y == '/'){ postfix[j++]=y; pop(&(int)y);} break; }}//end case '*' else { //token = '/' if( S.top==0 || S.item[S.top]=='(' || S.item[S.top]=='+' || S.item[S.top]=='-') push(token); else{pop(&(int)y); while(y == '*' || y == '/'){ postfix[j++]=y; pop(&(int)y);} break; }}//end case '/' }//end else 符號 }//end 掃入工作 while(S.top > 0){//當掃入完成後清空stack pop(&(int)y); postfix[j]=y; j++;} for(i=0;postfix[i]!='\0';i++) printf("%c",postfix[i]); system("PAUSE"); return 0; }//end main void push(char x){ if (S.top<100){ S.top++; S.item[S.top]=x;} } void pop(int *y){ if (S.top > 0){ *y=S.item[S.top]; S.top--;} } 經過幾次修改和精減變成這樣... 可是程式轉為postfix還是不對... 雖然pop和push的問題解決了... 不過目前還是不知道為甚麼跑一跑會自動跳出(沒出現任何錯誤訊息) 當輸入少的時候還是正確的ex:a+b*c 當輸入一多就會跳出...也不會停在system("pause")的地方 不知道有沒有高名的版友看的出是哪邊出了問題.... (這種沒任何訊息的找半天都還是不知道問題出在哪) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.169.232.21

04/10 00:33, , 1F
你item設1000, pop那邊別設100啊
04/10 00:33, 1F

04/10 00:42, , 2F
沒全部看完,但是
04/10 00:42, 2F

04/10 00:42, , 3F
if( (S.top==0) || (S.item[S.top]=='(')) push(token
04/10 00:42, 3F

04/10 00:43, , 4F
這行就錯了。S.top == 0時,這是empty stack
04/10 00:43, 4F

04/10 00:45, , 5F
啊,我看錯了,你的top跟我的不一樣....
04/10 00:45, 5F

04/10 00:48, , 6F
…嗯,你的top==0的位完全不會用到。拿top==0的位置的
04/10 00:48, 6F

04/10 00:49, , 7F
資料去比對是錯的
04/10 00:49, 7F

04/10 00:51, , 8F
啊,你用的是||,所以top==0沒錯,我上面寫的不要看
04/10 00:51, 8F
文章代碼(AID): #19tY8GM- (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #19tY8GM- (C_and_CPP)