Simple statement LISP addition, subtraction, multiplication, and division are resolved and evaluated as a result, the four kinds of mathematical symbol for the add, sub, the mul, div, respectively, for addition, subtraction, multiplication, and division of part Numbers are integers, integer division, zero divisor output error,
Example:
(add 3 5 7) results of 15
(sub 1 9) results for the eight
Results (9) the mul 0 0
(div 8. 3) the result of 2
8 0 (div) results for the error
(add (sub (div 8 (2) (9) the mul 1) 20) the results of 15
Public static void main (String [] args) throws IOException {
String s="(add (sub (div 8 (2) (9) the mul 1) 20)";
//for (a String s1: s.s plit (\ \ "(")) {
//System. Out. Println (s1);
//}
System. The out. Println (operation (s));
}
Public static String operation (String STR) {
If (STR) indexOf (" (", 1)==1) {
The split String []=STR. The split (" \ \ s + ");
String ope=split [0]. Replace (" (", "");
Int c1=Integer. ParseInt (split [1]. The replace (") ", ""));
Int c2=Integer. ParseInt (split [2]. The replace (") ", ""));
Int result=1;
If (" div "equals (ope) & amp; & C2==0) {
Return "error";
}
The switch (ope) {
Case "div" :
Result=c1/c2;
break;
A case of "add" :
Result=c1 + c2;
break;
Case "the mul" :
Result=c2 c1 *;
break;
A case of "sub" :
Result=c1 and c2;
break;
}
Return a String. The valueOf (result);
}
Int left=STR. LastIndexOf (" ("), right=STR. IndexOf (") ", left);
String substring=STR. The substring (left and right + 1);
The String result="" + operation (substring);
The StringBuilder sb=new StringBuilder (STR);
+ 1 sb. Replace (left, right, and the result. The replace (") ", ""));
The return operation (sb. ToString ());
}