【Openjudge】表达式·表达式树·表达式求值

该程序将输入的中缀表达式转换为后缀表达式(逆波兰表示法),然后构建表达式树并进行广度优先遍历计算结果。通过输入变量值,程序可以处理包含加、减、乘、除运算符的表达式,并输出计算后的答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码写的比较烂,有时间再看看别人代码,重写一个好了。
#include<iostream>
#include<stack>
#include<map>
#include<queue>
#include<string>

using namespace std;

bool compare(char c, char c_in_stack){
	if (c == '*' || c == '/'){
		if (c_in_stack == '+' || c_in_stack == '-')
			return true;
	}
	return false;
}

int main()
{
	string s, revs;
	map<char, int> variable_dic;
	int variable_num;

	cin >> s;
	cin >> variable_num;
	for (int i = 0; i < variable_num; i ++){
		char c; int t;
		cin >> c >> t;
		variable_dic[c] = t;
	}

//make the string to a RPN(reverse polish notation)
	int length = s.length();
	stack<char> trans;
	for (int i = 0; i < length; i ++){
		if (s[i] >= 'a' && s[i] <= 'z'){
			revs.push_back(s[i]);
		}
		else if (s[i] == '(' || trans.empty()){
			trans.push(s[i]);
		}
		else if (s[i] == ')'){
			while(trans.top() != '('){
				revs.push_back(trans.top());
				trans.pop();
			}
			trans.pop();
		}
		else{
			if (compare(s[i], trans.top())){
				trans.push(s[i]);
			}
			else{
				while (!trans.empty()  && trans.top() != '(' && !compare(s[i], trans.top())){
					revs.push_back(trans.top());
					trans.pop();
				}
				trans.push(s[i]);
			}
		}
	}
	while(!trans.empty()){
		revs.push_back(trans.top());	trans.pop();
	}

	cout << revs << endl;

	//compute the depth and build the tree and compute the answer
	//node of the tree
	struct _node{
		char data;
		_node *left;
		_node *right;
		int depth;
		_node(char d = 0, _node *l = NULL, _node *r = NULL):data(d), left(l), right(r){
			if (left){
				if (right){
					depth = left->depth > right->depth ? left->depth:right->depth;
				}
				else{
					depth = left->depth;
				}
			}
			else{
				if (right){
					depth = right->depth;
				}
				else{
					depth = -1;
				}
			}
			depth ++;
		};
	};
	class computer{
	public:
		int operator()(int a, int b, char c){
			switch(c){
				case '+':
					return a + b;
				case '-':
					return a - b;
				case '*':
					return a * b;
				case '/':
					return a / b;
				default:
					return 1;
			}
		}
	};

//breadth first traversal
	class bftravel{
	public:
		int power(int a, int b){
			int ans = 1;
			for (int i = 0; i < b ; i ++){
				ans *= a;
			}
			return ans;
		}
		void operator()(_node* root){
			struct node_with_depth{
				_node *pointer;
				int pos;
				node_with_depth(_node* p = NULL, int d = 0):pointer(p), pos(d){};
			};
			int max_depth = root->depth;
			queue<node_with_depth> draw;
			node_with_depth draw_ele(root, power(2, max_depth));
			draw.push(draw_ele);
			while (!draw.empty()){
				string s(power(2, max_depth + 1) + 1, ' ');
				string sbar(power(2, max_depth + 1) + 1, ' ');
				int depth = draw.front().pointer->depth;
				while(!draw.empty() && depth == draw.front().pointer->depth){
					draw_ele = draw.front();
					draw.pop();
					s[draw_ele.pos - 1] = draw_ele.pointer->data;
					if (draw_ele.pointer->left){
						sbar[draw_ele.pos - 2] = '/';
						draw_ele.pointer->left->depth = depth - 1;
						draw.push(node_with_depth(draw_ele.pointer->left, draw_ele.pos - power(2,draw_ele.pointer->depth - 1)));
					}
					if (draw_ele.pointer->right){
						sbar[draw_ele.pos] = '\\';
						draw_ele.pointer->right->depth = depth - 1;
						draw.push(node_with_depth(draw_ele.pointer->right, draw_ele.pos + power(2, draw_ele.pointer->depth - 1)));
					}
				}
				s[draw_ele.pos] = 0;
				if (draw.empty()){
					sbar[0] = 0;
				}
				else{
					int i = draw_ele.pos + 2;
					while (sbar[i] == ' ')
						sbar[i --] = 0;
					sbar[i + 1] = '\n';
				}
				cout << s.c_str() << endl << sbar.c_str();
			}
		}
	};

	stack<_node*> compute_stack;
	stack<int> compute_ans;
	_node *pointer;
	length = revs.length();

	//build the tree and compute the answer for the formula
	for (int i = 0; i < length; i ++){
		if (revs[i] >= 'a' && revs[i] <= 'z'){
			pointer = new _node(revs[i]);
			compute_stack.push(pointer);
			compute_ans.push(variable_dic[revs[i]]);
		}
		else{
			_node *right_child = compute_stack.top();	compute_stack.pop();
			_node *left_child = compute_stack.top();	compute_stack.pop();
			pointer = new _node(revs[i], left_child, right_child);
			compute_stack.push(pointer);

			int b = compute_ans.top(); compute_ans.pop();
			int a = compute_ans.top(); compute_ans.pop();
			compute_ans.push(computer()(a, b, revs[i]));
		}
	}

	bftravel()(pointer);
	cout << compute_ans.top() << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值