#include<stdio.h>
#include<malloc.h>
typedef struct stack
{
char data;
struct stack *next;
}stack;
stack* init(stack *h)
{
h=(stack *)malloc(sizeof(struct stack));
h->next=NULL;
return h;
}
stack* push(stack *h,char d)
{
stack *p;
p=(stack *)malloc(sizeof(struct stack));
p->data=d;
p->next=h;
h=p;
return h;
}
char pop(stack *h)
{
char d;
if(h->next!=NULL)
{
d=h->data;
h=h->next;
}
else return NULL;
return d;
}
int judge(char s)
{
switch(s)
{
case '+':return 1;
case '-':return 1;
case '*':return 1;
case '/':return 1;
default: return 0;
}
}
int prior(char s1)
{
switch(s1)
{
case '*':return 1;case '/':return 1;
default :return 0;
}
}
int cout(char s,int a,int b)
{
switch(s)
{
case '*':return a*b;
case '/':return b/a;
case '+':return a+b;
case '-':return b-a;
default :return 0;
}
}
void main()
{
char *s,d,t,*p;
int a,b,c;
stack *h,*l;
s=(char *)malloc(sizeof(char)*20);
p=s;
h=init(h);
l=init(l);
gets(s);
while(*s!='/0')
{
d=*s;
if(judge(d))
{
t=pop(h);
if(t)
if(prior(d)<=prior(t))
{
a=pop(l);l=l->next;
b=pop(l);l=l->next;
//printf("%d",b);
//printf("%c",t);
//printf("%d",a);
c=cout(t,a,b);
//printf("=%d/n",c);
l=push(l,c);
h=h->next;
}
h=push(h,d);
}
else
l=push(l,d-48);
s++;
}
while(h->next&&l->next)
{
d=pop(h);
a=pop(l);l=l->next;
b=pop(l);l=l->next;
//printf("%d",b);
//printf("%c",d);
//printf("%d",a);
c=cout(d,a,b);
//printf("=%d/n",c);
l=push(l,c);
h=h->next;
}
c=pop(l);
printf("%s=%d",p,c);
}
4564

被折叠的 条评论
为什么被折叠?



