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

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

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

代码如下:

/************************************************************************/
/* 整数加减乘除四则运算,先要完成后续排列,然后完成数据进出栈               */
/************************************************************************/
#include
#include
#include 
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  stack1;
	stack  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='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())='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<

你可能感兴趣的:(数据结构——c++)