Pagini recente » Cod sursa (job #322352) | Cod sursa (job #1121596) | Cod sursa (job #642240) | Cod sursa (job #47133) | Cod sursa (job #2355270)
#include <fstream>
#include <cstring>
#define BRACKET -1000000001
#define PLUS -1000000002
#define MINUS -1000000003
#define MUL -1000000004
#define DIV -1000000005
using namespace std;
const int NMAX = 100005;
int n;
char s[NMAX];
int st[NMAX], top;
inline bool isDigit(char ch)
{
return '0' <= ch && ch <= '9';
}
int main()
{
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
fin >> (s + 1);
s[0] = '(';
n = strlen(s + 1);
s[n + 1] = ')';
++n;
for (int i = 0;i <= n;++i)
{
if (s[i] == '(')
st[++top] = BRACKET;
else if (s[i] == '+')
continue;
else if (s[i] == '-')
st[++top] = MINUS;
else if (s[i] == '*')
st[++top] = MUL;
else if (s[i] == '/')
st[++top] = DIV;
else if (s[i] == ')')
{
//fac adunarile apoi verific daca am mul sau div si il fac si pe el
int x = 0;
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] == MUL)
--top, st[top] *= x;
else if (st[top] == DIV)
--top, st[top] /= x;
else
st[++top] = x;
}
else
{
int x = 0;
while (i <= n && isDigit(s[i]))
{
x = x * 10 + (s[i] - '0');
++i;
}
--i;
if (top == 0)
st[++top] = x;
else if (st[top] == MINUS)
st[top] = -x;
else if (st[top] == MUL)
--top, st[top] *= x;
else if (st[top] == DIV)
--top, st[top] /= x;
else
st[++top] = x;
}
}
fout << st[1] << "\n";
fin.close();
fout.close();
return 0;
}