Pagini recente » Cod sursa (job #1971670) | Cod sursa (job #3005505) | Cod sursa (job #608042) | Cod sursa (job #479010) | Cod sursa (job #2514750)
#include <fstream>
#include <cstring>
char operand[4][4] = {"+-", "*/", "^", ""};
char s[100010];
char* p = s;
const long hmax = 2;
long operatie(long x, long y, char c) {
switch (c) {
case '+':
return x + y;
break;
case '-':
return x - y;
break;
case '*':
return x * y;
break;
case '/':
return x / y;
}
return 0;
}
long eval(long);
long element() {
long r = 0;
if (*p == '(') {
++p;
r = eval(0);
++p;
}
else {
while (strchr("0123456789", *p))
r = r * 10 + *(++p - 1) - '0';
}
return r;
}
long eval(long h) {
long r = (h == hmax) ? element() : eval(h + 1);
while (strchr(operand[h], *p))
r = operatie(r, eval(h + 1), *(++p - 1));
return r;
}
int main()
{
std::ifstream fin("evaluare.in");
std::ofstream fout("evaluare.out");
fin >> s;
fout << eval(0);
fin.close(), fout.close();
return 0;
}