jx4hjy 于 2014.06.03 22:10 提问

#include
#include
typedef struct rope
{
char date;
struct rope next;
} node,*pnode;
typedef struct rope2
{
char date;
struct rope2 *next;
} nodes,*pnodes;
typedef struct a
{
pnode top;
pnode bottom;
} OPND;
typedef struct b
{
pnodes top;
pnodes bottom;
} OPTR;
char tr[7]= {'+','-','
','/','(',')','#'}; //字符总类型
int comp[7][7]=
{{2,2,1,1,1,2,2}, //用来进行比较运算符优先级的矩阵,3代表'=',2代表'>',1代表'<',0代表不可比
{2,2,1,1,1,2,2},
{2,2,2,2,1,2,2},
{2,2,2,2,1,2,2},
{1,1,1,1,1,3,0},
{2,2,2,2,0,2,2},
{1,1,1,1,1,0,3}};
void initStackOPND(OPND pS)//初始化运算数栈
{
pS->top=(pnode)malloc(sizeof(node));
if(pS->top==NULL)
{
printf("动态内存分配失败!\n");
}
else
{
pS->bottom=pS->top;
pS->bottom->next=NULL;
}
}
void initStackOPTR(OPTR *pS)//初始化运算符栈
{
pS->top=(pnodes)malloc(sizeof(nodes));
if(pS->top==NULL)
{
printf("动态内存分配失败!\n");
}
else
{
pS->bottom=pS->top;
pS->bottom->next=NULL;
}
}
void push(OPND *pS,int val)//运算数压栈
{
pnode new=(pnode)malloc(sizeof(node));
new->date=val;
new->next=pS->top;
pS->top=new;
return;
}
void pushs(OPTR *pS,char val)//运算符压栈
{
pnodes new=(pnodes)malloc(sizeof(nodes));
new->date=val;
new->next=pS->top;
pS->top=new;
return;
}
void pop(OPND *pS,int *x)//运算数出栈
{
if(pS->top=NULL)
{
printf("运算数栈空!\n");
}
else
{
pnode r=pS->top;
*x=r->date;
pS->top=r->next;
free(r);
r=NULL;
}
}
void pops(OPTR *pS,char *x)//运算符出栈
{
if(pS->top=NULL)
{
printf("运算符栈空!\n");
}
else
{
pnodes r=pS->top;
*x=r->date;
pS->top=r->next;
free(r);
r=NULL;
}
}
int gettop(OPND *pS)//得到运算数栈顶元素
{
int c;
pnode r=pS->top;
c=r->date;
if(pS->top=NULL)
{
printf("运算数栈为空！\n");
}
else
{
return c;
}
}
char gettops(OPTR *pS)//得到运算符栈顶元素
{
char c;
pnodes r=pS->top;
c=r->date;
if(pS->top=NULL)
{
printf("运算数栈为空！\n");
}
else
{
return c;
}
}
int judge(char ch)//判断运算符是否正常
{
int i;
for(i=0; i {
if(ch==tr[i])
return 1;
}
return 0;
}
char compare(char ch1,char ch2)//比较运算符优先级
{
int i,a,b;
char t;
int cp;
for(i=0; i {
if(ch1==tr[i])
{
a=i;
}
if(ch2==tr[i])
{
b=i;
}
}
cp=comp[a][b];
switch(cp)
{
case 1:
t=' break;
case 2:
t='>';
break;
case 3:
t='=';
break;
case 0:
t='\$';
printf("表达式错误!\n");
break;
}
return t;
}
int caculate(int a,int b,char o)
{
int r;
switch(o)
{
case '+':
r=a+b;
break;
case '-':
r=a-b;
break;
case '
':
r=a*b;
break;
case '/':
r=a/b;
break;
}
return r;
}
int results()
{
int a,b,t,v,i=0;
char c,x;
char str;
OPND Sa;//运算数栈
OPTR Sb;//运算符栈
initStackOPND(&Sa);
initStackOPTR(&Sb);
pushs(&Sb,'#');
printf("请输入表达式，且在末尾加上‘#’！\n");
str=(char *)malloc(50*sizeof(char));
gets(str);
c=str[i];
i++;
while(c!='#'||gettops(&Sb)!='#')
{
if(!judge(c))
{
t=c-'0'; /

c=str[i];
i++;
while(!judge(c))
{
t=t*10 + c-'0'; /*将逐个读入运算数的各位转化为十进制数*/
c=str[i];
i++;
}
push(&Sa,t);
}
else
{
switch(compare(gettops(&Sb),c))
{
case '<':
pushs(&Sb,c);
c=str[i];
i++;
break;
case '=':
pops(&Sb,&x);//只有()##才会相等出栈
c=str[i];
i++;
break;
case '>':
pops(&Sb,&x);
pop(&Sa,&b);
pop(&Sa,&a);
v=caculate(a,b,x); /* 对a和b进行x运算 */
push(&Sa,v);
break;
}
}
}
v=gettop(&Sa);
return v;
}
int main(void)
{
int result;
void initStackOPND(OPND *pS);
void initStackOPTR(OPTR *pS);
void push(OPND *pS,int val);
void pushs(OPTR *ps,char val);
void pop(OPND *pS,int *x);
void pops(OPTR *pS,char *x);
int gettop(OPND *pS);
char gettops(OPTR *pS);
int judge(char ch);
char compare(char ch1,char ch2);
int caculate(int a,int b,char o);
int results();
result=results();
printf("计算结果为:%d",result);
return 0;
}