栈实现四则运算

#include <stdio.h>
#include <ctype.h>  //header file of isdigit
#define MAXSIZE 100//the max length of the expresion
char ch[7] = {'+','-','*','/','(',')','#'};
int in[7] = {3,3,5,5,1,6,0};//the order of operator in the stack
int out[7] = {2,2,4,4,6,1,0};//the order of operator out the stack
struct mystack
{
int stack[MAXSIZE];
int top;
};
typedef struct mystack mystack;
void Initstack(mystack *s)//initial the stack
{
s->top = 0;
}
void Push(mystack *s,int x)//to push a number into a stack
{
s->top++;
s->stack[s->top] = x; 
}
void Pop(mystack *s,int *x)//to pop a number out of a stack
{
*x = s->stack[s->top];
s->top--;
}
int Gettop(mystack s)//to get the top number of a stack
{
return s.stack[s.top];
}
int Trans(char c)//to translate the operator to a number in order 
{
switch(c)
{
case '+':return 0;break;
case '-':return 1;break;
case '*':return 2;break;
case '/':return 3;break;
case '(':return 4;break;
case ')':return 5;break;
default :return 6;break;
}
}
char Compare(char c1,char c2)//compare the order of the operator according to the array given at the front and return a char 
{
int i1 = Trans(c1);
int i2 = Trans(c2);
if(in[i1] < out[i2])
return '<';
else if(in[i1] > out[i2])
return '>';
else 
return '=';
}
int Operate(int a,int op,int b)//do the calculate according to the number in the array 
{
switch (op)
{
case 0:return a+b;break;
case 1:return a-b;break;
case 2:return a*b;break;
default:return a/b;break;
}
}
int Evaluate()//deal with the expression inputed char by char
{
char c;
int sum,op,a,b,x;
int j = 1,k = 1;
mystack operator,character;//operator for the operator stack,character for the number stack
Initstack(&operator);
Push(&operator,Trans('#'));
Initstack(&character);

c = getchar();
if (c == '+')
c = getchar();//ignore the '+' in front of the expression
else if (c == '-')
{
c = getchar();//ignore the '-' in front of the expresion
j = 0;//mark the negative number 
}
while(c != '#'|| ch[Gettop(operator)] != '#')//the condition of the cycle
{
if(isdigit(c))//include in the ctype.h to judge whether it is a number
{
sum = 0;
while(isdigit(c))
{
if(j)
sum = sum * 10 + (c - '0');
else 
sum = sum * 10 - (c - '0');
c = getchar();
}
Push(&character,sum);//push the inputed number into the character stack
j = 1;//back the mark 
}
else 
{
switch(Compare(ch[Gettop(operator)],c))
{
case '>'://do the calculte if the operator in the stack is bigger than the operator out of the stack
Pop(&operator,&op);
Pop(&character,&b);//notice the pop order
Pop(&character,&a);
Push(&character,Operate(a,op,b));
// c = getchar();//no need to getchar
break;
case '<':
Push(&operator,Trans(c));
c = getchar();
break;
case '='://very important //about '(' and ')'
Pop(&operator,&x);//pop the '(' in the stack
c = getchar();
break;
}
}
}
return Gettop(character);//the result of the expression
}
int main()
{ int result;
  printf("Input the expression:\n");
result = Evaluate();
printf("The result is :%d\n",result);

}

来公司自己写的第一个小程序,关于输入格式不正确等出错信息目前还没有调试。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值