Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <vector>
  3. #include <iostream>
  4. #include <stack>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <map>
  8. #include <fstream>
  9. #include <string>
  10.  
  11. using namespace std;
  12. enum tokentype { ttconst, ttvar, ttopbr, ttclbr, ttfun, ttop };
  13. struct token
  14. {
  15.     string name;
  16.     tokentype type;
  17.     token(string n = "", tokentype t = ttop)
  18.     {
  19.         name = n; type = t;
  20.     }
  21. };
  22.  
  23. string termsymbol = "+-*/^()[]%!&|";
  24.  
  25. bool istermsymbol(char c)
  26. {
  27.     return termsymbol.find(c) != -1;
  28. }
  29.  
  30. vector<token> BuildToken(string s)
  31. {
  32.     vector<token> toks;
  33.     size_t i = 0;
  34.     s += " ";
  35.     while (i < s.length()) {
  36.         if (s[i] == ' ') {
  37.             i++;
  38.             continue;
  39.         }
  40.         string tmp = "";
  41.         do {
  42.             tmp += s[i++];
  43.         } while (!istermsymbol(s[i - 1]) || !istermsymbol(s[i]));
  44.         toks.push_back(token(tmp));
  45.     }
  46.     for (size_t i = 0; i < toks.size(); i++){
  47.         char tokchar = toks[i].name[0];
  48.         if (isdigit(tokchar))
  49.             toks[i].type = ttconst;
  50.         else if (tokchar == '(')
  51.             toks[i].type = ttopbr;
  52.         else if (tokchar == ')')
  53.             toks[i].type = ttclbr;
  54.         else if (isalpha(tokchar))
  55.             if (i + 1 < toks.size() && toks[i + 1].name == "(")
  56.                 toks[i].type = ttfun;
  57.             else toks[i].type = ttvar;
  58.     }
  59.     return toks;
  60. }
  61.  
  62. double pop(stack<double> &s)
  63. {
  64.     double res = s.top();
  65.     s.pop();
  66.     return res;
  67. }
  68.  
  69. map<string, int> priority;
  70. void BuildPriorityMap()
  71. {
  72.     priority["+"] = 10;
  73.     priority["-"] = 10;
  74.     priority["*"] = 20;
  75.     priority["/"] = 20;
  76.     priority["^"] = 30;
  77. }
  78. template <typename T>
  79. T pop(stack<T> &s) {
  80.     T res = s.top();
  81.     s.pop();
  82.     return res;
  83. }
  84. vector<token> BuildPostfix(vector<token> toks) {
  85.     if (priority.empty())
  86.         BuildPriorityMap();
  87.     stack<token> s;
  88.     vector<token> res;
  89.     for (auto &t : toks)
  90.     {
  91.         switch (t.type) {
  92.         case ttvar: case ttconst: res.push_back(t); break;
  93.         case ttopbr: s.push(t); break;
  94.         case ttclbr:
  95.             while (s.top().type != ttclbr) res.push_back(pop(s));
  96.                 s.pop();
  97.             break;
  98.         case ttfun: s.push(t); break;
  99.         case ttop:
  100.             while (s.size() && (s.top().type == ttfun || (s.top().type == ttop && priority[s.top().name] >= priority[s.top().name])))
  101.                 res.push_back(pop(s));
  102.             s.push(t);
  103.         }
  104.     }
  105.     while (s.size())
  106.         res.push_back(pop(s));
  107.     return res;
  108. }
  109.  
  110.  
  111. map<string, double> variable;
  112. void SelectFromFile(stack<char> &s) {
  113.     ifstream ifile("funct.txt");
  114.     while (!ifile.eof()) {
  115.         s.push(ifile.get());
  116.     }
  117.     ifile.close();
  118.     //At the other lines variables with their values
  119. }
  120.  
  121. double op_plus(stack<double> &S)
  122. {
  123.     return pop(S) + pop(S);
  124. }
  125.  
  126.  
  127.  
  128. map<string, double(*)(stack<double> &) > functions;
  129. void BuildFunctionMap()
  130. {
  131.     functions["+"] = op_plus;
  132.     functions["*"] = [](stack<double> &s) { return pop(s)*pop(s); };
  133.     functions["-"] = [](stack<double> &s) { return -pop(s) + pop(s); };
  134.     functions["/"] = [](stack<double> &s) { return 1 / pop(s) / pop(s); };
  135.     functions["^"] = [](stack<double> &s) { auto ad = pop(s); return pow(pop(s), ad); };
  136.     functions["sin"] = [](stack<double> &s) {return sin(pop(s)); };
  137.     functions["!"] = [](stack<double> &s) {double ad = 1;  for (int i = 1; i < pop(s); i++) ad *= i; return ad; };
  138.     functions["cos"] = [](stack<double> &s) {return sin(pop(s)); };
  139. }
  140.  
  141. void GetExpressionFromStream(istream &inp, string &expr, map<string, double> &vars) {
  142.     getline(inp, expr);
  143.     while (!inp.eof()) {
  144.         string s;
  145.         getline(inp, s);
  146.         int pos = s.find('=');
  147.         if (pos == -1) continue;
  148.         string var = s.substr(0, pos);
  149.         string val = s.substr(pos + 1, s.length() - pos);
  150.         vars[var] = stod(val);
  151.     }
  152. }
  153.  
  154. double CalculatePostfix(vector<token> &postfix, map<string, double> &vars){
  155.     //todo
  156. }
  157.  
  158. int main()
  159. {
  160.     vector<token> T1 = BuildToken("2.15*abc+sin(x+abc/(a-exp(x+y)/3.55))^(a/b*c - cos(3*x))");
  161.     auto T2 = BuildPostfix(T1);
  162.     for (auto &t : T2) {
  163.         cout << t.name << " ";
  164.     }
  165.     cout << endl;
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement