Pagini recente » Cod sursa (job #342469) | Cod sursa (job #2258487) | Cod sursa (job #1148175) | Cod sursa (job #1894666) | Cod sursa (job #3154421)
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int bracket = 1e9 + 1, divide = 1e9 + 2, times = 1e9 + 3, Minus = 1e9 + 4;
int st[100001], top;
string a;
int main()
{
ifstream fin("evaluare.in");
fin >> a;
for (int i = 0; i < a.size(); i++)
{
if (a[i] == '+')
continue;
else if (a[i] == '-')
st[++top] = Minus;
else if (a[i] == '*')
st[++top] = times;
else if (a[i] == '/')
st[++top] = divide;
else if (a[i] == '(')
st[++top] = bracket;
else
{
int x = 0;
if (isdigit(a[i])) {
while (isdigit(a[i]))
x = x * 10 + (a[i++] - '0');
i--;
}
else // a[i] == ')'
{
while (st[top] != bracket)
x += st[top--];
top--;
}
if (!top)
st[++top] = x;
else if (st[top] == Minus)
st[top] = -x;
else if (st[top] == times)
{
st[top - 1] *= x;
top--;
}
else if (st[top] == divide)
{
st[top - 1] /= x;
top--;
}
else st[++top] = x;
}
}
int ans = 0;
for (int i = 1; i <= top; i++)
ans += st[i];
ofstream fout("evaluare.out");
fout << ans << "\n";
}