Cod sursa(job #2671175)

Utilizator thewildnathNathan Wildenberg thewildnath Data 11 noiembrie 2020 17:00:51
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>
#include <string>

using namespace std;

string sir;

int poz;

int eval();
int termen();
int factor();

int expr()
{
  int val = termen();

  while (poz < sir.size())
  {
    if (sir[poz] == '+')
    {
      ++poz;
      val += termen();
    }
    else // if (sir[poz] == '-')
    {
      ++poz;
      val -= termen();
    }
  }

  return val;
}

int termen()
{
  int val = factor();

  while (poz < sir.size())
  {
    if (sir[poz] == '*')
    {
      ++poz;
      val *= factor();
    }
    else // if (sir[poz] == '/')
    {
      ++poz;
      val /= factor();
    }
  }

  return val;
}

int factor()
{
  int val = 0;

  if (sir[poz] == '(')
  {
    ++poz; // '('
    val = expr();
    ++poz; // ')'
  }
  else
  {
    while (poz < sir.size() && '0' <= sir[poz] && sir[poz] <= '9')
    {
      val = val * 10 + (sir[poz] - '0');
      ++poz;
    }
  }

  return val;
}

int main()
{
  ifstream cin("evaluare.in");
  ofstream cout("evaluare.out");

  cin >> sir;

  cout << expr() << '\n';

  return 0;
}