//¼ö½ÄÀ» ÀԷ¹޾Ƽ­ ÁßÀ§Ç¥±â¹ýÀ» ÇÏÀ§Ç¥±â¹ýÀ¸·Î ¹Ù²ÛµÚ ½ºÅÃÀ» ÀÌ¿ëÇØ¼­ ¼ö½ÄÀ» °è»êÇÏ´Â ÇÁ·Î±×·¥ #include #include #include #define FIRST 1 #define SECOND 2 #define THIRD 3 #define FOURTH 4 #define FIFTH 5 typedef struct stackNode{ int data; struct stackNode* link; }StackNode; typedef struct stackNode2{ char sign; int order; struct stackNode2* link; }StackNode2; StackNode *top; StackNode2 *top2; void push(int mData){ StackNode *temp; temp=(StackNode*)malloc(sizeof(StackNode)); temp->data=mData; temp->link=top; top=temp; } void push2(char mSign,int mOrder){ StackNode2 *temp2; temp2=(StackNode2*)malloc(sizeof(StackNode2)); temp2->sign=mSign; temp2->order=mOrder; temp2->link=top2; top2=temp2; } char pop2(){ StackNode2 *temp3=top2; char a; a=top2->sign; top2=top2->link; free(temp3); return a; } int peek2(){ if(top2==NULL){ return -1; }else{ return top2->order; } } char peek3(){ if(top2==NULL){ return 'a'; }else{ return top2->sign; } } int pop(){ StackNode *temp4=top; int b=top->data; top=top->link; free(temp4); return b; } int main(){ int i; int length=0; char input[100]; char temp[100]; int x=0; int aData=0,bData=0; scanf("%s",input); length=strlen(input); top=NULL; top2=NULL; x=0; for(i=0;iSECOND){ push2(input[i],SECOND); }else if(peek2()==SECOND){ temp[x]=pop2(); x++; push2(input[i],SECOND); }else{ while(top2 != NULL){ temp[x]=pop2(); x++; if(peek2()>SECOND){ break; } if(peek2()==FIFTH){ break; } } push2(input[i],SECOND); } }else if(input[i]=='*' || input[i]=='/'){ if(peek2()==-1){ push2(input[i],THIRD); }else if(peek2()>THIRD){ push2(input[i],THIRD); }else if(peek2()==THIRD){ temp[x]=pop2(); x++; push2(input[i],THIRD); } else{ while(top2 != NULL){ temp[x]=pop2(); x++; if(peek2()>THIRD){ break; } if(peek2()==FIFTH){ break; } } push2(input[i],THIRD); } }else if(input[i]=='+' || input[i]=='-'){ if(peek2()==-1){ push2(input[i],FOURTH); }else if(peek2()>FOURTH){ push2(input[i],FOURTH); }else if(peek2()==FOURTH){ temp[x]=pop2(); x++; push2(input[i],FOURTH); } else{ while(top2 != NULL){ temp[x]=pop2(); x++; if(peek2()>FOURTH){ break; } if(peek2()==FIFTH){ break; } } push2(input[i],FOURTH); } }else if(input[i]=='(' || input[i]==')'){ if(input[i]=='('){ push2(input[i],FIFTH); }else{ while(peek3() !='('){ temp[x]=pop2(); x++; } pop2(); } }else{ temp[x]=input[i]; x++; } } while(top2 != NULL){ temp[x]=pop2(); x++; } for(i=0;i