初探利用C++的STL实现数学四则运算计算结果

  在数学的四则运算表达式写入a.txt中,例如:

  其运算的结果要写入b.txt中,例如

  初探思路:

  利用getline读取文件中每行,并保存在string中,然后按数字和运算符拆分成两个vector。

  第一次遍历,把*、/、%等运算符等级高的运算

  剩下的就循环遍历运算+和-,直到vector中的第二个字符是=,把第一个字符的结果存入一个answer中,

  执行上面相同的操作,计算每行的表达式,把结果存入answer中

  最后把answer的结果存入到b.txt中

附上代码:

  1 // Demo.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include <iostream>
  6 #include <fstream>
  7 #include <string>
  8 #include <vector>
  9 #include <deque>
 10 #include <stdio.h>
 11 
 12 using namespace std;
 13 
 14 
 15 void writeAnswer(vector<int>& iAnswer)
 16 {
 17     ofstream ofs("b.txt");
 18     for(vector<int>::size_type ix = 0; ix != iAnswer.size(); ix++)
 19     {
 20         ofs << iAnswer[ix] << '
';
 21     }
 22     ofs.close();
 23 }
 24 
 25 void countAnswer(vector<int>& iAnswer,vector<int>& iVec,vector<char>& cVec)
 26 {
 27     for (vector<char>::size_type iy = 0; iy != cVec.size(); iy++)
 28     {
 29         vector<char>::size_type t = cVec.size();
 30         switch (cVec[iy])
 31         {
 32         case '*':
 33             iVec[iy] *= iVec[iy + 1];
 34             iVec.erase(iVec.begin() + iy + 1);
 35             cVec.erase(cVec.begin() + iy);
 36             iy--;
 37             break;
 38         case '/':
 39             iVec[iy] /= iVec[iy + 1];
 40             iVec.erase(iVec.begin() + iy + 1);
 41             cVec.erase(cVec.begin() + iy);
 42             iy--;
 43             break;
 44         case '%':
 45             iVec[iy] %= iVec[iy + 1];
 46             iVec.erase(iVec.begin() + iy + 1);
 47             cVec.erase(cVec.begin() + iy);
 48             iy--;
 49             break;
 50         }
 51     }
 52 
 53     int n;
 54     n = iVec[0];
 55     for (vector<char>::size_type ix = 0; ix != cVec.size(); ix++)
 56     {
 57         vector<char>::size_type t = cVec.size();
 58         switch(cVec[ix])
 59         {
 60         case '+':
 61             n += iVec[ix + 1];
 62             break;
 63         case '-':
 64             n -= iVec[ix + 1];
 65             break;
 66         }
 67     }
 68     iAnswer.push_back(n);
 69 
 70 
 71 }
 72 
 73 
 74 void countMath()
 75 {
 76     ifstream ifs("a.txt");
 77     string temp;
 78     deque<string> sDeq;
 79     while (!ifs.eof())
 80     {
 81         getline(ifs,temp);
 82         sDeq.push_back(temp);
 83     }
 84 
 85     ifs.close();
 86     
 87 
 88     vector<int> iAnswer;
 89     for (deque<string>::size_type ix = 0; ix != sDeq.size(); ix++)
 90     {
 91         string str;
 92         str = sDeq.front();
 93         int temp = 0;
 94         vector<int> iVec;
 95         vector<char> cVec;
 96         for (string::size_type iy = 0; iy != str.size(); iy++)
 97         {
 98             if (str[iy] == '=')
 99             {
100                 iVec.push_back(temp);
101                 temp = 0;
102                 countAnswer(iAnswer,iVec,cVec);
103                 break;
104             }
105             else if (str[iy] >= '0' && str[iy] <= '9')
106             {
107                 temp = str[iy] - '0' + temp * 10;
108             }
109             else
110             {
111                 iVec.push_back(temp);
112                 temp = 0;
113                 cVec.push_back(str[iy]);
114             }
115         }
116         iVec.clear();
117         cVec.clear();
118 
119     }
120 
121     writeAnswer(iAnswer);
122 }
123 
124 int _tmain(int argc, _TCHAR* argv[])
125 {
126     countMath();
127     return 0;
128 }
原文地址:https://www.cnblogs.com/SamRichard/p/3645033.html