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

    题目描述:

                    能自动生成小学四则运算题目的命令行 “软件”,满足以下需求:

      1. 除了整数以外,还要支持真分数的四则运算,真分数的运算,例如:1/6 + 1/8 = 7/24
      2. 运算符为 +, −, ×, ÷
      3. 并且要求能处理用户的输入,并判断对错,打分统计正确率
      4. 要求能处理用户输入的真分数, 如 1/2, 5/12 等
      5. 使用 -n 参数控制生成题目的个数,例如执行下面命令将生成10个题目

    功能设计:

                   1、实现让使用者自行选择题目数量

                   2、可以选择两种模式,分别实现整数和真分数两种计算题型,题目自动生成

                   3、做题结束后,能够与正确答案核对,并计算和显示错题、正确答案和正确率

     

     

    个人软件过程:

     

    代码:

    码市地址:

    https://coding.net/u/zy97/p/Arithmetic/git/tree/master

    主要代码:

    分数计算:

    package arithmetic;
    
    public class Fraction {//分数计算
        public static int[] yuefen(int[] fraction) {            
            // TODO Auto-generated method stub
            int a = fraction[0];
            if (fraction[0]>fraction[1]) {
                a = fraction[1];
            }
            while (!justify(fraction)) {
                for (int i = 2; i < a; i++) {            
                    if (fraction[0]%i == 0 && fraction[1]%i == 0) {
                        fraction[0]/=i;
                        fraction[1]/=i;
                    }
                }
            }
            
            return fraction;
        }
        
           public static boolean justify(int[] fraction) {            
                  
                int a = fraction[0];
                if (fraction[0]>fraction[1]) {
                    a = fraction[1];
                }
                if (a<0    ) { 
                    a=-a;
                }
                for (int i = 2; i <= a; i++) {
                    if (fraction[0]%i == 0 && fraction[1]%i == 0) {
                        return false;        
                    }
                }
                return true;
            }
        public static int[] create() {            
            // TODO Auto-generated method stub
            int[] fraction = new int[2];
            fraction[0] = (int)(Math.random()*10);
            fraction[1] = (int)(Math.random()*20)+1;
            return fraction;
        }
        
        
        static int[] plus(int[] x, int[] y) {
            int n = x[1]*y[1];
            int m = x[0]*y[1] + x[1]*y[0];
            x[0] = m;x[1] = n;
            while (!justify(x)) {
                yuefen(x);
            }
            return x;
            
            
        }//加法运算
        static int[] subtract(int[] x,int[] y) {
            int n = x[1]*y[1];
            int m = x[0]*y[1] - x[1]*y[0];
            x[0] = m;x[1] = n;
            while (!justify(x)) {
                yuefen(x);
            }
            return x;
         
        }//减法运算
        static int[] multiply(int[] x,int[] y) {
            int m = x[0]*y[0];
            int n = x[1]*y[1];
            x[0] = m;x[1] = n;
            while (!justify(x)) {
                yuefen(x);
            }
            return x;
            
        }//乘法运算
        static int[] divide(int[] x,int[] y) {
            int a = y[0];
            y[0] = y[1];
            y[1] = a;
            x=multiply(x, y);
            while (!justify(x)) {
                yuefen(x);
            }
            return x;
           
        }//除法运算
        
        
        
     
    }

    普通数值计算:

    package arithmetic;
    
    public class Operate {
        static int sum(int x, int y) {
            return x+y;
            // TODO Auto-generated method stub
            
        }//加法运算
        static int sub(int x,int y) {
            return x-y;
            // TODO Auto-generated method stub
        }//减法运算
        static int mul(int x,int y) {
            return x*y;
            // TODO Auto-generated method stub
        }//乘法运算
        static int[] div(int[] x,int[] y) {
            return Fraction.divide(x, y);
            // TODO Auto-generated method stub
        }//除法运算
    }

    测试结果:

    选择分数计算题

    选择普通计算题:

    实验小结:

        这次是第一次软件工程课的实验,虽然题目不是太难,但是由于自己编程能力较为薄弱,所以在实践过程中还是遇到很多困难。特别是在刚开始编程思路不明确,导致走了很多弯路,在实际编程中也浪费了很多时间在一些简单的问题上。第一次采用博客的形式上交博客和代码也是十分生疏,以前的实验报告格式统一,写完程序截图即可,但现在改成以博客的形式上交,还需要考虑排版等诸多细节问题。

  • 相关阅读:
    Java版本及历史简述
    ASCII、Unicode、UTF-8、UTF-16、GBK、GB2312、ANSI等编码方式简析
    同步(Synchronous)和异步(Asynchronous)方法的区别
    例10-12 *uva1637(概率dp)
    例10-11 uva11181
    例10-10 uva10491(简单概率)
    例10-9 uva1636简单概率问题
    全排列hash-康拓展开
    10-8 uva1262密码
    例10-6 uva1635(唯一分解定理)
  • 原文地址:https://www.cnblogs.com/zy97/p/6514288.html
Copyright © 2011-2022 走看看