zoukankan      html  css  js  c++  java
  • 软件工程个人作业02。

    1.设计思想:

      建立一个主函数,声明参数变量。另建一个空成员变量的类,用于设置主函数中的成员变量,或对调用的数组进行相关操作,然后在主函数将字符串进行连接。

    2.程序代码:

    package 课堂;
    import java.util.Random;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    public class TheAirthmeticOperation
    {
        public static void main(String args[]) throws IOException
        {
            TheAirthmeticOperation_Fuction Fuction = new TheAirthmeticOperation_Fuction();//声明Fuction。
            FileOutputStream fs = new FileOutputStream(new File("D:\text.txt"));
            PrintStream p = new PrintStream(fs);
            
            int num = Fuction.Setnumber();//num位数组上限。
            int n1 = 100;//n1为随机数上限,默认100。
            int n2 = 2;//n2控制有无乘除法,2:加减法;4:加减乘除法。
            int n3 = 0;//n3控制输出方式,0:直接输出;1:输出到txt。
            int n4 = 0;//n4控制是否定制多数运算。
            int n5 = 0;//n5控制多数运算数量。
            int n6 = 0;//n6控制多数运算有无括号。
            String str = "";//初始化str,用于输出式子。
            int []Array1 = new int[num];
            int []Array2 = new int[num];//Array1,Array2数组用于求加减法或分数运算的第一个分数。
            int []Array3 = new int[num];
            int []Array4 = new int[num];//Array3,Array4用于生成第二个分数。
            int []judge1 = new int[num];//根据随机结果输出算数符号。
            int []judge2 = new int[num];//判断该组数据用于整数运算还是分数运算,0:整数;1:分数。
            n1 = Fuction.Setrange();//为n1设置值。
            n2 = Fuction.Set_mul_and_div();//为n2设置值。
            
            for(int i = 0;i < num;i++)
            {
                Array1[i] = Fuction.Randomnumber(n1);
                Array2[i] = Fuction.Randomnumber(n1);
                Array3[i] = Fuction.Randomnumber(n1);
                Array4[i] = Fuction.Randomnumber(n1);
                judge1[i] = new Random().nextInt(n2);
                judge2[i] = new Random().nextInt(2);
            }
            
            Fuction.Setnegativenumber(Array1,Array2,num);//随机为Array1,Array2数组添加负号(可选择不添加)。
            Array1 = Fuction.Isrepetition(Array1,num,n1);//使Array1变得不相同。
            Fuction.Setremainder(Array1,Array2,num,n2);//设置除法有无余数。
            n3 = Fuction.Printmethod();//为n3设置值。
            n4 = Fuction.Is_customization();//为n4设置值。
            n5 = Fuction.customization_number();//为n5设置值。
            n6 = Fuction.customization_brackets();//为n6设置值。
            
            for(int i = 0;i < num;i++)
            {
                if(judge2[i] == 0)//judge2值为0时,执行整数运算。
                {
                    str = "";
                    str += Array1[i] + Fuction.Symbol(str,judge1[i]);
                    if(Array2[i] < 0)//Array2为负时,为其加括号。
                    {
                        str += "(" + Array2[i] + ") = ";
                    }
                    else//不为负时,跳过。
                        str += (Array2[i] + " = ");
                    if(n3 == 0)
                    {
                        System.out.println(str);
                    }
                    else
                    {
                        p.println(str);
                    }
                        
                }
                if(judge2[i] == 1)//judge2值为1时,执行真分数运算。
                {
                    Fuction.Absolutevaluecomparison(Array1,Array2,i);
                    Fuction.Absolutevaluecomparison(Array3,Array4,i);
                    
                    str = "";
                    str += "(" + Array1[i] + "/" + Array2[i] + ")"
                    + Fuction.Symbol(str,judge1[i])
                    + "(" + Array3[i] + "/" + Array4[i] + ") = ";
                    
                    if(n3 == 0)
                    {
                        System.out.println(str);
                    }
                    else
                    {
                        p.println(str);
                    }
                }
            }
            if(n4 == 1)
            {
                for(int i = 0;i < n5;i++)
                {
                    str = "";
                    str = Fuction.customization(n6);
                    if(n3 == 0)
                    {
                        System.out.println(str);
                    }
                    else
                    {
                        p.println(str);
                    }
                }
            }
            p.close();
        }
    }

    import java.util.Random;
    import java.util.Scanner;
    
    public class TheAirthmeticOperation_Fuction
    {
        Scanner sc = new Scanner(System.in);
        
        public int Randomnumber(int n)//重写的随机函数,避免随机出零。
        {
            int number = 0;
            while(number == 0)
            {
                number = new Random().nextInt(n);
            }
            return number;
        }
        
        public int Setnumber()//设置数组,及习题数量。
        {
            int n = 30;
            System.out.println("输入数组数量");
            n = sc.nextInt();
            return n;
        }
        
        public int Setrange()//设置随机数范围。
        {
            int n = 100;
            System.out.println("输入数值范围");
            n = sc.nextInt();
            return n;
        }
        
        public int Set_mul_and_div()//设置习题中是否有乘除法。
        {
            int n = 0;int m = 2;
            System.out.println("是否有乘除法:(0:无  1:有)");
            n = sc.nextInt();
            if(n == 0)
            {
                m = 2;
            }
            else
                m = 4;
            return m;
        }
        
        public int Printmethod()//设置输出习题方式。
        {
            int n = 0;
            System.out.println("打印方式:(0:直接输出  1:打印到txt)");
            n = sc.nextInt();
            return n;
        }
        
        public int Is_customization()//是否定制多数运算。
        {
            int n = 0;
            System.out.println("是否定制多数运算:(0:否  1:是)");
            n = sc.nextInt();
            return n;
        }
        
        public int customization_number()//设置多数运算数量。
        {
            int n = 0;
            System.out.println("输入多数运算数量");
            n = sc.nextInt();
            return n;
        }
        
        public int customization_brackets()//设置多数运算数量。
        {
            int n = 0;
            System.out.println("十位数运算是否有括号:(0:否  1:是)");
            n = sc.nextInt();
            return n;
        }
        
        public String customization(int n)//定制多数习题。
        {
            int []Array = new int[10];//定义一个数组。
            int []a = new int[10];//控制数与数之间符号。
            int []b = new int[20];//控制在某位值是否出现括号。
            String []brackets = new String[20];//用于存放括号。
            String str = "";
                
            for(int i = 0;i < 10;i++)//随机数。
            {
                a[i] = new Random().nextInt(4);
                Array[i] = Randomnumber(20);
            }
            for(int i = 0;i < 20;i++)//初始化括号数组。
            {
                brackets[i] = "";
            }
            
            if(n == 1)//用户选择定制。
            {
                for(int i = 0;i < 20;i += 2)
                {
                    b[i] = new Random().nextInt(4);/*为b[i生成随机数,当b[i]为零时,
                    brackets[i]添加左括号,brackets[i + 3]添加右括号,括号套括号未考虑。*/
                    if(i >= 0 && i < 17)//i > 16 时,没有位置添加右括号,遂向下执行。
                    {
                        if(b[i] == 0)
                        {
                            brackets[i] += "(";
                            brackets[i + 3] += ")";
                            i += 2;
                        }
                    }
                }
            }
                    
            for(int i = 0;i < 9;i++)//将数,括号,符号相连。
            {
                str += brackets[2 * i] + Array[i] + brackets[2 * i + 1];
                str = Symbol(str,a[i]);
            }
            str += Array[9] + brackets[19] + " =";
            
            return str;
        }
        
        public void Setremainder(int []Array1,int []Array2,int num,int n2)
        {//若设置除法无余数,另Array1[i] < Array2[i]。
            if(n2 == 2)
            {
                return;
            }
            int n = 0;
            System.out.println("除法是否有余数:(0:无  1:有)");
            n = sc.nextInt();
            if(n == 0)
            {
                for(int i = 0;i < num;i++)
                {
                    Absolutevaluecomparison(Array1,Array2,i);
                }
            }
            else
                return;
        }
        
        public void Setnegativenumber(int []Array1,int []Array2,int num)
        {//若设置有负数,随机为Array[i]添加符号,i为随机
            int n = 0;int stochastic1 = 0;int stochastic2 = 0;
            System.out.println("运算有无负数:(0:无  1:有)");
            n = sc.nextInt();
            if(n == 0)
            {
                return;
            }
            else
            {
                while(true)
                {
                    stochastic1 = Randomnumber(num);
                    stochastic2 = Randomnumber(num);
                    Array1[stochastic1] = -Array1[stochastic1];
                    Array2[stochastic2] = -Array2[stochastic2];
                    if(stochastic1 == 1)//当stochastic1 = 1 时,跳出循环。
                    {
                        break;
                    }
                }
            }
        }
    
        public void Absolutevaluecomparison(int []Array1,int []Array2,int i)
        {//使两个数最小的数放在Array[i]中,保证无余数或分数为真分数。
            if(Math.abs(Array1[i]) > Math.abs(Array2[i]))
            {
                int t = Array1[i];
                Array1[i] = Array2[i];
                Array2[i] = t;
            }
        }
        
        public int[] Isrepetition(int[] Array,int num,int n1)
        {//删除Array数组中重复元素,缺省数重新随机(此处未考虑随机后还有相等情况,因重复几率较低)。
            int length = 1;
            boolean flag = false;
            for(int i = 1;i < Array.length;i++)
            {
                for(int j = 0;j < length;j++)
                {
                    if(Array[i] == Array[j])
                    {  
                        flag = true;
                    }
                }
                if(!flag)
                {
                    Array[length] = Array[i];  
                    length++;  
                }
                flag = false;  
            }
            int[] newArray = new int[num];
            System.arraycopy(Array,0,newArray,0,length);
            for(int i = length;i < num;i++)
            {
                newArray[i] = Randomnumber(n1);
            }
            return newArray;
        }
        
        public String Symbol(String str,int value)
        {//根据值为str添加符号。
            if(value == 0)
            {
                str += " + ";
            }
            if(value == 1)
            {
                str += " - ";
            }
            if(value == 2)
            {
                str += " * ";
            }
            if(value == 3)
            {
                str += " / ";
            }
            return str;
        }
    }

    3.运行截图



    4.总结:

      用java写这么多行的程序还是头一次。平常还是习惯用c写。其中还是遇到了一些问题,比如java中似乎没有值地址递,想在别的函数中直接修改调用的值是不行的(除非调用的是数组的首地址),只能返回出来再赋值给它。想不这么实现也可以,就得把参数定义给成员变量。后来想到这里,确实可行,把设置的参数看成一组成员变量,不过顺序运行下来,也就一组对象,也没有必要重写大费周章了。在这点上,我好像看出了java的方便与麻烦之处。另一方面,通过网上的查询,也明白了点java中的输出流。总的来讲,收获还是不小的。

    5.每周计划

      听课 编程 读书 总计
    周一 2 2.5 0 5
    周二 0 1 0 0.5
    周三 0 1 0 1
    周四 0 1 0 1
    周五 0 0.5 0 0.5
    周六 0 0.5 0 0.5
    周日 0 0 1.5 1.5

    6.时间记录日志

    日期 开始时间 结束时间 中断时间(min) 净时间(min) 活动 备注
    3.14 8:00 9:50 10 100 上课 课间休息十分钟
    14:00 17:30 48 132 编程 中断时间为躺着冥思或溜达
    3.15 14:00 15:00 2 58 编程  
    21:00 21:40 5 35 编程  
    3.16 7:00 7:20 0 20 编程  
    16:00 16:30 1 29 编程  
    20:00 20:40 5 35 编程  
    3.17 16:30 17:20 8 42 编程  
    3.18 7:00 7:15 0 15 编程  
    17:00 17:30 6 24 编程  
    3.19 11:05 11:32 0 27 编程  
    总计 / / 85 507 /  

    7.缺陷记录日志

      都是些数组超出长度的小错误,没有在修改缺陷这里花费多少时间。

    
    
    
     
  • 相关阅读:
    初探nodejs事件循环机制event loop
    夯实基础之--new关键字、instanceOf原理
    分享-结合demo讲解JS引擎工作原理
    Linux-centos安装node、nginx小记
    openlayers5实战--踩坑总结
    node+koa中转层开发实践总结
    vue预渲染实践总结
    css多行省略-webkit-box-orient打包编译后失效原因
    使用mpVue开发小程序实战总结
    Linux crontab定时执行任务
  • 原文地址:https://www.cnblogs.com/shenshenxin/p/5295556.html
Copyright © 2011-2022 走看看