Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <list>
- #include <sstream>
- #include<cmath>
- using namespace std;
- enum Type {
- Number, Operator, Brace, Unary, Defualt
- };
- struct Element {
- Type type;
- float value;
- Element() { type = Defualt; value = 0; }
- Element(Type type, float value) {
- this->type = type;
- this->value = value;
- }
- };
- ostream & operator<<(std::ostream & os, const Element & m) {
- switch (m.type)
- {
- case Number:
- os << m.value << " ";
- break;
- case Brace:
- os << ((m.value == 1) ? ("(") : (")")) << " ";
- break;
- case Operator:
- switch ((int)m.value)
- {
- case 1: os << "+ "; break;
- case 2: os << "- "; break;
- case 3: os << "* "; break;
- case 4: os << "/ "; break;
- default:
- break;
- }
- break;
- case Unary:
- os << "_ ";
- break;
- default:
- os << "ERR ";
- break;
- }
- return os;
- }
- int OperToNum(char c) {
- switch (c)
- {
- case '+':
- return 1;
- break;
- case '*':
- return 3;
- break;
- case '/':
- return 4;
- break;
- }
- }
- int main()
- {
- stack<char> stk;
- stack<list<Element>::iterator> Stack;
- list<Element> lis;
- string str;
- getline(cin, str);
- bool ValidZagradi = true;
- for (char i : str)
- {
- if (i == '(' || i == '[' || i == '{')
- stk.push(i);
- if (i == ')')
- if (!stk.empty() && stk.top() == '(') stk.pop();
- else ValidZagradi = false;
- if (i == ']')
- if (!stk.empty() && stk.top() == '[') stk.pop();
- else ValidZagradi = false;
- if (i == '}')
- if (!stk.empty() && stk.top() == '{') stk.pop();
- else ValidZagradi = false;
- }
- if (ValidZagradi == false || !stk.empty())
- cout << "Loshi Zagradi\n";
- else {
- bool VnesuvaBr = false;
- float VlezenBr = 0.0f;
- lis.push_back(Element(Brace, 1));
- for (int i = 0; i < str.size(); i++) {
- char c = str[i];
- if (c >= '0' && c <= '9') {
- VnesuvaBr = true;
- VlezenBr *= 10;
- VlezenBr += c - '0';
- if (i + 1 >= str.size() || str[i + 1] < '0' || str[i + 1]>'9') {
- lis.push_back(Element(Number, VlezenBr));
- VlezenBr = 0; VnesuvaBr = false;
- }
- }
- if (c == '+' || c == '*' || c == '/') {
- lis.push_back(Element(Operator, OperToNum(c)));
- }
- if (c == '(' || c == '[' || c == '{') {
- lis.push_back(Element(Brace, 1));
- }
- if (c == ')' || c == ']' || c == '}') {
- lis.push_back(Element(Brace, 2));
- }
- if (c == '-') {
- if (lis.size() == 1 || ((--lis.end()))->type != Number && !((--lis.end())->type == Brace && (--lis.end())->value == 2))
- lis.push_back(Element(Unary, 1));
- else
- lis.push_back(Element(Operator, 2));
- }
- }
- lis.push_back(Element(Brace, 2));
- for (list<Element>::iterator it = lis.begin(); it != lis.end(); it++) {
- if (it->type == Brace) {
- if (it->value == 1) {
- Stack.push(it);
- }
- else {
- list<Element>::iterator LeftBracket = Stack.top(), RightBracket = it; Stack.pop();
- //Presmetuvanje na Unaren Minus
- for (list<Element>::iterator El = LeftBracket; El != RightBracket; El++) {
- if (El->type == Unary) {
- list<Element>::iterator Num = El; Num++;
- if (Num->type != Number) {
- cout << "GRESHEN IZRAZ"; return 0;
- }
- Num->value *= -1;
- lis.erase(El);
- El = Num;
- }
- }
- //Presmetuvanje na * i /
- for (list<Element>::iterator El = LeftBracket; El != RightBracket; El++) {
- if (El->type == Operator && El->value > 2) {
- list<Element>::iterator Numl = El, Numr = El; Numr++; Numl--;
- if (Numr->type != Number || Numl->type != Number)
- {
- cout << "GRESHEN IZRAZ"; return 0;
- }
- if (El->value == 3)Numl->value *= Numr->value;
- else if (El->value == 4) Numl->value /= Numr->value;
- else {
- cout << "GRESHEN IZRAZ"; return 0;
- }
- lis.erase(El);
- lis.erase(Numr);
- El = Numl;
- }
- }
- //Presmetuvanje na + i bin -
- for (list<Element>::iterator El = LeftBracket; El != RightBracket; El++) {
- if (El->type == Operator && El->value <= 2) {
- list<Element>::iterator Numl = El, Numr = El; Numr++; Numl--;
- if (Numr->type != Number || Numl->type != Number)
- {
- cout << "GRESHEN IZRAZ"; return 0;
- }
- if (El->value == 2)Numl->value -= Numr->value;
- else if (El->value == 1) Numl->value += Numr->value;
- else {
- cout << "GRESHEN IZRAZ"; return 0;
- }
- lis.erase(El);
- lis.erase(Numr);
- El = Numl;
- }
- }
- //Brishenje na zagradi
- list<Element>::iterator Num = RightBracket; Num--;
- lis.erase(LeftBracket);
- lis.erase(RightBracket);
- it = Num;
- }
- }
- }
- for (Element el : lis) {
- cout << el;
- }cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement