编程练习5:后缀表达式的计算(int上计算器)

要求:将输入的一个中缀表达式转换为后缀表达式进行输出,输出后利用后缀表达式计算式子的结果,将结果进行输出。

思路:

中缀转后缀详见编程练习4

这里处理的时候讲所得到的后缀的结果放置在一个vector中,相比较放在stack中更好的进行取数进行运算

这里需要注意的一点是vector为string类型:原因:以为可能出现229  16这样的数字就不能够每一个符号一个符号的进行判断。

在进行运算时需要将vector中用字符串表示的数字转化为int类型这里使用string自带的函数atoi完成转换

#include <iostream>
#include <stack>
#include<string>
#include<vector>
using namespace std;
int main(void)
{
	char a[100];
	cin >> a;
	int length = 0;//用来存储数据的长度
	while (a[length] != '\0')
	{
		length++;
	}
	cout << "数组的长度为 ###" << length << endl;;
	/*接下来对这个数组进行处理,这时会用到一个栈进行处理,同时申请一个新的char型数组
	用来存储最后要打印出来的东西*/
	stack<char>sum;//申请一个栈用来处理符号
	vector<string>result;
	int re1;
	for (int i = 0; i < length; i++)
	{
		cout << "第"<<i<< "次循环" << endl;
		if (a[i] == '+' || a[i] == '-')
		{
			cout << "pandan+/-" << endl;
			//若果为加号或者减号
			if (sum.empty())
			{
				sum.push(a[i]);
			}
			else
			{
				while (!sum.empty() && (sum.top() == '*' || sum.top() == '/' || sum.top() == '+' || sum.top() == '-'))
				{
					string str = "";
					str = str + sum.top();
					result.push_back(str);
				
					sum.pop();
				}
				sum.push(a[i]);
			}

		}
		else if (a[i] == '*' || a[i] == '/')
		{
			cout << "pandaun*//" << endl;
			while (!sum.empty() && (sum.top() == '*' || sum.top() == '/' ))
			{
				string str = "";
				str = str + sum.top();
				result.push_back(str);

				sum.pop();
			}
			sum.push(a[i]);
		}
		else if (a[i] == '(')
		{
			//左括号压入栈中
			sum.push(a[i]);
		}
		else if (a[i] == ')')
		{
			cout << "oandaun)" << endl;
			//右括号弹栈
			while (sum.top() != '(')
			{
				string str = "";
				str = str + sum.top();
				result.push_back(str);
				sum.pop();
			}
			sum.pop();
		}
		else
		{
			cout << "直接输出"<<i << endl;
			string str = "";
			while (a[i] >= '0'&&a[i] <= '9')
			{
				cout << "limian"<<i << endl;
				str = str + a[i];
				i++;
				cout << "这时a[i]为"<<a[i] << endl;
			}
			i--;
			result.push_back(str);
		}
	}
	//将栈中剩下的内容打印出来
	while (!sum.empty())
	{
		string str = "";
		str = str + sum.top();
		result.push_back(str);

		sum.pop();

	}

	for (int k = 0; k <result.size(); k++)
	{
	cout << result[k]<<" ";
	}
	
	//进行计算
	int re = 0;
	stack<int>shuzi;
	for (int p = 0; p < result.size(); p++) 
	{
		if (result[p] != "+" && result[p] != "-" && result[p] 
			!= "*" && result[p] != "/")
		{
			//将数字单独放在一个栈里
			cout << "得到一个数字" << atoi(result[p].c_str()) << endl;;
			shuzi.push(atoi(result[p].c_str()));	
		}
		else if(result[p] == "+")
		{
			int a = shuzi.top();
			shuzi.pop();
			int b = shuzi.top();
			shuzi.pop();
			re = a + b;
			shuzi.push(re);
		}
		else if (result[p] == "-")
		{
			int a = shuzi.top();
			shuzi.pop();
			int b = shuzi.top();
			shuzi.pop();
			re = a - b;
			shuzi.push(re);
		}
		else if (result[p] == "*")
		{
			int a = shuzi.top();
			shuzi.pop();
			int b = shuzi.top();
			shuzi.pop();
			re = a * b;
			shuzi.push(re);
		}
		else if (result[p] == "/")
		{
			int a = shuzi.top();
			shuzi.pop();
			int b = shuzi.top();
			shuzi.pop();
			re = a / b;
			shuzi.push(re);
		}
	}
	cout << "最终结果是" << endl;
	while (!shuzi.empty())
	{
		cout << shuzi.top() << endl;
		shuzi.pop();
	}


	system("pause");
	return 0;
}

本算法可以实现多个括号计算的功能,可以完成一个应用在整型数据上的计算器

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值