/*表达树形式*/
#include<iostream>
#include<cstring>
using namespace std;
const int MAX=1000;
int lchild[MAX],rchild[MAX];
char ope[MAX];
int data[MAX];
int count=0;
bool isPureData(const string& str,int from,int to,int& data){
data=0;
for(int i=from;i<to&&str[i]<='9'&&str[i]>='0';++i){
data*=10;
data+=str[i]-'0';
}
return i==to;
}
int build_tree(const string& str,int from,int to){
int p=0,f1=-1,f2=-1;
int da;
if(isPureData(str,from,to,da)){
int n=count++;
lchild[n]=-1,rchild[n]=-1;
data[n]=da;
return n;
}
int t=from;
while(t<to){
switch(str[t]){
case '(':
--p;
break;
case ')':
++p;
break;
case '+':
case '-':
if(!p)f1=t;
break;
case '*':
case '/':
if(!p)f2=t;
break;
}
++t;
}
if(f1<0)f1=f2;
if(f1<0) return build_tree(str,from+1,to-1);//( )
int lch=build_tree(str,from,f1);
int rch=build_tree(str,f1+1,to);
int n=count++;
lchild[n]=lch;
rchild[n]=rch;
ope[n]=str[f1];
return n;
}
int val(int pos){
if(lchild[pos]==-1) return data[pos];
int a=val(lchild[pos]);
int b=val(rchild[pos]);
switch(ope[pos]){
case '+':
return a+b;
break;
case '-':
return a-b;
break;
case '*':
return a*b;
break;
case '/':
if(b==0)return -1;
return a/b;
break;
}
}
int calculator(const string& str,int l){
count=0;
build_tree(str,0,l);
return val(count-1);
}
int main(){
string str="(12-(2/(2/2)+2*4))/2-10";
cout<<calculator(str,str.length())<<endl;
return 0;
}