四则运算(结对开发)
任务总结:
1、第一阶段目标:重构四则运算-- 能把计算的功能封装起来,通过API 接口调用计算方法。
定义一个计算核心类:把四则运算的计算功能包装在一个模块中 (这个模块可以是一个类 Class, 一个DLL等等)。
2 第二阶段目标 - 通过测试程序和API 接口测试其简单的加减乘除功能。并能看到代码覆盖率。
3第三阶段目标 – 定义异常处理。
如果输入是有错误的,例如 “1 ++ 2”, 在数值范围是 -1000 .. 1000 的时候,传进去 “10000 + 32768 * 3”, 或者是 “ 248.04 / 0” 怎么办? 怎么告诉函数的调用者 “你错了”? 把返回的字符串定义为 “-1” 来表示? 那么如果真的计算结果是 “-1” 又怎么处理呢?
程序代码:
import java.math.BigDecimal;import java.text.NumberFormat;import java.util.*;public class Core { public int calc(String zifu,int max,int fanwei1,int fanwei2,int jingdu) throws ZeroException, MoreException, CharNumException { float a[]=new float [20]; int b[]=new int [20]; char c[]=new char [20]; int j=0; int shu; float sum = 0; String fu="+-/*"; //System.out.println(zifu.length()); for(int i=0;i<zifu.length();i++) { if(zifu.charAt(i)=='+') { b[j]=i; c[j]='+'; j++; //System.out.println("++++"); } else if(zifu.charAt(i)=='-') { b[j]=i; c[j]='-'; j++; // System.out.println("---"); } else if(zifu.charAt(i)=='*') { b[j]=i; c[j]='*'; j++; } else if(zifu.charAt(i)=='/') { b[j]=i; c[j]='/'; j++; } } //System.out.println(j); //System.out.println(zifu.substring(2,5)); for(int i=0;i<j+1;i++) { if(i==0) a[i]=Integer.parseInt(zifu.substring(0,b[i])); else if(i==j) a[i]=Integer.parseInt(zifu.substring(b[i-1]+1,zifu.length())); else a[i]=Integer.parseInt(zifu.substring(b[i-1]+1,b[i])); } /*for(int i=0;i<j+1;i++) { System.out.print(a[i]+" "); } for(int i=0;i<j;i++) { System.out.println(c[i]); }*/ String yunsuanfu1=String.valueOf(c); if(j-1>max) { throw new CharNumException("字符数大于"+max+""); } for( shu=0;shu<j+1;shu++) {//System.out.println("111"); if(a[shu]>fanwei2||a[shu]<fanwei1) { throw new MoreException("有些数字不在范围内"); } } //System.out.println(j+"!!!!!!!"); StringBuffer yunsuanfu=new StringBuffer(yunsuanfu1); //System.out.println(yunsuanfu.indexOf("*")); //System.out.println(yunsuanfu); //System.out.println(yunsuanfu.indexOf("*")); while(yunsuanfu.indexOf("*")!=-1||yunsuanfu.indexOf("/")!=-1) { if(yunsuanfu.indexOf("/")==-1&&yunsuanfu.indexOf("*")!=-1) { sum=a[yunsuanfu.indexOf("*")]*a[yunsuanfu.indexOf("*")+1]; a[yunsuanfu.indexOf("*")]=a[yunsuanfu.indexOf("*")]*a[yunsuanfu.indexOf("*")+1]; for(int i=yunsuanfu.indexOf("*")+1;i<yunsuanfu.length()-1;i++) { a[i]=a[i+1]; } //System.out.println(sum); yunsuanfu.delete(yunsuanfu.indexOf("*"), yunsuanfu.indexOf("*")+1); } else if(yunsuanfu.indexOf("*")<yunsuanfu.indexOf("/")&&yunsuanfu.indexOf("*")!=-1) { sum=a[yunsuanfu.indexOf("*")]*a[yunsuanfu.indexOf("*")+1]; a[yunsuanfu.indexOf("*")]=a[yunsuanfu.indexOf("*")]*a[yunsuanfu.indexOf("*")+1]; for(int i=yunsuanfu.indexOf("*")+1;i<yunsuanfu.length()-1;i++) { a[i]=a[i+1]; } //System.out.println(sum); yunsuanfu.delete(yunsuanfu.indexOf("*"), yunsuanfu.indexOf("*")+1); } else if(yunsuanfu.indexOf("/")<yunsuanfu.indexOf("*")&&yunsuanfu.indexOf("/")!=-1) { if(a[yunsuanfu.indexOf("/")+1]==0) { throw new ZeroException("除数不能为0!"); } sum=a[yunsuanfu.indexOf("/")]/a[yunsuanfu.indexOf("/")+1]; a[yunsuanfu.indexOf("/")]=a[yunsuanfu.indexOf("/")]/a[yunsuanfu.indexOf("/")+1]; for(int i=yunsuanfu.indexOf("/")+1;i<yunsuanfu.length()-1;i++) { a[i]=a[i+1]; } //System.out.println(sum); yunsuanfu.delete(yunsuanfu.indexOf("/"), yunsuanfu.indexOf("/")+1); } else if(yunsuanfu.indexOf("*")==-1&&yunsuanfu.indexOf("/")!=-1) { if(a[yunsuanfu.indexOf("/")+1]==0) { throw new ZeroException("除数不能为0!"); } sum=a[yunsuanfu.indexOf("/")]/a[yunsuanfu.indexOf("/")+1]; a[yunsuanfu.indexOf("/")]=a[yunsuanfu.indexOf("/")]/a[yunsuanfu.indexOf("/")+1]; for(int i=yunsuanfu.indexOf("/")+1;i<yunsuanfu.length()-1;i++) { a[i]=a[i+1]; } //System.out.println(sum); yunsuanfu.delete(yunsuanfu.indexOf("/"), yunsuanfu.indexOf("/")+1); } } while(yunsuanfu.indexOf("+")!=-1||yunsuanfu.indexOf("-")!=-1) { if(yunsuanfu.indexOf("+")!=-1&&yunsuanfu.indexOf("+")<yunsuanfu.indexOf("-")) { sum=a[yunsuanfu.indexOf("+")]+a[yunsuanfu.indexOf("+")+1]; a[yunsuanfu.indexOf("+")]=a[yunsuanfu.indexOf("+")]+a[yunsuanfu.indexOf("+")+1]; for(int i=yunsuanfu.indexOf("+")+1;i<yunsuanfu.length()-1;i++) { a[i]=a[i+1]; } yunsuanfu.delete(yunsuanfu.indexOf("+"), yunsuanfu.indexOf("+")+1); // System.out.println(yunsuanfu); //System.out.println(1); } else if(yunsuanfu.indexOf("-")!=-1) { sum=a[yunsuanfu.indexOf("-")]-a[yunsuanfu.indexOf("-")+1]; a[yunsuanfu.indexOf("-")]=a[yunsuanfu.indexOf("-")]-a[yunsuanfu.indexOf("-")+1]; for(int i=yunsuanfu.indexOf("-")+1;i<yunsuanfu.length()-1;i++) { a[i]=a[i+1]; } yunsuanfu.delete(yunsuanfu.indexOf("-"), yunsuanfu.indexOf("-")+1); //System.out.println(yunsuanfu); //System.out.println(2); } else{ sum=a[yunsuanfu.indexOf("+")]+a[yunsuanfu.indexOf("+")+1]; a[yunsuanfu.indexOf("+")]=a[yunsuanfu.indexOf("+")]+a[yunsuanfu.indexOf("+")+1]; for(int i=yunsuanfu.indexOf("+")+1;i<yunsuanfu.length()-1;i++) { a[i]=a[i+1]; } yunsuanfu.delete(yunsuanfu.indexOf("+"), yunsuanfu.indexOf("+")+1); //System.out.println(yunsuanfu); //System.out.println(3); } //System.out.println(sum); } BigDecimal bd = new BigDecimal(sum); bd = bd.setScale(jingdu,BigDecimal.ROUND_HALF_UP); System.out.println(bd); System.out.println(yunsuanfu); /*for(int i=0;i<j+1;i++) { System.out.print(a[i]+" "); }*/ return 0; } }
import java.util.Scanner; public class Calculator { /** * @param args */ public static void main(String[] args) { Core c=new Core(); try{ c.calc("20-8/2-20*5+5",4,-1000,1000,4); } catch(ZeroException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch(MoreException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch(CharNumException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } } class CharNumException extends Exception { public CharNumException(String msg) { super(msg); } } class MoreException extends Exception { public MoreException(String msg) { super(msg); } } class ZeroException extends Exception { public ZeroException(String msg) { super(msg); } }
截图: