1importjava.util.*;23publicclassMain{4publicstaticvoidmain(String[] args)throwsException{56strConversion("(1 + 2) * 3");78}91011publicstaticvoidstrConversion(String str){12String ans ="";1314for(int i =0; i <= str.length()-1; i++){15charX= str.charAt(i);16if(Character.isDigit(X)){17//数値18 ans +=Integer.valueOf(X)-48;//UniCode 19System.out.println("数値:"+X);20}else{21//文字列22 ans +=String.valueOf(X);23System.out.println("文字:"+X);24}2526}27System.out.println(ans);28}29}30
1importjava.io.*;23classExpr{4char c;int pos =0, val =0;String str, o ="+-*/";56Expr(String s){ str = s;}78charget(){9while(pos < str.length())10if((c = str.charAt(pos++))!=' '){11if(!Character.isDigit(c))return c;12for(val = c -'0';;){13if(pos == str.length())return c ='0';14 c = str.charAt(pos++);15if(!Character.isDigit(c)){ pos--;return c ='0';}16 val = val *10+(c -'0');17}18}19return c =0;20}2122intexpr(int i){23int v;24if(i < o.length())25for(v =expr(i +2); c == o.charAt(i)|| c == o.charAt(i +1);)26if(c =='+') v +=expr(i +2);27elseif(c =='-') v -=expr(i +2);28elseif(c =='*') v *=expr(i +2);29else v /=expr(i +2);30else31if(get()=='0'){ v = val;get();}32elseif(c =='('){33 v =expr(0);34if(c ==')')get();35else c =2;36}37elseif(c =='+') v =expr(i);38elseif(c =='-') v =-expr(i);39else{ v =0; c =1;}40return v;41}4243publicstaticvoidmain(String[] args)throwsException{44BufferedReader br =newBufferedReader(newInputStreamReader(System.in));45for(;;){46System.out.print(">> ");47String s = br.readLine();48if(s.equals("."))break;49Expr e =newExpr(s);50int v = e.expr(0);51System.out.println(e.c ==0?" "+ v :" Error");52}53}54}
バグのご指摘歓迎します。
追記
get の中の return '0'; を return c = '0'; に修正しました。
追記2
逆ポーランド記法に変換してからそれを評価するようにしてみました。
Java
1importjava.util.*;23classRpn{4char c;int pos =0, len, val;String str, o ="+-*/";5StringBuilder sb =newStringBuilder();67Rpn(String s){ str = s; len = s.length();}89charget(){10while(pos < len){11 c = str.charAt(pos++);12if(c !=' '){13if(!Character.isDigit(c))return c;14for(val = c -'0';;){15if(pos == len)return c ='0';16 c = str.charAt(pos++);17if(!Character.isDigit(c)){ pos--;return c ='0';}18 val = val *10+(c -'0');19}20}21}22return c =0;23}2425voidput(Object o){ sb.append(' ').append(o);}2627voidexpr(int i){28if(i <4)29for(expr(i +2); c == o.charAt(i)|| c == o.charAt(i +1);){30char d = c;expr(i +2);put(d);31}32elseif(get()=='0'){put(val);get();}33elseif(c =='('){expr(0);if(c ==')')get();else c =2;}34elseif(c =='+')expr(i);35elseif(c =='-'){expr(i);put('_');}36else c =1;37}3839inteval(String rpn){40Scanner scn =newScanner(rpn);41Stack<Integer> s =newStack<>();42int v;43for(;;)44if(scn.hasNextInt())45 s.push(scn.nextInt());46elseif(scn.hasNext()){47String op = scn.next();48if(op.equals("+")) s.push(s.pop()+ s.pop());49elseif(op.equals("-")) s.push(-s.pop()+ s.pop());50elseif(op.equals("*")) s.push(s.pop()* s.pop());51elseif(op.equals("/")){ v = s.pop(); s.push(s.pop()/ v);}52elseif(op.equals("_")) s.push(-s.pop());53elsereturn0;54}55elsereturn s.pop();56}5758publicstaticvoidmain(String[] args)throwsException{59Scanner scn =newScanner(System.in);60for(;;){61System.out.print(">> ");62if(!scn.hasNextLine())break;63String s = scn.nextLine();64if(s.equals("."))break;65Rpn rpn =newRpn(s);66 rpn.expr(0);67if(rpn.c ==0){68 s = rpn.sb.toString();69System.out.println(s +" ==> "+ rpn.eval(s));70}71elseSystem.out.println(" Error");72}73}74}
new しているクラスは Scanner, Stack, StringBuilder だけです。
ラムダ式なんか使っていません。
理解できなくてもかまいませんが、どこが分からないかを教えていただけませんか?
追記3
計算を double で行うようにしてみました。
Java
1importjava.util.Scanner;2importjava.util.regex.*;34classExpr{5String str;int len, pos;double val;6char c, op[]={'+','-','*','/','^','^'};7Pattern p =Pattern.compile("\d+\.?\d*([eE][+-]?\d+)?");89Expr(String s){ str = s; len = s.length(); pos =0;}1011charget(){12while(pos < len)13if((c = str.charAt(pos++))!=' '){14if(!Character.isDigit(c))return c;15String s = str.substring(--pos);16Matcher m = p.matcher(s);17 m.find();18 s = m.group();19 val =Double.parseDouble(s);20 pos += s.length();21return c ='0';22}23return c =0;24}2526doubleexpr(int i){27double v;28if(i <6)29for(v =expr(i+2); c == op[i]|| c == op[i+1];)30if(c =='+') v +=expr(i+2);31elseif(c =='-') v -=expr(i+2);32elseif(c =='*') v *=expr(i+2);33elseif(c =='/') v /=expr(i+2);34else v =Math.pow(v,expr(i));35elseif(get()=='0'){ v = val;get();}36elseif(c =='('){ v =expr(0);if(c ==')')get();else c =2;}37elseif(c =='+') v =expr(i);38elseif(c =='-') v =-expr(i);39elseif(str.startsWith("sqrt(", pos-1)){ pos +=3; v =Math.sqrt(expr(i));}40else v = c =1;41return v;42}4344publicstaticvoidmain(String[] args){45for(Scanner scn =newScanner(System.in);;){46System.out.print(">> ");47if(!scn.hasNextLine())break;48String s = scn.nextLine();49if(s.equals("."))break;50Expr e =newExpr(s);51double v = e.expr(0);52System.out.println(e.c ==0?" "+ v :" Error");53}54}55}