Pagini recente » Cod sursa (job #2226837) | Cod sursa (job #326546) | Cod sursa (job #1998867) | Cod sursa (job #539738) | Cod sursa (job #2124770)
#include <stdio.h>
using namespace std;
char c[100005],stiva[33333];
int v[33333],vt=1,lenghtN,st=1;
void citire(){
scanf("%s",c);
}
int citireNumar(int i){
int n=0;
while(c[i]<=57&&c[i]>=48){
n=n*10+c[i++]-48;
}
v[++vt]=n;
return i;
}
int strlenght(char c[]){
int a;
for(a=0;c[a];)
a++;
return a;
}
char prioritete(char c){
switch (c){
case '(' :
return 1;
case '-' :
case '+' :
return 2;
case '*' :
case '/' :
return 3;
}
}
int calcul(int m,int n,char c){
switch(c){
case '*' :
return m*n;
case '/' :
return m/n;
case '-' :
return m-n;
case '+' :
return m+n;
default :
return 10;
}
}
int main()
{
freopen("evaluare.in","r",stdin);
freopen("evaluare.out","w",stdout);
citire();
lenghtN=strlenght(c);
///vt=v;
///st=stiva;
for(int i=0;i<=lenghtN-1;i++){
if(c[i]<=57&&c[i]>=48){
i=citireNumar(i)-1;
}else{
if(c[i]=='(')
{
stiva[++st]='(';
continue;
}
if(c[i]==')'){
while(stiva[st]!='('){
v[vt-1]=calcul(v[vt-1],v[vt],stiva[st--]);
--vt;
}
st--;
continue;
}
while(prioritete(c[i])<=prioritete(stiva[st])){
v[vt-1]=calcul(v[vt-1],v[vt],stiva[st--]);
--vt;
}
stiva[++st]=c[i];
}
}
while(st!=1){
v[vt-1]=calcul(v[vt-1],v[vt],stiva[st--]);
--vt;
}
printf("%d",v[vt]);
return 0;
}