基于堆栈的计算器代码

基于堆栈的计算器代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<cmath>

using namespace std;

stack<double> b;   //操作数栈

stack<int> c;   //操作符栈

int main()
{
	puts(
			"**********************************************************************");
	puts(
			"*                                                                    *");
	puts("*                  简     易     计     算      器                   *");
	puts(
			"*                                                                    *");
	puts("*                       软件2班      王凯旋                          *");
	puts(
			"**********************************************************************\n\n");
	string a;
	long l, i, j;
	double sum, t, sum1;
	double deal(int);
	recycle:   //循环起点
	while (getline(cin, a))
	{
		l = a.size();   //得到算式
		if (l == 0)   //特殊情况处理
		{
			printf("您没有输入数据\n请输入数据:\n\n");
			continue;
		}
		while (!b.empty())
			b.pop();
		while (!c.empty())
			c.pop();   //清空栈
		a[l] = '#';
		i = 0;
		c.push(1);   //栈初始化
		int neg = 0;
		while (1)
		{
			if ((a[i] >= '0' && a[i] <= '9') && i < l)   //得到数据
			{
				sum = 0;
				while ((a[i] >= '0' && a[i] <= '9') && i < l)
				{
					sum = sum * 10 + a[i] - '0';
					i++;
				}
				sum1 = 0;
				if (a[i] == '.')
				{
					i++;
					t = 0.1;
					while ((a[i] >= '0' && a[i] <= '9') && i < l)
					{
						sum1 = sum1 + (a[i] - '0') * t;
						t = t * 0.1;
						i++;
					}
				}
				if (neg)
				{
					if (sum + sum1 == 0)
						b.push(0);
					else
						b.push((-1.0) * (sum + sum1));
					neg = 0;
				}
				else
					b.push(sum + sum1);
			}
			switch (a[i])
			//操作符处理
			{
			case '#':
			{
				while ((c.top()) != 1)
				{
					b.push(deal(c.top()));
					c.pop();
				}
				if (b.top() == 0)
					printf("=0\n\n");
				else
					printf("=%g\n\n", b.top());
				b.pop();
				goto recycle;
			}
			case '+':
			{
				if (c.top() < 3)
					c.push(3);
				else
				{
					while (c.top() >= 3)
					{
						b.push(deal(c.top()));
						c.pop();
					}
					c.push(3);
				}
			}
				break;
			case '-':
			{
				if (i == 0 || a[i - 1] == '(')
					neg = 1;
				else if (c.top() < 3)
					c.push(4);
				else
				{
					while (c.top() >= 3)
					{
						b.push(deal(c.top()));
						c.pop();
					}
					c.push(4);
				}
			}
				break;
			case '*':
			{
				if (c.top() < 5)
					c.push(5);
				else
				{
					while (c.top() >= 5)
					{
						b.push(deal(c.top()));
						c.pop();
					}
					c.push(5);
				}
			}
				break;
			case '/':
			{
				if (c.top() < 5)
					c.push(6);
				else
				{
					while (c.top() >= 5)
					{
						b.push(deal(c.top()));
						c.pop();
					}
					c.push(6);
				}
			}
				break;
			case '(':
				c.push(2);
				break;
				right: case ')':
				{
					while (c.top() != 2)
					{
						if (c.top() == 4)
						{
							j = i - 1;
							while (a[j] != '-')
								j--;
							j--;
							if (a[j] == '(')
							{
								c.pop();
								b.push(b.top() * (-1));
								b.pop();
							}
							else
							{
								b.push(deal(c.top()));
								c.pop();
							}
						}
						else
						{
							b.push(deal(c.top()));
							c.pop();
						}
					}
					c.pop();
				}
				break;
			case '^':
			{
				if (c.top() < 7)
					c.push(7);
				else
				{
					while (c.top() >= 7)
					{
						b.push(deal(c.top()));
						c.pop();
					}
					c.push(7);
				}
			}
				break;
			}
			i++;
		}
	}
}
double deal(int n)   //计算处理
{
	double x, y;
	x = b.top();
	b.pop();
	y = b.top();
	b.pop();
	switch (n)
	{
	case 3:
		return (x + y);
		break;
	case 4:
		return (y - x);
		break;
	case 5:
		return (x * y);
		break;
	case 6:
	{
		if (x == 0)
		{
			printf("数据错误(除数不能为零)下面的结果无意义\n");
			return 0;
		}
		return (y / x);
	}
		break;
	case 7:
	{
		if (x == 0 && y == 0)
		{
			printf("数据错误(0^0无意义)下面的结果无意义\n");
			return 0;
		}
		else
			return (pow(y, x));
	}
		break;
	};
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注