Pagini recente » Cod sursa (job #2124414) | Cod sursa (job #1844461) | Cod sursa (job #2696640) | Cod sursa (job #2904271) | Cod sursa (job #2721368)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
string str;
int index = 0;
int firstPriority();
int secondPriority();
int thirdPriority();
// ()
int firstPriority()
{
int nr = 0;
if (str[index] == '(')
{
index++;
nr += thirdPriority();
index++;
}
else
{
while(isdigit(str[index]))
{
nr *= 10;
nr += str[index++] - '0';
}
}
return nr;
}
// * /
int secondPriority()
{
int nr = firstPriority();
while (str[index] == '*' || str[index] == '/')
{
if (str[index] == '*')
{
index++;
nr *= firstPriority();
}
else
{
index++;
nr /= firstPriority();
}
}
return nr;
}
// + -
int thirdPriority()
{
int nr = secondPriority();
while (str[index] == '+' || str[index] == '-')
{
if (str[index] == '+')
{
index++;
nr += secondPriority();
}
else
{
index++;
nr -= secondPriority();
}
}
return nr;
}
int main()
{
fin >> str;
fout << thirdPriority();
return 0;
}