算术表达式转化为二叉运算树
二叉运算树的实质是不仅记录了表达式而且记录了表达式的运算顺序,运算时直接后序遍历就行了(注意:是后序遍历求值,中序遍历只能打印出表达式)
核心思路:二元表达式由三部分构成: 数 + 运算符 + 数,(数也可以是表达式)在一个表达式中是否能计算其中的单个二元表达式,只取决于此运算符与其前后运算符的优先级,如:3+25,不能计算3+2,因为优先级高于+,应该先计算2*5,在计算+号
注意: 测试代码必需以#结束,如:2*3+5/(4+1)#
#include <iostream>
#include <string.h>
#include <stdio.h>
#define maxnum 50
using namespace std;
typedef struct tree
{
char data;
struct tree *lchild,*rchild;
}Btree;
char optr[maxnum]; //存储运算符
int top = -1;
Btree* expt[maxnum]; //存储根节点
int top1 = -1;
char judge(char s,char c) //判断栈顶元素和ch的优先级
{
//s代表栈顶元素 c代表输入的函数
if(c == '#')
{
if(s == '#')
return '=';
else
return '<';
}
else if(c == '(')
return '>';
else if(c