用数组顺序栈实现表达式运算(后缀表达式)

//这个是用中缀转为后缀再进行运算,根据书上的改写,感觉书上写的有点麻烦...没办法,按照要求.还是硬生生的改写了函数,虽然有点不堪入目,但还好实现了带浮点,括号,负数的运算

#include<math.h>
#include"SeqStack.h"
class Calculator
{
public:
	Calculator(int sz=100):s(sz){}
	void Run();
	void Clear();
private:
	SeqStack<double> s;
	void AddOperand(double value);
	bool Get2Operands(double& left,double& right);
	void DoOperator(char op);
};


 

#include"Calcuator.h"
#include<fstream>
void Calculator::DoOperator(char op)
{
	double left,right,value;
	int result;
	result=Get2Operands(left,right);
	if(result)
		switch(op)
	{
		case '+':value=left+right;
			s.Push(value);
			break;
		case '-':value=left-right;
			s.Push(value);
			break;
		case '*':value=left*right;
			s.Push(value);
			break;
		case '/':if(right==0.0)
				 {
					cerr<<"Divide by 0!"<<endl;
					Clear();
				 }
				 else
				 {
					value=left/right;
					s.Push(value);
				 }
				 break;
	}
	else
		Clear();
};
bool Calculator::Get2Operands(double& left,double& right)
{
	if(s.IsEmpty()==true)
	{
		cerr<<"缺少右操作数!"<<endl;
		return false;
	}
	s.Pop(right);
	if(s.IsEmpty()==true)
	{
		cerr<<"缺少左操作数!"<<endl;
		return false;
	}
	s.Pop(left);
	return true;
};
void Calculator::AddOperand(double value)
{
	s.Push(value);
}
void Calculator::Run()
{
	char ch,ch1;
	double newOperand;
	
	fstream ofile;
	ofile.open("temp.txt");
	if(!ofile)
	{
	cout<<"不能打开文件"<<endl;
	return;
	}
	while(ofile>>ch,ch!='#')
	{
		switch(ch)
		{
		case '+':
		case '-':
			ofile.get(ch1);
			if(ch1!=' ')
			{
				ofile.seekp(-2,ios::cur);

				ofile>>newOperand;
				AddOperand(newOperand);
				break;
			}
		case '*':
		case '/':
		DoOperator(ch);
		break;
		default:ofile.seekp(-1,ios::cur);
			ofile>>newOperand;
			AddOperand(newOperand);
		}
	}
	
	
	while(!s.IsEmpty())
	{
		double temp;
		s.Pop(temp);
		cout<<temp<<' ';
	}
};
void Calculator::Clear()
{
	s.MakeEmpty();
}

 

 

主函数:

#include"Calcuator.h"
#include<fstream>
char expression[100];
int k=0;
int icp(char);
int isp(char);
void postfix()
{
	SeqStack<char> s;
	char ch='=',ch1,op,temp='=';
	s.Push(ch);
	cin>>ch;
	while(!s.IsEmpty())
	{
		if(isdigit(ch)||ch=='.')
		{
			expression[k++]=ch;
			temp=ch;
			cin>>ch;
		}
		else
		{
			expression[k++]=' ';
			if(ch=='-'&&!isdigit(temp)&&temp!=')')
			{
				expression[k++]=ch;
				temp=ch;
				cin>>ch;
			}
			else
			{
			s.getTop(ch1);
			if(isp(ch1)<icp(ch))
			{
				s.Push(ch);
				temp=ch;
				cin>>ch;
			}
			else
				if(isp(ch1)>icp(ch))
				{
					s.Pop(op);
					expression[k++]=op;
					expression[k++]=' ';
				}
				else
				{
					s.Pop(op);
					if(op=='(')
					{
						temp=ch;
						cin>>ch;
					}
				}
			}
		}
	}
}
int icp(char a)
{
	switch(a)
	{
	case '=':return 0;
	case '(':return 6;
	case '*':
	case '/':
	case '%':return 4;
	case '+':
	case '-':return 2;
	case ')':return 1;
	}
}
int isp(char a)
{
	switch(a)
	{
	case '=':return 0;
	case '(':return 1;
	case '*':
	case '/':
	case '%':return 5;
	case '+':
	case '-':return 3;
	case ')':return 6;
	}
}
void main()
{
	Calculator a;
	cout<<"输入表达式:";
	postfix();
	expression[k]='#';

	ofstream ifile;
	ifile.open("temp.txt");
	if(!ifile)
	{
	cout<<"不能打开文件"<<endl;
	return;
	}
	for(int i=0;i<=k;i++)
		ifile<<expression[i];
	ifile.close();
	cout<<"结果是:";
	a.Run();
	cout<<endl;
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值