/*学号:2014329620069 姓名:张卓鹏 班级:14计科2*/
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stack>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<sstream>
using namespace std;
typedef struct tree{
string data;
tree *lchild;
tree *rchild;
}Bitree, *pBitree;
stack<string>sta1;
stack<string>sta2;
int turn_str(char s[], string str[])//将计算表达式储存在string类中。方便以后将数字从字符型转换成数据
{
int i, j = 0, k = 0, count = 0;
for (i = 0; s[i] != ' '; i++)
{
if ((s[i] == '+') || (s[i] == '/') || (s[i] == '*') || (s[i] == '-') || (s[i] == '(') || (s[i] == ')'))
{
str[j] += s[i];
}
else
{
for (; s[i] != '+' && s[i] != '-' &&s[i] != '*' &&s[i] != '/' &&s[i] != ')'&& i < strlen(s); i++)
{
str[j] += s[i];
}
i--;
}
j++;
count++;
}
return count;
}
int judge(string str)
{
string s;
s = sta2.top();
if (str == "+" || str == "-"){
if (s == "+" || s == "-" || s == "*" || s == "/")
return 1;
else
return 0;
}
else if (str == "*" || str == "/")
{
if (s == "*" || s == "/")
return 1;
else
return 0;
}
}
void turn_postorder(string str[], int count) //变为后缀表达式
{
string temp;
for (int i = 0; i <count; i++)
{
if (str[i] == "+" || str[i] == "-" || str[i] == "*" || str[i] == "/")
{
while (!sta2.empty() && judge(str[i]))
{
temp += sta2.top();
sta1.push(temp);
sta2.pop();
temp = "";
}
sta2.push(str[i]);
}
else if (str[i] == "(")
{
sta2.push(str[i]);
}
else if (str[i] == ")")
{
while ((temp += sta2.top()) != "(")
{
sta1.push(temp);
sta2.pop();
temp = "";
}
sta2.pop();
}
else
{
sta1.push(str[i]);
}
temp = "";
}
while (!sta2.empty())
{
temp += sta2.top();
sta2.pop();
sta1.push(temp);
temp = "";
}
/*while (!sta1.empty()) //测试栈中的数据
{
temp += sta1.top();
cout<<temp<<endl;
sta1.pop();
temp = "";
}*/
}
void creat_tree(pBitree &root) //通过后序表达式建树
{
string temp;
if (!sta1.empty())
{
temp += sta1.top();
sta1.pop();
}
root = new Bitree;
root->data += temp;
if (temp != "+" && temp != "-" && temp != "*" && temp != "/")
{
root->rchild = NULL;
root->lchild = NULL;
return;
}
else
{
creat_tree(root->rchild);
creat_tree(root->lchild);
}
}
void preorder(pBitree root)
{
if (root != NULL)
{
cout << root->data << " ";
preorder(root->lchild);
preorder(root->rchild);
}
}
void postorder(pBitree T) //后序遍历 递归
{
if (T)
{
postorder(T->lchild);
postorder(T->rchild);
cout << T->data << " ";
}
}
void inorder(pBitree T) //中序遍历 递归
{
if (T)
{
inorder(T->lchild);
cout << T->data << " ";
inorder(T->rchild);
}
}
double str_turn_double(string str) //字符串转为double
{
int i = 0, flag = 0, c = 1;
double n = 0;
while (str[i] != ' ')
{
if (str[i] != '.' && flag == 0)
n = n * 10 + (str[i] - '0');
else
{
if (flag == 0)
{
i++;
n = n + (str[i] - '0')*pow(0.1, c);
}
else
{
n = n + (str[i] - '0')*pow(0.1, c);
}
c++;
flag++;
}
i++;
}
return n;
}
string double_turn_str(double d) { //double转为string
ostringstream os;
if (os << d)
return os.str();
}
string result(pBitree root) //从树中计算表达式的值
{
string num1, num2;
double n1, n2;
if (root->data == "+"){
num1 = result(root->lchild);
n1 = str_turn_double(num1);
num2 = result(root->rchild);
n2 = str_turn_double(num2);
root->data = double_turn_str(n1 + n2);
}
else if (root->data == "-"){
num1 = result(root->lchild);
n1 = str_turn_double(num1);
num2 = result(root->rchild);
n2 = str_turn_double(num2);
root->data = double_turn_str(n1 - n2);
}
else if (root->data == "*"){
num1 = result(root->lchild);
n1 = str_turn_double(num1);
num2 = result(root->rchild);
n2 = str_turn_double(num2);
root->data = double_turn_str(n1 * n2);
}
else if(root->data =="/") {
num1 = result(root->lchild);
n1 = str_turn_double(num1);
num2 = result(root->rchild);
n2 = str_turn_double(num2);
root->data = double_turn_str(n1 / n2);
}
return root->data;
}
int main()
{
int count;
char s[50];
string res;
double r;
pBitree root = NULL;
string str[30];
printf("请输入表达式:");
scanf("%s", &s);
count = turn_str(s, str);
turn_postorder(str, count);
creat_tree(root);
preorder(root);
printf("以上先序遍历
");
inorder(root);
printf("以上中序遍历
");
postorder(root);
printf("以上后序遍历
");
printf("结果:");
res=result(root);
cout << res << endl;
system("pause");
return 0;
}
//(5-1)*9+6*9-5-3
//(5-7)*(9-7)-1
//(5-1)-1
//(99.3+44.23)*93/34+9