用二叉树实现中缀表达式转后缀表达式并构建计算机求值
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
string ShowMiddleExpression() {
return e;
}
//计算函数,输入为两个数和一个操作符,返回值为计算结果(字符串形式)
string calculate(string num2, string num1, string op) {
double n2, n1;
string result;
number += str[i];
i++;
}
expression += number;
//加空格以区别
expression += " ";
number = "";
continue;
} else {
//判断是括号还是运算符
if (x == '(') {
op.push(x);
//判断是否为负号
if (str[i+1] == '-') {
if (op == "/")
x << (n2 / n1);
x >> result;
x.clear();
return result;
}
//判别符号的优先级
int Priority(char a) {
if (a == '(')
return 0;
else if (a == '+' || a == '-')
return 1;
else if (a == '*' || a == '/')
return 2;
return 0;
}
//用于去除输入中多余的空格输入为要除去空格的算术表达式返回去掉空格的算术表达式
string RidSpace(string origin) {
string transfer;
for (int i = 0; i < origin.length(); i++) {
string tem = "";
for (int i = 0; i < le.length(); ) {
while (le[i] != ' ') {
tem += le[i];
i++;
}
//若为数字,压入结点栈
if (tem.length() > 1 || (tem.length() == 1 && tem[0] >= '0' && tem[0] <= '9')) {
}
//弹出右括号
op.pop();
} else {
//弹出栈中优先级较高的运算符运算符后加空格以区别
while (!op.empty() && (Priority(op.top()) >= Priority(x))) {
expression += op.top();
expression += " ";
if (origin[i] != ' ')
transfer += origin[i];
}
return transfer;
}
//中缀表达式转后缀表达式,因为要兼容多位数,括号,负数和小数等功能,
//由于多位数在转为后缀表达式时会分不清,故在每个数和运算符后面加上一个空格作为区别
//expression为输出的后缀表达式
}
calculator_tree(string expression) {
getexpression(expression);
}
//获得算术表达式
void getexpression(string expression) {
e = expression;
}
~calculator_tree() {
}
//返回中缀表达式
number = "-";
i++;
}
} else {
//遇到右括号直接弹出运算符栈里的运算符到表达式中运算符后加空格以区别
if (x == ')') {
while (op.top() != '(') {
expression += op.top();
expression += " ";
op.pop();
{
if(p) {
//假如为符号执行计算后返回计算结果
if ((p->father).length() == 1 && Priority((p->father)[0])) {
PostOrder(p->right);
return calculate(PostOrder(p->left), PostOrder(p->right), p->father);
}
return expression;
}
//获得后缀表达式
string GetLastExpression() {
return MidToLast(RidSpace(e));
}
//生成一棵二叉树
void makeTree(TNode *&p) {
stack <TNode*> Nodes;
string le = GetLastExpression();
string father;
TNode *left, *right;
TNode()
{
left = right = NULL;
}
TNode(string a)
{
father = a;
left = right = NULL;
}
};
class calculator_tree
{
public:
calculator_tree() {
//第一位做特殊处理判断是否为负号
if (i == 0 && str[i] == '-') {
number = "-";
i++;
}
x = str[i];
//录入数字
if ((x >= '0' && x <= '9') || number == "-" || x == '.') {
while ((str[i] >= '0' && str[i] <= '9') || str[i] == '.') {
string MidToLast(string str) {
str = RidSpace(str);
string expression = "";
string number = "";
char x;
stack<char> op;
for (int i = 0; i < str.length(); ) {
stringstream x;
x << num2;
x >> n2;
x.clear();
x << num1;
x >> n1;
x.clear();
if (op == "+")
x << (n2 + n1);
if (op == "-")
x << (n2 - n1);
if (op == "*")
x << (n2 * n1);
op.pop();
}
//判断是否为负号
if (str[i+1] == '-') {
number = "-";
i++;
}
//将运算符压入栈
op.push(x);
}
}
}
i++;
}
while (!op.empty()) {
expression += op.top();
expression += " ";
op.pop();
p =new TNode(tem);
Nodes.push(p);
} else {
//来自百度文库为运算符且栈非空将栈顶元素拿出来分别作为左子树跟右子树的结点
//处理如-()的情况
if (Nodes.empty()) {
tem = "-";
i++;
continue;
}
p = new TNode(tem);
if (!Nodes.empty()) {
p -> right = Nodes.top();
Nodes.pop();
}
if (!Nodes.empty()) {
p -> left = Nodes.top();
Nodes.pop();
}
Nodes.push(p);
}
i++;
tem = "";
}
}
//后序遍历二叉树并计算将计算结果返回
string PostOrder(TNode *p)
#include <string>
#include <stack>
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
//节点类
class TNode
{
public:
friend class calculator_tree;
} else {
//假如为数字,直接返回
return p->father;
}
}
}
private:
//存放输入的表达式
string e;
};
return e;
}
//计算函数,输入为两个数和一个操作符,返回值为计算结果(字符串形式)
string calculate(string num2, string num1, string op) {
double n2, n1;
string result;
number += str[i];
i++;
}
expression += number;
//加空格以区别
expression += " ";
number = "";
continue;
} else {
//判断是括号还是运算符
if (x == '(') {
op.push(x);
//判断是否为负号
if (str[i+1] == '-') {
if (op == "/")
x << (n2 / n1);
x >> result;
x.clear();
return result;
}
//判别符号的优先级
int Priority(char a) {
if (a == '(')
return 0;
else if (a == '+' || a == '-')
return 1;
else if (a == '*' || a == '/')
return 2;
return 0;
}
//用于去除输入中多余的空格输入为要除去空格的算术表达式返回去掉空格的算术表达式
string RidSpace(string origin) {
string transfer;
for (int i = 0; i < origin.length(); i++) {
string tem = "";
for (int i = 0; i < le.length(); ) {
while (le[i] != ' ') {
tem += le[i];
i++;
}
//若为数字,压入结点栈
if (tem.length() > 1 || (tem.length() == 1 && tem[0] >= '0' && tem[0] <= '9')) {
}
//弹出右括号
op.pop();
} else {
//弹出栈中优先级较高的运算符运算符后加空格以区别
while (!op.empty() && (Priority(op.top()) >= Priority(x))) {
expression += op.top();
expression += " ";
if (origin[i] != ' ')
transfer += origin[i];
}
return transfer;
}
//中缀表达式转后缀表达式,因为要兼容多位数,括号,负数和小数等功能,
//由于多位数在转为后缀表达式时会分不清,故在每个数和运算符后面加上一个空格作为区别
//expression为输出的后缀表达式
}
calculator_tree(string expression) {
getexpression(expression);
}
//获得算术表达式
void getexpression(string expression) {
e = expression;
}
~calculator_tree() {
}
//返回中缀表达式
number = "-";
i++;
}
} else {
//遇到右括号直接弹出运算符栈里的运算符到表达式中运算符后加空格以区别
if (x == ')') {
while (op.top() != '(') {
expression += op.top();
expression += " ";
op.pop();
{
if(p) {
//假如为符号执行计算后返回计算结果
if ((p->father).length() == 1 && Priority((p->father)[0])) {
PostOrder(p->right);
return calculate(PostOrder(p->left), PostOrder(p->right), p->father);
}
return expression;
}
//获得后缀表达式
string GetLastExpression() {
return MidToLast(RidSpace(e));
}
//生成一棵二叉树
void makeTree(TNode *&p) {
stack <TNode*> Nodes;
string le = GetLastExpression();
string father;
TNode *left, *right;
TNode()
{
left = right = NULL;
}
TNode(string a)
{
father = a;
left = right = NULL;
}
};
class calculator_tree
{
public:
calculator_tree() {
//第一位做特殊处理判断是否为负号
if (i == 0 && str[i] == '-') {
number = "-";
i++;
}
x = str[i];
//录入数字
if ((x >= '0' && x <= '9') || number == "-" || x == '.') {
while ((str[i] >= '0' && str[i] <= '9') || str[i] == '.') {
string MidToLast(string str) {
str = RidSpace(str);
string expression = "";
string number = "";
char x;
stack<char> op;
for (int i = 0; i < str.length(); ) {
stringstream x;
x << num2;
x >> n2;
x.clear();
x << num1;
x >> n1;
x.clear();
if (op == "+")
x << (n2 + n1);
if (op == "-")
x << (n2 - n1);
if (op == "*")
x << (n2 * n1);
op.pop();
}
//判断是否为负号
if (str[i+1] == '-') {
number = "-";
i++;
}
//将运算符压入栈
op.push(x);
}
}
}
i++;
}
while (!op.empty()) {
expression += op.top();
expression += " ";
op.pop();
p =new TNode(tem);
Nodes.push(p);
} else {
//来自百度文库为运算符且栈非空将栈顶元素拿出来分别作为左子树跟右子树的结点
//处理如-()的情况
if (Nodes.empty()) {
tem = "-";
i++;
continue;
}
p = new TNode(tem);
if (!Nodes.empty()) {
p -> right = Nodes.top();
Nodes.pop();
}
if (!Nodes.empty()) {
p -> left = Nodes.top();
Nodes.pop();
}
Nodes.push(p);
}
i++;
tem = "";
}
}
//后序遍历二叉树并计算将计算结果返回
string PostOrder(TNode *p)
#include <string>
#include <stack>
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
//节点类
class TNode
{
public:
friend class calculator_tree;
} else {
//假如为数字,直接返回
return p->father;
}
}
}
private:
//存放输入的表达式
string e;
};