C++ primer 练习9.52 适配器stack 中缀表达式
//调试环境 VS2015
//本人菜鸟一枚,不喜勿喷! 谢谢!!!
//主要思想引自 http://www.cnblogs.com/dolphin0520/p/3708602.html
//主要代码引自 https://blog.csdn.net/fengzhanghao23/article/details/47380793
//改动:1.可支持负数运算,但采用的是字符串string的find搜索操作和substr拷贝操作
// 2.循环操作采用c++11标准的范围for语句实现
// 3.输入采用文件输入,其文件名,存储地址及数据可自行修改
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<stack>
using namespace std;
int priority(string opt)
{
int p = 0;
if (opt == "(")
p = 1;
if (opt == "+" || opt == "-")
p = 2;
if (opt == "*" || opt == "/")
p = 3;
return p;
}
void calculate(stack<double> &opdstack, string opt)
{
double ropd = 0;
double lopd = 0;
double result = 0;
if (opt == "+")
{
ropd = opdstack.top();
opdstack.pop();
lopd = opdstack.top();
opdstack.pop();
result = lopd + ropd;
opdstack.push(result);
}
if (opt == "-")
{
ropd = opdstack.top();
opdstack.pop();
lopd = opdstack.top();
opdstack.pop();
result = lopd - ropd;
opdstack.push(result);
}
if (opt == "*")
{
ropd = opdstack.top();
opdstack.pop();
lopd = opdstack.top();
opdstack.pop();
result = lopd * ropd;
opdstack.push(result);
}
if (opt == "/")
{
ropd = opdstack.top();
opdstack.pop();
lopd = opdstack.top();
opdstack.pop();
result = lopd / ropd;
opdstack.push(result);
}
}
double calExpression(vector<string> sv)
{
stack<double> stack_opd;
stack<string> stack_opt;
for (auto c : sv)
{
if (c == "+" || c == "-" || c == "*" || c == "/")
{
if (stack_opt.size() == 0)
stack_opt.push(c);
else
{
int c_p = priority(c);
string top_opt = stack_opt.top();
int opt_p = priority(top_opt);
if (c_p > opt_p)
stack_opt.push(c);
else
{
while (c_p <= opt_p)
{
calculate(stack_opd, top_opt);
stack_opt.pop();
if (stack_opt.size() > 0)
{
top_opt = stack_opt.top();
opt_p = priority(top_opt);
}
else
break;
}
stack_opt.push(c);
}
}
}
else if (c == "(")
stack_opt.push(c);
else if (c == ")")
{
while (stack_opt.top() != "(")
{
string top_opt = stack_opt.top();
calculate(stack_opd, top_opt);
stack_opt.pop();
}
stack_opt.pop();
}
else
{
if (c.find('-') == 0)
stack_opd.push(-stod(c.substr(c.find('-') + 1)));
else
stack_opd.push(stod(c));
}
}
while (stack_opt.size() != 0)
{
string top_opt = stack_opt.top();
calculate(stack_opd, top_opt);
stack_opt.pop();
}
return stack_opd.top();
}
int main()
{
cout << "------This is a function test of test_9_52------" << endl;
cout << "Caution!!! The data stored in file of test_9_52.txt" << endl;
ifstream ifs("test_9_52.txt");
string s;
vector<string> sv;
if (ifs)
{
while (ifs >> s)
sv.push_back(s);
}
else
cout << "The file is not open!!!" << endl;
cout << "The expression: ";
for (auto c : sv)
cout << c << " ";
cout << endl;
cout << "It's calculation results is: " << calExpression(sv) << endl;
cout << endl;
return 0;
}
C++ primer 练习9.52 适配器stack 中缀表达式的更多相关文章
- STL之容器适配器stack的实现框架
说明:本文仅供学习交流,转载请标明出处,欢迎转载! 一提到适配器(adapter).我们就想到了早期用电话线上网所用的调制解调器,俗称"猫"."猫"的作用是实现 ...
- 利用stack结构,将中缀表达式转换为后缀表达式并求值的算法实现
#!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...
- Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算
中缀表达式与后缀表达式的转换和计算 目录 中缀表达式转换为后缀表达式 后缀表达式的计算 1 中缀表达式转换为后缀表达式 中缀表达式转换为后缀表达式的实现方式为: 依次获取中缀表达式的元素, 若元素为操 ...
- 中缀表达式转逆波兰式(后缀表达式)求值 C++ Stack
给一个包含小数的中缀表达式 求出它的值 首先转换为后缀表达式然后利用stack求出值 转换规则: 如果字符为'(' push else if 字符为 ')' 出栈运算符直到遇到‘(' else if ...
- C语言- 基础数据结构和算法 - 09 栈的应用_中缀表达式转后缀表达式20220611
09 栈的应用_中缀表达式转后缀表达式20220611 听黑马程序员教程<基础数据结构和算法 (C版本)>, 照着老师所讲抄的, 视频地址https://www.bilibili.com/ ...
- sicily 中缀表达式转后缀表达式
题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...
- RPN-逆波兰计算器-中缀表达式转后缀表达式-javascript
1.利用栈(Stack)来存储操作数和操作符: 2.包含中缀表达式转后缀表达式的函数,这个是难点,也是关键点: 2.1.将输入字符串转为数组: 2.2.对转换来的字符进行遍历:创建一个数组,用来给存储 ...
- javascript使用栈结构将中缀表达式转换为后缀表达式并计算值
1.概念 你可能听说过表达式,a+b,a+b*c这些,但是前缀表达式,前缀记法,中缀表达式,波兰式,后缀表达式,后缀记法,逆波兰式这些都是也是表达式. a+b,a+b*c这些看上去比较正常的是中缀表达 ...
- ACM题目————中缀表达式转后缀
题目描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2.同理,后缀表达式就是操作符在两 ...
随机推荐
- 初识MySQL——人生若如初相逢
CREATE TABLE `student`(`studentNo` INT (4) NOT NULL PRIMARY KEY COMMENT '学号',`loginPwd` VARCHAR(20) ...
- 高效的jQuery代码编写技巧
缓存变量 DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $(); // 建议 $element = $('#element'); ...
- 用nodejs做一个svn密码修改页面
linux上配置好svn服务后,管理修改密码还得去手工修改passwd这个文件,略麻烦,其实网上应该有配套的web管理修改界面程序.但我想自己用nodejs写一个,因为用node不用配置复杂的服务器. ...
- 慧都启动“正版IDE联合超值推广计划
越来越多的中国软件企业为盗版所害而蒙受巨大损失,正版化意识逐渐兴起.IDE(集成开发环境)是软件开发.编写代码必备工具,而正版IDE更具有运行更稳定.编码更安全.保障更加完善等特点,逾为中国软件行业企 ...
- PHP 数字转汉字函数
/** * 数字转汉字描述 */ function numToStr($num) { // 判断正确数字 if (!preg_match('/^(\d*)(\.\d+)?$/', $num)) ret ...
- 一键完成SAP部署的秘密,想知道么?
诸如 SAP 这样的企业级应用已成为普遍的流行趋势.考虑到不同行业和需求的特点,所选平台必须能够为不同层面用户和各种 IT 活动提供灵活的容量需求. 此时上云也许是种不错的选择,而想上云的企业,一方面 ...
- how to determint wether two column equals
question: How to determint wether two column equals? Answer: Table one:RecordId = 1,CommonId = 5,Ap ...
- 插上翅膀,让Excel飞起来——xlwings(三)
xlwings基本对象 xlwings基本对象 App相当于Excel程序,Book相当于工作簿.N个Excel程序则由apps表示,N个工作簿由books表示. 对工作簿的操作 #导入xlwings ...
- C++函数委托
环境: win7_x64旗舰版.VS2015企业版 场景: C++标准库提供std::function类来将一个对象的调用操作封装在一个对象内部,然后可以委托调用,但是有一些弊端,例如下面的需求: 我 ...
- POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】
题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total ...