View Code of Problem 94

import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		String str = in.nextLine();
		ArrayList<String> al = new ArrayList<String>();						
		String[] s0 = str.split("");
		for(String a :s0) {
			if(a.equals(")")) {
				al.add(" ");
			}
			al.add(a);
			if(a.equals("(")) {
				al.add(" ");
			}
		}
		String s1 = "";
		for(String a:al) {
			s1+=a;
		}
		String[] s=s1.split(" ");
		System.out.print(eval(s));
	}
	public static int eval(String[] s) {
		int result=0;
		if(s[1].equals("+")) {
			result=0;
			for(int i =2;i<s.length;i++) {
				if(s[i].equals("(")){
					int loc=i;
					loc++;
					int left=1;
					int right=0;
					while(right!=left) {
						if(s[loc].equals("(")) {
							left++;
						}else if(s[loc].equals(")")) {
							right++;
						}
						loc++;
					}
					loc--;
					String[] newstr =new String[loc-i+1];
					for(int j =i;j<=loc;j++) {
						newstr[j-i]=s[j];
					}
					result+=eval(newstr);
					i=loc;
				}else if(s[i].equals(")")) {
					return result;
				}else {
					result+=Integer.parseInt(s[i]);
				}
			}	
		}else if(s[1].equals("-")) {
			int start=2;
			if(s[3].equals("(")) {
				int loc=3;
				int left=1;
				int right=0;
				while(right!=left) {
					if(s[loc].equals("(")) {
						left++;
					}else if(s[loc].equals(")")) {
						right++;
					}
					loc++;
				}
				loc--;
				String[] newstr =new String[loc-start+1];
				for(int j =start;j<=loc;j++) {
					newstr[j-start]=s[j];
				}
				result=eval(newstr);
				start=loc+1;
			}else {
				result=Integer.parseInt(s[2]);
				start=3;
			}
			for(int i =start;i<s.length;i++) {
				if(s[i].equals("(")){
					int loc=i;
					loc++;
					int left=1;
					int right=0;
					while(right!=left) {
						if(s[loc].equals("(")) {
							left++;
						}else if(s[loc].equals(")")) {
							right++;
						}
						loc++;
					}
					loc--;
					String[] newstr =new String[loc-i+1];
					for(int j =i;j<=loc;j++) {
						newstr[j-i]=s[j];
					}
					result-=eval(newstr);
					i=loc;
				}else if(s[i].equals(")")) {
					return result;
				}else {
					result-=Integer.parseInt(s[i]);
				}
			}
		}else if(s[1].equals("*")) {
			result=1;
			for(int i =2;i<s.length;i++) {
				if(s[i].equals("(")){
					int loc=i;
					loc++;
					int left=1;
					int right=0;
					while(right!=left) {
						if(s[loc].equals("(")) {
							left++;
						}else if(s[loc].equals(")")) {
							right++;
						}
						loc++;
					}
					loc--;
					String[] newstr =new String[loc-i+1];
					for(int j =i;j<=loc;j++) {
						newstr[j-i]=s[j];
					}
					result*=eval(newstr);
					i=loc;
				}else if(s[i].equals(")")) {
					return result;
				}else {
					result*=Integer.parseInt(s[i]);
				}
			}
		}else if(s[1].equals("/")) {
			int start=2;
			if(s[2].equals("(")) {
				int loc=3;
				int left=1;
				int right=0;
				while(right!=left) {
					if(s[loc].equals("(")) {
						left++;
					}else if(s[loc].equals(")")) {
						right++;
					}
					loc++;
				}
				loc--;
				String[] newstr =new String[loc-start+1];
				for(int j =start;j<=loc;j++) {
					newstr[j-start]=s[j];
				}
				result=eval(newstr);
				start=loc+1;
			}else {
				result=Integer.parseInt(s[2]);
				start=3;
			}
			for(int i =start;i<s.length;i++) {
				if(s[i].equals("(")){
					int loc=i;
					loc++;
					int left=1;
					int right=0;
					while(right!=left) {
						if(s[loc].equals("(")) {
							left++;
						}else if(s[loc].equals(")")) {
							right++;
						}
						loc++;
					}
					loc--;
					String[] newstr =new String[loc-i+1];
					for(int j =i;j<=loc;j++) {
						newstr[j-i]=s[j];
					}
					result/=eval(newstr);
					i=loc;
				}else if(s[i].equals(")")) {
					return result;
				}else {
					result/=Integer.parseInt(s[i]);
				}
			}
		}
		return result;
	}
}

Double click to view unformatted code.


Back to problem 94