Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. #include <vector>
  2. #include <stack>
  3. #include <algorithm>
  4. #include <map>
  5. #include <cctype>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. enum tokentype { ttconst, ttvar, ttopbr, ttclbr, ttfun, ttop };
  11. struct token
  12. {
  13. string name;
  14. tokentype type;
  15. token(string n = " ", tokentype t = ttop)
  16. {
  17. name = n;
  18. type = t;
  19. }
  20. token(char n = ' ', tokentype t = ttop)
  21. {
  22. name = n;
  23. type = t;
  24. }
  25. void printToken()
  26. {
  27. cout << name.c_str() << " " << type;
  28. cout << endl;
  29. }
  30. };
  31.  
  32. string termsymbol = "+-*/^()[]%!&|";
  33. bool istermsymbol(char c)
  34. {
  35. return termsymbol.find(c) != -1;
  36.  
  37. }
  38.  
  39. tokentype ExpressionFunctionOrVariable(string s)
  40. {
  41. if (s.find("(") != -1) //(s == "sin" || s == "cos" || s == "tan" || s == "arcsin" || s == "arccos" || s == "arctan")
  42. return ttfun;
  43. return ttvar;
  44. }
  45.  
  46. bool WhetherAStringIsANumber(string s)
  47. {
  48.  
  49. for (int i = 0; i < s.size(); i++)
  50. if (s[i] != '.' && isdigit(s[i]) == false)
  51. return false;
  52. return true;
  53. }
  54.  
  55.  
  56. vector<token> BuildToken(string s) // "sin(x)+7*abc^13-9.3*acrtg(1)"
  57. {
  58. vector<token> toks;
  59. size_t i = 0;
  60. s += " ";
  61. while (i < s.length())
  62. {
  63. if (s[i] == ' ') // проигнорировали пробелы
  64. {
  65. i++;
  66. continue;
  67. }
  68. }
  69. //двигаемся слева - направо
  70. string tmp = "";
  71. do
  72. {
  73. tmp += s[i++];
  74.  
  75. } while (!istermsymbol(s[i - 1] || !istermsymbol(s[i]))); // текущий или след за ним не терм.
  76.  
  77. toks.push_back(token(tmp)); // разодрали на токены
  78.  
  79.  
  80. for(size_t i = 0; i < toks.size(); i++) // проход по токенам
  81. {
  82. char tokchar = toks[i].name[0]; // eсли первый символ токена цифра
  83. if (isdigit(tokchar))
  84. toks[i].type = ttconst;
  85. else if (tokchar == '(')
  86. toks[i].type = ttopbr;
  87. else if (tokchar == ')')
  88. toks[i].type = ttclbr;
  89. else if (isalpha(tokchar))
  90. if (i + 1 < toks.size() && toks[i + 1].name == "(")
  91. toks[i].type = ttfun;
  92. else
  93. toks[i].type = ttvar;
  94. }
  95.  
  96. return toks;
  97. }
  98. map<string, int> variable;
  99. map<string, int> priority;
  100.  
  101. void BuildPriorityMap()
  102. {
  103. priority["+"] = 10;
  104. priority["-"] = 10;
  105. priority["*"] = 20;
  106. priority["/"] = 20;
  107. priority["^"] = 30;
  108.  
  109. }
  110.  
  111. vector<token> BuildPostfix(vector<token> toks)
  112. {
  113. if (priority.empty())
  114. BuildPriorityMap();
  115. stack<token> s;
  116. stack<token> res;
  117. for (auto &t : toks)
  118. {
  119. switch (t.type)
  120. {
  121. case ttvar: case ttconst: res.push_back(t); break;
  122. case ttopbr: s.push(t); break;
  123. case ttclbr:
  124. while (s.top().type != ttclbr)
  125. res.push_back(pop(s));
  126. s.pop();
  127. break;
  128. case ttfun: s.push(t); break;
  129. case ttop:
  130. while (s.size() && (s.top().type == ttfun || (s.top().type == ttop && priority[s.top().name) >= )
  131. res.push_back(pop(s));
  132. s.push(t);
  133. }
  134. }
  135. while (s.size())
  136. res.push_back(pop(s));
  137. return res;
  138.  
  139. }
  140.  
  141. double pop(stack<double> &s)
  142. {
  143. double res = s.top();
  144. s.pop();
  145. return res;
  146. }
  147.  
  148. map<string, double(*)(stack<double> &)> functions;
  149.  
  150. void BuildFunctionMap()
  151. {
  152. functions["+"] = [](stack<double> &s) {return pop(s) + pop(s); };
  153. functions["*"] = [](stack<double> &s) {return pop(s) * pop(s); };
  154. functions["-"] = [](stack<double> &s) {return -pop(s) + pop(s); };
  155. functions["/"] = [](stack<double> &s) {return 1/pop(s) * pop(s); };
  156. functions["^"] = [](stack<double> &s) {auto p = pop(s); return pow(pop(s), pop(s));};
  157. functions["sin"] = [](stack<double> &s) {return sin(pop(s)); };
  158. functions["cos"] = [](stack<double> &s) {return cos(pop(s)); };
  159.  
  160.  
  161. void GetExpressionFromStream(istream &inp, string &expr, map<string, double> &vars)
  162. {
  163. getline(inp, expr); // забрать все до символа перевода строки
  164. while (!inp.eof()) // не конец потока
  165. {
  166. string s;
  167. getline(inp, s);
  168. int pos = s.find('=');
  169. if (pos == -1)
  170. continue;
  171. string var = s.substr(0, pos); // забрать пост после 0-й позиции
  172. string var = s.substr(pos+1, s.length - pos);// чтобы пробел не прибавился к переменной
  173. //var.first_not_of(' ') - первый символ, не пробел
  174. //var.last_not_of(' ')
  175. vars[var] = stod(val); // string to double
  176. }
  177. }
  178. // дома написать (короткая) алгоритм
  179. double CalculatePostFix(vector<token> &postfix, map<string, double> &vars)
  180. {
  181.  
  182. }
  183.  
  184.  
  185. int main()
  186. {
  187. string s;
  188. string expression;
  189. ifstream file("test.txt");
  190. bool key = true;
  191. while (getline(file, s))
  192. if (key == true)
  193. {
  194. expression = s;
  195. key = false;
  196. }
  197. else
  198. variable[string(s, 0, s.find("="))] = atoi(string(s, s.find("="), s.size() - 1).c_str());
  199.  
  200.  
  201.  
  202.  
  203. vector<token> T1;
  204. 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)
  205. cout << "700+sin(x) + 7 * abc ^ 13 - 9.3*acrtg(1)";
  206. /*for (int i = 0; i<T1.size(); i++)
  207. {
  208. T1[i].printToken();
  209. }*/
  210.  
  211. auto T2 = BuildPostfix(T1);
  212. for (auto &t : T2)
  213. {
  214. }
  215.  
  216. cout << endl;
  217. return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement