问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
#include <string>
#include <stack>
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
//节点类
class TNode
{
public:
friend class calculator_tree;
string father;
TNode *left, *right;
TNode()
{
left = right = NULL;
}
TNode(string a)
{
father = a;
left = right = NULL;
}
};
class calculator_tree
{
public:
calculator_tree() {
}
calculator_tree(string expression) {
getexpression(expression);
}
//获得算术表达式
void getexpression(string expression) {
e = expression;
}
~calculator_tree() {
}
//返回中缀表达式
string ShowMiddleExpression() {
return e;
}
//计算函数, 输入为两个数和一个操作符, 返回值为计算结果(字符串形式)
string calculate(string num2, string num1, string op) {
double n2, n1;
string result;
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);
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++) {
if (origin[i] != ' ')
transfer += origin[i];
}
return transfer;
}
//中缀表达式转后缀表达式, 因为要兼容多位数, 括号, 负数和小数等功能,
//由于多位数在转为后缀表达式时会分不清, 故在每个数和运算符后面加上一个空格作为区别
//expression为输出的后缀表达式
string MidToLast(string str) {
str = RidSpace(str);
string expression = "";
string number = "";
char x;
stack<char> op;
for (int i = 0; i < str.length(); ) {
//第一位做特殊处理 判断是否为负号
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] == '.') {
number += str[i];
i++;
}
expression += number;
//加空格以区别
expression += " ";
number = "";
continue;
} else {
//判断是括号还是运算符
if (x == '(') {
op.push(x);
//判断是否为负号
if (str[i+1] == '-') {
number = "-";
i++;
}
} else {
//遇到右括号直接弹出运算符栈里的运算符到表达式中 运算符后加空格以区别
if (x == ')') {
while (op.top() != '(') {
expression += op.top();
expression += " ";
op.pop();
}
//弹出右括号
op.pop();
} else {
//弹出栈中优先级较高的运算符 运算符后加空格以区别
while (!op.empty() && (Priority(op.top()) >= Priority(x))) {
expression += op.top();
expression += " ";
op.pop();
}
//判断是否为负号
if (str[i+1] == '-') {
number = "-";
i++;
}
//将运算符压入栈
op.push(x);
}
}
}
i++;
}
while (!op.empty()) {
expression += op.top();
expression += " ";
op.pop();
}
return expression;
}
//获得后缀表达式
string GetLastExpression() {
return MidToLast(RidSpace(e));
}
//生成一棵二叉树
void makeTree(TNode *&p) {
stack <TNode*> Nodes;
string le = GetLastExpression();
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')) {
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)
{
if(p) {
//假如为符号 执行计算后返回计算结果
if ((p->father).length() == 1 && Priority((p->father)[0])) {
PostOrder(p->right);
return calculate(PostOrder(p->left), PostOrder(p->right), p->father);
} else {
//假如为数字, 直接返回
return p->father;
}
}
}
private:
//存放输入的表达式
string e;
};
上面是代码
为什么运行提示这个:
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用
有大佬解答一下吗