Pagini recente » Cod sursa (job #96080) | Cod sursa (job #2835046) | Cod sursa (job #501525) | Cod sursa (job #819189) | Cod sursa (job #1648095)
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <cstdio>
using namespace std;
struct stiva
{
float v[100000];
float ordin[100000];
int varf=-1;
int vordin=0;
};
void pushordin(stiva &stk,int x)
{
stk.vordin++;
stk.ordin[stk.vordin]=x;
}
void push(stiva &stk, float x)
{
stk.varf++;
stk.v[stk.varf]=x;
}
void isEmpty()
{
cout<<"Stiva este goala!"<<endl;
}
void pop(stiva &stk)
{
stk.varf--;
}
void popordin(stiva &stk)
{
stk.vordin--;
}
void afiseaza(stiva &stk)
{
if(stk.varf>=0)
for(int i=0;i<=stk.varf;i++)
cout<<stk.v[i]<<" ";
}
void afiseazao(stiva &stk)
{
if(stk.vordin>=0)
for(int i=1;i<=stk.vordin;i++)
cout<<stk.ordin[i]<<" ";
}
int main()
{
freopen( "evaluare.in", "r", stdin);
freopen( "evaluare.out", "w", stdout);
char s[100];
stiva stk1,stk2;
int i=0, x=0,k=0,o=1;
long long int S=0;
cin>>s;
while(s[i]!='\0'){
switch (s[i])
{
case '+':
push(stk1,s[i]);
pushordin(stk1,1+k);
o++;
x=0;
break;
case '-':
push(stk1,s[i]);
pushordin(stk1,1+k);
o++;
x=0;
break;
case '*':
push(stk1,s[i]);
pushordin(stk1,2+k);
o++;
x=0;
break;
case '/':
push(stk1,s[i]);
pushordin(stk1,2+k);
o++;
x=0;
break;
case '(':
k=k+10;
o=0;
break;
case ')':
k=k-10;
break;
default:
x = x * 10 + int(s[i]-'0');
if(s[i+1]<'0'||s[i+1]>'9')
{
push(stk2,x);
}
break;
}
i++;
if((stk1.vordin>=2) && (stk1.ordin[stk1.vordin]>stk1.ordin[stk1.vordin-1])&&stk2.varf>stk1.varf&&o>1)
{
pop(stk2);
pop(stk2);
pop(stk1);
popordin(stk1);
if(stk1.v[stk1.varf+1]=='/'){
float rez = stk2.v[stk2.varf+1] /stk2.v[stk2.varf+2];
push(stk2,rez);}
if(stk1.v[stk1.varf+1]=='*'){
float rez = stk2.v[stk2.varf+1] *stk2.v[stk2.varf+2];
push(stk2,rez);}
if(stk1.v[stk1.varf+1]=='+'){
float rez = stk2.v[stk2.varf+1] +stk2.v[stk2.varf+2];
push(stk2,rez);}
if(stk1.v[stk1.varf+1]=='-'){
float rez = stk2.v[stk2.varf+1] -stk2.v[stk2.varf+2];
push(stk2,rez);}
}
}
S=0;int t=0;
if(stk1.v[0]=='*'||stk1.v[0]=='/'){
S=stk2.v[0];
t=1;}
for(i=0;i<=stk2.varf-1;i++){
if(stk1.v[i]=='+')
S=S+stk2.v[i+1];
else if(stk1.v[i]=='-')
S=S-stk2.v[i+1];
else if(stk1.v[i]=='*')
S=S*stk2.v[i+1];
else if(stk1.v[i]=='/')
S=S/stk2.v[i+1];
}
if(t==0)
cout<<S+stk2.v[0];
else
cout<<S;
return 0;
}