数据结构C语言编程—栈实现简单四则运算

#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0
#define Elemtype int
#define elemtype char

char opt[7][7]=
{
{’>’,’>’,’<’,’<’,’<’,’>’,’>’},
{’>’,’>’,’<’,’<’,’<’,’>’,’>’},
{’>’,’>’,’>’,’>’,’<’,’>’,’>’},
{’>’,’>’,’>’,’>’,’<’,’>’,’>’},
{’<’,’<’,’<’,’<’,’<’,’=’,’ ‘},
{’>’,’>’,’>’,’>’,’ ‘,’>’,’>’},
{’<’,’<’,’<’,’<’,’<’,’ ‘,’=’},
};

char op[7]={’+’,’-’,’*’,’/’,’(’,’)’,’#’};

typedef struct stack1
{
Elemtype data;
struct stack1 *next;
}InitStack,*LinkStack1;

typedef struct stack2
{
elemtype optr;
struct stack2 *next;
}initStack,*LinkStack2;

int Push_dat(LinkStack1 top1,Elemtype x)
{
LinkStack1 temp;
temp=(LinkStack1)malloc(sizeof(InitStack));
if(!temp)
return FALSE;
temp->data=x;
temp->next=top1->next;
top1->next=temp;
return TRUE;
}

int Pop_dat(LinkStack1 top1,Elemtype *x)
{
LinkStack1 temp;
temp=top1->next;
if(temp==NULL)
return FALSE;
top1->next=temp->next;
*x=temp->data;
free(temp);
return TRUE;
}

int GetTop_dat(LinkStack1 top1)
{
int x;
LinkStack1 temp;
temp=top1->next;
if(temp==NULL)
return FALSE;
x=temp->data;
return x;
}

int Push_optr(LinkStack2 top2,elemtype ch)
{
LinkStack2 temp;
temp=(LinkStack2)malloc(sizeof(initStack));
if(!temp)
return FALSE;
temp->optr=ch;
temp->next=top2->next;
top2->next=temp;
return TRUE;
}

int Pop_optr(LinkStack2 top2,elemtype *ch)
{
LinkStack2 temp;
temp=top2->next;
if(temp==NULL)
return FALSE;
top2->next=temp->next;
*ch=temp->optr;
free(temp);
return TRUE;
}

char GetTop_optr(LinkStack2 top2)
{
LinkStack2 temp;
char c;
temp=top2->next;
if(temp==NULL)
return FALSE;
c=temp->optr;
return c;
}

char Precede(char cr,char ch)
{
int i,j,x1,x2;
for(i=0;i<7;i++)
{
if(crop[i])
{
x1=i;
break;
}
}
for(j=0;j<7;j++)
{
if(ch
op[j])
{
x2=j;
break;
}
}
return opt[x1][x2];
}

int IsOpr(char c)
{
if(c==’+’||c==’-’||c==’*’||c==’/’||c==’(’||c==’)’||c==’#’)
return 1;
else
return 0;
}

int Operate(int x,char opr,int y)
{
int result;
switch(opr)
{
case’+’:
result=x+y;
break;
case’-’:
result=x-y;
break;
case’’:
result=x
y;
break;
case’/’:
result=x/y;
break;
}
return result;
}

int ch_dat(char ch)
{
int x,i;
i=(int)ch;
switch(i)
{
case 48:{x=0;break;}
case 49:{x=1;break;}
case 50:{x=2;break;}
case 51:{x=3;break;}
case 52:{x=4;break;}
case 53:{x=5;break;}
case 54:{x=6;break;}
case 55:{x=7;break;}
case 56:{x=8;break;}
case 57:{x=9;break;}
}
return x;
}

int EvaluateExpression()
{
char ch,cr,theta,ch1;
int result,a,b,tranvalue;
InitStack OPND;
initStack OPTR;
LinkStack1 top1;
LinkStack2 top2;
top1=&OPND;
top2=&OPTR;
top1->next=NULL;
top2->next=NULL;
Push_optr(&OPTR,’#’);
printf(“Please input an exprssion such as ‘3+7*6#’ :”);
ch=getchar();
while((ch!=’#’)||(GetTop_optr(&OPTR)!=’#’))
{
if(!IsOpr(ch))
{
tranvalue=ch_dat(ch);
Push_dat(&OPND,tranvalue);
ch=getchar();
}
else
{
cr=GetTop_optr(&OPTR);
switch(Precede(cr,ch))
{
case ‘<’:
Push_optr(&OPTR,ch);
ch=getchar();
break;
case ‘=’:
Pop_optr(&OPTR,&ch);
ch=getchar();
break;
case ‘>’:
Pop_optr(&OPTR,&theta);
Pop_dat(&OPND,&b);
Pop_dat(&OPND,&a);
result=Operate(a,theta,b);
Push_dat(&OPND,result);
break;
}
}
}
printf(“The answer is :%d\n”,GetTop_dat(&OPND));
}

void main()
{
char anw;
printf("\t\tThe Simlpe Expreesion Evaluation\n\n");
EvaluateExpression();
getchar();
while(1)
{ printf(“Do you want to continue?(Y/N) “);
scanf(”%c”,&anw);
printf("\n");
getchar();
if(anw==‘Y’)
{
EvaluateExpression();
getchar();
}
else if(anw==‘N’)
break;
}
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长风弦歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值