中缀表达式转后缀表达式并求值

应杨茜小师妹的请求写了这个程序,找找当年的感觉,呵呵。我把中缀转后缀并求值的代码写成了一个类,注释写的很详细,不多说。首先是类声明:

#ifndef _CALSUFFIX_H_
#define _CALSUFFIX_H_

#include <iostream>
#include <stack>
#include <string>

using namespace std;

//中缀转后缀并求值的类
class CalSuffix
{
    private:
	string infix; //中缀表达式
	stack<char> oper; //存取操作符和操作数的栈,用于中缀变后缀
	string suffix; //后缀表达式
	stack<bool> intermedia; //用于存储后缀表达式的中间结果
	bool result; //计算结果
    public:
	CalSuffix(); //默认构造函数
	CalSuffix(string input); //构造函数
	~CalSuffix(); //7构函数

	void InfixToSuffix(); //中缀转后缀
	void Calculate(); //求后缀表达式的值

	string GetSuffix(); //获取后缀表达式
	bool GetResult(); //获取计算结果
};

#endif

然后是类定义:

#include <iostream>
#include <stack>
#include <string>

#include "calsuffix.h"

using namespace std;

CalSuffix::CalSuffix()
{
    result = false;
}

CalSuffix::CalSuffix(string input)
{
    infix = input;
    result = false;
}

CalSuffix::~CalSuffix()
{
}

void CalSuffix::InfixToSuffix()
{
    string::iterator infixIter; //suffix的迭代器,用于逐个字符扫描suffix

    //中缀转后缀
    for(infixIter = infix.begin(); infixIter != infix.end(); ++ infixIter)
    {
	switch(*infixIter)
	{
	    //若是V或F,直接放入后缀表达式字符串中
	    case 'V':
	    case 'F':
		suffix += *infixIter;
		break;

	    //若是(,压入操作符栈
	    case '(':
		oper.push('(');
		break;

	    //若是!,压入操作符栈
	    case '!':
		oper.push('!');
		break;

	    //若是&,且栈顶操作符为!或&,则将栈顶操作符放入后缀表达式并弹栈,然后将&压入操作符栈;否则直接压入
	    case '&':
		if(!oper.empty() && (oper.top() == '!' || oper.top() == '&'))
		{
		    suffix += oper.top();
		    oper.pop();
		}
		oper.push('&');
		break;
		
	    //若是|,且栈顶操作符为!或&或|,则将栈顶操作符放入后缀表达式并弹栈,然后将|压入操作符栈;否则直接压入
	    case '|':
		if(!oper.empty() && (oper.top() == '!' || oper.top() == '&' || oper.top() == '|'))
		{
		    suffix += oper.top();
		    oper.pop();
		}
		oper.push('|');
		break;

	    //若是),则将栈顶操作符放入后缀表达式并弹栈,直至弹出(
	    case ')':
		while(oper.top() != '(')
		{
		    suffix += oper.top();
		    oper.pop();
		}
		oper.pop();
		break;

	    default: break;
	}
    } 
    //将栈中剩余的操作符全部放入后缀表达式中
    while(!oper.empty())
    {
	suffix += oper.top();
	oper.pop();
    }
}

void CalSuffix::Calculate()
{
    bool op1, op2; //操作数
    string::iterator iter; //用于遍历suffix的迭代器

    //计算后缀表达式的值
    for(iter = suffix.begin(); iter != suffix.end(); ++ iter)
    {
	switch(*iter)
	{
	    //若是V,直接压入中间结果栈
	    case 'V':
		intermedia.push(true);
		break;

	    //若是F,直接压入中间结果栈
	    case 'F':
		intermedia.push(false);
		break;

	    //若是!,弹出栈顶元素并做!运算,将结果压回栈中
	    case '!':
		op1 = intermedia.top();
		intermedia.pop();
		intermedia.push(!op1);
		break;

	    //若是&,弹出栈顶两个元素并做&运算,将结果压回栈中
	    case '&':
		op1 = intermedia.top();
		intermedia.pop();
		op2 = intermedia.top();
		intermedia.pop();
		intermedia.push(op1 & op2);
		break;

	    //若是|,弹出栈顶两个元素并做|运算,将结果压回栈中
	    case '|':
		op1 = intermedia.top();
		intermedia.pop();
		op2 = intermedia.top();
		intermedia.pop();
		intermedia.push(op1 | op2);
		break;

	    default: break;
	}
    }
    //栈中的最后一个元素就是计算结果
    result = intermedia.top();
    intermedia.pop();
}

string CalSuffix::GetSuffix()
{
    return suffix;
}

bool CalSuffix::GetResult()
{
    return result;
}

测试代码也贴上:

#include <iostream>
#include <string>

#include "calsuffix.h"

using namespace std;

int main()
{
    string expression;

    cout << "Input infix expression: " << endl;
    cin >> expression;

    CalSuffix cal(expression);
    cal.InfixToSuffix();
    cout << "Suffix expression is: \n" << cal.GetSuffix() << endl;

    cal.Calculate();
    cout << "The result is: \n" << cal.GetResult() << endl;
  
    return 0;
}

就这样吧!我记得当年我们做课程设计的时候好像也有这么一道题,不过我没选。自己选的什么也记不得了。


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值