qq_37830405
qq_37830405
采纳率0%
2017-05-13 07:55 阅读 742

逆波兰式的转换 总是输出中断 求大佬帮忙

程序思路应该是对的 也能正确输入 但是输出时总是中断 输出的部分也是对的 求大佬看看到底是什么问题
#include #include typedef struct Node{ char data; struct Node next;}Node,*pNode;typedef struct Stack{ pNode top;}Stack,*pStack;pStack createStack(){ pStack pstack; pstack=(pStack)malloc(sizeof(Stack)); pstack->top=NULL; return pstack;}void Push(pStack pstack,char c){ pNode p; p=(pNode)malloc(sizeof(Node)); p->data=c; p->next=pstack->top; pstack->top=p;}void Pop(pStack pstack){ pNode p; p=pstack->top; char item; item=pstack->top->data; printf("%c",item); pstack->top=pstack->top->next; free(p);}void deletetop(pStack pstack){ pNode p; p=pstack->top; pstack->top=pstack->top->next; free(p);}int first(char a,char b){ int a1; if(a=='+') { if(b==''||b=='/') a1=0; else if(b=='-') a1=1; } else if(a=='-') { if(b=='*'||b=='/') a1=0; else if(b=='+') a1=1; } else if(a=='*') a1=1; else if(a=='/') a1=1; return a1;}void transfo(pStack pstack){ char c; int a; while((c=getchar())!='\n') { if(c>96&&ctop->data=='+'||pstack->top->data=='-'||pstack->top->data=='*'||pstack->top->data=='/') { a=first(c,pstack->top->data); if(a==0) { while(a==0) { Pop(pstack); } Push(pstack,c); } else if(a==1) { Push(pstack,c); } } else if(pstack->top->data=='(') Push(pstack,c); else if(pstack->top==NULL) Push(pstack,c); } else if(c=='(') { Push(pstack,c); } else if(c==')') { while(pstack->top->data!='(') { Pop(pstack); } // if(pstack->top->data=='(') deletetop(pstack); } } while(pstack->top!=NULL) { Pop(pstack); }}int main(){ pStack pstack; pstack=createStack(); transfo(pstack); return 0;}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • jokercsj jokercsj 2017-05-14 10:23

    应该有个插入代码片的功能,你这代码换行都没有,谁愿意看……

    点赞 评论 复制链接分享

相关推荐