View Code of Problem 94

#include<bits/stdc++.h>
using namespace std;
string s;
int len,f,i;
int cal(int p){
	char c=s[p++];
	queue<int>q;
	for(i=p;i<len;++i){
		if(isdigit(s[i])){
			int x=0;
			while(isdigit(s[i])){
				x=x*10+s[i]-'0';
				i++;
			}
			i--;
			q.push(x);
		}
		else if(s[i]=='(')
		q.push(cal(i+1));
		else if(s[i]==')'){
			i++;
			break;
		}
	}
	int val=0;
	if(c=='+')
		while(!q.empty()){
			val+=q.front();
			q.pop();
		}
	else if(c=='-'){
		val=q.front();
		q.pop();
		while(!q.empty()){
			val-=q.front();
			q.pop();
		}
	}
	else if(c=='*'){
		val=1;
		while(!q.empty()){
			val*=q.front();
			q.pop();
		}
	}
	else if(c=='/'){
		val=q.front();
		q.pop();
		while(!q.empty()){
			val/=q.front();
			q.pop();
		}
	}
	return val;
}
int main(){
	getline(cin,s);
	len=s.size();
	cout<<cal(1);
}

Double click to view unformatted code.


Back to problem 94