Pagini recente » Cod sursa (job #2205391) | Cod sursa (job #2694962) | Cod sursa (job #567480) | Cod sursa (job #1163904) | Cod sursa (job #1342240)
#include <iostream>
#include <fstream>
using namespace std;
long nr[50000], nrI = -1, opI = -1;
char op[50000];
bool isDigit(char c) {
return ('0' <= c) && (c <= '9');
}
int isOperator(char c)
{
return ((c == '(') || (c == '+') || (c == '-') || (c == '*') || (c == '/'));
}
int getPriority(char c)
{
if ((c == '(') || (c == ')')) {
return 1;
} else if ((c == '+') || (c == '-')) {
return 2;
} else if ((c == '*') || (c == '/')) {
return 3;
}
return 0;
}
long doOp(long a, long b, char op) {
if (op == '+') {
return a + b;
} else if (op == '-') {
return a - b;
} else if (op == '*') {
return a * b;
} else if (op == '/') {
return a / b;
}
return 0;
}
void eval()
{
nr[nrI - 1] = doOp(nr[nrI - 1], nr[nrI], op[opI]);
--nrI;
--opI;
}
int main()
{
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char old_c = 0, c;
while (fin >> c) {
if (isDigit(c)) {
if (!isDigit(old_c)) {
nr[++nrI] = 0;
}
nr[nrI] = (nr[nrI] * 10) + (c - '0');
} else if (getPriority(c)) {
if (c == ')') {
while (op[opI] != '(') {
eval();
}
--opI;
} else {
while ((getPriority(c) < getPriority(op[opI])) && (opI > -1)) {
eval();
}
op[++opI] = c;
}
}
old_c = c;
}
while (opI > -1) {
eval();
}
fout << nr[0] << "\n";
return 0;
}