zoukankan      html  css  js  c++  java
  • 个人作业1——四则运算题目生成程序(基于java)

    代码地址:https://git.coding.net/nibaijiejiea/test.git

    题目:

    从《构建之法》第一章的 “程序” 例子出发,像阿超那样,花二十分钟写一个能自动生成小学四则运算题目的命令行 “软件”,满足以下需求:
    
    1.除了整数以外,还要支持真分数的四则运算,真分数的运算,例如:1/6 + 1/8 = 7/24
    
    2.运算符为 +, −, ×, ÷
    
    3.并且要求能处理用户的输入,并判断对错,打分统计正确率。
    
    4.要求能处理用户输入的真分数, 如 1/2, 5/125.使用 -n 参数控制生成题目的个数,例如执行下面命令将生成10个题目
    Myapp.exe -n 10  

    需求分析:

    根据题目要求需要实现
    
    1.可以自动生成四则运算题目并且支持真分数的四则运算,真分数的运算
    
    2.用户可以填写答案
    
    3.程序可以判断对错并生成正确答案
    
    4.可以输出正确率

    程序设计与实现:

    java类有:

    1.Week

    2.number

    3.question

    Week类

    import java.util.Scanner;
    
    public class Week {
           
         public static void main (String args[]) {
             Scanner scan = new Scanner(System.in);
             question q = new question();                                                
             int r = Integer.parseInt(args[0]);                                                
             int n = Integer.parseInt(args[1]);                                            
             int l = Integer.parseInt(args[2]);                                               
             int count = 0;                                                             
          for (int i = 0; i < n; i ++ ) {
               q.initialize(r,l);                                                
               q.print(l);                                                                     
               if (scan.nextLine().equals (q.res))   { 
                   System.out.println("        !
    ");
                   count ++;
                   }
                     else{
                         System.out.println("        !           : " + q.res + "
    ");
                    }
                     
                }
          System.out.println("        " + count + "            :" + 100 * count / n + "!
    ");
          scan.close();
          }
    
    }

    number类

    public class number {
           int n;                                             
           int d;                                                          
    }

    question类

    import java.util.Stack;
    public class question {
    char opr[]={'+','-','*','/'};
    number oprd[]=new number[100];
    char op[]=new char[100];
    number result=new number();
    String res;
    private Object PRT;
    
       public void initialize(int r, int l){
           int i,j;
           for(i=0;i<1;i++){
               oprd[i]=new number();
               oprd[i].d=(int) (Math.random()*r)+1;
               oprd[i].n=(int) (Math.random()*r)+1;
               if(oprd[i].d>oprd[i].n){
                   oprd[i].d/=oprd[i].n;
                   oprd[i].n=1;
               }
               yf(oprd[i]);
           }
               
           for (i=0;i<l-1;i++){
               op[i]=opr[(int) (Math.random()*4)];
           }
           op[l-1]= '=';
           Stack<Character>operator=new Stack<Character>();
           Stack<number>operand=new Stack<number>();
           number x= new number(),y=new number(),z=new number();
           i=j=0;
           boolean ture = false;
        while (ture){
               operand.push(oprd[i++]);
               if(j==l-1)   break;
               while(!operator.isEmpty() && PRT(op[j]) <=PRT(operator.peek())){
                   y=operand.pop();
                   x=operand.pop();
                   z=solve(x,y,((Stack<Character>) operator).pop());
                   operand.push(z);
               }
               operator.push(op[j++]);
           }
           while (operand.size() !=1){
               y=operand.pop();
               x=operand.pop();
               z=solve(x,y,operator.pop());
               operand.push(z);
           }
           result=operand.pop();
           if(result.n==0) res=String.valueOf(0);
           else if(result.d == 1) res =String.valueOf(result.n );
           else res =String.valueOf(result.n) + '/' + String.valueOf(result.d);
       }
       public number solve(number x, number y, char op){
           number t1=new number(),t2= new number();
           int lcm=LCM(x.d,y.d);
           t1.n=x.n * lcm /x.d;
           t1.n=y.n * lcm /y.d;
           t1.d=t2.d = lcm;
           switch(op){
           case '+':
               t1.n += t2.n;
               break;
           case '-':
               t1.n -= t2.n;
               break;
           case '*':
               t1.n = x.n * y.n;
               t1.d = x.d * y.d;
               break;
           case '/':
               t1.n = x.n * y.d;
               t1.d = x.d * y.n;
               break;
           default:
               break;
           }
           yf(t1);
           return t1;
       }
       public int PRT(char c){
           if(c=='+' || c=='/') return 0;
           else if (c== '*' || c=='/') return 1;
           else return -1;
       }
       public void print(int l){
           String exp = new String();
           for (int i=0;i<1;i++){
               if(oprd[i].d ==1) exp+= String.valueOf(oprd[i].n);
               else exp +=String.valueOf(oprd[i].n) + '/' + String.valueOf(oprd[i].d);
               exp += " " + op[i] + " ";
           }
           System.out.println(exp);
       }
       public int GCD (int a,int b){
           int gcd=1;
           for (int i=1;i<=a;i++){
               if(a%i == 0 && b%i == 0){
                   gcd=i;
               }
           }
           return gcd;
       }
       public int LCM (int a, int b){
           return a * b /GCD(a,b);
       }
       public void yf (number p){
           int gcd=GCD (p.d,p.n);
           p.d /=gcd;
           p.n /=gcd;
       }
    }

    程序测试:

    psp

    PSP2.1 Personal Software Process Stages Time (%) Senior Student Time (%)
    Planning 计划 6 8
    · Estimate 估计这个任务需要多少时间 8 10
    Development 开发 88 125
    · Analysis 需求分析 (包括学习新技术) 6 10
    · Design Spec 生成设计文档 5 8
    · Design Review 设计复审 4 6
    · Coding Standard 代码规范 3 3
    · Design 具体设计 15 18
    · Coding 具体编码 30 35
    · Code Review 代码复审 7 9
    · Test 测试(自我测试,修改代码,提交修改) 12 21
    Reporting 报告 6 6
    · 测试报告 3 2
    · 计算工作量 2 1
    · 并提出过程改进计划 3 3

     小结

    程序不够完善,在试验过程中遇到很多问题,问了好多人,实现真分数四则运算比较困难,遇到的这些问题在解决中也进一步提高了自己的能力,今后还会多加练习,好好做每一次作业。

                                                                                                                                                                            

  • 相关阅读:
    macOS免费的NTFS读写软件
    Python模块和模块引用(一)
    Python Class (一)
    Ubuntu系统管理systemd
    Case Closed?
    The 'with' and 'as' Keywords
    Buffering Data
    rstrip
    堆排序
    堆 续9
  • 原文地址:https://www.cnblogs.com/baijiejie/p/6516733.html
Copyright © 2011-2022 走看看