四则运算2

一.设计思路

     1.输入

            1)将条件以0和1的形式输入,通过判断0或1来执行相应的功能;

     2.计算

            1)建一个主类,在主类中编写方法,其中两个综合方法,分别为整数运算和分数运算,在这两个方法中再调用其他小函数,包括求运算符方法,最大公约数方法,输出方法。                 

           2)是否有乘除法:如果有则将各种符号通过switch语句控制,产生0-4的随机数来控制输出加减法还是输出乘除法;如果没有有乘除法,将上边随机数的范围改为0-2。

           3)加减有无负数:在求加减法的函数中加入switch语句,产生0-3的随机数,然后通过判断随机数来确定是否为负数;

           5)除法有无余数:在求除法的函数中添加判断求余是否为0;

     3.输出

           在综合方法中调用输出函数,参数分别是参与运算的数,输出结果;

二.程序代码:

package sizeyunsuan;

import java.util.Random;
import java.util.Scanner;
public class FourOperations {
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        FourOperations FO=new FourOperations();
        Random rand=new Random();
        
        System.out.println("请输入题目的数量:");
        int num=in.nextInt();
        
        System.out.println("是否有乘除法?(1:有 ,0:没有)");
        int select1=in.nextInt();
        
        System.out.println("加减有无负数?(1:有 ,0:没有)");
        int select2=in.nextInt();
        
        System.out.println("除法有无余数?(1:有 ,0:没有)");
        int select3=in.nextInt();
        
        System.out.println("请输入取值范围上限:");
        int random=in.nextInt();
        int n[]={0};
        for(int i=0;i<num+n[0];i++){
            int key=rand.nextInt();
            
            if(Math.abs(key%2)==0)
                FO.ZhengShu(n,select1,select2,select3,random);
            if(Math.abs(key%2)==1)
                FO.FenShu(n,select1,select2,select3,random);
        }
        in.close();
    }
    
    public String JiaJian(){
        String sign=null;
        Random rand=new Random();
        int mark=rand.nextInt();
        switch(Math.abs(mark%2))
        {
        case 0:sign = "+";break;
        case 1:sign = "-";break;
        }
        return sign;
    }
    public String ChengChu(){
        String sign=null;
        Random rand=new Random();
        int mark=rand.nextInt();
        switch(Math.abs(mark%4))
        {
        case 0:sign = "+";break;
        case 1:sign = "-";break;
        case 2:sign = "*";break;
        case 3:sign = "/";break;
        }
        return sign;
    }
    
    public void OutWay(String dividend,String sign,String divider){
        
        if(sign=="+")
            System.out.println(dividend + " + " + divider);
        if(sign=="-")
            System.out.println(dividend + "-" + "(" + divider + ")");
        if(sign=="*")
            System.out.println(dividend + " * " + divider);
        if(sign=="/")
            System.out.println(dividend + " / " + divider);
        
    }
    public void FenShu(int n[],int s1,int s2,int s3,int random){
        String dividend=null;
        String divider=null;
        FourOperations FO=new FourOperations();
        String sign=null;
        Random rand=new Random();
        int dividend1,divider1,dividend2,divider2;
        boolean index=false;
        

        dividend1=rand.nextInt(random);
        divider1=rand.nextInt(random);
        dividend2=rand.nextInt(random);
        divider2=rand.nextInt(random);
        String dd1=null,dd2=null,dr1=null,dr2=null;
        dd1=String.valueOf(dividend1);
        dd2=String.valueOf(dividend2);
        dr1=String.valueOf(divider1);
        dr2=String.valueOf(divider2);
        if((divider1==0)||(divider2==0)){
            n[0]++;
            index=true;
        }
        else{
            int gcd;
            gcd=GCD(dividend1,divider1);
            dividend1=dividend1/gcd;
            divider1=divider1/gcd;
            gcd=GCD(dividend2,divider2);
            dividend2=dividend2/gcd;
            divider2=divider2/gcd;
            if(s1==1)
                sign=FO.ChengChu();
            else
                sign=FO.JiaJian();
            if(s2==1){
                if(sign=="+"||sign=="-"){
                    int key1=rand.nextInt();
                    switch(Math.abs(key1%3)){
                    case 0:{dividend1=-dividend1;break;}
                    case 1:{dividend1=-dividend1;dividend2=-dividend2;break;}
                    case 2:{dividend2=-dividend2;break;}
                    }
                }
            }
            dividend=dd1+"/"+dr1;
            divider=dd2+"/"+dr2;
            if(sign=="/"){
                if(dividend2==0){
                    n[0]++;
                    index=true;
                }
                if(s3==0){
                    if((divider1*dividend2)%(dividend1*divider2)!=0){
                        n[0]++;    
                        index=true;
                        }
                }
            }
            if(!index)
                OutWay(dividend,sign,divider);
        }
        
    }
    public void ZhengShu(int n[],int s1,int s2,int s3,int random){
        String divider=null;
        String dividend=null;
        
        FourOperations FO=new FourOperations();
        String sign=null;
        Random rand=new Random();
        int dividend1,divider1;
        dividend1=rand.nextInt(random);
        divider1=rand.nextInt(random);
        boolean index=false;
        if(s1==1)
            sign=FO.ChengChu();
        else
            sign=FO.JiaJian();
        if(s2==1){
            if((sign=="+")||(sign=="-")){
                int key1=rand.nextInt();
                switch(Math.abs(key1%3)){
                case 0:{dividend1=-dividend1;break;}
                case 1:{divider1=-divider1;break;}
                case 2:{dividend1=-dividend1;divider1=-divider1;break;}
                }
            }
        }
        dividend=String.valueOf(dividend1);
        divider=String.valueOf(divider1);
        if(sign=="/"){
            if(divider1==0){
                n[0]++;
                index=true;
            }
            if(s3==0){
                if((dividend1%divider1)!=0){
                    n[0]++;
                    index=true;
                }
            }
        }
        if(!index)
            OutWay(dividend,sign,divider);
    }

    public int GCD(int x,int y){
        
        int r;
        while(y>0){
            r=x%y;
            x=y;
            y=r;
        }
        return x;
    }
}

三.程序结果截图

4.项目计划总结

时间记录日志

缺陷记录日志

posted @ 2016-03-19 20:15  梦玄庭  阅读(203)  评论(0编辑  收藏  举报