Pagini recente » Cod sursa (job #444627) | Cod sursa (job #135122) | Cod sursa (job #591676) | Cod sursa (job #174171) | Cod sursa (job #1615676)
#include <bits/stdc++.h>
using namespace std;
#define FILE_IO
const int INF = 1 << 30;
const long long LLINF = 1LL << 62;
const int mod = 1000000007;
char ch;
char opr[2][2] = {
'+', '-',
'*', '/'
};
int operate(int x, int y, char op)
{
if(op == '+') return (x + y);
if(op == '-') return (x - y);
if(op == '*') return (x * y);
if(op == '/') return (x / y);
return 0;
}
bool goodoperator(int grd, char op)
{
for(int i = 0; i <= 1; i++)
if(opr[grd][i] == op)
return true;
return false;
}
int expresie(int grd)
{
if(grd == 2)
{
if(ch == '(')
{
ch = getchar();
int ans = expresie(0);
ch = getchar();
return ans;
}
int ans = 0;
while('0' <= ch && ch <= '9')
{
ans = ans * 10 + ch - '0';
ch = getchar();
}
return ans;
}
int ans = expresie(grd + 1);
while( goodoperator(grd, ch) )
{
char op = ch;
ch = getchar();
ans = operate(ans, expresie(grd + 1), op);
}
return ans;
}
int main()
{
#ifdef FILE_IO
freopen("evaluare.in", "r", stdin);
freopen("evaluare.out", "w", stdout);
#endif
ch = getchar();
printf("%d\n", expresie(0));
return 0;
}