#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stack>
using namespace std;
typedef struct tree{
char data;
int ltag;
int rtag;
tree *lchild;
tree *rchild;
}Bitree, *pBitree;
pBitree pre = NULL;
stack<char>sta1;
stack<char>sta2;
void creat_tree(pBitree &root)
{
char ch = '#';
if (!sta1.empty())
{
ch = sta1.top();
sta1.pop();
}
root = new Bitree;
root->data = ch;
if (ch != '+' && ch != '-' && ch != '*' && ch != '/')
{
root->rchild = NULL;
root->rtag = 1;
root->ltag = 1;
root->lchild = NULL;
root->rtag = 1;
root->ltag = 1;
return;
}
else
{
creat_tree(root->rchild);
root->ltag = root->rtag = 0;
creat_tree(root->lchild);
root->ltag = root->rtag = 0;
}
}
int judge(char t)
{
char ch = '#';
ch = sta2.top();
switch (t)
{
case '+':
case '-':
{
if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
return 1;
else
return 0;
break;
}
case'*':
case'/':
{
if (ch == '*' || ch == '/')
return 1;
else
return 0;
}
}
}
void turn_postorder(char *str) //变为后缀表达式
{
char ch = '#';
for (int i = 0; i < strlen(str); i++)
{
if ('0' <= str[i] && str[i] <= '9')
{
sta1.push(str[i]);
}
else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
{
while (!sta2.empty() && judge(str[i]))
{
ch = sta2.top();
sta1.push(ch);
sta2.pop();
}
sta2.push(str[i]);
}
else if (str[i] == '(')
{
sta2.push(str[i]);
}
else if (str[i] == ')')
{
while ((ch = sta2.top()) != '(')
{
sta1.push(ch);
sta2.pop();
}
sta2.pop();
}
}
while (!sta2.empty())
{
ch = sta2.top();
sta2.pop();
sta1.push(ch);
}
/*while (!sta1.empty())
{
ch = sta1.top();
printf("%c", ch);
sta1.pop();
}*/
}
char result(pBitree root)
{
char num1, num2;
int n1, n2;
switch (root->data)
{
case '+':
num1 = result(root->lchild); n1 = num1 - '0';
num2 = result(root->rchild); n2 = num2 - '0';
root->data = n1 + n2 + '0';
break;
case'-':
num1 = result(root->lchild); n1 = num1 - '0';
num2 = result(root->rchild); n2 = num2 - '0';
root->data = n1 - n2 + '0';
break;
case '*':
num1 = result(root->lchild); n1 = num1 - '0';
num2 = result(root->rchild); n2 = num2 - '0';
root->data = n1*n2 + '0';
break;
case '/':
num1 = result(root->lchild); n1 = num1 - '0';
num2 = result(root->rchild); n2 = num2 - '0';
root->data = n1 / n2 + '0';
break;
}
return root->data;
}
void increat_thtree(pBitree &root)
{
if (root != NULL)
{
increat_thtree(root->lchild);
if (!root->lchild)
{
root->ltag = 1;
root->lchild = pre;
}
if (!pre->rchild)
{
pre->rtag = 1;
pre->rchild = root;
}
pre = root;
increat_thtree(root->rchild);
}
}
void in_thread_head(pBitree &root)
{
pBitree p;
p = pre;
pre->lchild = root;
pre->ltag = 0;
increat_thtree(root);//中序线索化
root=pre;
pre = p;
pre->rchild = root;
pre->rtag = 1;
root->rchild = pre;
}
void inorder_thread(pBitree p)
{
pBitree root;
root = p->lchild;
while (root!=p)
{
while (root->ltag == 0)
root = root->lchild;
printf("%c", root->data);
while (root->rtag == 1 && root->rchild != p)
{
root = root->rchild;
printf("%c", root->data);
}
root = root->rchild;
}
}
int main()
{
char str[50], res;
int r;
pBitree root = NULL,p=NULL;
pre = new Bitree;
pre->data = '#';
p = pre;
printf("请输入表达式:");
scanf("%s", &str);
turn_postorder(str);//转为后缀表达式
creat_tree(root);//后缀表达式建树
in_thread_head(root);//加入头结点,并线索化
inorder_thread(p);//线索中序输出
system("pause");
return 0;
}
参考 http://blog.chinaunix.net/uid-26548237-id-3476920.html