Pagini recente » Cod sursa (job #265936) | Cod sursa (job #2592751) | Cod sursa (job #207808) | Cod sursa (job #2334847) | Cod sursa (job #3172972)
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int bracket = 1e9 + 1, times = 1e9 + 2, divide = 1e9 + 3, Minus = 1e9 + 4;
int st[100001], top;
int main()
{
string s;
ifstream fin ("evaluare.in");
fin >> s;
for (int i = 0; i < s.size(); i++)
if (s[i] == '+')
continue;
else if (s[i] == '(')
st[++top] = bracket;
else if (s[i] == '*')
st[++top] = times;
else if (s[i] == '/')
st[++top] = divide;
else if (s[i] == '-')
st[++top] = Minus;
else {
int x = 0;
if (isdigit(s[i])) {
while (isdigit(s[i]))
x = x * 10 + s[i++] - '0';
i--;
}
else {
while (st[top] != bracket)
{
x += st[top];
top--;
}
top--;
}
if (top == 0)
st[++top] = x;
else if (st[top] == Minus)
st[top] = -x;
else if (st[top] == times) {
top--;
st[top] *= x;
} else if (st[top] == divide) {
top--;
st[top] /= x;
} else st[++top] = x;
}
int sum = 0;
for (int i = 1; i <= top; i++)
sum += st[i];
ofstream fout ("evaluare.out");
fout << sum;
return 0;
}