View Code of Problem 94

#include<iostream>
#include<string> 
using namespace std;

int lips(string b){
	static int i=0;
	int k=0,sum=0,a[100],j=0,temp=i+1;
	if(b[i]=='('){
		i+=2;
		k++;
	}
	while(k){
		while(isdigit(b[i])){
			a[j]=b[i]-'0';
			i++;j++;
		}
		if(b[i]=='('){
			a[j]=lips(b);
			j++;
		}
		if(b[i]==')') k--;
	}
	sum=a[0];
	if(b[temp]=='+'){
		for(int l=1;l<j;l++){
			sum+=a[l];
		} 
	}else if(b[temp]=='-'){
		for(int l=1;l<j;l++){
			sum-=a[l]; 
		}
	}else if(b[temp]=='*'){
		for(int l=1;l<j;l++){
			sum*=a[l];
		}
	}else if(b[temp]=='/'){
		for(int l=1;l<j;l++){
			sum/=a[l];
		}
	}
	i++;
	return sum;
}
int main(void){
	string s,b;
	getline(cin,s);
	for(int i=0;i<s.length();i++){
		if(s[i]!=' ')
			b+=s[i];
	}
	cout<<lips(b); 
}

Double click to view unformatted code.


Back to problem 94