Mai intai trebuie sa te autentifici.
Cod sursa(job #2846083)
Utilizator | Data | 8 februarie 2022 19:05:33 | |
---|---|---|---|
Problema | Evaluarea unei expresii | Scor | 0 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.92 kb |
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <queue>
using namespace std;
queue <char> ops;
queue <int> valori;
int precedence(char op)
{
if (op == '+' or op == '-')
return 1;
if (op == '*' or op == '/')
return 2;
return 0;
}
int operatie(char op, int a, int b)
{
if (op == '+')
return a + b;
else if (op == '-')
return a - b;
else if (op == '*')
return a * b;
else if (op == '/')
return a / b;
}
int main()
{
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char c[100005], op;
int val2, val1, val;
fin.getline(c, 100005);
for (int i = 0; i < strlen(c); i++)
{
if (c[i] == ' ')
continue;
else if (c[i] == '(')
ops.push(c[i]);
else if (isdigit(c[i]))
{
val = 0;
while (i < strlen(c) and isdigit(c[i]))
{
val = val * 10 + (c[i] - '0');
i++;
}
if (!ops.empty())
{
if (ops.front() != '(')
{
val1 = valori.front();
valori.pop();
op = ops.front();
ops.pop();
valori.push(operatie(op, val1, val));
}
else
{
valori.push(val);
i--;
}
}
else
{
valori.push(val);
i--;
}
}
else if (c[i] == ')')
{
if (!ops.empty())
ops.pop();
}
else
{
ops.push(c[i]);
}
}
while (!ops.empty())
{
ops.pop();
}
fout << valori.front();
return 0;
}