作业5 四则运算 测试与封装 5.1 5.2
第二阶段目标 - 通过测试程序和API 接口测试其简单的加减乘除功能。并能看到代码覆盖率。
第三阶段目标 - 通过测试程序和API 接口测试对于各种参数的支持。并能看到代码覆盖率。
代码如下:
1 import java.math.BigDecimal; 2 import java.text.NumberFormat; 3 import java.util.*; 4 public class Core { 5 public int calc(String zifu,int max,int fanwei1,int fanwei2,int jingdu) throws ChushulingException, fanweiException, zifushuException 6 { 7 float a[]=new float [20]; 8 int b[]=new int [20]; 9 char c[]=new char [20]; 10 int j=0; 11 int shu; 12 float sum = 0; 13 String fu="+-/*"; 14 //System.out.println(zifu.length()); 15 for(int i=0;i<zifu.length();i++) 16 { 17 if(zifu.charAt(i)=='+') 18 { 19 b[j]=i; 20 c[j]='+'; 21 j++; //System.out.println("++++"); 22 } 23 else if(zifu.charAt(i)=='-') 24 { 25 b[j]=i; 26 c[j]='-'; 27 j++; // System.out.println("---"); 28 } 29 else if(zifu.charAt(i)=='*') 30 { 31 b[j]=i; 32 c[j]='*'; 33 j++; 34 } 35 else if(zifu.charAt(i)=='/') 36 { 37 b[j]=i; 38 c[j]='/'; 39 j++; 40 } 41 42 } 43 //System.out.println(j); 44 //System.out.println(zifu.substring(2,5)); 45 for(int i=0;i<j+1;i++) 46 { 47 if(i==0) a[i]=Integer.parseInt(zifu.substring(0,b[i])); 48 else if(i==j) a[i]=Integer.parseInt(zifu.substring(b[i-1]+1,zifu.length())); 49 else a[i]=Integer.parseInt(zifu.substring(b[i-1]+1,b[i])); 50 } 51 52 /*for(int i=0;i<j+1;i++) 53 { 54 System.out.print(a[i]+" "); 55 } 56 for(int i=0;i<j;i++) 57 { 58 System.out.println(c[i]); 59 }*/ 60 String yunsuanfu1=String.valueOf(c); 61 if(j-1>max) 62 { 63 throw new zifushuException("字符数大于"+max+""); 64 65 } 66 for( shu=0;shu<j+1;shu++) 67 {//System.out.println("111"); 68 if(a[shu]>fanwei2||a[shu]<fanwei1) 69 { 70 throw new fanweiException("有些数字不在范围内"); 71 72 } 73 } 74 //System.out.println(j+"!!!!!!!"); 75 StringBuffer yunsuanfu=new StringBuffer(yunsuanfu1); 76 //System.out.println(yunsuanfu.indexOf("*")); 77 //System.out.println(yunsuanfu); 78 //System.out.println(yunsuanfu.indexOf("*")); 79 while(yunsuanfu.indexOf("*")!=-1||yunsuanfu.indexOf("/")!=-1) 80 { 81 if(yunsuanfu.indexOf("/")==-1&&yunsuanfu.indexOf("*")!=-1) 82 { 83 sum=a[yunsuanfu.indexOf("*")]*a[yunsuanfu.indexOf("*")+1]; 84 a[yunsuanfu.indexOf("*")]=a[yunsuanfu.indexOf("*")]*a[yunsuanfu.indexOf("*")+1]; 85 for(int i=yunsuanfu.indexOf("*")+1;i<yunsuanfu.length()-1;i++) 86 { 87 a[i]=a[i+1]; 88 } 89 90 //System.out.println(sum); 91 yunsuanfu.delete(yunsuanfu.indexOf("*"), yunsuanfu.indexOf("*")+1); 92 } 93 94 else if(yunsuanfu.indexOf("*")<yunsuanfu.indexOf("/")&&yunsuanfu.indexOf("*")!=-1) 95 { 96 sum=a[yunsuanfu.indexOf("*")]*a[yunsuanfu.indexOf("*")+1]; 97 a[yunsuanfu.indexOf("*")]=a[yunsuanfu.indexOf("*")]*a[yunsuanfu.indexOf("*")+1]; 98 for(int i=yunsuanfu.indexOf("*")+1;i<yunsuanfu.length()-1;i++) 99 { 100 a[i]=a[i+1]; 101 } 102 //System.out.println(sum); 103 104 yunsuanfu.delete(yunsuanfu.indexOf("*"), yunsuanfu.indexOf("*")+1); 105 } 106 else if(yunsuanfu.indexOf("/")<yunsuanfu.indexOf("*")&&yunsuanfu.indexOf("/")!=-1) 107 { 108 if(a[yunsuanfu.indexOf("/")+1]==0) 109 { 110 throw new ChushulingException("除数不能为0!"); 111 } 112 sum=a[yunsuanfu.indexOf("/")]/a[yunsuanfu.indexOf("/")+1]; 113 a[yunsuanfu.indexOf("/")]=a[yunsuanfu.indexOf("/")]/a[yunsuanfu.indexOf("/")+1]; 114 for(int i=yunsuanfu.indexOf("/")+1;i<yunsuanfu.length()-1;i++) 115 { 116 a[i]=a[i+1]; 117 } 118 //System.out.println(sum); 119 120 yunsuanfu.delete(yunsuanfu.indexOf("/"), yunsuanfu.indexOf("/")+1); 121 } 122 else if(yunsuanfu.indexOf("*")==-1&&yunsuanfu.indexOf("/")!=-1) 123 { 124 if(a[yunsuanfu.indexOf("/")+1]==0) 125 { 126 throw new ChushulingException("除数不能为0!"); 127 } 128 sum=a[yunsuanfu.indexOf("/")]/a[yunsuanfu.indexOf("/")+1]; 129 a[yunsuanfu.indexOf("/")]=a[yunsuanfu.indexOf("/")]/a[yunsuanfu.indexOf("/")+1]; 130 for(int i=yunsuanfu.indexOf("/")+1;i<yunsuanfu.length()-1;i++) 131 { 132 a[i]=a[i+1]; 133 } 134 //System.out.println(sum); 135 136 yunsuanfu.delete(yunsuanfu.indexOf("/"), yunsuanfu.indexOf("/")+1); 137 } 138 } 139 while(yunsuanfu.indexOf("+")!=-1||yunsuanfu.indexOf("-")!=-1) 140 { 141 142 if(yunsuanfu.indexOf("+")!=-1&&yunsuanfu.indexOf("+")<yunsuanfu.indexOf("-")) 143 { 144 sum=a[yunsuanfu.indexOf("+")]+a[yunsuanfu.indexOf("+")+1]; 145 a[yunsuanfu.indexOf("+")]=a[yunsuanfu.indexOf("+")]+a[yunsuanfu.indexOf("+")+1]; 146 for(int i=yunsuanfu.indexOf("+")+1;i<yunsuanfu.length()-1;i++) 147 { 148 a[i]=a[i+1]; 149 } 150 yunsuanfu.delete(yunsuanfu.indexOf("+"), yunsuanfu.indexOf("+")+1); 151 // System.out.println(yunsuanfu); 152 //System.out.println(1); 153 } 154 else if(yunsuanfu.indexOf("-")!=-1) 155 { 156 sum=a[yunsuanfu.indexOf("-")]-a[yunsuanfu.indexOf("-")+1]; 157 a[yunsuanfu.indexOf("-")]=a[yunsuanfu.indexOf("-")]-a[yunsuanfu.indexOf("-")+1]; 158 for(int i=yunsuanfu.indexOf("-")+1;i<yunsuanfu.length()-1;i++) 159 { 160 a[i]=a[i+1]; 161 } 162 yunsuanfu.delete(yunsuanfu.indexOf("-"), yunsuanfu.indexOf("-")+1); 163 //System.out.println(yunsuanfu); 164 //System.out.println(2); 165 } 166 else{ 167 sum=a[yunsuanfu.indexOf("+")]+a[yunsuanfu.indexOf("+")+1]; 168 a[yunsuanfu.indexOf("+")]=a[yunsuanfu.indexOf("+")]+a[yunsuanfu.indexOf("+")+1]; 169 for(int i=yunsuanfu.indexOf("+")+1;i<yunsuanfu.length()-1;i++) 170 { 171 a[i]=a[i+1]; 172 } 173 yunsuanfu.delete(yunsuanfu.indexOf("+"), yunsuanfu.indexOf("+")+1); 174 //System.out.println(yunsuanfu); 175 //System.out.println(3); 176 } 177 //System.out.println(sum); 178 } 179 BigDecimal bd = new BigDecimal(sum); 180 bd = bd.setScale(jingdu,BigDecimal.ROUND_HALF_UP); 181 System.out.println(bd); 182 System.out.println(yunsuanfu); 183 /*for(int i=0;i<j+1;i++) 184 { 185 System.out.print(a[i]+" "); 186 }*/ 187 return 0; 188 189 } 190 191 192 }
1 import java.util.Scanner; 2 3 public class Calculator { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 Core c=new Core(); 10 try{ 11 c.calc("20-8/2-20*5+5",4,-1000,1000,4); 12 } 13 catch(ChushulingException e) 14 { 15 System.out.println(e.getMessage()); 16 e.printStackTrace(); 17 } 18 catch(fanweiException e) 19 { 20 System.out.println(e.getMessage()); 21 e.printStackTrace(); 22 } 23 catch(zifushuException e) 24 { 25 System.out.println(e.getMessage()); 26 e.printStackTrace(); 27 } 28 29 30 } 31 32 }
class ChushulingException extends Exception { public ChushulingException(String msg) { super(msg); } }
class fanweiException extends Exception { public fanweiException(String msg) { super(msg); } }
class zifushuException extends Exception {
public zifushuException(String msg)
{
super(msg);
}
}
![](http://images0.cnblogs.com/blog2015/734569/201505/081733292354204.jpg)
![](http://images0.cnblogs.com/blog2015/734569/201505/081734025325965.jpg)
![](http://images0.cnblogs.com/blog2015/734569/201505/081734552513626.jpg)
我的伙伴是罗凯旋 http://home.cnblogs.com/u/kasion/