• 软件工程个人作业—四则运算2


    1、程序设计思想:

          1)首先定义生成的计算式的总数,确定计算的不同方式,选择计算的数值范围;

          2)定义不同的类,编写对应的代码进行整数运算、真分数运算(分数的约分化简)、乘法运算、括号运算、有负数的加减法运算、有余数的除法运算;

          3)判断所生成的计算式中是否存在重复的题目,如果存在则返回false,不然返回true;

    2、程序源代码:

    package demo;
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class lianxi2 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int N,M;
            Scanner scan = new Scanner(System.in);
            System.out.print("请输入总数:");
            N = scan.nextInt();
            String[] jss = new String[N];
            System.out.println("1、整数计算式,2、真分数计算式,3、乘除法运算,4、括号运算(最多可以支持十个数参与运算),5、有负数的加减运算,6、有余数的除法运算");
            System.out.print("请选择运算的方式:");
            int p = scan.nextInt();
            if(p == 1) {
                jss = createZhengShuJiSuanShi(N);
            }
            else if(p==2){
                jss = createZhenFenShuJiSuanShi(N);
            }
            else if(p==3){
                jss = createChengChuFaYunSuan(N);
            }
        /*else if(p==4){
                jss = createKuoHaoYunSuan(N);
            } */
            else if(p==5){
                jss = createFuShuYunSuan(N);
            }
            else if(p==6){
                jss=createYuShuYunSuan(N);
            } 
            scan.close();
            
            //显示结果
            for(int i = 0;i < N;i++) {
                System.out.println((i + 1) + " : " + jss[i]);
            }
        }
        
        //生成真分数计算式
        public static String[] createZhenFenShuJiSuanShi(int n) {
            int x,y,z,a,b,i = 0;
            int M;
            Scanner scan=new Scanner(System.in);
            System.out.print("请输入运算的数值范围:");
            M=scan.nextInt();
            String[] jss = new String[n];
            String t = "";
            Random rd = new Random();
            
            while(i < n) {
                a = rd.nextInt(M);
                b = rd.nextInt(M);
                
                if(a != 0 && b != 0){
                    x = rd.nextInt(a);
                    y = rd.nextInt(b);
                    z = rd.nextInt(4);
                    t = "";
                    if(y != 0){
                        if(z == 0) {
                            if((x * b + y * a) < (a * b))
                                t = "" + yueJian(x, a) + " + " + yueJian(y, b) + "=" + yueJian((x * b + y * a), (a * b));
                        
                        }else if(z == 1) {
                            if((x * b - y * a) >= 0 && (x * b - y * a) < (a * b))
                                t = "" + yueJian(x, a) + " - " + yueJian(y, b) + "=" + yueJian((x * b - y * a), (a * b));
                        
                        }else if(z == 2) {
                            if((x * y) < (a * b))
                                t = "" + yueJian(x, a) + " * " + yueJian(y, b) + "=" + yueJian((x * y),(a * b));
                        
                        }else{
                            if((y / b) != 0){
                                if((x * b) < (a * y))
                                    t = "" + yueJian(x, a) + " / " + yueJian(y, b) + "=" + yueJian((x * b),(a * y));
                            }
                        }
                        if((!t.equals("")) && buChong(jss,t,i)){
                            jss[i++] = t;
                        }
                    }
                }
            }
            return jss;
        }
        
        //生成整数计算式
        public static String[] createZhengShuJiSuanShi(int n) {
            int x,y,z,i = 0;
            int M;
            Scanner scan=new Scanner(System.in);
            System.out.print("请输入运算的数值范围:");
            M=scan.nextInt();
            String[] jss = new String[n];
            String t = "";
            Random rd = new Random();
            while(i < n) {
                x = rd.nextInt(M);
                y = rd.nextInt(M);
                z = rd.nextInt(4);
                
                    if(z == 0) {
                        t = "" + x + " + " + y  + "="+(x+y);
                    }else if(z == 1 && x >= y) {
                        t = "" + x + " - " + y  + "=" +(x-y) ;
                    }else if(z == 2) {
                        t = "" + x + " * " + y  + "=" + (x*y);
                    }else{
                        if(y != 0){
                            if(x % y == 0){
                                t = "" + x + " / " + y  + "=" + (x/y);
                                }
                        }
                    }
                    
                    if(buChong(jss,t,i)){
                        jss[i++] = t;
                    }
                
            }
            return jss;
        }
        
        //约简分子分母,如果分子为0则返回0
        public static String yueJian(int a,int b){
            
            int y = 1;
            for(int i = a;i >= 1;i--) {
                if(a % i == 0 && b % i == 0){
                    y = i;
                    break;
                }
            }
            int z = a / y;
            int m = b / y;
            if(z == 0) {
                return "0";
            }
            return "" + z + "/" + m;
        }
        //乘除法运算
        public static String[] createChengChuFaYunSuan(int n) {
            int x,y,z,i = 0;
            int M;
            Scanner scan=new Scanner(System.in);
            System.out.print("请输入运算的数值范围:");
            M=scan.nextInt();
            String[] jss = new String[n];
            String t = "";
            Random rd = new Random();
            while(i < n) {
                x = rd.nextInt(M);
                y = rd.nextInt(M);
                z = rd.nextInt(2);
                
                    if(z == 0) {
                        t = "" + x + " * " + y  + "=" + (x*y);
                    }else{
                        if(y != 0){
                            if(x % y == 0){
                                t = "" + x + " / " + y  + "=" + (x / y);
                                }
                        }
                    }
                    if(buChong(jss,t,i)){
                        jss[i++] = t;
                    }
                
            }
            return jss;
        }
        //括号运算
        /*public static String[] createKuoHaoYunSuan(int n) {
    
        } */
        //负数运算
        public static String[] createFuShuYunSuan(int n) {
            int x,y,z,i = 0;
            int M;
            Scanner scan=new Scanner(System.in);
            System.out.print("请输入运算的数值范围:");
            M=scan.nextInt();
            String[] jss = new String[n];
            String t = "";
            Random rd = new Random();
            while(i < n) {
                x = -rd.nextInt(M);
                y = -rd.nextInt(M);
                z = rd.nextInt(1);
                if(z == 0) {
                    t = "(" + x +")"+ " + " +"("+ y+")"+"=" + (x + y);
                    
                }else{
                    t = "(" + x+")" + " - " +"("+ y+")"  + "=" + (x - y);
            }
                if(buChong(jss,t,i)){
                    jss[i++] = t;
                }
        }
            return jss;
    }
        //余数运算
        public static String[] createYuShuYunSuan(int n) {
            int x,y,z,i = 0;
            int M;
            Scanner scan=new Scanner(System.in);
            System.out.print("请输入运算的数值范围:");
            M=scan.nextInt();
            String[] jss = new String[n];
            String t = "";
            Random rd = new Random();
            while(i < n) {
                x = rd.nextInt(M);
                y = rd.nextInt(M);
                if(y != 0){
                    if(x % y == 0){
                        t = "" + x + " / " + y  + "=" + (x / y);}
                    else{
                        int a,b;
                        a=x%y;
                        b=(x-a)/y;
                        t=""+x+"/"+y+"="+b+"……"+a;
                    }
                }
                if(buChong(jss,t,i)){
                    jss[i++] = t;
                }
            }
            return jss;
        }  
        //检测是否重复(检测j是否与jss数组的前n个重复)重复返回FALSE 不重复返回TRUE
        public static boolean buChong(String[] jss,String j,int n) {
            for(int i = 0;i < n;i++) {
                if(jss[i].equals(j)){
                    return false;
                }
            }
            return true;
        }
    }

    3、运行结果截图:

    4、总结:

          在这次的实验中,也更加熟悉了程序的编码设计,对于程序进行了不少的完善,不过略有遗憾的是没有实现四则运算的括号运算,不清楚怎样产生随机的括号,之后自己会去学习,然后对于程序进行进一步的完善。

  • 相关阅读:
    Apache 阿帕奇 配置运行环境
    2019年6月多校联训b层——搜索算法 Problem A 宽搜 营救
    西安集训B Day1 test 问题 C: 小明的城堡
    西安集训B层Day1 test 问题 A: 旅行日记
    二分答案—洛谷P1182 数列分段`Section II`
    2019.5.25 Noip模拟测试2 T2题解
    2019.5.25 Noip模拟测试2 T1题解
    DP专题练习 toasting
    2019.5.1 DP专题训练 山峰数(hill)
    React 点击按钮显示div与隐藏div
  • 原文地址:https://www.cnblogs.com/th1314/p/6527765.html
Copyright © 2020-2023  润新知