Pagini recente » Cod sursa (job #2432443) | Cod sursa (job #657464) | Cod sursa (job #1707589) | Atasamentele paginii Poze preONI 2007 - gratar | Cod sursa (job #1520545)
#include <fstream>
#include <string>
#include <stack>
#include <stdio.h>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
//FILE* f(fopen("expresie.out", "w"));
stack <char> stiva;
string x;
long double polo[1000000];
int j=1;
void poloneza(string &x)
{
int nrcrt=0;
bool paranteza=false;
for(int i=0; i<x.size(); i++)
{
if(x[i]>='0' && x[i]<='9')
{
nrcrt=nrcrt*10+x[i]-'0';
}
else if(x[i]=='+')
{
polo[j]=nrcrt;
j++;
nrcrt=0;
if(!stiva.empty())
{
if(stiva.top()=='*')
{
polo[j]=-1000000003;
j++;
stiva.pop();
stiva.push('+');
}
else if(stiva.top()=='/')
{
polo[j]=-1000000004;
j++;
stiva.pop();
stiva.push('+');
}
else
stiva.push('+');
}
else
stiva.push('+');
}
else if(x[i]=='-')
{
polo[j]=nrcrt;
j++;
nrcrt=0;
if(!stiva.empty())
{
if(stiva.top()=='*')
{
polo[j]=-1000000003;
j++;
stiva.pop();
stiva.push('-');
}
else if(stiva.top()=='/')
{
polo[j]=-1000000004;
j++;
stiva.pop();
stiva.push('-');
}
else
stiva.push('-');
}
else
stiva.push('-');
}
else if(x[i]=='*')
{
if(x[i-1]!=')')
{
polo[j]=nrcrt;
j++;
nrcrt=0;
}
stiva.push('*');
}
else if(x[i]=='/')
{
if(x[i-1]!=')')
{
polo[j]=nrcrt;
j++;
nrcrt=0;
}
stiva.push('/');
}
else if(x[i]=='(')
{
stiva.push('(');
}
else if(x[i]==')')
{
if(i==x.size()-1)
paranteza=true;
if(x[i-1]!=')')
{
polo[j]=nrcrt;
j++;
nrcrt=0;
}
while(stiva.top()!='(')
{
if(stiva.top()=='+')
polo[j]=-1000000001;
if(stiva.top()=='-')
polo[j]=-1000000002;
if(stiva.top()=='*')
polo[j]=-1000000003;
if(stiva.top()=='/')
polo[j]=-1000000004;
j++;
stiva.pop();
}
stiva.pop();
}
}
if(!paranteza)
{
polo[j]=nrcrt;
j++;
}
while(!stiva.empty())
{
if(stiva.top()=='+')
polo[j]=-1000000001;
if(stiva.top()=='-')
polo[j]=-1000000002;
if(stiva.top()=='*')
polo[j]=-1000000003;
if(stiva.top()=='/')
polo[j]=-1000000004;
j++;
stiva.pop();
}
}
void modifica(long double a[])
{
bool sem1=false, sem2=false;
for(int i=j-1; i>=1; i--)
{
if(a[i]==-1000000004)
{
if(sem2==true)
a[i]=-1000000003;
sem1=false;
sem2=true;
}
else if(a[i]==-1000000002)
{
if(sem1==true)
a[i]=-1000000001;
sem1=true;
sem2=false;
}
else
{
sem1=false;
sem2=false;
}
}
}
void rezolva(long double a[])
{
int p;
int lung=j;
for(int i=1; i<=(lung-1)/2; i++)
{
p=1;
while(a[p]!=-1000000001 && a[p]!=-1000000002 && a[p]!=-1000000003 && a[p]!=-1000000004)
p++;
if(a[p]==-1)
a[p-2]=a[p-2]+a[p-1];
if(a[p]==-2)
a[p-2]=a[p-2]-a[p-1];
if(a[p]==-3)
a[p-2]=a[p-2]*a[p-1];
if(a[p]==-4)
a[p-2]=a[p-2]/a[p-1];
a[p-1]=0; a[p]=0;
for(int k=p+1; k<=j-1; k++)
a[k-2]=a[k];
j-=2;
}
}
int main()
{
fin >> x;
poloneza(x);
modifica(polo);
rezolva(polo);
// fprintf(f, "%.5d", polo+1);
fout << polo[1];
return 0;
}