Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <stack>
- #include <algorithm>
- #include <map>
- #include <cctype>
- 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;
- }
- token(char n = ' ', tokentype t = ttop)
- {
- name = n;
- type = t;
- }
- void printToken()
- {
- cout << name.c_str() << " " << type;
- cout << endl;
- }
- };
- string termsymbol = "+-*/^()[]%!&|";
- bool istermsymbol(char c)
- {
- return termsymbol.find(c) != -1;
- }
- tokentype ExpressionFunctionOrVariable(string s)
- {
- if (s.find("(") != -1) //(s == "sin" || s == "cos" || s == "tan" || s == "arcsin" || s == "arccos" || s == "arctan")
- return ttfun;
- return ttvar;
- }
- bool WhetherAStringIsANumber(string s)
- {
- for (int i = 0; i < s.size(); i++)
- if (s[i] != '.' && isdigit(s[i]) == false)
- return false;
- return true;
- }
- vector<token> BuildToken(string s) // "sin(x)+7*abc^13-9.3*acrtg(1)"
- {
- 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]; // eсли первый символ токена цифра
- 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;
- }
- map<string, int> variable;
- map<string, int> priority;
- void BuildPriorityMap()
- {
- priority["+"] = 10;
- priority["-"] = 10;
- priority["*"] = 20;
- priority["/"] = 20;
- priority["^"] = 30;
- }
- vector<token> BuildPostfix(vector<token> toks)
- {
- if (priority.empty())
- BuildPriorityMap();
- stack<token> s;
- stack<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) >= )
- res.push_back(pop(s));
- s.push(t);
- }
- }
- while (s.size())
- res.push_back(pop(s));
- return res;
- }
- double pop(stack<double> &s)
- {
- double res = s.top();
- s.pop();
- return res;
- }
- map<string, double(*)(stack<double> &)> functions;
- void BuildFunctionMap()
- {
- functions["+"] = [](stack<double> &s) {return pop(s) + pop(s); };
- 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 p = pop(s); return pow(pop(s), pop(s));};
- functions["sin"] = [](stack<double> &s) {return sin(pop(s)); };
- functions["cos"] = [](stack<double> &s) {return cos(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); // забрать пост после 0-й позиции
- string var = s.substr(pos+1, s.length - pos);// чтобы пробел не прибавился к переменной
- //var.first_not_of(' ') - первый символ, не пробел
- //var.last_not_of(' ')
- vars[var] = stod(val); // string to double
- }
- }
- // дома написать (короткая) алгоритм
- double CalculatePostFix(vector<token> &postfix, map<string, double> &vars)
- {
- }
- int main()
- {
- string s;
- string expression;
- ifstream file("test.txt");
- bool key = true;
- while (getline(file, s))
- if (key == true)
- {
- expression = s;
- key = false;
- }
- else
- variable[string(s, 0, s.find("="))] = atoi(string(s, s.find("="), s.size() - 1).c_str());
- vector<token> T1;
- 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)
- cout << "700+sin(x) + 7 * abc ^ 13 - 9.3*acrtg(1)";
- /*for (int i = 0; i<T1.size(); i++)
- {
- T1[i].printToken();
- }*/
- auto T2 = BuildPostfix(T1);
- for (auto &t : T2)
- {
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement