Pagini recente » Cod sursa (job #2196391) | Cod sursa (job #420631) | Cod sursa (job #1965432) | Cod sursa (job #1779521) | Cod sursa (job #1615679)
#include <bits/stdc++.h>
using namespace std;
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()
{
freopen("evaluare.in", "r", stdin);
freopen("evaluare.out", "w", stdout);
ch = getchar();
printf("%d\n", expresie(0));
return 0;
}