栈 实现四则运算的计算器

  这几天一直在做停车场和计算器项目,在停车场做完后,再去做计算器,突然发现简单了很多,对大体的一个思路有了很清楚的了解,与停车场相比,计算器只用了一个结构变量,只有一个操作数栈和一个运算符栈,而停车场用了一个停车栈、一个让车栈和一个候车队列。因此,只要将计算器的算法理清后,写起来是挺简单的。

/**********************************************************
File Name:         
Author:          xxx     Date:2016-12-16
Description:   实现计算器功能
Fuction List:
************************************************************/
#include <stdio.h>

#define SIZE      100
#define ok          0
#define error      -1

typedef struct
{
	int data[SIZE];
	int top;
}DATA;

//初始化
int Init(DATA *p)
{
	if(p == NULL)
	{
		return error;
	}
	p->top = -1;
	return ok;
}

//判断空栈
int Empty(DATA *p)
{
	if(p == NULL)
	{
		return error;
	}
	return p->top == -1;
}

//判断满栈
int Full(DATA *p)
{
	if(p == NULL)
	{
		return error;
	}
	return p->top == SIZE - 1;
}

//压栈
int Push(DATA *p,int data)
{
	if(p == NULL)
	{
		return error;
	}
	if(Full(p) != ok)
	{
		return error;
	}
	
	p->top++;
	p->data[p->top] = data;
	
	return ok;
}

//出栈
int Pop(DATA *p)
{
	if(p == NULL)
	{
		return error;
	}
	if(Empty(p) != ok)
	{
		return error;
	}
	int data;
	data = p->data[p->top];
	p->top--;
	
	return data;
}

//获取栈顶元素
int Getpop(DATA *p)
{
	if(p == NULL)
	{
		return error;
	}
	if(Empty(p) != ok)
	{
		return error;
	}
	
	return p->data[p->top];
}

//判断优先级
int judge(char ch)
{
	switch (ch)
	{
		case '(' :
		{
			return 3;
		}
		case '*' :
		{
			return 2;
		}
		case '/' :
		{
			return 2;
		}
		case '+' :
		{
			return 1;
		}
		case '-' :
		{
			return 1;
		}
		default :
		{
			return error;
		}
	}
}

int main()
{
	char a[SIZE] = {0};
	int i = 0;
	int temp = 0;
	int j;
	
	DATA num;
	if(Init(&num) != ok)
	{
		return error;
	}
	
	DATA opt;
	if(Init(&opt) != ok)
	{
		return error;
	}
	if ((Init(&num) && Init(&opt)) != ok)
	{
		return error;
	}
	
	printf("please input:\n");
	scanf("%s",a);
	
	while(a[i] != '\0' || Empty(&opt) != 1)
	{
		if(a[i] >= '0' && a[i] <= '9')
		{
			temp = temp * 10 + a[i] - '0';         
			i++;
			
			if(a[i] < '0' || a[i] > '9')
			{
				Push(&num,temp);
				temp = 0;
			}
		}
		else
		{
			if((a[i] != '\0' && Empty(&opt)) || (a[i] != ')' && Getpop(&opt) == '(') || (judge(a[i]) > judge(Getpop(&opt))))
			{
				Push(&opt,a[i]);         
				i++;
				continue;
			}
					
			if(a[i] == ')' && Getpop(&opt) == '(')
			{
				Pop(&opt);
				i++;
				continue;
			}
						
			if(a[i] == '\0' || a[i] == ')' || judge(a[i]) <= judge(Getpop(&opt)))
			{
				switch (Pop(&opt))
				{
					case '*' :
					{
						Push(&num,Pop(&num)*Pop(&num));
						break;
					}
					case '/':
					{
						j = Pop(&num);
						Push(&num,Pop(&num)/j);
						break;
					}
					case '+':
					{
						Push(&num,Pop(&num)+Pop(&num));
						break;
					}
					case '-':
					{
						j = Pop(&num);
						Push(&num,Pop(&num)-j);
						break;
					}
					default:
					{
						break;
					}
				}
				continue;						
			}
		}
		
			
	}
	printf("\n%s = %d\n",a,Pop(&num));
	
	return 0;
}


  • 10
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值