Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.31 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstdio>
  6. #include <vector>
  7. #include <stack>
  8. #include <algorithm>
  9. #include <map>
  10. #include <cctype>�
  11.  
  12. using namespace std;
  13.  
  14.  
  15. template <typename T>
  16. T pop(stack<T> &s)
  17. {
  18.     T res = s.top();
  19.     s.pop();
  20.     return res;
  21. }
  22.  
  23. enum tokentype { ttconst, ttvar, ttopbr, ttclbr, ttfun, ttop };
  24. struct token
  25. {
  26.     string name;
  27.     tokentype type;
  28.     token(string n = " ", tokentype t = ttop)
  29.     {
  30.         name = n;
  31.         type = t;
  32.     }
  33.     token(char n = ' ', tokentype t = ttop)
  34.     {
  35.         name = n;
  36.         type = t;
  37.     }
  38.     void printToken()
  39.     {
  40.         cout << name.c_str() << " " << type;
  41.         cout << endl;
  42.     }
  43. };
  44.  
  45. string termsymbol = "+-*/^()[]%!&|";
  46. bool istermsymbol(char c)
  47. {
  48.     return termsymbol.find(c) != -1;
  49.  
  50. }
  51.  
  52. tokentype ExpressionFunctionOrVariable(string s)
  53. {
  54.     if (s.find("(") != -1) //(s == "sin" || s == "cos" || s == "tan" || s == "arcsin" || s == "arccos" || s == "arctan")
  55.         return ttfun;
  56.     return ttvar;
  57. }
  58.  
  59. bool WhetherAStringIsANumber(string s)
  60. {
  61.  
  62.     for (int i = 0; i < s.size(); i++)
  63.         if (s[i] != '.' && isdigit(s[i]) == false)
  64.             return false;
  65.     return true;
  66. }
  67.  
  68.  
  69. vector<token> BuildToken(string s)   // "sin(x)+7*abc^13-9.3*acrtg(1)"
  70. {
  71.     //vector<token> TokenVec;
  72.     vector<token> toks;
  73.     size_t i = 0;
  74.     s += " ";
  75.     while (i < s.length())
  76.     {
  77.         if (s[i] == ' ')
  78.         {
  79.             i++;
  80.             continue;
  81.         }
  82.         string tmp = "";
  83.         do
  84.         {
  85.             tmp += s[i++];
  86.         } while (istermsymbol(s[i - 1] || istermsymbol(s[i])));
  87.         toks.push_back(token(tmp));
  88.     }
  89.     for (size_t i = 0; i < toks.size(); i++)
  90.     {
  91.         char tokchar = toks[i].name[0];
  92.         if (isdigit(toks[i].name[0]))
  93.             toks[i].type = ttconst;
  94.         else if (tokchar == '(')
  95.             toks[i].type = ttopbr;
  96.         else if (tokchar == ')')
  97.             toks[i].type = ttclbr;
  98.         else if (isalpha(tokchar))
  99.             if (i + 1 < toks.size() && toks[i + 1].name == "(")
  100.                 toks[i].type = ttfun;
  101.             else
  102.                 toks[i].type = ttvar;
  103.     }
  104.     //for (int i = 0; i< s.size(); i++)
  105.     //{
  106.     //  if (istermsymbol(s[i]))
  107.     //  {
  108.     //      if (WhetherAStringIsANumber(tmp) && tmp != "")
  109.     //          TokenVec.push_back(token(tmp, ttconst));
  110.     //      else if (tmp != "")
  111.     //          TokenVec.push_back(token(tmp, ExpressionFunctionOrVariable(tmp + s[i])));
  112.     //      switch (s[i])
  113.     //      {
  114.     //      case '(':
  115.     //          TokenVec.push_back(token(s[i], ttopbr));
  116.     //          break;
  117.     //      case ')':
  118.     //          TokenVec.push_back(token(s[i], ttclbr));
  119.     //          break;
  120.     //      default:
  121.     //          TokenVec.push_back(token(s[i], ttop));
  122.     //          break;
  123.     //      }
  124.     //      tmp = "";
  125.  
  126.     //  }
  127.     //  else
  128.     //  {
  129.     //      tmp += s[i];
  130.     //      if (tmp.find(" ") != -1)
  131.     //          tmp.erase(tmp.find(" "), 1);
  132.     //  }
  133.     //}
  134.  
  135.     return toks;
  136. }
  137.  
  138. map<string, int> priority;
  139.  
  140. void BuildPriorityMap()
  141. {
  142.     priority["+"] = 10;
  143.     priority["-"] = 10;
  144.     priority["*"] = 20;
  145.     priority["/"] = 20;
  146.     priority["^"] = 30;
  147.  
  148. }
  149.  
  150. vector<token> Postfix(vector<token> toks)
  151. {
  152.     if (priority.empty())
  153.         BuildPriorityMap();
  154.     stack<token> s;
  155.     vector<token> res;
  156.     for (auto &t : toks)
  157.     {
  158.         switch (t.type)
  159.         {
  160.         case ttvar: case ttconst: res.push_back(t); break;
  161.         case ttopbr: s.push(t); break;
  162.         case ttclbr: while (s.top().type != ttopbr)
  163.             res.push_back(pop(s));
  164.             s.pop();
  165.             break;
  166.         case ttfun: s.push(t); break;
  167.         case ttop:
  168.             while (s.size() && (s.top().type == ttfun || (s.top().type == ttop && priority[s.top().name] >= priority[t.name])))
  169.                 res.push_back(pop(s));
  170.             s.push(t);
  171.         }
  172.     }
  173.     while (s.size())
  174.         res.push_back(pop(s));
  175.     return res;
  176. }
  177.  
  178.  
  179.  
  180. /*double op_plus(stack<double> &S)
  181. {
  182. return  pop(S) + pop(S);
  183.  
  184. }*/
  185.  
  186. map<string, double(*)(stack<double> &)> functions;
  187. void BuildFunctionMap()
  188. {
  189.     functions["+"] = [](stack<double> &s) {return pop(s) + pop(s); };
  190.     functions["*"] = [](stack<double> &s) {return pop(s) * pop(s); };
  191.     functions["-"] = [](stack<double> &s) {return -pop(s) + pop(s); };
  192.     functions["/"] = [](stack<double> &s) {return pop(s) / pop(s); };
  193.     functions["^"] = [](stack<double> &s) {auto p = pop(s);  return pow(pop(s), p); };
  194.     functions["sin"] = [](stack<double> &s) {return sin(pop(s)); };
  195.     functions["cos"] = [](stack<double> &s) {return cos(pop(s)); };
  196.     //functions["!"] = [&f](stack<double> &s) -> int { return (n) ? pop(s)*f(pop(s) - 1) : 1; };
  197.     //functions["^"] = [](stack<double> &s) {
  198.  
  199. }
  200.  
  201. void GetExpressionStream(istream &inp, string &expr, map<string, double> &vars)
  202. {
  203.     getline(inp, expr);
  204.     while (!inp.eof())
  205.     {
  206.         string s;
  207.         getline(inp, s);
  208.         int pos = s.find('=');
  209.         if (pos == -1)
  210.             continue;
  211.         //var.first_not_of(' ');
  212.         vars[s.substr(0, pos)] = stod(s.substr(pos + 1, s.length() - pos));
  213.     }
  214. }
  215.  
  216. //double CalculatePostFix(vector<token> &postfix, map<string, double> &vars)
  217. //{
  218. //  // РЕАЛИЗОВАТЬ
  219. //
  220. //}
  221.  
  222. int main()
  223. {
  224.     //string s;
  225.     //string expression;
  226.     //ifstream file("test.txt");
  227.     //bool key = true;
  228.     //while (getline(file, s))
  229.     //  if (key == true)
  230.     //  {
  231.     //      expression = s;
  232.     //      key = false;
  233.     //  }
  234.     //  else
  235.     //      variable[string(s, 0, s.find("="))] = atoi(string(s, s.find("="), s.size() - 1).c_str());
  236.  
  237.     //file.close();
  238.  
  239.  
  240.     vector<token> T1;
  241.     T1 = BuildToken("700 + sin(x) + 7 * abc ^ 13 - 9.3*acrtg(1)"); // ttconst, ttvar, ttopbr, ttclbr, ttfun, ttop  700+sin(x) + 7 * abc ^ 13 - 9.3*acrtg(1)
  242.     cout << "700 + sin(x) + 7 * abc ^ 13 - 9.3*acrtg(1)"<< endl;;
  243.     auto T2 = Postfix(T1);
  244.     for (auto &t : T2)
  245.     {
  246.         cout << t.name << " ";
  247.     }
  248.     cout << endl;
  249.     return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement