Pagini recente » Cod sursa (job #276120) | Cod sursa (job #2547221)
#include <fstream>
using namespace std;
ifstream cin("evaluare.in");
ofstream cout("evaluare.out");
const int dim = 100001;
int current;
char a[dim];
int evaluare();
int termen();
int factor();
bool isOp1(char c)
{
return (c == '+' || c == '-');
}
bool isOp2(char c)
{
return (c == '*' || c == '/');
}
bool isDigit(char c)
{
if(c >= '0' && c <='9')
return true;
return false;
}
int getSign(char c)
{
if(c == '-')
return -1;
else
return 1;
}
int getDigit(char c)
{
return c - '0';
}
int evaluare()
{
int rez = termen(), sign = 0;
while(isOp1(a[current]))
{
sign = getSign(a[current++]);
rez += sign * termen();
}
return rez;
}
int termen()
{
int rez = factor();
if(isOp2(a[current]))
{
if(a[current] == '*')
current++, rez *= factor();
else
current++, rez /= factor();
}
return rez;
}
int factor()
{
int rez = 0, sign = 1;
while(isOp1(a[current]) && getSign(a[current]) == -1)
{
current++;
sign *= -1;
}
if(a[current] == '(')
{
current++;
rez = evaluare();
current++;
return rez * sign;
}
while(isDigit(a[current]))
{
rez = rez * 10 + getDigit(a[current]);
current++;
}
return rez * sign;
}
int main()
{
cin>>a;
int rez = evaluare();
cout<<rez;
return 0;
}