Cod sursa(job #2375499)

Utilizator AlexPop28Pop Alex-Nicolae AlexPop28 Data 8 martie 2019 09:55:10
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.71 kb
#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define fi first
#define se second
#define DEBUG(x) cerr << (#x) << ": " << (x) << '\n'
/// am zis ca nu lucrez nimic cu o zi inainte de oji da bag asta doar sa fiu eu linistit ca mi iese first try
using namespace std;

typedef pair <int, int> pii;
typedef vector <int> vi;
typedef long long ll;
typedef unsigned long long ull;

template <class T> void uin (T &a, T b) {a = min (a, b);}
template <class T> void uax (T &a, T b) {a = max (a, b);}

ifstream f ("evaluare.in");
ofstream g ("evaluare.out");

const int NMAX = 1e5 + 5;

int n, pos;
char s[NMAX];

int eval();

int getInt() {
  int ret = 0;
  while (isdigit (s[pos])) {
    (ret *= 10) += s[pos++] - '0';
  }

  return ret;
}

int factor() {
  int ret = 0;
  if (s[pos] == '(') {
    ++pos;
    ret = eval();
    ++pos;
  } else {
    ret = getInt();
  }

  return ret;
}

int term() {
  int ret = factor();
  while (s[pos] == '*' || s[pos] == '/') {
    if (s[pos++] == '*') {
      ret *= factor();
    } else {
      ret /= factor();
    }
  }

  return ret;
}

int eval() {
  int ret = 0, sign;
  ret = term();
  while (s[pos] == '+' || s[pos] == '-') {
    if (s[pos++] == '+') sign = 1;
    else sign = -1;

    ret += sign * term();
  }

  return ret;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL_DEFINE
  freopen (".in", "r", stdin);
#endif

  f.getline (s + 1, NMAX);
  n = strlen (s + 1);

  pos = 1;
  g << eval() << '\n';

  f.close();
  g.close();

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}