Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <vector>
- #include <iostream>
- #include <stack>
- #include <algorithm>
- #include <cmath>
- #include <map>
- #include <fstream>
- #include <string>
- using namespace std;
- enum tokentype { ttconst, ttvar, ttopbr, ttclbr, ttfun, ttop };
- struct token
- {
- string name;
- tokentype type;
- token(string n = "", tokentype t = ttop)
- {
- name = n; type = t;
- }
- };
- string termsymbol = "+-*/^()[]%!&|";
- bool istermsymbol(char c)
- {
- return termsymbol.find(c) != -1;
- }
- vector<token> BuildToken(string s)
- {
- vector<token> toks;
- size_t i = 0;
- s += " ";
- while (i < s.length()) {
- if (s[i] == ' ') {
- i++;
- continue;
- }
- string tmp = "";
- do {
- tmp += s[i++];
- } while (!istermsymbol(s[i - 1]) || !istermsymbol(s[i]));
- toks.push_back(token(tmp));
- }
- for (size_t i = 0; i < toks.size(); i++){
- char tokchar = toks[i].name[0];
- if (isdigit(tokchar))
- toks[i].type = ttconst;
- else if (tokchar == '(')
- toks[i].type = ttopbr;
- else if (tokchar == ')')
- toks[i].type = ttclbr;
- else if (isalpha(tokchar))
- if (i + 1 < toks.size() && toks[i + 1].name == "(")
- toks[i].type = ttfun;
- else toks[i].type = ttvar;
- }
- return toks;
- }
- double pop(stack<double> &s)
- {
- double res = s.top();
- s.pop();
- return res;
- }
- map<string, int> priority;
- void BuildPriorityMap()
- {
- priority["+"] = 10;
- priority["-"] = 10;
- priority["*"] = 20;
- priority["/"] = 20;
- priority["^"] = 30;
- }
- template <typename T>
- T pop(stack<T> &s) {
- T res = s.top();
- s.pop();
- return res;
- }
- vector<token> BuildPostfix(vector<token> toks) {
- if (priority.empty())
- BuildPriorityMap();
- stack<token> s;
- vector<token> res;
- for (auto &t : toks)
- {
- switch (t.type) {
- case ttvar: case ttconst: res.push_back(t); break;
- case ttopbr: s.push(t); break;
- case ttclbr:
- while (s.top().type != ttclbr) res.push_back(pop(s));
- s.pop();
- break;
- case ttfun: s.push(t); break;
- case ttop:
- while (s.size() && (s.top().type == ttfun || (s.top().type == ttop && priority[s.top().name] >= priority[s.top().name])))
- res.push_back(pop(s));
- s.push(t);
- }
- }
- while (s.size())
- res.push_back(pop(s));
- return res;
- }
- map<string, double> variable;
- void SelectFromFile(stack<char> &s) {
- ifstream ifile("funct.txt");
- while (!ifile.eof()) {
- s.push(ifile.get());
- }
- ifile.close();
- //At the other lines variables with their values
- }
- double op_plus(stack<double> &S)
- {
- return pop(S) + pop(S);
- }
- map<string, double(*)(stack<double> &) > functions;
- void BuildFunctionMap()
- {
- functions["+"] = op_plus;
- functions["*"] = [](stack<double> &s) { return pop(s)*pop(s); };
- functions["-"] = [](stack<double> &s) { return -pop(s) + pop(s); };
- functions["/"] = [](stack<double> &s) { return 1 / pop(s) / pop(s); };
- functions["^"] = [](stack<double> &s) { auto ad = pop(s); return pow(pop(s), ad); };
- functions["sin"] = [](stack<double> &s) {return sin(pop(s)); };
- functions["!"] = [](stack<double> &s) {double ad = 1; for (int i = 1; i < pop(s); i++) ad *= i; return ad; };
- functions["cos"] = [](stack<double> &s) {return sin(pop(s)); };
- }
- void GetExpressionFromStream(istream &inp, string &expr, map<string, double> &vars) {
- getline(inp, expr);
- while (!inp.eof()) {
- string s;
- getline(inp, s);
- int pos = s.find('=');
- if (pos == -1) continue;
- string var = s.substr(0, pos);
- string val = s.substr(pos + 1, s.length() - pos);
- vars[var] = stod(val);
- }
- }
- double CalculatePostfix(vector<token> &postfix, map<string, double> &vars){
- //todo
- }
- int main()
- {
- vector<token> T1 = BuildToken("2.15*abc+sin(x+abc/(a-exp(x+y)/3.55))^(a/b*c - cos(3*x))");
- auto T2 = BuildPostfix(T1);
- for (auto &t : T2) {
- cout << t.name << " ";
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement