中缀表达式转后缀表达式

#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<algorithm>
#include<string>
using namespace std;
//stack<char>s1;
const int maxsize = 105;
struct Stack{
	char Data[maxsize];
	int ptr;
	Stack() {
		memset(Data, '\0', sizeof(Data));
		ptr = -1;
	}
	int empty() {
		if (ptr == -1) return 1;
		else return 0;
	}
	int full() {
		if (ptr == maxsize - 1) return 1;
		else return 0;
	}
	void push(char ch) {
		if (!full()) {
			Data[++ptr] = ch;
		}
		else printf("栈满");
	}
	void pop() {
		if (!empty()) {
			Data[ptr] = '\0';
			ptr--;
		}
	}
	char top() {
		if (!empty()) return Data[ptr];
	}
}s1,s2;
map<char, int>mpp;
int calcul(char ch, int num1, int num2) {
	if (ch == '+') return num1 + num2;
	else if (ch == '-') return num1 - num2;
	else if (ch == '*') return num1 * num2;
	else if (ch == '/' && num2 != 0) return num1 / num2;
}
int cal(string ans) {
	int len = ans.length();
	for (int i = 0; i < len; i++) {
		if (isdigit(ans[i])) s2.push(ans[i]);
		else {
			int num1 = s2.top() - '0'; s2.pop();
			int num2 = s2.top() - '0'; s2.pop();
			s2.push(calcul(ans[i], num2, num1) + '0');
		}
	}
	return s2.top() - '0';
}
int main()
{
	string buf, ans = "";
	getline(cin, buf);
	mpp['+'] = 1; mpp['-'] = 1; mpp['*'] = 2; mpp['/'] = 2;
	for (int i = 0; i < buf.length(); i++) {
		if (isdigit(buf[i])) ans += buf[i];
		else if (buf[i] == ' ') continue;
		else {
			char ch = buf[i];
			if (s1.empty() || s1.top() == '(' || ch == '(') s1.push(ch);
			else if (ch == ')') {
				while (ch != '(') {
					ch = s1.top(); s1.pop();
					if (ch != '(') ans += ch;
				}
			}
			else {
				if (mpp[ch] > mpp[s1.top()]) s1.push(ch);
				else {
					while (!s1.empty()&&mpp[ch] <= mpp[s1.top()]) {
						ans += s1.top();
						s1.pop(); 
					}
					s1.push(ch);
				}
			}
		}
	}
	while (!s1.empty()) {
		ans += s1.top();
		s1.pop();
	}
	//cout << ans;
	cout << cal(ans);
}
//2*(9+6/3-5)+4
#include "pch.h"
#include<iostream>
#include<queue>
#include<map>
#include<sstream>
#include<stack>
#include<algorithm>
#include<string>
using namespace std;
const int TreeSize = 105;
typedef int ElementType;
//双亲表示法
typedef struct PTNode {
	ElementType data;
	int parent;
};
typedef struct PTree* TreePtr;
typedef struct PTree {
	PTNode nodes[tree_size];
	int root, num_of_ndoes;
};
TreePtr InitTree() {
	return new PTree();
}
void CreatTree(TreePtr& T) {
	printf("输入树的节点个数:");
	int nodes_num;
	scanf("%d", &nodes_num);
	T->num_of_ndoes = nodes_num;
	for (int i = 0; i < nodes_num; i++) {
		scanf("%d %d", &T->nodes[i].data, &T->nodes[i].parent);
	}
}
//孩子表示法
typedef struct Node {
	ElementType data;
	Node * Next;
}* ChildPtr;
typedef struct {
	ElementType Data;
	ChildPtr Next;
}HeadNode;
typedef struct {
	HeadNode Nodes[TreeSize];
	int NumOfNodes, Root;
}Tree;
int main() {

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值