SHOW:
|
|
- or go back to the newest paste.
1 | - | #include <iostream> //((a+b)*((-d)+(-e))) |
1 | + | #include <algorithm> |
2 | - | #include <fstream> |
2 | + | #include <cctype> |
3 | #include <string> | |
4 | ||
5 | - | using namespace std; |
5 | + | namespace parser |
6 | { | |
7 | class parse_symbol | |
8 | - | int fun(string &str) |
8 | + | { |
9 | constexpr bool is_letter(const char & C) const { return (C >= 'a') && (C <= 'z'); } | |
10 | ||
11 | - | while(str.size()) |
11 | + | constexpr bool is_number(const char & C) const { return (C >= '0') && (C <= '9'); } |
12 | ||
13 | - | cout<<str<<endl; |
13 | + | constexpr bool is_operator(const char & C) const { return (C == '-') || (C == '+') || (C == '*'); } |
14 | ||
15 | - | if(str[0]=='(') |
15 | + | public: |
16 | parse_symbol() = default; | |
17 | - | str=str.substr(1,str.size()-1); |
17 | + | |
18 | - | int k=fun(str); |
18 | + | constexpr int operator<<(const char & C) const |
19 | - | str=str.substr(k,str.size()-k); |
19 | + | |
20 | - | if(str[0]==')') |
20 | + | if (is_letter(C)) |
21 | - | continue; |
21 | + | return 1; |
22 | - | if(str[0]=='\0') |
22 | + | else if (is_operator(C)) |
23 | - | return 0; |
23 | + | return 2; |
24 | - | if((str[0]=='-'||str[0]=='*'||str[0]=='+')&&((str[1]>=97&&str[1]<=122)||str[1]=='(')) |
24 | + | else if (C == '(') |
25 | - | { |
25 | + | return 3; |
26 | - | str=str.substr(2,str.size()-2); |
26 | + | else if (C == ')') |
27 | - | continue; |
27 | + | return 4; |
28 | - | } |
28 | + | return 0; |
29 | } | |
30 | - | if(str[0]!='*'&&str[0]!='+'&&str[0]!='-') |
30 | + | }; // End of 'parse_symbol' class |
31 | - | { |
31 | + | |
32 | static parse_symbol Parser; | |
33 | ||
34 | - | cout<<"Error1"<<endl; |
34 | + | bool parse_string(std::string & Str) |
35 | - | exit(0); |
35 | + | { |
36 | - | } |
36 | + | if (Str.size() <= 0) |
37 | return false; | |
38 | ||
39 | - | else |
39 | + | // remove spaces |
40 | Str.erase(std::remove(Str.begin(), Str.end(), ' '), Str.end()); | |
41 | // switch to lowercase | |
42 | - | if(str[0]==')') |
42 | + | std::transform(Str.begin(), Str.end(), Str.begin(), [](unsigned char c) { return std::tolower(c); }); |
43 | - | { |
43 | + | |
44 | - | if(str[1]>=97&&str[1]) |
44 | + | int state = 0; // 1 - letter, 2 - operation, 3 - end, 4 - ), 5 - btw |
45 | - | { |
45 | + | |
46 | - | cout<<"Error2"<<endl; |
46 | + | for (auto & a : Str) |
47 | - | exit(0); |
47 | + | |
48 | - | } |
48 | + | switch (Parser << a) |
49 | - | /* |
49 | + | { |
50 | - | if(str[1]!=')'&&(str.size()-1)) |
50 | + | case 3: // ( |
51 | - | str=str.substr(1,str.size()-1); |
51 | + | return ((Parser << Str.back()) == 4) ? parse_string(Str.substr(1, Str.size() - 2)) : false; |
52 | - | */ |
52 | + | break; |
53 | - | str=str.substr(1,str.size()-1); |
53 | + | case 1: // a |
54 | - | if((str[0]=='-'||str[0]=='*'||str[0]=='+')&&((str[1]>=97&&str[1]<=122)||str[1]=='(')) |
54 | + | switch (state) |
55 | - | { |
55 | + | |
56 | - | str=str.substr(2,str.size()-2); |
56 | + | case 0: // no |
57 | - | continue; |
57 | + | case 2: // after op |
58 | - | } |
58 | + | state++; |
59 | - | continue; |
59 | + | break; |
60 | - | } |
60 | + | default: // end or after a |
61 | - | if(str[0]>=97&&str[0]<=122&&str[2]>=97&&str[2]<=122&&(str[1]>='*'||str[1]<='+'||str[1]>='-')) |
61 | + | return false; |
62 | - | return 3; |
62 | + | break; |
63 | - | cout<<"Error3"<<endl; |
63 | + | |
64 | - | cout<<str<<endl; |
64 | + | break; |
65 | - | exit(0); |
65 | + | case 2: // + |
66 | switch (state) | |
67 | { | |
68 | case 1: // after a | |
69 | state = 2; | |
70 | break; | |
71 | default: | |
72 | return false; | |
73 | break; | |
74 | - | return 0; |
74 | + | |
75 | - | } |
75 | + | break; |
76 | case 4: // ) | |
77 | - | int main() |
77 | + | case 0: // odd symbols |
78 | return false; | |
79 | - | string str, command; |
79 | + | break; |
80 | - | cout << R"(Would u like to get input from "input.txt"? Y/N)" << endl; |
80 | + | } |
81 | - | cin>>str; |
81 | + | |
82 | - | if (str[0] == 'Y') { |
82 | + | return true; |
83 | - | cout << R"(Searching for it...)" << endl; |
83 | + | } |
84 | - | ifstream input ("input.txt", ifstream::in); |
84 | + |