Pagini recente » Cod sursa (job #2723514) | Cod sursa (job #1170766) | Cod sursa (job #879066) | Cod sursa (job #2134880) | Cod sursa (job #2875419)
#include <iostream>
#include <fstream>
#include <stack>
#include <string.h>
using namespace std;
ifstream in("evaluare.in");
ofstream out("evaluare.out");
int i, j, nr, nr2, suma;
char s[100100], actual[100];
stack<string> stiva;
int main()
{
cin>>s;
for(i = 0; s[i]; i++)
{
if(s[i] >= '0' && s[i] <= '9')
{
nr = 0;
while(s[i] && s[i] >= '0' && s[i] <= '9')
{
nr = nr * 10 + (s[i] - '0');
i++;
}
i--;
if(stiva.empty() == false)
{
if(stiva.top() == "*")
{
stiva.pop();
nr2 = 0;
string elemAnt = stiva.top();
for(j = 0; elemAnt[j]; j++)
nr2 = nr2 * 10 + (elemAnt[j] - '0');
stiva.pop();
nr = nr * nr2;
}
else if(stiva.top() == "/")
{
stiva.pop();
nr2 = 0;
string elemAnt = stiva.top();
for(j = 0; elemAnt[j]; j++)
nr2 = nr2 * 10 + (elemAnt[j] - '0');
stiva.pop();
nr = nr2 / nr;
}
}
stiva.push(to_string(nr));
}
else if(s[i] == '(')
{
stiva.push("(");
}
else if(s[i] == ')')
{
suma = 0;
while(stiva.top() != "(")
{
string elem = stiva.top();
stiva.pop();
nr = 0;
for(j = 0; elem[j]; j++)
nr = nr * 10 + (elem[j] - '0');
if(stiva.top() == "-")
{
suma -= nr;
stiva.pop();
}
else if(stiva.top() == "+")
{
suma += nr;
stiva.pop();
}
else if(stiva.top() == "(")
suma += nr;
}
stiva.pop(); //pop la (
if(stiva.empty() == false)
{
if(stiva.top() == "*")
{
stiva.pop();
nr2 = 0;
string elemAnt = stiva.top();
for(j = 0; elemAnt[j]; j++)
nr2 = nr2 * 10 + (elemAnt[j] - '0');
stiva.pop();
suma = suma * nr2;
}
else if(stiva.top() == "/")
{
stiva.pop();
nr2 = 0;
string elemAnt = stiva.top();
for(j = 0; elemAnt[j]; j++)
nr2 = nr2 * 10 + (elemAnt[j] - '0');
stiva.pop();
suma = nr2 / suma;
}
}
stiva.push(to_string(suma));
}
else if(s[i] == '+')
stiva.push("+");
else if(s[i] == '-')
stiva.push("-");
else if(s[i] == '*')
stiva.push("*");
else if(s[i] == '/')
stiva.push("/");
}
suma = 0;
while(stiva.empty() == false)
{
string elem = stiva.top();
stiva.pop();
nr = 0;
for(j = 0; elem[j]; j++)
nr = nr * 10 + (elem[j] - '0');
if(stiva.empty() == true)
suma += nr;
else if(stiva.top() == "-")
{
suma -= nr;
stiva.pop();
}
else if(stiva.top() == "+")
{
suma += nr;
stiva.pop();
}
}
cout<<suma;
/*while(stiva.empty() == false)
{
cout<<stiva.top()<<" ";
stiva.pop();
}*/
return 0;
}