//调试环境 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 中缀表达式的更多相关文章

  1. STL之容器适配器stack的实现框架

    说明:本文仅供学习交流,转载请标明出处,欢迎转载! 一提到适配器(adapter).我们就想到了早期用电话线上网所用的调制解调器,俗称"猫"."猫"的作用是实现 ...

  2. 利用stack结构,将中缀表达式转换为后缀表达式并求值的算法实现

    #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...

  3. Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算

    中缀表达式与后缀表达式的转换和计算 目录 中缀表达式转换为后缀表达式 后缀表达式的计算 1 中缀表达式转换为后缀表达式 中缀表达式转换为后缀表达式的实现方式为: 依次获取中缀表达式的元素, 若元素为操 ...

  4. 中缀表达式转逆波兰式(后缀表达式)求值 C++ Stack

    给一个包含小数的中缀表达式 求出它的值 首先转换为后缀表达式然后利用stack求出值 转换规则: 如果字符为'('  push else if 字符为 ')' 出栈运算符直到遇到‘(' else if ...

  5. C语言- 基础数据结构和算法 - 09 栈的应用_中缀表达式转后缀表达式20220611

    09 栈的应用_中缀表达式转后缀表达式20220611 听黑马程序员教程<基础数据结构和算法 (C版本)>, 照着老师所讲抄的, 视频地址https://www.bilibili.com/ ...

  6. sicily 中缀表达式转后缀表达式

    题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...

  7. RPN-逆波兰计算器-中缀表达式转后缀表达式-javascript

    1.利用栈(Stack)来存储操作数和操作符: 2.包含中缀表达式转后缀表达式的函数,这个是难点,也是关键点: 2.1.将输入字符串转为数组: 2.2.对转换来的字符进行遍历:创建一个数组,用来给存储 ...

  8. javascript使用栈结构将中缀表达式转换为后缀表达式并计算值

    1.概念 你可能听说过表达式,a+b,a+b*c这些,但是前缀表达式,前缀记法,中缀表达式,波兰式,后缀表达式,后缀记法,逆波兰式这些都是也是表达式. a+b,a+b*c这些看上去比较正常的是中缀表达 ...

  9. ACM题目————中缀表达式转后缀

    题目描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2.同理,后缀表达式就是操作符在两 ...

随机推荐

  1. js把数据处理成钱的格式

    1.var Rmoney = parseFloat(money).toFixed(2);//把money处理成保存2位小数的格式

  2. SpringBoot ------ 使用AOP处理请求

    一.AOP统一处理请求日志 1.spring的两大核心:AOP ,  IOC 2.面向对象OOP关注的是将需求功能垂直,划分为不同的,并且相对独立的,   会封装成良好的类,并且类有属于自己的行为. ...

  3. scp命令的使用

    scp命令是什么 scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. scp命令用法 scp [-1246BCpqrv] [-c cipher ...

  4. fireworks图片边缘化 fireworks羽化图片边缘的教程

    fireworks羽化图片边缘的教程如下: 1. 打开一个图片. 2.点击“工具”面板“位图”部分的“选取框”工具.也可以选择“椭圆选取框”工具. 3.选择部分图象. 4.在属性检查器中,“边缘”项中 ...

  5. linux 中环境变量配置文件说明

    1. 修改/etc/profile文件 特点:所有用户的shell都有权使用你配置好的环境变量 说明:如果你的电脑仅用作开发,建议使用此配置,因为所有用户的shell都有权使用你配置好的环境变量,所以 ...

  6. YAML 格式学习

    目录 什么是YAML 一.注释和多文件 二.格式要求 三.数据结构 1.对象 2. 数组 3.常量 四.字符串 YAML的特殊字符 什么是YAML YAML是"YAML不是一种标记语言&qu ...

  7. March 25 2017 Week 12 Saturday

    Better master one than engage with ten. 会十事不如精一事. My colleagues think I have known a lot of things, ...

  8. [EffectiveC++]item40:明智而审慎地使用多重继承

  9. CSS基础语法(二) CSS的9种选择器

    样式表的选择器 1.类选择器 根据HTML标签的class属性选择样式应用的属性 .类值{ … } 2.ID选择器 根据HTML标签的ID属性选择样式应用的元素 #id值{ … }  3.标签选择器 ...

  10. 广义mandelbrot集,使用python的matplotlib绘制,支持放大缩小

    迭代公式的指数,使用的1+5j,这是个复数.所以是广义mandelbrot集,大家能够自行改动指数,得到其它图形.各种库安装不全的,自行想办法,能够在这个站点找到差点儿全部的python库 http: ...