求助:哪位帮忙看下我写的这个代码的问题在哪里啊?
程序代码:
#include<cstdio> #include<stdio.h> #include<malloc.h> #define NULL 0 typedef struct node{ char date; struct node *next; }SNode; SNode *InitStack(){ //构造一个空栈 SNode *top; top=(SNode *)malloc(sizeof(SNode)); top->next=NULL; return top; } void PushOptr(SNode *top,char x){ //压入运算符 SNode *p; p=(SNode *)malloc(sizeof(SNode)); p->date=x; p->next=top->next; top->next=p; } char PopOptr(SNode *top){ //弹出运算符 SNode *p; char x; if(top==NULL) return NULL; p=top->next; x=p->date; top->next=p->next; free(p); return x; } void PushOpnd(SNode *top,char x){ //压入操作数 SNode *p; p=(SNode *)malloc(sizeof(SNode)); p->date=x; p->next=top->next; top->next=p; } char PopOpnd(SNode *top){ //弹出操作数 SNode *p; char x; if(top==NULL) return NULL; p=top->next; x=p->date; top->next=p->next; free(p); return x; } char GetTop(SNode *top){ //得到栈顶元素 return (top->next)->date; } int In(char c){ int n; switch(c){ case '+': case '-': case '*': case '/': case '(': case ')': case '#':n=1;break; default:n=0;break; } return n; } char Precede(char x,char y){ int i,j; int form[7][7]={{1,1,-1,-1,-1,1,1},{1,1,-1,-1,-1,1,1},{1,1,1,1,-1,1,1},{1,1,1,1,-1,1,1},{-1,-1,-1,-1,-1,0,2},{1,1,1,1,2,1,1},{-1,-1,-1,-1,-1,2,0}}; switch(x){ case '+':i=0;break; case '-':i=1;break; case '*':i=2;break; case '/':i=3;break; case '(':i=4;break; case ')':i=5;break; case '#':i=6;break; } switch(y){ case '+':j=0;break; case '-':j=1;break; case '*':j=2;break; case '/':j=3;break; case '(':j=4;break; case ')':j=5;break; case '#':j=6;break; } if(form[i][j]==1) return '>'; else if(form[i][j]==-1) return '<'; else return '='; } int Operate(char x,char z,char y){ int a=x-'0',b=y-'0'; switch(z){ case '+':return a+b; case '-':return a-b; case '*':return a*b; case '/':return a/b; } } char Eval_Exp(){ char a,b,c,r,f,z; int result; SNode *top[2]; top[0]=InitStack(); PushOptr(top[0],'#'); top[1]=InitStack(); c=getchar(); while(c!='#'||(GetTop(top[0]))!='#'){ if(!In(c)){ PushOpnd(top[1],c); c=getchar(); } else{ r=Precede(GetTop(top[0]),c); switch(r){ case '<':PushOptr(top[0],c); c=getchar(); break; case '=':PopOptr(top[0]); c=getchar(); break; case '>':b=PopOptr(top[0]); a=PopOpnd(top[1]); z=PopOpnd(top[1]); result=Operate(z,b,a); f=result+'0'; PushOpnd(top[1],f); break; } } } return f; } main(){ int result; printf("Please input the num with #:\n"); scanf("%d",&result); Eval_Exp(); printf("%d\n",result-'0'); }要求: 1 设置运算符栈和操作数栈辅助分析算符优先关系;
2 在读入表达式字符序列的同时完成运算符和操作数(整数)的识别处理,以及相应的运算;
3 在识别出操作数的同时,要将其字符序列形式转换成整数形式。