软件工程概论作业02
本程序为 小学生四则运算程序的升级版,因程序功能过多,网页版仍在改良种 ,先上java版试试水。
一.设计思想:延续上题,增加要求 是否有乘除法
是否有括号 (最多可以支持十个数参与计算)
数值范围
加减有无负数
乘除有无余数
本次程序 是否有括号未能按要求完成, 将加减乘除放入数组,控制随机数范围生成乘除,数值范围 。 int类型取整数使乘除
二。源程序代码:
1 import java.io.*; 2 import java.util.*; 3 import java.text.DecimalFormat; 4 import java.text.NumberFormat; 5 import java.util.regex.Matcher; 6 import java.util.regex.Pattern; 7 8 class ReportUtil { 9 10 /** 11 * 格式化数字 12 * @param obj 数字对象 13 * @param format 格式化字符串 14 * @return 15 */ 16 public static String formatNumber(Object obj, String format) { 17 if (obj == null) 18 return ""; 19 20 String s = String.valueOf(obj); 21 if (format == null || "".equals(format.trim())) { 22 format = "#.00"; 23 } 24 try { 25 if (obj instanceof Double || obj instanceof Float) { 26 if (format.contains("%")) { 27 NumberFormat numberFormat = NumberFormat.getPercentInstance(); 28 s = numberFormat.format(obj); 29 } else { 30 DecimalFormat decimalFormat = new DecimalFormat(format); 31 s = decimalFormat.format(obj); 32 } 33 } else { 34 NumberFormat numberFormat = NumberFormat.getInstance(); 35 s = numberFormat.format(obj); 36 } 37 } catch (Exception e) { 38 } 39 return s; 40 } 41 42 /** 43 * 计算字符串四则运算表达式 44 * @param string 45 * @return 46 */ 47 public static String computeString(String string) { 48 String regexCheck = "[\\(\\)\\d\\+\\-\\*/\\.]*";// 是否是合法的表达式 49 50 if (!Pattern.matches(regexCheck, string)) 51 return string; 52 53 Matcher matcher = null; 54 String temp = ""; 55 int index = -1; 56 String regex = "\\([\\d\\.\\+\\-\\*/]+\\)";// 提取括号表达式 57 string = string.replaceAll("\\s", "");// 去除空格 58 try { 59 Pattern pattern = Pattern.compile(regex); 60 // 循环计算所有括号里的表达式 61 while (pattern.matcher(string).find()) { 62 matcher = pattern.matcher(string); 63 while (matcher.find()) { 64 temp = matcher.group(); 65 index = string.indexOf(temp); 66 string = string.substring(0, index) 67 + computeStirngNoBracket(temp) 68 + string.substring(index + temp.length()); 69 } 70 } 71 // 最后计算总的表达式结果 72 string = computeStirngNoBracket(string); 73 } catch (NumberFormatException e) { 74 return e.getMessage(); 75 } 76 return string; 77 } 78 /** 79 * 计算不包含括号的表达式 80 * @param string 81 * @return 82 */ 83 private static String computeStirngNoBracket(String string) { 84 string = string.replaceAll("(^\\()|(\\)$)", ""); 85 String regexMultiAndDivision = "[\\d\\.]+(\\*|\\/)[\\d\\.]+"; 86 String regexAdditionAndSubtraction = "(^\\-)?[\\d\\.]+(\\+|\\-)[\\d\\.]+"; 87 88 String temp = ""; 89 int index = -1; 90 91 // 解析乘除法 92 Pattern pattern = Pattern.compile(regexMultiAndDivision); 93 Matcher matcher = null; 94 while (pattern.matcher(string).find()) { 95 matcher = pattern.matcher(string); 96 if (matcher.find()) { 97 temp = matcher.group(); 98 index = string.indexOf(temp); 99 string = string.substring(0, index) + doMultiAndDivision(temp) 100 + string.substring(index + temp.length()); 101 } 102 } 103 104 // 解析加减法 105 pattern = Pattern.compile(regexAdditionAndSubtraction); 106 while (pattern.matcher(string).find()) { 107 matcher = pattern.matcher(string); 108 if (matcher.find()) { 109 temp = matcher.group(); 110 index = string.indexOf(temp); 111 if (temp.startsWith("-")) { 112 string = string.substring(0, index) 113 + doNegativeOperation(temp) 114 + string.substring(index + temp.length()); 115 } else { 116 string = string.substring(0, index) 117 + doAdditionAndSubtraction(temp) 118 + string.substring(index + temp.length()); 119 } 120 } 121 } 122 123 return string; 124 } 125 126 /** 127 * 执行乘除法 128 * @param string 129 * @return 130 */ 131 private static String doMultiAndDivision(String string) { 132 String value = ""; 133 double d1 = 0; 134 double d2 = 0; 135 String[] temp = null; 136 if (string.contains("*")) { 137 temp = string.split("\\*"); 138 } else { 139 temp = string.split("/"); 140 } 141 142 if (temp.length < 2) 143 return string; 144 145 d1 = Double.valueOf(temp[0]); 146 d2 = Double.valueOf(temp[1]); 147 if (string.contains("*")) { 148 value = String.valueOf(d1 * d2); 149 } else { 150 value = String.valueOf(d1 / d2); 151 } 152 153 return value; 154 } 155 156 /** 157 * 执行加减法 158 * @param string 159 * @return 160 */ 161 private static String doAdditionAndSubtraction(String string) { 162 double d1 = 0; 163 double d2 = 0; 164 String[] temp = null; 165 String value = ""; 166 if (string.contains("+")) { 167 temp = string.split("\\+"); 168 } else { 169 temp = string.split("\\-"); 170 } 171 172 if (temp.length < 2) 173 return string; 174 175 d1 = Double.valueOf(temp[0]); 176 d2 = Double.valueOf(temp[1]); 177 if (string.contains("+")) { 178 value = String.valueOf(d1 + d2); 179 } else { 180 value = String.valueOf(d1 - d2); 181 } 182 183 return value; 184 } 185 186 /** 187 * 执行负数运算 188 * @param string 189 * @return 190 */ 191 private static String doNegativeOperation(String string) { 192 String temp = string.substring(1); 193 if (temp.contains(" ")) { 194 temp = temp.replace(" ", "-"); 195 } else { 196 temp = temp.replace("-", " "); 197 } 198 temp = doAdditionAndSubtraction(temp); 199 if (temp.startsWith("-")) { 200 temp = temp.substring(1); 201 } else { 202 temp = "-" +temp; 203 } 204 return temp; 205 } 206 207 } 208 public class yunsuan { 209 public static void main(String args[]){ 210 int o=1; 211 Scanner scanner=new Scanner(System.in); 212 System.out.println("请输入题目数量"); 213 int m=scanner.nextInt(); 214 System.out.println("请输入题目类型" 215 + " 1.整数" 216 + " 2.分数" 217 + " 3.混合数字" 218 + " 4.退出"); 219 int m1=scanner.nextInt(); 220 System.out.println("是否有乘除" 221 + " 1.无乘除" 222 + " 2.有乘除"); 223 int a2=scanner.nextInt(); 224 a2=a2*2;if(a2!=2&&a2!=4){a2=2;}//纠错 225 System.out.println("请输入数值范围(1~N)"); 226 int num =scanner.nextInt(); 227 System.out.println("请输入参与运算的数字数目(2~10)"); 228 int number =scanner.nextInt();if(number>10){number=10;}//限定纠错 229 while(o==1){ 230 for(int k=0;k<m;k++){ 231 if (m1==1){sheng(number,num,a2,k,m);o=2;} 232 else if(m1==2){fenshu(number,num,a2,k,m);o=2;} 233 else if(m1==3){ 234 235 int a=(int) (Math.random()*2+1); 236 if(a==2){sheng(number,num,a2,k,m);o=2;} 237 else {fenshu(number,num,a2,k,m);} o=2;} 238 else if(m1==4){k=m;o=2;System.out.println("退出成功");} 239 else {System.out.println("错误,请重新输入" 240 + " 1.整数" 241 + " 2.分数" 242 + " 3.混合数字" 243 + " 4.退出");} 244 } 245 m1=scanner.nextInt(); 246 } 247 } 248 249 250 public static void sheng(int number,int num,int a2,int i,int m){ 251 char [] a=new char[5];int k=2;String ss=""; 252 a[0]='+';a[1]='-';a[2]='*';a[3]='/'; 253 Random rand=new Random(); 254 int b=(int)rand.nextInt(num); 255 int c=(int)rand.nextInt(num); 256 int d=(int) (Math.random()*a2); 257 ss=b+""+a[d]+""+c; 258 while (k!=number){ 259 c=(int)rand.nextInt(num); 260 d=(int) (Math.random()*a2); 261 ss+=""+a[d]+""+c; 262 k++; 263 }System.out.println(ss+" ="); 264 ReportUtil am=new ReportUtil(); 265 ss = am.computeString(ss); 266 System.out.println(ss); 267 268 } 269 270 public static void fenshu (int number,int num,int a2,int i,int m){ 271 char [] a=new char[5]; int k=2;String ss=""; 272 273 a[0]='+';a[1]='-';a[2]='*';a[3]='/'; 274 Random rand=new Random(); 275 int b=(int)rand.nextInt(num); 276 int c=(int)rand.nextInt(num); 277 int e=(int)rand.nextInt(num); 278 int d=(int)rand.nextInt(num); 279 while (d==0||e==0){ 280 d=(int)rand.nextInt(num); 281 e=(int)rand.nextInt(num); 282 } 283 int f=(int) (Math.random()*a2); 284 ss=b+"/"+d+""+a[f]+""+c+"/"+e; 285 while (k!=number){ 286 f=(int) (Math.random()*a2); 287 c=(int)rand.nextInt(num); 288 while(e==0){ 289 e=(int)rand.nextInt(num);} 290 ss+=""+a[f]+""+c+"/"+e; 291 k++; 292 }System.out.println(ss+" ="); 293 ReportUtil am=new ReportUtil(); 294 ss = am.computeString(ss); 295 System.out.println(ss); 296 } 297 }
三.结果截图:
四.项目计划日志:
五.事件记录日志:
六.缺陷日志: