希冀作业

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef long long ll;
using namespace std;
//#include<sstream>
//int main() {
//	stringstream s("11 1 11  ");
//	while (s >> val) {
//
//	}
//}



string s;
struct ele
{
	string sx; bool isops;
	ele* left, * right;
	ele() { left = right = NULL; }
};
void init()
{
	int i = 0;
	while (i < s.length())
	{
		if (s[i] == ' ')
		{
			s = s.substr(0, i) + s.substr(i + 1, s.length() - i - 1);
			i--;
		}i++;
	}
}
void frontgo(ele* p)
{
	ele* to = p;
	cout << to->sx;
	if (!to->isops)cout << '#';
	if (to->left)frontgo(to->left);
	if (to->right)frontgo(to->right);
}
void midgo(ele* p)
{
	ele* to = p;
	if (to->left)midgo(to->left);
	cout << to->sx;
	if (!to->isops)cout << '#';
	if (to->right)midgo(to->right);
	
}
void lastgo(ele* p)
{
	ele* to = p;
	if (to->left)lastgo(to->left);
	if (to->right)lastgo(to->right);
	cout << to->sx;
	if (!to->isops)cout << '#';
	
}
void ccgo(ele* p)
{
	queue<ele*>q; q.push(p);
	while (!q.empty())
	{
		ele* now = q.front(); q.pop();
		cout << now->sx; if (!now->isops)cout << '#';
		if (now->left)q.push(now->left);
		if (now->right)q.push(now->right);
	}
}
stack<int>stt;
int tonum(string s)
{
	int num = 0;
	for (int i = 0; i < s.length(); i++)num = num * 10 + s[i] - '0';
	return num;
}
//int frontgocal(ele* p)
//{
//	if (p->left->isops and !p->right->isops) 
//	{ 
//		frontgocal(p->left); int n = stt.top(); stt.pop();
//		
//	}
//	if (p->left->isops and p->right->isops)
//	{
//		frontgocal(p->left); frontgocal(p->right);
//
//	}
//	if (p->right->isops and !p->left->isops) { frontgocal(p->right); }
//	if(!p->left->isops and !p->right->isops)
//	{
//		if (p->sx == "+")stt.push(tonum(p->left->sx) + tonum(p->right->sx));
//		else if(p->sx=="*")stt.push(tonum(p->left->sx) * tonum(p->right->sx));
//		else if(p->sx =="/")stt.push(tonum(p->left->sx) / tonum(p->right->sx));
//		else stt.push(tonum(p->left->sx) - tonum(p->right->sx));
//	}
//		
//}
int toa(string s, int l, int r)
{
	int ans = 0;
	for (int i = l; i < r; i++)
		ans = ans * 10 + s[i] - '0';
	return ans;
}
int main()
{
	//中缀转树
	getline(cin,s);
	init();
	map<string, int>yxj;
	yxj["+"] = yxj["-"] = 1;
	yxj["*"] = yxj["/"] = 2;
	yxj["("] = 0;
	stack<ele*>st;
	stack<string>ops;
	for (int i = 0; i < s.length(); i++)
	{
		string num;
		while (s[i] >= '0' and s[i] <= '9') { num += s[i]; i++; }
		ele* now = new ele;
		now->isops = false; now->sx = num;//now:存储数字
		if(now->sx.length())st.push(now);
		if (s[i] == '(')
		{
			ops.push(string(1,s[i]));
		}
		else if (s[i] == ')')
		{
			string opnow = ops.top();
			while (opnow != "(")
			{
				ops.pop();
				ele* num2 = st.top(); st.pop();
				ele* num1 = st.top(); st.pop();
				ele* nex = new ele;
				nex->isops = true; nex->sx = opnow;
				nex->left = num1, nex->right = num2;
				st.push(nex);
				opnow = ops.top();
			}
			ops.pop();
		}
		else if (s[i] != '=')
		{
			while (!ops.empty() and yxj[ops.top()] >= yxj[string(1,s[i])])
			{
				string opnow = ops.top(); ops.pop();
				ele* num2 = st.top(); st.pop();
				ele* num1 = st.top(); st.pop();
				ele* nex = new ele;
				nex->isops = true; nex->sx = opnow;
				nex->left = num1, nex->right = num2;
				st.push(nex);
			}
			ops.push(string(1,s[i]));
		}
		else
		{
			while (!ops.empty())
			{
				string opnow = ops.top(); ops.pop();
				ele* num2 = st.top(); st.pop();
				ele* num1 = st.top(); st.pop();
				ele* nex = new ele;
				nex->isops = true; nex->sx = opnow;
				nex->left = num1, nex->right = num2;
				st.push(nex);
			}
		}
	}
	ele* head = st.top();
	frontgo(head);
	cout << endl;
	midgo(head); cout << endl;
	lastgo(head); cout << endl;
	ccgo(head); cout << endl;
	ele* h = head;
	int ans = 0;
	map<char, int>opsk;
	opsk['-'] = 1;
	opsk['+'] = 1;
	opsk['*'] = 2;
	opsk['/'] = 2;
	opsk['('] = 0;
	stack<char>sta;
	stack<int>num;
	int i = 0; ll j = 0;
	while (s[j] != '=')
	{
		while (j < s.length() - 1 and s[j] >= '0' and s[j] <= '9')j++;
		if (s[i] <= '9' and s[i] >= '0')num.push(toa(s, i, j));
		if (s[j] != '=')
		{
			if (s[j] != ')')
			{
				while (!sta.empty() and opsk[sta.top()] >= opsk[s[j]] and s[j] != '(')
				{
					int ans = 0;
					char op = sta.top();
					sta.pop();
					int b = num.top(); num.pop();
					int a = num.top(); num.pop();
					if (op == '+')
					{
						ans = a + b;
					}
					else if (op == '-')
					{
						ans = a - b;
					}
					else if (op == '*')
						ans = a * b;
					else ans = a / b;
					num.push(ans);
				}
				sta.push(s[j]);
			}
			else
			{
				while (sta.top() != '(')
				{
					int ans = 0;
					char op = sta.top();
					sta.pop();
					int b = num.top(); num.pop();
					int a = num.top(); num.pop();
					if (op == '+')
					{
						ans = a + b;
					}
					else if (op == '-')
					{
						ans = a - b;
					}
					else if (op == '*')
						ans = a * b;
					else ans = a / b;
					num.push(ans);
				}
				sta.pop();
			}
			j++; i = j;
		}
	}
	while (!sta.empty())
	{
		int ans = 0;
		char op = sta.top();
		sta.pop();
		int b = num.top(); num.pop();
		int a = num.top(); num.pop();
		if (op == '+')
		{
			ans = a + b;
		}
		else if (op == '-')
		{
			ans = a - b;
		}
		else if (op == '*')
			ans = a * b;
		else ans = a / b;
		num.push(ans);
	}
	cout << num.top();
	return 0;
}

posted on 2024-05-12 15:38  WHUStar  阅读(8)  评论(0编辑  收藏  举报