Cod sursa(job #2872160)

Utilizator simaderalSimader Albert simaderal Data 16 martie 2022 13:42:46
Problema Evaluarea unei expresii Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.08 kb
#include <fstream>
#include <string>
#include <iostream>
#include <stack>
using namespace std;

int priority(char c) {
    /*if (c == '^')
        return 3;*/
    if (c == '/' || c == '*')
        return 2;
    else if (c == '+' || c == '-')
        return 1;
    else
        return -1;
}
string toPostfix(string s) {
    int i = 0, number = 0;
    int n = s.size();
    char c = s[0];
    bool exit = false, first = true;
    stack<char> st;
    string res ="";
    while (i < n) {
        if (isdigit(c)) {
            number = 0;
            while (isdigit(c) && !exit) {
                number = number * 10 + (c-'0');
                i++;
                if (i != n)
                    c = s[i];
                else
                    exit = 1;
            }
            if (first) res = res + to_string(number), first = false;
            else res = res + " " + to_string(number);
            
            if (exit) break;
        }
        if (c == '(')
            st.push(c);
        else if (c == ')') {
            while (st.top() != '(')
            {
                res = res + st.top();
                st.pop();
            }
            st.pop();
        }
        else {
            while (!st.empty() && priority(c) <= priority(st.top())) {
                res = res + st.top();
                st.pop();
            }
            st.push(c);
        }
        ++i;
        c = s[i];
    }
a:
    while (!st.empty()) {
        res = res + st.top();
        st.pop();
    }
    return res;
}
int char_to_int(char c) {
    return c - '0';
}
bool isOperator(char c) {
    if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
        return true;
    return false;
}
bool isOperand(char c) {
    if (c >= '0' && c <= '9')
        return true;
    return false;
}
int operation(int a, int b, char op) {
    switch (op) {
    case '+':  return a + b;
    case '-':  return a - b;
    case '*':  return a * b;
    case '/':  return a / b;
    }
    //else if (op == '^')
      //  return pow(b, a);
}
bool isSpace(char c) {
    return c == ' ';
}
bool cifra(char c) {
    int n;
    n = c - '0';
    return n >= 0 && n <= 9;
}
int expr_eval(string s) {
    int a, b;
    int k = 0;
    int n = s.size();
    stack<int> stk;
    int i = 0;
    do {
        if (isSpace(s[i]))
            ++i;
        else if (isOperator(s[i])) {
            b = stk.top();
            stk.pop();
            a = stk.top();
            stk.pop();
            if (b == 0 and s[i] == '/') { cout << "\n#####invalid operation\n"; return 0; }
            stk.push(operation(a, b, s[i]));
            ++i;
        }
        else if (cifra(s[i])) {
            k = 0;
            while (i < n && cifra(s[i])) {
                k = k * 10 + ((int)s[i] - '0');
                ++i;
            }
            stk.push(k);
        }
    } while (i<n);
    return stk.top();
}
int main(void) {
    string exp;
    ifstream f("evaluare.in");
    ofstream g("evaluare.out");
    getline(f, exp);
    g << expr_eval(toPostfix(exp));
}