Pagini recente » Borderou de evaluare (job #2171179) | Borderou de evaluare (job #977961) | Borderou de evaluare (job #1568961) | Borderou de evaluare (job #1859816) | Cod sursa (job #2501709)
#include <iostream>
#include <fstream>
#include <stack>
#include <queue>
using namespace std;
stack <char> ch;
queue <int> st;
int pr[300],hashh[300],f[10],v[100005];
int main()
{
ifstream cin("evaluare.in");
ofstream cout("evaluare.out");
string s;
cin>>s;
int n=s.size();
pr['+']=1;
pr['-']=1;
pr['/']=2;
pr['*']=2;
hashh['+']=1000000001;
hashh['-']=1000000002;
hashh['/']=1000000003;
hashh['*']=1000000004;
hashh['(']=1000000006;
hashh[')']=1000000007;
f[1]='+';
f[2]='-';
f[3]='/';
f[4]='*';
f[6]='(';
f[7]=')';
///return 0;
int i;
//cout<<n<<"\n";
//cout<<s<<"\n\n";
for(i=0;i<n;i++){
bool ok=0;
int nr=0;
//cout<<s<<"\n";
while(s[i]<='9' and s[i]>='0' and i<n){
nr=nr*10+(s[i]-'0');
//cout<<i<<" "<<nr<<" "<<s[i]-'0'<<"\n";
ok=1;
i++;
}
//cout<<nr;
if(ok==1){
i--;
}
//cout<<"\n";
//cout<<"i:"<<i<<"\n";
if(ok==1){
//cout<<nr<<"\n";
st.push(nr);
}
else{
//cout<<s[i]<<"\n";
//cout<<"-=\n";
if(s[i]=='('){
ch.push(s[i]);
}
else{
if(s[i]==')'){
//cout<<"2\n";
while(ch.top()!='('){
st.push(hashh[ch.top()]);
ch.pop();
}
ch.pop();
}
else{
//cout<<"3\n";
if(ch.empty()==false)
while(pr[ch.top()]>=pr[s[i]] and ch.top()!='('){
st.push(hashh[ch.top()]);
ch.pop();
if(ch.empty()==true)
break;
}
ch.push(s[i]);
}
}
}
}
///
int a;
while(ch.empty()==false){
st.push(hashh[ch.top()]);
ch.pop();
}
int cnt=0;
while(st.empty()==false){
a=st.front();
st.pop();
if(a>=1000000000){
//cout<<char(f[a-1000000000])<<" ";
if(f[a-1000000000]=='+'){
cnt--;
v[cnt]=v[cnt]+v[cnt+1];
}
if(f[a-1000000000]=='-'){
cnt--;
v[cnt]=v[cnt]-v[cnt+1];
}
if(f[a-1000000000]=='*'){
cnt--;
v[cnt]=v[cnt]*v[cnt+1];
}
if(f[a-1000000000]=='/'){
cnt--;
v[cnt]=v[cnt]/v[cnt+1];
}
}
else{
//cout<<a<<" ";
v[++cnt]=a;
}
}
//cout<<"\n\n";
cout<<v[1];
return 0;
}