栈实现四则运算

#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
    评论
可以使用实现四则运算表达式的计算。具体步骤如下: 1. 定义一个结构,用于存放运算符和数字。 2. 读入表达式字符串,逐个字符进行处理。 3. 遇到数字字符时,将其转换为数字并压入中。 4. 遇到运算符时,从中弹出两个数字进行计算,并将结果压入中。 5. 最后中只剩下一个数字,即为表达式的计算结果。 下面是使用C语言实现的应用实现四则运算的示例代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 50 typedef struct { int top; int data[MAX_SIZE]; } Stack; void push(Stack *stack, int value) { if (stack->top == MAX_SIZE) { printf("Error: stack overflow\n"); exit(1); } stack->data[++stack->top] = value; } int pop(Stack *stack) { if (stack->top == -1) { printf("Error: stack underflow\n"); exit(1); } return stack->data[stack->top--]; } int calculate(char *expr) { Stack stack = {-1}; char *p = expr; while (*p != '\0') { if (*p >= '0' && *p <= '9') { int num = 0; while (*p >= '0' && *p <= '9') { num = num * 10 + (*p - '0'); p++; } push(&stack, num); } else if (*p == '+' || *p == '-' || *p == '*' || *p == '/') { int b = pop(&stack); int a = pop(&stack); switch (*p) { case '+': push(&stack, a + b); break; case '-': push(&stack, a - b); break; case '*': push(&stack, a * b); break; case '/': if (b == 0) { printf("Error: divide by zero\n"); exit(1); } push(&stack, a / b); break; } p++; } else { printf("Error: invalid character '%c'\n", *p); exit(1); } } if (stack.top != 0) { printf("Error: invalid expression\n"); exit(1); } return pop(&stack); } int main() { char expr[100]; printf("请输入四则运算表达式:"); scanf("%s", expr); int result = calculate(expr); printf("计算结果:%d\n", result); return 0; } ``` 在程序中,我们定义了一个结构 Stack,包含一个指向顶的 top 指针和一个数组 data 用于存放中的元素。push 函数实现元素的入,pop 函数实现元素的出。calculate 函数接收一个字符串类型的表达式,逐个字符进行处理,并使用来计算表达式的值。 使用示例: ``` 请输入四则运算表达式:3+4*2/(1-5)^2 计算结果:3 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值