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.同理,后缀表达式就是操作符在两 ...
随机推荐
- RestTemplate请求出现401错误
最近遇到一个请求API接口总是报401 Unauthorized错误,起初是认为这个是平台返回的,后来用Postman请求,发现平台其实返回的是一串json,里面带有一些权限验证失败的消息,但到我们代 ...
- 键盘按键keyCode大全,js页面快捷键
字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 74 S 83 1 49 B 66 K 75 T 84 2 50 C 67 L 76 U 85 3 ...
- Use the list and while to Build Shop car
#Author: Gordonsalary = int(input("请输入你的工资:"))goods = [('0',"Iphone",5000),('1', ...
- Csharp:字符串操作
public class StringControl { /// <summary> /// 客户端浏览器 /// http://en.wikipedia.org/wiki/Web_bro ...
- CSS基础语法与选择器
CSS基础 语法 : <head> <style type="text/css"> 选择器(即修饰对象){ 修饰属性:属性值; 修饰属性:属性值; } &l ...
- window.frames在不同浏览器中的用法
document.frames 等同于 window.frames,用来取得当前页面内 window 对象的集合. 不支持Firefox,其他浏览器(chrome.opera.IE.360)均支持. ...
- <Android 基础(十八)> XLIFF
介绍 XLIFF ,XML Localization Interchange File Format,XML本地化数据交换格式. 实际使用 1.布局文件 activity_main.xml <? ...
- jQuery动态添加删除CSS样式
jQuery框架提供了两个CSS样式操作方法,一个是追加样式addClass,一个是移除样式removeClass,下面通过一个小例子讲解用法. jQuery动态追加移除CSS样式 <!DOCT ...
- 《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:概述
1.前言 数据生产和数据展示是常见的两大专业级移动GIS应用场景,这里我们针对数据生产环节的ArcGIS的离在线一体化技术给大家做一个基本的介绍和梳理. 使用ArcGIS离在线一体化技术首先需要以下基 ...
- HTML 5网页设计入门必读(书)
今天看了一本由人民邮电出版社出版.邢薇薇 郭俊飞 王雪翻译的<HTML 5网页设计入门必读>,在此整理一下知识点,以及写一些自己的读后感. 本书的开章还是和大部分HTML 5书籍一样,用极 ...