欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++ primer 练习9.52 适配器stack 中缀表达式

程序员文章站 2022-05-08 18:44:06
//调试环境 VS2015//本人菜鸟一枚,不喜勿喷! 谢谢!!!//主要思想引自 http://www.cnblogs.com/dolphin0520/p/3708602.html//主要代码引自 https://blog.csdn.net/fengzhanghao23/article/detai ......

//调试环境 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;
}