#include<stdio.h>
#include<malloc.h>
typedef struct node
{
double data;
struct node *next;
}OPRD,*OPRDstack;//操作数栈
typedef struct NODE
{
char data;
struct NODE *next;
}OPTR,*OPTRstack;//操作符栈
OPRDstack Creat_OPRDstack();//建立操作数栈
int empty(OPRDstack H);//操作数栈判空
int push(OPRDstack H,double e);//入操作数栈
double pop(OPRDstack H,double *x);//出操作数栈
double gettop(OPRDstack H);//获得操作数栈顶的值
OPTRstack Creat_OPTRstack();//操作符栈
int Empty(OPTRstack h);//操作符栈判空
int Push(OPTRstack h,char c);//入操作符栈
char Pop(OPTRstack h,char *c);//出操作符栈
char Gettop(OPTRstack h);//获得操作符栈顶的值
int isOPRT(char c);//判断输入的字符是否为操作符
char compare(char a,char b);//比较操作符栈的栈顶字符与读取的下一个字符
double calculate(double left,double right,char operators);//操作数栈连续弹出两个数时将其计算
int main()
{
OPRDstack H;
H=Creat_OPRDstack();
OPTRstack h;
h=Creat_OPTRstack();
char operators,x;
double left,right,data,value;
Push(h,'#');
printf("请输入运算表达式,以#结束\n");
char ch;
ch=getchar();
while(ch!='#'||Gettop(h)!='#')
{
if(!isOPRT(ch))
{
data=ch-'0';
ch=getchar();
while(!isOPRT(ch))
{
data=data*10+ch-'0';
ch=getchar();
}//将字符转换为数字
push(H,data);
}
else
{
switch(compare(Gettop(h),ch))
{
case '<':Push(h,ch);//如果栈顶的运算符优先级低,新运算符直接入栈
ch=getchar();break;
case '=':Pop(h,&x);//如果栈顶运算符的优先级等于读取的运算符优先级,则将栈顶运算符出栈
ch=getchar();break;
case '>'://如果栈顶的运算符优先级高,先出栈计算,新运算符再入栈
operators=Pop(h,&operators);
right=pop(H,&right);
left=pop(H,&left);
push(H,calculate(left,right,operators));break;
case '!':printf("ERROR!");break;
}
}
}
value=gettop(H);
printf("%lf",value);
}
OPRDstack Creat_OPRDstack()
{
OPRDstack H=(OPRDstack)malloc(sizeof(OPRD));
H->next=NULL;
return H;
}
int empty(OPRDstack H)
{
if(H->next==NULL) return 1;
else return 0;
}
int push(OPRDstack H,double e)
{
OPRDstack p;
p=(OPRDstack)malloc(sizeof(OPRD));
p->data=e;
p->next=H->next;
H->next=p;
return true;
}
double pop(OPRDstack H,double *x)
{
OPRDstack p;
if(empty(H)) return 0;
else{
p=H->next;
H->next=p->next;
*x=p->data;
free(p);
return *x;
}
}
double gettop(OPRDstack H)
{
if(empty(H)) return 0;
else{
OPRDstack e=H->next;
return e->data;
}
}
OPTRstack Creat_OPTRstack()
{
OPTRstack h=(OPTRstack)malloc(sizeof(OPTR));
h->next=NULL;
return h;
}
int Empty(OPTRstack h)
{
if(h->next==NULL) return 1;
else return 0;
}
int Push(OPTRstack h,char c)
{
OPTRstack p;
p=(OPTRstack)malloc(sizeof(OPTR));
p->data=c;
p->next=h->next;
h->next=p;
return true;
}
char Pop(OPTRstack h,char *c)
{
OPTRstack p;
if(Empty(h)) return 0;
else{
p=h->next;
h->next=p->next;
*c=p->data;
free(p);
return *c;
}
}
char Gettop(OPTRstack h)
{
if(Empty(h)) return 0;
else{
OPTRstack e=h->next;
return e->data;
}
}
int isOPRT(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='#') return 1;
else return 0;
}
char compare(char a,char b)
{
if(a=='+')
{
if(b=='+'||b=='-'||b==')'||b=='#') return '>';
else return '<';
}
else if(a=='-')
{
if(b=='+'||b=='-'||b==')'||b=='#') return '>';
else return '<';
}
else if(a=='*')
{
if(b=='(') return '<';
else return '>';
}
else if(a=='/')
{
if(b=='(') return'<';
else return '>';
}
else if(a=='(')
{
if(b==')') return '=';
else if(b=='#') return '!';
else return '<';
}
else if(a==')')
{
if(b=='(') return '!';
else return '>';
}
else if(a=='#')
{
if(b=='#') return '=';
else if(b==')') return '!';
else return '<';
}
}
double calculate(double left,double right,char operators)
{
switch(operators)
{
case '+':
return 1.0*left+right;
case '-':
return 1.0*left-right;
case '*':
return 1.0*left*right;
case '/':
return 1.0*left/right;
}
}