zoukankan      html  css  js  c++  java
  • 编写一个能自动生成小学四则运算题目的程序——软件工程课第一周作业

    主要功能:运算小学四则运算,可以选择加减乘除以及混合运算。

          除了整数以外,还要支持真分数的四则运算。 (例如:  1/6 + 1/8 = 7/24)

    思考:

        1.支持整数和分数,基本数据类型难以满足,考虑新建一个封装的类number,含有numerator和denominator,表示分子分母,整数的分母为1;

        2.随机生成题目,考虑调用库函数中的随机数生成数字和符号和题目长度;

        3.考虑到JAVA在处理字符串方面使用更为简便,本次程序采用JAVA语言编写。

    代码核心:

        number部分只显示了框架,对功能进行了封装,可能有些冗余写的比较长,所有代码在

        https://github.com/Victorianuonuo/projects-for-Software-Engineering

               上进行了开源,开源部分多包含了Question2.java 和 Test.java ,Question2.java 是开始写的不能处理真分数的一个生成程序的框架,Test.java是对numer类进行测试的代码,只保留了最后一次测试的代码。

    测试:

        在测试代码完善功能的过程中,对程序进行了微小的修改,主要是对随机数范围进行修改(因为感觉题太难了==),最后设置在25题,每道题长度(number个数)在【3,5】之间,数的取值范围不超过20,每道题答题后会立即显示正确答案和正确与否的判定,正确答案将以整数或分数形式显示,所有题做完后会显示正确率。答题时按回车被视为放弃回答,被判为错误,结果支持整数,分数和小数形式,小数形式误差不超过1e-5被视为正确答案,输入格式错误被判定为错误如“233..”,“2/2/e”,“2 / 3 3”等,数字之间不能用空格隔开,“22/”被视为等同于“22”。每次答题情况都会被追加在文件里,第一行会先记录答题开始的时刻,默认存储文件是"out.txt",也可以在命令行指定新的文件名进行存储。

     1 package calc;
     2 
     3 public class number {
     4     private int numerator,denominator;
     5     
     6     public number() {        
     7     }
     8     
     9     public number (String str) {    
    10     }
    11     
    12 
    13     public number(int a) {    
    14     }
    15     
    16     public number(int a, int b){  
    17     }
    18     
    19     public void set(int a) {    
    20     }
    21     
    22     public void set(int a,int b) {    
    23     }
    24 
    25     public String toString() {    
    26     }
    27     
    28     public void check(){    
    29     }
    30     
    31     
    32     public boolean equals(number n2) {    
    33     }
    34     
    35     public number yuefen() {    
    36     }
    37     
    38     
    39     public number add(number n2){  
    40     }
    41     
    42     private int gcd(int n1,int n2){    
    43     }
    44     
    45     public double ParseDouble(){    
    46     }
    47     
    48     public number sub(number n2){    
    49     }
    50     
    51     public number mul(number n2){
    52     }
    53     
    54     public number div(number n2) {    
    55     }
    56         
    57 }
    number.java
      1 package calc;
      2 
      3 /*支持分数和随机长度,并可以将每次答题情况记录到文件里 */
      4 
      5 import java.io.FileNotFoundException;
      6 import java.io.FileOutputStream;
      7 import java.io.PrintStream;
      8 import java.util.Date;
      9 import java.util.Scanner;
     10 
     11 public class Question {
     12 
     13     public static String str = "";//题目
     14     public static int num = 4;//每题中数的个数
     15     public static int num_i = 0;//题目中已有数的个数    
     16     public static int numberRange = 20;//运算中数的最大取值    
     17     public static number sum = new number();//结果
     18     
     19     public static void main(String[] args) {
     20         
     21         System.out.println();
     22         System.out.println("Please finish the following 25 questions!");
     23         System.out.println("Note: don't seperate your answer with space, ");
     24         System.out.println("or you will be considered wrong!");
     25         System.out.println();
     26         
     27         Scanner input=new Scanner(System.in);
     28         
     29         String file="out.txt";
     30         
     31         int right=0;
     32         PrintStream out = null;  
     33         
     34         
     35         if(args.length>=1)
     36             file=args[0];
     37         
     38         try {
     39             out=new PrintStream(new FileOutputStream(file));
     40         } catch (FileNotFoundException e1) {
     41             // TODO Auto-generated catch block
     42             e1.printStackTrace();
     43             input.close();
     44             return ;
     45         }
     46         
     47         out.println("
    
    "+new Date()+"
    
    ");
     48 
     49         for (int i = 0; i < 25; i++) {
     50             
     51             GetQuestion();
     52             System.out.print(i+1);
     53             System.out.print(". " + str +"
    Your answer:      ");
     54             String answer="";
     55             try{
     56                 answer=input.nextLine();
     57             }catch(Exception e){
     58             }
     59             System.out.print("true answer:  "+sum.toString()+"    ");
     60             number re=sum.add(new number(10));
     61             try{
     62                 re=new number(answer);
     63                 if(re.equals(sum))
     64                 {
     65                     System.out.println("You are right!");
     66                     right=right+1;
     67                 }else {
     68                     System.out.println("You are wrong!");
     69                 }
     70             }catch(Exception e){
     71                 if(answer.indexOf('.')!=-1)
     72                 {
     73                     try{
     74                         double ant=Double.parseDouble(answer);
     75                         if(Math.abs(ant-sum.ParseDouble())<1e-5)
     76                         {
     77                             System.out.println("You are right!");
     78                             right=right+1;
     79                         }else {
     80                             System.out.println("You are wrong!");
     81                         }
     82                     }catch(Exception ex){
     83                         System.out.println("You are wrong!");
     84                     }
     85                 }else{
     86                     System.out.println("You are wrong!");
     87                 }
     88             }        
     89             out.print(i+1);
     90             out.println(". " + str );
     91             out.println("Your answer:      "+answer);
     92             out.println("True answer:     "+sum.toString());
     93         }
     94         System.out.println(right+" / 25, So your accuracy is "+right/25.0);
     95         out.println(right+" / 25, So your accuracy is "+right/25.0);
     96         out.println();
     97         input.close();
     98         out.close();
     99         System.out.println();
    100     }
    101 
    102     private static void GetQuestion() {
    103     
    104         str = "";
    105         sum.set(0);;
    106         num_i = (int) (Math.random()*3)+3;
    107         quesGrow();
    108     }
    109 
    110     private static void quesGrow() {
    111         if( num_i > 1 ) {
    112             int j = num_i;
    113             num_i--;
    114             quesGrow();
    115             
    116             int ck=(int)(Math.random()*4);
    117             number w;
    118             if(ck!=0)
    119                 w=new number(1+(int)(Math.random()*numberRange));
    120             else w=new number(1+(int)(Math.random()*numberRange),1+(int)(Math.random()*numberRange));
    121             int t=(int)(Math.random()*2);
    122             int f=(int)(Math.random()*4);
    123             
    124             if(t == 0)
    125             {
    126                 if( f == 0 ) {
    127                     sum = sum.add(w);
    128                     str = str + "+" + w.toString();
    129                 }
    130                 if( f == 1 ) {
    131                     sum = sum.sub(w);
    132                     str = str + "-" +w.toString();    
    133                 }
    134                 if( f == 2 ) {
    135                     if( j < 3 ) {
    136                         sum = sum.mul(w);
    137                         str = str + "*" + w.toString();
    138                     }
    139                     else {
    140                         sum = sum.mul(w);
    141                         str = "(" +str+ ")" + "*" + w.toString();
    142                     }
    143                 }
    144                 if ( f == 3 ) {
    145                     
    146                     if( j < 3 ) {
    147                         sum = sum.div(w);
    148                         str = str + " / " + w.toString();
    149                     }
    150                     else {
    151                         sum = sum.div(w);
    152                         str = "(" +str+ ")" + " / " + w.toString();
    153                     }
    154                 }
    155             }
    156             else
    157             {
    158                 if( f == 0 ) {
    159                     sum = sum.add(w);
    160                     str = w.toString() + "+" + str;    
    161                 }
    162                 if( f == 1 ) {
    163                     if( j < 3 ) {
    164                         sum = w.sub(sum);
    165                         str = w.toString() + "-" + str;
    166                     }
    167                     else {
    168                         sum = w.sub(sum);
    169                         str = w.toString() + "-" + "(" +str+ ")";
    170                     }
    171                 }
    172                 if( f == 2 ) {
    173                     if( j < 3 ) {
    174                         sum = sum.mul(w);
    175                         str = w.toString()+ "*" + str;
    176                     }
    177                     else {
    178                         sum = sum.mul(w);
    179                         str = w.toString() + "*" + "(" +str+ ")";
    180                     }
    181                 }
    182                 if( f == 3) {
    183                     if( j < 3 ) {
    184                         sum = w.div(sum);
    185                         str = w.toString() + " / " + str;
    186                     }
    187                     else {
    188                         sum = w.div(sum);
    189                         str = w.toString()+ " / " + "(" +str+ ")";
    190                     }
    191                 }
    192             }
    193         }
    194         else if( num_i == 1 ) {
    195             int ck=(int)(Math.random()*4);
    196             number w;
    197             if(ck!=0)
    198                 w=new number(1+(int)(Math.random()*numberRange));
    199             else w=new number(1+(int)(Math.random()*numberRange),1+(int)(Math.random()*numberRange));
    200             sum = sum.add(w);
    201             str = str + w.toString();
    202         }
    203     }
    204 }
    Question.java
  • 相关阅读:
    活动安排
    中国剩余定理
    欧拉回路
    单词游戏
    Ant Trip
    John's Trip
    太鼓达人
    相框
    原始生物
    Blockade
  • 原文地址:https://www.cnblogs.com/victorianuonuo/p/5884846.html
Copyright © 2011-2022 走看看