[問題] 中序轉後序
最近在學這邊 就練習了一下
不過真不知錯在哪??
希望各位先進不吝次教 >"<
感激不盡 (對了 可以RUN但結果很噁!)
#include <iostream>
using namespace std;
#define size 100
/* Create Stack and initial */
char Stack[size];
int top = 0;
/* Stack op */
bool Isempty();
bool Isfull();
void push(char datum);
char pop();
char top_value();
bool Islegal();
bool OPcompare(char , char);
int main(){
/* Infix to Posfix */
char *str = "a+b*(c+d)-(e-f)";
char y = 0;
while(*str != '\0'){
if(*str >= 'a' && *str <= 'z') cout << *str << " ";
else if(*str == ')') {
while((y = top_value())!='('){
cout << pop() << " ";
}
}
else {
if(Isempty()) push(*str);
else if(OPcompare(*str, y = top_value())) /* str > y */
push(*str);
else {
while(!OPcompare(*str, y = top_value()))
cout << pop() << " ";
push(*str);
}
}
str++;
}
while(!Isempty()) cout << pop()<< " " << endl;
system("pause");
return 0;
}
bool Isempty(){
if(top == 0) return true;
return false;
}
bool Isfull(){
if(top == size) return true;
return false;
}
void push(char datum){
if(Isfull()) cout << "Stack is full!\n";
else
Stack[top++] = datum;
}
char pop(){
if(Isempty()) { cout << "Stack is empty!!\n"; system("pause"); exit(0);
}
return Stack[top--];
}
bool OPcompare(char a, char popvalue){
if((a == '+' || a == '-') && (popvalue == '*' || popvalue == '/'))
return false;
else if((popvalue == '+' || popvalue == '-') && (a == '*' || a ==
'/'))return true;
else if(a == '(') return true;
else return false;
}
char top_value(){
return Stack[top];
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.218.120
→
08/31 00:48, , 1F
08/31 00:48, 1F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章