Cod sursa(job #2956503)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 19 decembrie 2022 17:48:44
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.85 kb
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
using ll = long long;

using namespace std;

/*******************************/
// INPUT / OUTPUT

ifstream f("evaluare.in");
ofstream g("evaluare.out");
/*******************************/
/// GLOBAL DECLARATIONS

string s;
int ans, p;

int expresie();
int termen();
int factor();
int numar();
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    f >> s;
}
///-------------------------------------
int expresie()
{
    int sum = termen();

    while (s[p] == '+' || s[p] == '-')
    {
        if (s[p] == '+')
            ++ p, sum += termen();
        else
            ++ p, sum -= termen();
    }
    
    return sum;
}
///-------------------------------------
int termen()
{
    int sum = factor();

    while (s[p] == '*' || s[p] == '/')
    {
        if (s[p] == '*')
            ++ p, sum *= factor();
        else
            ++ p, sum /= factor();
    }
    
    return sum;
}
///-------------------------------------
int factor()
{
    int sum;
    if (s[p] == '(')
        ++ p, sum = expresie(), ++ p;
    else
        sum = numar();
    
    return sum;
}
///-------------------------------------
int numar()
{
    int val = 0, sign = 1;
    while (s[p] == '-')
        ++ p, sign *= -1;

    while (isdigit(s[p]))
    {
        val = val * 10 + (s[p] - '0');
        ++ p;
    }

    return val;
}
///-------------------------------------
inline void Solution()
{
    ans = expresie();
}
///-------------------------------------
inline void Output()
{
    g << ans;
}
///-------------------------------------
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ReadInput();
    Solution();
    Output();
    return 0;
}