四则运算
一. PSP表格
二. 项目要求:
- 能自动生成小学四则运算题目
- 除了整数以外,还要支持真分数的四则运算
三. 解题思路:
- 了解四则运算的基本法则
- 利用随机函数随机生成数字以及运算符
- 用户输入答案程序需要判断答案是否正确
- 支持真分数运算
四. 符号说明:
- 真分数:1/2, 1/3, 2/3, 1/4, 1’1/2, …。
- 运算符:+, −, ×, ÷。
- 括号:(, )。
- 等号:=。
- 分隔符:空格(用于四则运算符和等号前后)。
- 算术表达式: e = n | e1 + e2 | e1 − e2 | e1 × e2 | e1 ÷ e2 | (e),
其中e, e1和e2为表达式,n为自然数或真分数。
- 四则运算题目:e = ,其中e为算术表达式。
五. 项目功能
六. 代码设计
1 package Common; 2 3 public class Node { 4 private String value; 5 private Node leftChild; 6 private Node rightChild; 7 private boolean hasBrackets; 8 9 public boolean getHasBrackets() { 10 return hasBrackets; 11 } 12 13 public void setHasBrackets(boolean hasBrackets) { 14 this.hasBrackets = hasBrackets; 15 } 16 17 public String getValue() { 18 return value; 19 } 20 21 public void setValue(String value) { 22 this.value = value; 23 } 24 25 public Node getLeftChild() { 26 return leftChild; 27 } 28 29 public void setLeftChild(Node leftChild) { 30 this.leftChild = leftChild; 31 } 32 33 public Node getRightChild() { 34 return rightChild; 35 } 36 37 public void setRightChild(Node rightChild) { 38 this.rightChild = rightChild; 39 } 40 41 public Node(String value) { 42 this.value = value; 43 leftChild = null; 44 rightChild = null; 45 } 46 public Node(Node node) { 47 this.value = node.getValue(); 48 if(node.getLeftChild()!=null) 49 this.leftChild = new Node(node.getLeftChild()); 50 else 51 this.leftChild=null; 52 if(node.getRightChild()!=null) 53 this.rightChild = new Node(node.getRightChild()); 54 else 55 this.rightChild=null; 56 this.hasBrackets = node.getHasBrackets(); 57 } 58 59 public void display() { 60 System.out.print(this.value + "\t"); 61 } 62 63 @Override 64 public String toString() { 65 if(value.equals("+")||value.equals("-")||value.equals("*")||value.equals("➗")) 66 if(this.hasBrackets) 67 return "("+this.leftChild+this.value+this.rightChild+")"; 68 else 69 return this.leftChild+this.value+this.rightChild; 70 else 71 return value; 72 } 73 74 }
1 package Entity; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Random; 6 import Common.*; 7 8 public class ExpressionBuilder { 9 ExpressionBuilder(){ 10 11 } 12 13 //生成表达式组 14 public List<Node> CreateExpressionTreeList(int n,int size){ 15 int num=0; 16 List<Node> list = new ArrayList<Node>(); 17 for(int i=0;i<n;i++){ 18 Node node = CreateExpressionTree(size); 19 boolean flag = false; 20 for(Node no:list) { 21 if(CheckTheSame(RealySortToComputable(no),RealySortToComputable(node))) { 22 num++; 23 i--; 24 flag = true; 25 break; 26 } 27 } 28 if(!flag) 29 list.add(node); 30 } 31 System.out.println("重复次数: "+num); 32 return list; 33 } 34 35 //表达式二叉树生成 36 public Node CreateExpressionTree(int size) { 37 Node root=new Node(CreateOperator()); 38 Random random = new Random(); 39 int operatorNum = random.nextInt(3);//运算符数量 40 root = CreatNode(root,operatorNum,size); 41 root.setHasBrackets(false); 42 return root; 43 } 44 45 //查重 46 public boolean CheckTheSame(Node node1,Node node2) { 47 Node node11 = SortForCheck(SortForCheck(node1)); 48 Node node22 = SortForCheck(SortForCheck(node2)); 49 if(!node11.getValue().equals(node22.getValue())) { 50 return false; 51 } 52 if(node11.getHasBrackets()!=node22.getHasBrackets()) { 53 return false; 54 } 55 if(node11.getLeftChild()!=null&&node22.getLeftChild()!=null) 56 if(!CheckTheSame(node11.getLeftChild(),node22.getLeftChild())){ 57 return false; 58 } 59 if(node11.getLeftChild()==null&&node22.getLeftChild()!=null) 60 return false; 61 if(node11.getLeftChild()!=null&&node22.getLeftChild()==null) 62 return false; 63 if(node11.getRightChild()!=null&&node22.getRightChild()!=null) 64 if(!CheckTheSame(node11.getRightChild(),node22.getRightChild())){ 65 return false; 66 } 67 if(node11.getRightChild()==null&&node22.getRightChild()!=null) 68 return false; 69 if(node11.getRightChild()!=null&&node22.getRightChild()==null) 70 return false; 71 return true; 72 } 73 74 75 76 //查重时排序 77 public Node SortForCheck(Node no) { 78 Node node = new Node(no); 79 if(isOperator(node.getValue())){ 80 if(node.getValue().equals("+")||node.getValue().equals("*")) { 81 if(String1BiggerString2OrNot(SolveExpretionTree(node.getLeftChild()),SolveExpretionTree(node.getRightChild()))==-1){ 82 //大于 83 Node l = SortForCheck(node.getLeftChild()); 84 Node r = SortForCheck(node.getRightChild()); 85 node.setLeftChild(r); 86 node.setRightChild(l); 87 return node; 88 } 89 if(String1BiggerString2OrNot(SolveExpretionTree(node.getLeftChild()),SolveExpretionTree(node.getLeftChild()))==0) { 90 if(isOperator(node.getLeftChild().getValue())&&isOperator(node.getRightChild().getValue())) { 91 //左右子树都是运算符 92 if(ThisNodeOperatorValue(node.getLeftChild())==ThisNodeOperatorValue(node.getRightChild())) { 93 Node l = SortForCheck(node.getLeftChild()); 94 Node r = SortForCheck(node.getRightChild()); 95 if(String1BiggerString2OrNot(SolveExpretionTree(l.getLeftChild()),SolveExpretionTree(r.getLeftChild()))==-1) { 96 node.setLeftChild(r); 97 node.setRightChild(l); 98 return node; 99 } 100 }else if(ThisNodeOperatorValue(node.getLeftChild())<ThisNodeOperatorValue(node.getRightChild())) { 101 Node l = SortForCheck(node.getLeftChild()); 102 Node r = SortForCheck(node.getRightChild()); 103 node.setLeftChild(r); 104 node.setRightChild(l); 105 return node; 106 } 107 }else if(isOperator(node.getRightChild().getValue())) { 108 Node l = SortForCheck(node.getLeftChild()); 109 Node r = SortForCheck(node.getRightChild()); 110 node.setLeftChild(r); 111 node.setRightChild(l); 112 return node; 113 }else { 114 if(String1BiggerString2OrNot(SolveExpretionTree(node.getLeftChild()),SolveExpretionTree(node.getRightChild()))==-1) { 115 Node l = SortForCheck(node.getLeftChild()); 116 Node r = SortForCheck(node.getRightChild()); 117 node.setLeftChild(r); 118 node.setRightChild(l); 119 return node; 120 } 121 } 122 }else if(String1BiggerString2OrNot(SolveExpretionTree(node.getLeftChild()),SolveExpretionTree(node.getLeftChild()))==-1){ 123 Node l = SortForCheck(node.getLeftChild()); 124 Node r = SortForCheck(node.getRightChild()); 125 node.setLeftChild(r); 126 node.setRightChild(l); 127 return node; 128 } 129 } 130 node.setLeftChild(SortForCheck(node.getLeftChild())); 131 node.setRightChild(SortForCheck(node.getRightChild())); 132 return node; 133 }else { 134 return node; 135 } 136 } 137 138 139 //表达式二叉树生成核心函数 140 public Node CreatNode(Node no,int n,int size) { 141 Node node = new Node(no.getValue()); 142 node.setHasBrackets(no.getHasBrackets()); 143 Random random = new Random(); 144 int flag = random.nextInt(2); 145 Node left; 146 Node right; 147 if(flag==0) { 148 node.setHasBrackets(false); 149 } 150 else { 151 node.setHasBrackets(true); 152 } 153 if(n!=0) { 154 left = new Node(CreateOperator()); 155 left = CreatNode(left,n-1,size); 156 }else { 157 left = new Node(CreateNum(size)); 158 } 159 right=new Node(CreateNum(size)); 160 int flag2 = random.nextInt(2); 161 if(flag2==0) { 162 node.setValue(CreateOperator()); 163 node.setLeftChild(left); 164 node.setRightChild(right); 165 }else { 166 node.setLeftChild(right); 167 node.setRightChild(left); 168 } 169 return node; 170 } 171 172 173 //算二叉树表达式 174 public String SolveExpretionTree(Node no) { 175 no = RealySortToComputable(no); 176 if(isOperator(no.getValue())) { 177 if(no.getValue().equals("+")) { 178 // System.out.println(SolveExpretionTree(no.getLeftChild())+no.getValue()+SolveExpretionTree(no.getRightChild())); 179 return jiaFa(SolveExpretionTree(no.getLeftChild()),SolveExpretionTree(no.getRightChild())); 180 }else if(no.getValue().equals("-")){ 181 // System.out.println(SolveExpretionTree(no.getLeftChild())+no.getValue()+SolveExpretionTree(no.getRightChild())); 182 return jianFa(SolveExpretionTree(no.getLeftChild()),SolveExpretionTree(no.getRightChild())); 183 }else if(no.getValue().equals("*")) { 184 // System.out.println(SolveExpretionTree(no.getLeftChild())+no.getValue()+SolveExpretionTree(no.getRightChild())); 185 return chengFa(SolveExpretionTree(no.getLeftChild()),SolveExpretionTree(no.getRightChild())); 186 }else { 187 // System.out.println(SolveExpretionTree(no.getLeftChild())+no.getValue()+daoShu(SolveExpretionTree(no.getRightChild()))); 188 return chengFa(SolveExpretionTree(no.getLeftChild()),daoShu(SolveExpretionTree(no.getRightChild()))); 189 } 190 }else { 191 return no.getValue(); 192 } 193 } 194 public Node RealySortToComputable(Node no) { 195 return SortToComputable(SortToComputable(no)); 196 } 197 198 //假分数化简等 199 public String FinalAnswer(Node Expretion) { 200 String s = yueFen(SolveExpretionTree(Expretion)); 201 int[] a = decomposeFraction(s); 202 if(a[0]<0) { 203 if(-a[0]>a[1]) { 204 int z = (-a[0])/a[1]; 205 int yu = (-a[0])%a[1]; 206 if(yu!=0) 207 return "-"+z+"'"+yu+"/"+a[1]; 208 else 209 return "-"+z; 210 }else 211 return s; 212 }else if(a[0]==0) { 213 return "0"; 214 }else {//a[0]>0 215 if(a[0]>a[1]) { 216 int z = a[0]/a[1]; 217 int yu = a[0]%a[1]; 218 if(yu!=0) 219 return z+"'"+yu+"/"+a[1]; 220 else 221 return z+""; 222 }else 223 return s; 224 } 225 } 226 227 //二叉树排列成可直接计算二叉树 228 public Node SortToComputable(Node no) { 229 Node node= new Node(no); 230 if(isOperator(node.getValue())) {//该节点是否操作符 231 Node left = null; 232 Node right = null; 233 if(isOperator(node.getLeftChild().getValue())) {//左孩子节点是否为操作符 234 //System.out.println(" 左:"+node.getLeftChild().getValue()+" "+isOperator(node.getLeftChild().getValue())); 235 //左孩子节点为操作符 236 if(node.getLeftChild().getHasBrackets()) {//左孩子节点是否有括号 237 //有括号,则将该节点作为另一个根节点去调用排列(递归调用) 238 node.setLeftChild(SortToComputable(node.getLeftChild())); 239 }else {//没括号,则比较操作符级别 240 if(ThisNodeOperatorValue(node)>OperatorValue(node.getLeftChild())) { 241 //若该节点操作符优先级大于左孩子节点的操作符,则进行变换后处理左孩子节点,顶点节点为left 242 // System.out.println(" 左:"+ThisNodeOperatorValue(node)+">"+OperatorValue(node.getLeftChild())); 243 // System.out.println(" 左:"+node.getValue()+">"+node.getLeftChild().getValue()); 244 left = new Node(node.getLeftChild()); 245 node.setLeftChild(SortToComputable(node.getLeftChild().getRightChild())); 246 boolean s = left.getHasBrackets(); 247 left.setHasBrackets(node.getHasBrackets()); 248 node.setHasBrackets(s); 249 left.setRightChild(new Node(node)); 250 }else {//若优先级小于等于,则直接处理左孩子节点,顶点节点不变 251 node.setLeftChild(SortToComputable(node.getLeftChild())); 252 } 253 } 254 } 255 if(isOperator(node.getRightChild().getValue())) {//右孩子节点是否为操作符 256 //System.out.println(" 右:"+node.getRightChild().getValue()+" "+isOperator(node.getRightChild().getValue())); 257 //右孩子节点为操作符 258 if(node.getRightChild().getHasBrackets()) {//右孩子节点是否有括号 259 //有括号,则将该节点作为另一个根节点去调用排列(递归调用) 260 node.setRightChild(SortToComputable(node.getRightChild())); 261 }else {//没括号,则比较操作符级别 262 if(ThisNodeOperatorValue(node)>=OperatorValue(node.getRightChild())) { 263 // System.out.println(" 右:"+ThisNodeOperatorValue(node)+">"+OperatorValue(node.getRightChild())); 264 // System.out.println(" 右:"+node.getValue()+">"+node.getRightChild().getValue()); 265 //若该节点操作符优先级大于右孩子节点的操作符,则进行变换后处理左孩子节点 266 right = new Node(node.getRightChild()); 267 boolean s = right.getHasBrackets(); 268 right.setHasBrackets(node.getHasBrackets()); 269 node.setHasBrackets(s); 270 node.setRightChild(SortToComputable(node.getRightChild().getLeftChild())); 271 right.setLeftChild(new Node(node)); 272 }else {//若优先级小于等于,则直接处理右孩子节点,顶点节点不变 273 node.setRightChild(SortToComputable(node.getRightChild())); 274 } 275 } 276 } 277 if(left!=null&&right!=null) {//两边都有变化则left为顶点节点 278 left.setRightChild(right); 279 return left; 280 }else if(left!=null) {//只有左节点变换则left为顶点节点 281 return left; 282 }else if(right!=null) {//只有右节点变换则right为顶点节点 283 return right; 284 }else {//若都没有变化则仍为node为顶点节点 285 return node; 286 } 287 }else {//该节点为操作数,直接返回 288 return node; 289 } 290 } 291 292 293 294 //是否操作符 295 public boolean isOperator(String s) { 296 if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("➗")) { 297 return true; 298 }else { 299 return false; 300 } 301 } 302 303 public int OperatorValue(Node node) { 304 String s = node.getValue().trim(); 305 if(node.getHasBrackets())return 3;//有括号的则当作操作数 306 if(s.equals("+"))return 1; 307 if(s.equals("-"))return 1; 308 if(s.equals("*"))return 2; 309 if(s.equals("➗"))return 2; 310 return 3;//操作数优先级3 311 } 312 public int ThisNodeOperatorValue(Node node) { 313 String s = node.getValue().trim(); 314 if(s.equals("+"))return 1; 315 if(s.equals("-"))return 1; 316 if(s.equals("*"))return 2; 317 if(s.equals("➗"))return 2; 318 return 3;//操作数优先级3 319 } 320 321 322 //生成运算符 323 public String CreateOperator(){ 324 Random random = new Random(); 325 int t = random.nextInt(4); 326 switch(t){ 327 case 0:return "+"; 328 case 1:return "-"; 329 case 2:return "*"; 330 default:return "➗"; 331 } 332 } 333 334 //生成操作数 335 public String CreateNum(int size) { 336 Random random = new Random(); 337 int t = random.nextInt(3); 338 if(t<=1){ 339 Integer a = random.nextInt(size)+1; 340 return a.toString(); 341 }else{ 342 return CreateFraction(size); 343 } 344 } 345 346 //创建一个分数 347 public String CreateFraction(int size){ 348 Random random = new Random(); 349 int a = random.nextInt(size-1)+2; 350 int b = random.nextInt(a-1)+1; 351 int factor = CommonFactor(a,b); 352 if(factor>1){ 353 a/=factor; 354 b/=factor; 355 } 356 int c = random.nextInt(5); 357 if(c<3){ 358 return b+"/"+a; 359 }else{ 360 return (random.nextInt(size)+1)+"'"+b+"/"+a; 361 } 362 } 363 364 //求公因数 365 public int CommonFactor(int a,int b){ 366 while(b != 0){ 367 int temp = a % b; 368 a = b; 369 b = temp; 370 } 371 return a; 372 } 373 374 //计算减法 375 public String jianFa(String s1,String s2) { 376 int[] a = decomposeFraction(s1); 377 int[] b = decomposeFraction(s2); 378 int[] c = new int[2]; 379 c[0]=a[0]*b[1]-b[0]*a[1]; 380 c[1]=a[1]*b[1]; 381 int d; 382 if(c[0]<0) { 383 d=CommonFactor(-c[0],c[1]); 384 }else 385 d= CommonFactor(c[0],c[1]); 386 c[0]/=d; 387 c[1]/=d; 388 if(c[1]==1) { 389 //System.out.println(c[0]+""); 390 return c[0]+""; 391 }else{ 392 //System.out.println(c[0]+"/"+c[1]); 393 return c[0]+"/"+c[1]; 394 } 395 } 396 //计算加法 397 public String jiaFa(String s1,String s2) { 398 int[] a = decomposeFraction(s1); 399 int[] b = decomposeFraction(s2); 400 int[] c = new int[2]; 401 c[0]=a[0]*b[1]+b[0]*a[1]; 402 c[1]=a[1]*b[1]; 403 int d; 404 if(c[0]<0) { 405 d=CommonFactor(-c[0],c[1]); 406 }else 407 d= CommonFactor(c[0],c[1]); 408 c[0]/=d; 409 c[1]/=d; 410 if(c[1]==1) { 411 return c[0]+""; 412 }else{ 413 return c[0]+"/"+c[1]; 414 } 415 } 416 //计算乘法 417 public String chengFa(String s1,String s2) { 418 int[] a = decomposeFraction(s1); 419 int[] b = decomposeFraction(s2); 420 int[] c = new int[2]; 421 c[0]=a[0]*b[0]; 422 c[1]=a[1]*b[1]; 423 int d; 424 if(c[0]<0) { 425 d=CommonFactor(-c[0],c[1]); 426 }else 427 d= CommonFactor(c[0],c[1]); 428 c[0]/=d; 429 c[1]/=d; 430 if(c[1]==1) { 431 //System.out.println(c[0]+""); 432 return c[0]+""; 433 }else{ 434 //System.out.println(c[0]+"/"+c[1]); 435 return c[0]+"/"+c[1]; 436 } 437 } 438 //倒数 439 public String daoShu(String s) { 440 if(s.contains("/")) { 441 int[] a = decomposeFraction(s); 442 if(s.contains("-")) { 443 return (-a[1])+"/"+(a[0]); 444 }else 445 return a[1]+"/"+a[0]; 446 }else { 447 if(s.contains("-")) { 448 return "-1/"+s.substring(1); 449 }else 450 return "1/"+s; 451 } 452 } 453 //比较String的大小 454 public int String1BiggerString2OrNot(String s1,String s2) { 455 int[] a = decomposeFraction(s1); 456 int[] b = decomposeFraction(s2); 457 if(a[0]*b[1]>b[0]*a[1]) { 458 return 1; 459 }else if(a[0]*b[1]==b[0]*a[1]) { 460 return 0; 461 }else { 462 return -1; 463 } 464 } 465 //分解分数 466 public int[] decomposeFraction(String s) { 467 char[] fra = s.toCharArray(); 468 if(s.contains("'")) { 469 int m = s.indexOf("'"); 470 int n = s.indexOf("/"); 471 String a="",b="",c=""; 472 for(int i=0;i<m;i++) { 473 a+=fra[i]; 474 } 475 for(int i=m+1;i<n;i++) { 476 b+=fra[i]; 477 } 478 for(int i=n+1;i<s.length();i++) { 479 c+=fra[i]; 480 } 481 int[] result = new int[2]; 482 int a1=Integer.parseInt(a),b1=Integer.parseInt(b),c1=Integer.parseInt(c); 483 result[0]=a1*c1+b1; 484 result[1]=c1; 485 return result; 486 }else if(s.contains("/")) { 487 //System.out.println("需要转化的分数: "+s); 488 int n = s.indexOf("/"); 489 String a="",b=""; 490 if(s.contains("-")) { 491 for(int i=1;i<n;i++) { 492 a+=fra[i]; 493 } 494 }else { 495 for(int i=0;i<n;i++) { 496 a+=fra[i]; 497 } 498 } 499 for(int i=n+1;i<s.length();i++) { 500 b+=fra[i]; 501 } 502 int[] result = new int[2]; 503 //System.out.println("要转换的数a: "+a+" 要转换的数b: "+b); 504 int a1=Integer.parseInt(a),b1=Integer.parseInt(b); 505 if(s.contains("-")) { 506 result[0]=-a1; 507 }else { 508 result[0]=a1; 509 } 510 result[1]=b1; 511 return result; 512 }else { 513 int[] result = new int[2]; 514 //System.out.println("要转换的数: "+s); 515 if(s.contains("-")) { 516 result[0]=-Integer.parseInt(s.substring(1)); 517 }else{ 518 result[0]=Integer.parseInt(s); 519 } 520 result[1]=1; 521 return result; 522 } 523 } 524 //约分 525 public String yueFen(String s) { 526 int[] c = decomposeFraction(s); 527 int d; 528 if(c[0]<0) { 529 d=CommonFactor(-c[0],c[1]); 530 }else 531 d= CommonFactor(c[0],c[1]); 532 c[0]/=d; 533 c[1]/=d; 534 if(c[1]==1) { 535 return c[0]+""; 536 }else{ 537 return c[0]+"/"+c[1]; 538 } 539 } 540 541 542 }
1 package Entity; 2 import java.io.*; 3 4 public class FileWirter { 5 6 private static String filePath = "D:\\java_Practice\\FourOperation2\\"; 7 8 /** 9 * 创建文件 10 * 11 * @param fileName 12 * @return 13 */ 14 public static boolean createFile(File fileName) throws Exception { 15 try { 16 if (!fileName.exists()) { 17 fileName.createNewFile(); 18 } 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } 22 return true; 23 } 24 25 /** 26 * 读取TXT内容 27 * 28 * @param file 29 * @return 30 */ 31 public static String readTxtFile(File file) { 32 String result = ""; 33 try { 34 InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "gbk"); 35 BufferedReader br = new BufferedReader(reader); 36 String s = null; 37 while ((s = br.readLine()) != null) { 38 result = result + s; 39 System.out.println(s); 40 } 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 return result; 45 } 46 47 /** 48 * 写入TXT,覆盖原内容 49 * 50 * @param content 51 * @param fileName 52 * @return 53 * @throws Exception 54 */ 55 public static boolean writeTxtFile(String content, File fileName) throws Exception { 56 RandomAccessFile mm = null; 57 boolean flag = false; 58 FileOutputStream fileOutputStream = null; 59 try { 60 fileOutputStream = new FileOutputStream(fileName); 61 fileOutputStream.write(content.getBytes("gbk")); 62 fileOutputStream.close(); 63 flag = true; 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 return flag; 68 } 69 70 /** 71 * 写入TXT,追加写入 72 * 73 * @param filePath 74 * @param content 75 */ 76 public static void fileChaseFW(String filePath, String content) { 77 try { 78 // 构造函数中的第二个参数true表示以追加形式写文件 79 FileWriter fw = new FileWriter(filePath, true); 80 81 fw.write(content+"\r\n"); 82 fw.close(); 83 } catch (IOException e) { 84 System.out.println("文件写入失败!" + e); 85 } 86 } 87 88 public static void wirter(String args) throws Exception { 89 File file = new File(filePath+"Exercises.txt"); 90 createFile(file); 91 //readTxtFile(file); 92 // writeTxtFile("我是写入的内容11",file); 93 fileChaseFW(filePath+"Exercises.txt", args); 94 } 95 public static void wirter2(String args) throws Exception { 96 File file = new File(filePath+"Answers.txt"); 97 createFile(file); 98 //readTxtFile(file); 99 // writeTxtFile("我是写入的内容11",file); 100 fileChaseFW(filePath+"Answers.txt", args); 101 } 102 public static void wirter3(String args) throws Exception { 103 File file = new File(filePath+"Grade.txt"); 104 createFile(file); 105 //readTxtFile(file); 106 // writeTxtFile("我是写入的内容11",file); 107 fileChaseFW(filePath+"Grade.txt", args); 108 } 109 }
1 package Entity; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Scanner; 6 7 import Common.Node; 8 9 public class Test { 10 public static void main(String[] arg) throws Exception{ 11 ExpressionBuilder eb = new ExpressionBuilder(); 12 FileWirter fw = new FileWirter(); 13 Scanner sc = new Scanner(System.in); 14 System.out.println("请输入要生成的题目数量"); 15 int n = sc.nextInt(); 16 System.out.println("请输入题目中数值的最大值"); 17 int size = sc.nextInt(); 18 while(size>200) { 19 System.out.println("数值过大,请重新输入题目中数值的最大值"); 20 size = sc.nextInt(); 21 } 22 List<Node> list1 = eb.CreateExpressionTreeList(n, size); 23 int i=1; 24 List<Integer> correctAnswer = new ArrayList<Integer>(); 25 List<Integer> wrongAnswer = new ArrayList<Integer>(); 26 27 System.out.println("**********开始答题**********\n"); 28 for(Node s:list1){ 29 System.out.println("----------第"+i+"题----------"); 30 System.out.print(s + " = "); 31 String yourAnswer = sc.next(); 32 String answer = eb.FinalAnswer(s); 33 if(yourAnswer.equals(answer)) { 34 System.out.println("回答正确"); 35 correctAnswer.add(i); 36 }else { 37 System.out.println("回答错误"); 38 System.out.println("正确答案: "+answer); 39 wrongAnswer.add(i); 40 } 41 System.out.println("\n"); 42 fw.wirter("第"+i+"题: "+s); 43 fw.wirter2("第"+i+"题答案: "+answer); 44 i++; 45 } 46 //Correct: 5 (1, 3, 5, 7, 9) 47 //Wrong: 5 (2, 4, 6, 8, 10) 48 String CorrectS = "Correct:"+correctAnswer.size()+"("; 49 for(Integer s:correctAnswer) { 50 CorrectS+=(s+","); 51 } 52 CorrectS+=")"; 53 String WrongS = "Wrong:"+wrongAnswer.size()+"("; 54 for(Integer s:wrongAnswer) { 55 WrongS+=(s+","); 56 } 57 WrongS+=")"; 58 System.out.println(CorrectS); 59 System.out.println(WrongS); 60 fw.wirter3(CorrectS); 61 fw.wirter3(WrongS); 62 } 63 }