数算 表达式·表达式树·表达式求值 二叉树

表达式·表达式树·表达式求值

题目内容:
众所周知,任何一个表达式,都可以用一棵表达式树来表示。例如,表达式a+b*c,可以表示为如下的表达式树:

   +
  / \
 a   *
    / \
    b c

现在,给你一个中缀表达式,这个中缀表达式用变量来表示(不含数字),请你将这个中缀表达式用表达式二叉树的形式输出出来。

输入格式:
输入分为三个部分。
第一部分为一行,即中缀表达式(长度不大于50)。中缀表达式可能含有小写字母代表变量(a-z),也可能含有运算符(+、-、*、/、小括号),不含有数字,也不含有空格。
第二部分为一个整数n(n < 10),表示中缀表达式的变量数。
第三部分有n行,每行格式为C x,C为变量的字符,x为该变量的值。

输出格式:
输出分为三个部分,第一个部分为该表达式的逆波兰式,即该表达式树的后根遍历结果。占一行。
第二部分为表达式树的显示,如样例输出所示。如果该二叉树是一棵满二叉树,则最底部的叶子结点,分别占据横坐标的第1、3、5、7……个位置(最左边的坐标是1),然后它们的父结点的横坐标,在两个子结点的中间。如果不是满二叉树,则没有结点的地方,用空格填充(但请略去所有的行末空格)。每一行父结点与子结点中隔开一行,用斜杠(/)与反斜杠(\)来表示树的关系。/出现的横坐标位置为父结点的横坐标偏左一格,\出现的横坐标位置为父结点的横坐标偏右一格。也就是说,如果树高为m,则输出就有2m-1行。
第三部分为一个整数,表示将值代入变量之后,该中缀表达式的值。需要注意的一点是,除法代表整除运算,即舍弃小数点后的部分。同时,测试数据保证不会出现除以0的现象。

输入样例:
a+b*c
3
a 2
b 7
c 5

输出样例:

abc*+
   +
  / \
 a   *
    / \
    b c
37
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stack>
using namespace std;

bool judgeletter(char c)
{
	if (c >= 'a'&&c <= 'z')
		return 1;
	else
		return 0;
}
struct number
{
	char c;
	int val;
};
number a[10];
int n;
struct tree
{
	tree* left;
	tree* right;
	int deep;
	char c;
	tree()
	{
		left = NULL;
		right = NULL;
	}
};
int cur;
void buildtree(string s, tree* root)
{
	if (cur < 0)
		return;
	if (judgeletter(s[cur]))
	{
		root->right = new tree;
		root->right->c = s[cur];
		root->right->deep = root->deep + 1;
		cur--;
	}
	else
	{
		root->right = new tree;
		root->right->c = s[cur];
		root->right->deep = root->deep + 1;
		cur--;
		buildtree(s, root->right);
	}
	if (judgeletter(s[cur]))
	{
		root->left = new tree;
		root->left->c = s[cur];
		root->left->deep = root->deep + 1;
		cur--;
	}
	else
	{
		root->left = new tree;
		root->left->c = s[cur];
		root->left->deep = root->deep + 1;
		cur--;
		buildtree(s, root->left);
	}
}
int deep;
void getdeep(tree* root)
{
	deep = max(deep, root->deep);
	if (root->left != NULL)
		getdeep(root->left);
	if (root->right != NULL)
		getdeep(root->right);
}
char buf[1000][1000];
void out(int d, tree* root, int le, int ri, int l)
{
	buf[2 * d - 1][(le + ri) / 2] = root->c;
	if (d != 1)
	{
		if (l == 1)
			buf[2 * d - 2][ri] = '/';
		else
			buf[2 * d - 2][le] = '\\';
	}
	if (root->left != NULL)
		out(d + 1, root->left, le, (le + ri) / 2 - 1, 1);
	if (root->right != NULL)
		out(d + 1, root->right, (le + ri) / 2 + 1, ri, 0);
}
int c2i(char c)
{
	for (int i = 0; i < n; i++)
	{
		if (a[i].c == c)
			return a[i].val;
	}
}
bool judge(int i, int j, int n)
{
	for (int k = j + 1; k <= n; k++)
	{
		if (buf[i][k] != ' ')
			return 1;
	}
	return 0;
}
int main()
{
	stack<char> s;
	string be;
	char c;
	c = getchar();
	while (c != '\n')
	{
		if (judgeletter(c))
		{
			cout << c;
			be += c;
			c = getchar();
			continue;
		}
		else
		{
			if (c == '(')
				s.push(c);
			else if (c == ')')
			{
				char t = s.top();
				s.pop();
				while (t != '(')
				{
					cout << t;
					be += t;
					t = s.top();
					s.pop();
				}
			}
			else if (c == '*' || c == '/')
			{
				while (!s.empty())
				{
					char t = s.top();
					if (t == '(' || t == '+' || t == '-')
						break;
					cout << t;
					be += t;
					s.pop();
				}
				s.push(c);
			}
			else if (c == '+' || c == '-')
			{
				while (!s.empty())
				{
					char t = s.top();
					if (t == '(')
						break;
					cout << t;
					be += t;
					s.pop();
				}
				s.push(c);
			}
		}
		c = getchar();
	}
	while (!s.empty())
	{
		char t = s.top();
		s.pop();
		be += t;
		cout << t;
	}
	cout << endl;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i].c >> a[i].val;
	}
	int l = be.size();
	tree* root=new tree;
	root->c = be[l - 1];
	root->deep = 1;
	cur = l - 2;
	buildtree(be, root);
	deep = 1;
	getdeep(root);
	memset(buf, ' ', sizeof(buf));
	out(1, root, 1, pow(2.0, deep) - 1, 0);
	for (int i = 1; i <= 2 * deep - 1; i++)
	{
		for (int j = 1; j <= pow(2.0, deep) - 1; j++)
		{
			cout << buf[i][j];
			if (buf[i][j] != ' ')
			{
				if (judge(i, j, pow(2.0, deep) - 1))
					continue;
				else
					break;
			}
		}
		cout << endl;
	}
	stack<int> s2;
	int result = 0;
	for (int i = 0; i < l; i++)
	{
		if (judgeletter(be[i]))
			s2.push(c2i(be[i]));
		else
		{
			int ope1;
			int ope2;
			ope2 = s2.top();
			s2.pop();
			ope1 = s2.top();
			s2.pop();
			switch (be[i])
			{
			case '+':ope1 = ope1 + ope2; break;
			case '-':ope1 = ope1 - ope2; break;
			case '*':ope1 = ope1 * ope2; break;
			case '/':ope1 = ope1 / ope2; break;
			}
			s2.push(ope1);
		}
	}
	result = s2.top();
	cout << result << endl;

    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值