数据结构——整数的四则运算

栈的应用——整数的四则运算:

例如计算:(1+2)*3-4/4;

代码如下:

/************************************************************************/
/* 整数加减乘除四则运算,先要完成后续排列,然后完成数据进出栈               */
/************************************************************************/
#include<iostream>
#include<string>
#include <stack>
using namespace  std;
int fh_uxj(char c)
{
	switch(c)
	{
	case '+':return 0;
	case '-':return 0;
	case '*':return 1;
	case '/':return 1; 
	default: break;
	}
}
float szys(float a,float b,char c)
{
	switch (c)
	{
	case '+':return a+b;
	case '-':return a-b;
	case '*':return a*b;
	case '/':return a/b;
	}

}

int  main()
{
	stack <char> stack1;
	stack <float> stack2;
	string s_input,s_last,s_comput;
	cout<<"please input your data...."<<'\n';
	getline(cin,s_input);
	int j=0,last=-1;
	for (int i=0;i<s_input.size();i++)
	{
		char temp=s_input.at(i);
		if (s_input.at(i)>='0'&&s_input.at(i)<='9')
		{
			if (i-last!=1)
			{
				s_last.push_back(' ');
			}
			s_last.push_back(s_input.at(i));
			last=i;
		}
		else
		{
			if(stack1.empty())
			{
				stack1.push(s_input.at(i));
			}
			else
			{
				if (s_input.at(i)==')')
				{
					while (stack1.top()!='(')
					{
						s_last.push_back(stack1.top());
						stack1.pop();
					}
					stack1.pop();
				}
				else if (s_input.at(i)=='(')
				{
					stack1.push(s_input.at(i));
				}
				else 
				{
					char flag=1;
					while (stack1.top()!='(')
					{
						if (fh_uxj(stack1.top())<fh_uxj(s_input.at(i)))
						{
							flag=0;
							stack1.push(s_input.at(i));
							break;
						}
						else
						{
							s_last.push_back(stack1.top());
							stack1.pop();
							if (stack1.empty())
								break;
						}
					}
					if (flag) stack1.push(s_input.at(i));
				}
			}
		}
	}
	while (!stack1.empty())
	{
		s_last.push_back(stack1.top());
		stack1.pop();
	}
	for (int i=0;i<s_last.size();i++)
	{
		if (s_last.at(i)>='0'&&s_last.at(i)<='9')
		{
			float f_temp=0;
			while(s_last.at(i)>='0'&&s_last.at(i)<='9')
			{
				f_temp=f_temp*10;
				f_temp=f_temp+(float)(s_last.at(i)-48);
				i++;
			}
			stack2.push(f_temp);
			i--;
		}
		else if (s_last.at(i)==' ')
			continue;
		else
		{
			float a=(float)stack2.top();
			stack2.pop();
			float b=(float)stack2.top();
			stack2.pop();
			float c=szys(b,a,s_last.at(i));
			stack2.push(c);
		}
	}
	cout<<stack2.top();




	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值