//调试环境 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. C# 矩阵运算和一些基本的几何运算

    以前工作中写的,这里备个份,有可能用到 基本的矩阵运算类,测试20阶以内应该没啥问题,超过20阶不好使... /// <summary> /// 矩阵 异常 512索引 1024无解 20 ...

  2. Vue表格中,对数据进行转换、处理

    众所周知,后端从Mysql取出的数据,一般是很难单独处理某一个Key的数据的(需要处理的话,可能会浪费大量的性能.而且对页面加载时间有很大的影响),所以,从数据库取出的数据.只能由前端进行处理.但是在 ...

  3. flash8中利用遮罩制作图片切换效果

    http://www.56.com/w73/play_album-aid-8642763_vid-NDY5ODU2Mzg.html

  4. ArcMap没有工具条和菜单栏的解决方法

    问题: 在安装ArcGIS10后,打开竟然没有菜单栏和工具栏,安装10.1又有,卸了重装10还是没有 解决办法: 在ArcMap的状态栏上双击,在弹出的自定义对话框中,在Toolbars工具栏选择Ma ...

  5. eclipse插件svn和客户端工具TortoiseSvn的版本对应关系

    如果同时使用这两个软件,一定要保证版本的对应关系: 插件svn1.4.x对应TortoiseSvn 1.5.x 插件svn1.6.x对应TortoiseSvn 1.6.x 插件svn1.8.x对应To ...

  6. SQL Server ->> DISABLE索引后插入更新数据再REBUILD索引 和 保留索引直接插入更新数据的性能差异

    之前对于“DISABLE索引后插入更新数据再REBUILD索引 和 保留索引直接插入更新数据的性能差异”这两种方法一直认为其实应该差不多,因为无论如何索引最后都需要被维护,只不过是个时间顺序先后的问题 ...

  7. window medio player 完美代码

    var arr = [               '<object width="240" height="240" align="basel ...

  8. Oracle 查看session级别信息

    1. 查看活动会话信息[sql] view plain copySELECT *    FROM V$SESSION   WHERE USERNAME IS NOT NULL     AND STAT ...

  9. MySQL:数据库入门篇5

    1.存储引擎 innodb与MyIASM存储引擎的区别: 1.innodb 是mysql5.5版本以后的默认存储引擎, 而MyISAM是5.5版本以前的默认存储引擎. 2.innodb 支持事物,而M ...

  10. ACM-ICPC(10/21)

    写一发后缀数组套路题,看起来简单,写起来要人命哦~~~ 总共13题. 分两天debug吧,有点累了~~~ suffix(后缀数组的应用) sa[i] :排名第 i 的后缀在哪(i 从 1 开始) ra ...