Pagini recente » Cod sursa (job #2629992) | Cod sursa (job #2657206) | Cod sursa (job #280891) | Cod sursa (job #1549335) | Cod sursa (job #531335)
Cod sursa(job #531335)
#include <stdio.h>
#define DIM 100001
int expresie ();
int termeni ();
int factori ();
char S[DIM], *P = S;
int expresie ()
{
int n = termeni ();
while (*P == '+' || *P == '-')
if (*P == '+')
{
++P;
n += termeni ();
}
else
{
++P;
n -= termeni ();
}
return n;
}
int termeni ()
{
int n = factori ();
while (*P == '*' || *P == '/')
if (*P == '*')
{
++P;
n *= factori ();
}
else
{
++P;
n /= factori ();
}
return n;
}
int factori ()
{
int n = 0;
if (*P == '(')
{
++P;
n = expresie ();
++P;
}
else
while (*P >= '0' && *P <= '9')
{
n = n * 10 + *P - '0';
++P;
}
return n;
}
int main ()
{
freopen ("evaluare.in", "r", stdin);
freopen ("evaluare.out", "w", stdout);
fgets (S, DIM, stdin);
printf ("%d", expresie () );
return 0;
}