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.同理,后缀表达式就是操作符在两 ...
随机推荐
- Python爬虫之requests模块(1)
一.引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃 ...
- NGUI动态字体的使用
LZ今年六月刚刚毕业,在学校跟着老师做Android,OpenGL ES方面的项目(说白了就是干苦力╮(╯_╰)╭).年后来SZ了,就业前景好像并没有电视上渲染的那样糟糕,至少IT行业是这样吧,只要你 ...
- Siebel 基础入门--权限控制
企业应用最基本的要求就是只授予用户在他工作职责范围内的权限,一般而言,这种权限都包含两种,一种是对于相应的功能的可见性(或者形象地说,菜单的可见 性,这个是应用层面界面的,这种权限在Siebel里称为 ...
- Python 调度算法 死锁 静动态链接(七)
1 select poll epoll的区别 基本上select有3个缺点: 连接数受限 查找配对速度慢 数据由内核拷贝到用户态 poll改善了第一个缺点 epoll改了三个缺点. (1)select ...
- MySQL 5.7 修改数据物理文件目录
修改MySQL数据库物理文件存放位置,需要在MySQL配置文件中修改相关参数.安装MySQL5.7后,在MySQL安装目录下没有找到数据库物理文件,最后经过查找发现其在“C:\ProgramData\ ...
- I want to be a Great Web Front-end Developer
有时觉得特别的浮躁,可能是每天春运般的挤地铁,随处可见因为一点小磕小蹭吹胡子瞪眼睛的人,可能是身边的人貌似一下子都好有钱,买房买车或者买第N套房. 很想静下来心寻找到自己inner pace,但是忽然 ...
- day6-基础 模块详解
1.定义: 1)模块:本质上就是一个python程序,封装成一个"模块",可以调用里面的方法,用来实现一个功能.逻辑上用来组织python代码. 2)包:本质上是一个目录(必须带有 ...
- Linux --远程访问控制
1.SSH服务端 修改配置文件 vi /etc/ssh/sshd_config 监听选项 port 22 //监听端口地址 ListenAddress 192.168.1.50 //监听地址为本机地址 ...
- oracle 12c使用dblink克隆pdb
Multitenant : Hot Clone a Remote PDB or Non-CDB in Oracle Database 12c Release 2 (12.2)https://oracl ...
- js原型链继承及调用父类方法
方法1: var Parent= function () { }; Parent.prototype.process = function(){ alert('parent method'); }; ...