#include #include #define MAX 50 char infix_q[MAX]; int compare(char stack_o , char infix_o); void infix_to_postfix(); int get_priority(char c){ if(c=='+' || c=='-'){ return 1; }else if(c=='*' || c=='/'){ return 2; }else if(c=='^'){ return 3; }else{ return 0; } } int main(){ int i=0; for(i=0;i= index_i/2 ? 1 : 0; } void infix_to_postfix() { int rear = 0, top = 0, flag = 0, i = 0; char stack_t[MAX]; int tag = 1; for(i = 0; i < MAX; i++) { stack_t[i] = '\0'; } fgets(infix_q, MAX, stdin); infix_q[strcspn(infix_q, "\n")] = 0; i = 0; while(infix_q[i] != '\0') { i++; rear++; } infix_q[rear] = '#'; printf("\t後序表示法:"); stack_t[top] = '#'; for(flag = 0; flag <= rear; flag++) { tag = 1; switch(infix_q[flag]) { case ')': while(stack_t[top] != '(') { printf("%c", stack_t[top--]); } top--; break; case '#': while(stack_t[top] != '#') { printf("%c", stack_t[top--]); } break; case '(': case '*': case '/': case '^': while(compare(stack_t[top], infix_q[flag]) == 1) { printf("%c", stack_t[top--]); } stack_t[++top] = infix_q[flag]; break; case '+': case '-': if(tag==1){ while(compare(stack_t[top], infix_q[flag]) == 1) { printf("%c", stack_t[top--]); } stack_t[++top] = infix_q[flag]; break; }else if(tag==2){ } default: if(infix_q[flag] != ' ') { // 忽略空格 printf("%c", infix_q[flag]); } break; } } }