Mai intai trebuie sa te autentifici.
Cod sursa(job #2013000)
Utilizator | Data | 20 august 2017 01:17:25 | |
---|---|---|---|
Problema | Evaluarea unei expresii | Scor | 30 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 2.08 kb |
#include <cstdio>
#include <iostream>
#include <stack>
using namespace std;
char s[100005],notatia_postfixata[100005],*p=s,*n=notatia_postfixata;
int i,j=-1,v[100005];
int prioritate(char c)
{
switch(c)
{
case '+':
case '-':
return 3;
case '(':
return 1;
case ')':
return 2;
case '/':
case '*':
return 4;
}
}
stack <char> st;
int calculeaza()
{
while(*p!=NULL)
{
if(*p>='0'&&*p<='9')
{
while(*p>='0'&&*p<='9')
{
notatia_postfixata[++j]=*p++;
}
notatia_postfixata[++j]=',';
}
else
{
if(!st.empty())
{
if(*p=='(')
st.push(*p++);
else{
while(!st.empty()&&prioritate(st.top())>prioritate(*p))
notatia_postfixata[++j]=st.top(),
st.pop();
if(*p==')')
st.pop(),*p++;
else
st.push(*p++);
}
}
else
{
st.push(*p++);
}
}
}
while(!st.empty())
notatia_postfixata[++j]=st.top(),
st.pop();
return 0;
}
int rezolvare()
{
while(*n!=NULL)
{
if(*n>='0'&&*n<='9')
{
while(*n>='0'&&*n<='9')
v[i]=v[i]*10+*n-'0',*n++;
i++;
}
else
{
if(*n=='-')
v[i-2]=v[i-2]-v[i-1],i--,v[i]=0;
if(*n=='+')
v[i-2]=v[i-2]+v[i-1],i--,v[i]=0;
if(*n=='/')
v[i-2]=v[i-2]/v[i-1],i--,v[i]=0;
if(*n=='*')
v[i-2]=v[i-2]*v[i-1],i--,v[i]=0;
*n++;
}
}
return v[0];
}
int main()
{
freopen ("evaluare.in","r",stdin);
freopen ("evaluare.out","w",stdout);
scanf("%s",s);
calculeaza();
printf("\n%d",rezolvare());
return 0;
}