2018-07-21 16:57:26 update

  建立表达式树的基本思路:方法类似由下而上建立堆的思想,所以时间复杂度为O(n),这样算法就会变得很简单,只用考虑处理需要入栈的节点和栈中的节点即可。。。

  code:

#include "stdafx.h"
#include <iostream>
#include <stack>
#include <string> class expr_tree
{
struct base_node
{
char c;
base_node * lc, *rc;
base_node() {}
base_node(char c)
: c(c)
, lc(nullptr)
, rc(nullptr)
{
}
} * root;
protected:
void parse_expr(std::string str);
void proorder_print(base_node * n);
void inorder_print(base_node * n);
void postorder_print(base_node * n);
void destory(base_node * & n);
double eval(base_node * n); public:
void show(); expr_tree(std::string str)
{
parse_expr(str);
}
~expr_tree()
{
destory(this->root);
}
}; void expr_tree::parse_expr(std::string str)
{
std::stack<base_node *> stack_node;
for (auto c : str)
{
base_node * node = new base_node(c);
if (c >= '0' && c <= '9' || c >= 'a' && c <= 'z')
stack_node.push(node);
else if (c == '+' || c == '-' || c == '*' || c == '/')
{
if (!stack_node.empty())
{
node->rc = stack_node.top();
stack_node.pop();
}
if (!stack_node.empty())
{
node->lc = stack_node.top();
stack_node.pop();
}
stack_node.push(node);
}
else
{
std::cout << "表达式有误!" << std::endl;
exit(1);
}
}
root = stack_node.top();
while (!stack_node.empty())
stack_node.pop();
} void expr_tree::proorder_print(base_node * n)
{
if (n)
{
std::cout << n->c;
proorder_print(n->lc);
proorder_print(n->rc);
}
} void expr_tree::inorder_print(base_node * n)
{
if (n)
{
if (n->c >= '0' && n->c <= '9')
std::cout << n->c;
else
{
std::cout << '(';
inorder_print(n->lc);
std::cout << ' ' << n->c << ' ';
inorder_print(n->rc);
std::cout << ')';
}
}
} void expr_tree::postorder_print(base_node * n)
{
if (n)
{
postorder_print(n->lc);
postorder_print(n->rc);
std::cout << n->c;
}
} void expr_tree::show()
{
std::cout << "前缀表达式:" << std::endl;
proorder_print(this->root);
std::cout << std::endl;
std::cout << "中缀表达式:" << std::endl;
inorder_print(this->root);
std::cout << " = " << eval(this->root) << std::endl;
std::cout << "后缀表达式:" << std::endl;
postorder_print(this->root);
std::cout << std::endl;
} double expr_tree::eval(base_node * n)
{
switch (n->c)
{
case '+': return eval(n->lc) + eval(n->rc);
case '-': return eval(n->lc) - eval(n->rc);
case '*': return eval(n->lc) * eval(n->rc);
case '/': return eval(n->lc) / eval(n->rc);
default: return n->c - '0';
}
} void expr_tree::destory(base_node * & root)
{
if (root != nullptr)
{
destory(root->lc);
destory(root->rc);
delete root;
}
} int main()
{
std::string str = "23+456+**"; expr_tree res(str);
res.show();
return 0;
}

  

  

  

最新文章

  1. CapsLock与ctrl的键位修改
  2. Lua源码编译之CL编译器编译
  3. Apache与Nginx区别
  4. 多态、GC、Java数据类型
  5. phpcms /api/phpsso.php SQL Injection Vul
  6. 微信公众平台开发教程--方培工作室,PHP语言版本
  7. Day 16: Goose Extractor —— 好用的文章提取工具
  8. tl;drLegal ——开源软件license的搜索引擎
  9. 跳转到QQ聊天界面和QQ群界面
  10. 开发者应该避免使用的6个Java功能(转)
  11. 页面的拼装配置Appache SSI
  12. 一些有用的javascript实例分析(二)
  13. SOLID 设计原则 In C# 代码实现
  14. 【JVM】7、深入理解Java G1垃圾收集器
  15. 循环结构-for,while,do-while
  16. webapp定位
  17. C++项目參考解答:求Fibonacci数列
  18. 004-spring cloud gateway-网关请求处理过程
  19. Selenium2+python自动化35-获取元素属性
  20. 【LeetCode】64. Minimum Path Sum

热门文章

  1. mvc 上传文件 HTTP 错误 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求。 maxRequestLength与 maxReceivedMessageSize 和 maxAllowedContentL区别
  2. CSS3的一个伪类选择器:nth-child()
  3. Ant安装及环境配置
  4. STM32F103_外部RAM用作运存---IS62WV51216
  5. Servlet线程安全问题(转载)
  6. 网络辅助北斗/GPS位置服务平台业务量突破10亿次
  7. Linux下Nginx1.9.9的安装
  8. 关于 checkbox 的一些操作
  9. 移动端rem自适应方案
  10. JS中的提升(即变量和函数声明移动到代码顶部)