Pagini recente » Cod sursa (job #2814584) | Cod sursa (job #2306760) | Cod sursa (job #3205022) | Cod sursa (job #2706225) | Cod sursa (job #2653236)
#include <bits/stdc++.h>
using namespace std;
/************************************************/
///INPUT / OUTPUT
ifstream f("evaluare.in");
ofstream g("evaluare.out");
/************************************************/
/// GLOBAL DECLARATIONS
long long operatie[100001],q[100001],n,numIndex,opIndex,number;
char s[100001];
/************************************************/
/// FUNCTIONS
//main() -> Solution()
//Solution() -> Main Logic
//readInput() -> Reads Input
//Output() -> Displays Output
/************************************************/
///---------------------------------------------------------------------
inline void readInput()
{
f.getline(s, 100001);
n = strlen(s);
}
///---------------------------------------------------------------------
int grad(char c)
{
if(c == '+' || c == '-') return 1;
if(c == '*' || c == '/') return 2;
return 0;
}
///---------------------------------------------------------------------
void calculeaza(int a,int b)
{
if(operatie[b]=='+')
{
q[a-1]+=q[a];
}
if(operatie[b]=='-')
{
q[a-1]-=q[a];
}
if(operatie[b]=='*')
{
q[a-1]*=q[a];
}
if(operatie[b]=='/')
{
q[a-1]/=q[a];
}
return;
}
///---------------------------------------------------------------------
inline void Solution()
{
for(int i = 0 ; i < n ; ++ i)
{
if(isdigit(s[i]))
{
number = 0;
while(isdigit(s[i]))
{
number *= 10;
number += (s[i] - '0');
i++;
}
i--;
q[++numIndex] = number;
}
else
{
if(opIndex == 0 || s[i] == '(' || grad(operatie[opIndex]) < grad(s[i]))
{
operatie[++ opIndex] = s[i];
}
else if(s[i] == ')')
{
while(operatie[opIndex] != '(')
{
calculeaza(numIndex, opIndex);
numIndex --;
opIndex --;
}
opIndex --;
}
else
{
while(grad(s[i]) <= grad(operatie[opIndex]))
{
calculeaza(numIndex, opIndex);
numIndex --;
opIndex --;
}
operatie[++ opIndex] = s[i];
}
}
}
while(opIndex)
{
calculeaza(numIndex, opIndex);
numIndex --;
opIndex --;
}
}
///---------------------------------------------------------------------
inline void Output()
{
g << q[1];
}
///---------------------------------------------------------------------
int main()
{
readInput();
Solution();
Output();
return 0;
}