Pagini recente » Cod sursa (job #1318472) | Cod sursa (job #1853803) | Cod sursa (job #77646) | Cod sursa (job #194134) | Cod sursa (job #2351796)
#include <bits/stdc++.h>
#define nmax 100007
#define paran 100000001 /// (
#define minu 100000003 /// -
#define inmultire 100000004 /// *
#define impartire 100000005 /// /
using namespace std;
ifstream fin ("evaluare.in");
ofstream fout ("evaluare.out");
char s[nmax];
int a[nmax], top, rez;
int Numar(int &poz)
{
int x = 0;
while ('0' <= s[poz] && s[poz] <= '9')
x = x * 10 + int(s[poz++] - '0');
return x;
}
void Rezolvare()
{
int i, x;
for (i = 1; s[i] != 0; )
{
if (s[i] == '(')
{
a[++top] = paran;
i++;
}
if (s[i] == '+')
{
i++;
}
if (s[i] == '-')
{
a[++top] = minu;
i++;
}
if (s[i] == '*')
{
a[++top] = inmultire;
i++;
}
if(s[i] == '/')
{
a[++top] = impartire;
i++;
}
if ('0' <= s[i] && s[i] <= '9')
{
x = Numar(i);
/// minus ( - )
while (a[top] == minu)
{
x = x * (-1);
top--;
}
/// inmultire ( * )
while (a[top] == inmultire && top > 0)
{
top--;
x = x * a[top--];
}
/// impartire ( / )
while (a[top] == impartire && top > 0)
{
top--;
x = a[top--] / x;
}
a[++top] = x;
}
if (s[i] == ')')
{
x = 0;
while (a[top] != paran && top > 0)
x += a[top--];
top--; /// paranteza in sine
/// minus ( - )
while (a[top] == minu)
{
x = x * (-1);
top--;
}
/// inmultire ( * )
while (a[top] == inmultire && top > 0)
{
top--;
x = x * a[top--];
}
/// impartire ( / )
while (a[top] == impartire && top > 0)
{
top--;
x = a[top--] / x;
}
a[++top] = x;
i++;
}
}
while (top)
rez += a[top--];
}
int main()
{
fin >> (s + 1);
Rezolvare();
fout << rez << "\n";
fin.close();
fout.close();
return 0;
}