• 四则运算*3


    package 四则运算2;
    
    import java.util.Scanner;
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
            
            Scanner in = new Scanner(System.in);
            System.out.println("请输入题目的数量:");
            int n = in.nextInt();
            System.out.println("请输入每行题目的数目:");
            int num= in.nextInt();
            //乘除法
            System.out.println("请输入题目是否包含乘除法:1、有     2、没有");
            int j = in.nextInt();
            int k;//控制运算符号的数目,k=2没有乘除,反之有乘除
            if(j==1)
            {
                k = 3;
            }
            else 
                k = 1;
            //是否有括号
            System.out.println("请输入题目是否包含括号:1、有     2、没有");
            int if_kuohao = in.nextInt();
            
            
            
            //控制数值范围
            System.out.println("请输入题目数值的最大范围:");
            int num_max = in.nextInt();
            System.out.println("请输入数值的最小范围:");
            int num_min = in.nextInt();
            System.out.println( "请输入加减结果是否允许有负数:1、有负数  2、没有负数");
            int ifFushu = in.nextInt();
            System.out.println( "请输入除法结果是否允许有余数(如:5/4= 1...1):1、有余数  2、没有余数");
            int ifYushu = in.nextInt();
            yunsuan(in,n,num,k,num_max,num_min,ifFushu,ifYushu,if_kuohao);
            in.close();
        }
        //分数化简
        public static String huajian(int a,int b){
            if(a<b)
            {
                for(int i = a;i>0;i--){
                if(a%i==0&&b%i==0){
                    a/=i;
                    b/=i;
                    break;
                }}
                if(a%b==0)
                    return a/b+"";
                else
                    return a+"/"+b;
            }
            else{
    
                for(int i = b;i>0;i--){
                if(a%i==0&&b%i==0){
                    a/=i;
                    b/=i;
                    break;
                }}
                if(b%a==a)
                    return b/a+"";
                else
                    return b+"/"+a;
                }
            
            
        }
        public static String yushu(int a,int b){
            if(a%b==0)
                return a/b+"";
            else if(b%a==a)
                return b/a+"";
            else
                return a/b+"..."+a%b;
        }
        public static float opt(String s) throws Exception{
            if(s == null || "".equals(s.trim())) {
                return 0f;
            }
            int a1=s.indexOf("+");
            int a2=s.indexOf("-");
            int a3=s.indexOf("*");
            int a4=s.indexOf("/");
            int a5=s.indexOf("(");
            if(a1==-1&&a2==-1&&a3==-1&&a4==-1){
                if(s.trim()==null||"".equals(s.trim())){
                    throw new Exception("operate error");
                }
                return Float.parseFloat(s.trim());
            }
             
            if(a5!=-1){
            int a6=s.indexOf(")");
                if(a6==-1){
                    throw new Exception("括号不匹配");
                }else{
                    float f=opt(s.substring(a5+1,a6).trim());
                    s=s.replace(s.substring(a5,a6+1), String.valueOf(f));
                    return opt(s);
                }
            }
             
            if(a1!=-1){
                return opt(s.substring(0,a1))+opt(s.substring(a1+1,s.length()));
            }
            if(a2!=-1){
                return opt(s.substring(0,a2))-opt(s.substring(a2+1,s.length()));
            }
            if(a3!=-1){
                return opt(s.substring(0,a3))*opt(s.substring(a3+1,s.length()));
            }
            if(a4!=-1){
                return opt(s.substring(0,a4))/opt(s.substring(a4+1,s.length()));
            }
            return Integer.parseInt(s.trim());
        }
        //n为数的个数,
        public static float kuohao(int n,int num_max,int num_min) throws Exception{
            int x=(int) (1+(Math.random()*num_max));
            while(x<num_min)
                x = (int)(1+(Math.random()*num_min));
            int y=0;//符号随机
            int j;//寻找(
            int p;//确定)的位置
            int e=0;//确定表达式中是否有右括号
            int a = 10;
            String f = null ;
            String biaodashi = x+"";
            int num = 8;
            for(int i = 0;i<5-1;i++){
                
                y = (int)(1+(Math.random()*num));
                
                if(y==1)
                    f = "+";
                else if(y==2)
                    f = "-";
                else if(y==3)
                    f = "*";
                else if(y==4)
                    f = "/";
                else if(y==5)
                    f = "+(";
                else if(y==6)
                    f = "-(";
                else if(y==7)
                    f = "*(";
                else if(y==8)
                    f = "/(";
                
                biaodashi = biaodashi+ f + x ;
                x=(int)(1+(Math.random()*20));
                //控制左括号
                if(y>4)
                    {
                    num = 4;
                    a = i;
                    }
                else if(4-i>0)
                    num = 4;
                //控制右括号
                if(i-a>=1&&e==0)
                    {
                    p=(int)(1+(Math.random()*2));
                    if(p==1)
                        biaodashi+="";
                    else
                        {
                        biaodashi+=")";
                        e=1;
                        }
                    }
                
                
            }
            for(int q = 0;q<biaodashi.length();q++){
                if(biaodashi.charAt(q)=='(')
                    for(int w = q;w<biaodashi.length();w++)
                        if(biaodashi.charAt(w)==')')
                            break;
                        else
                            {
                            if(w==biaodashi.length()-1)
                                biaodashi+=")";
                            }
                                
                }
            
            System.out.print(biaodashi+"   ");
                float as = opt(biaodashi);
                return as;
                
            
            
        }
        //分数四则运算
        public static String[] fenshu(int n,int num,int num_max,int num_min,
                int fuhao_num,int ifYushu,int ifFushu){
            
            System.out.println("
    分数四则运算:");
            String result1[] = new String[n];
            for(int i1 = 0;i1<n;i1++)
            {
                int a =1+ (int)(Math.random()*num_max);
                int b = 1+(int)(Math.random()*num_max);
                int c = 1+(int)(Math.random()*num_max);
                int d = 1+(int)(Math.random()*num_max);
                while(a<num_min){
                    a=1+ (int)(Math.random()*num_max);
                }
                while(b<num_min){
                    b=1+ (int)(Math.random()*num_max);
                }
                while(c<num_min){
                    c=1+ (int)(Math.random()*num_max);
                }
                while(d<num_min){
                    d=1+ (int)(Math.random()*num_max);
                }
                int e;
                int fuhao1 = 1+(int)(Math.random()*fuhao_num);
                
                if(a>b)
                {
                    e = a;
                    a = b;
                    b = e;
                }
                if(c>d)
                {
                    e = c;
                    c = d;
                    d = e;
                }
                String x1 = a+"/"+b;
                String y1 = c+"/"+d;
                
                
                System.out.print((i1+1+n)+": ");
                
                
                if(fuhao1 == 0&&ifYushu == 1){
                    System.out.print(x1+ " + " +y1+"    ");
                    a = a*d+b*c;
                    b = b*d;
                    
                    result1[i1] = yushu(a,b);
                }
                else if(fuhao1 == 0&&ifYushu == 2)
                {
                    System.out.print(x1+ " + " +y1+"    ");
                    a = a*d+b*c;
                    b = b*d;
                    
                    result1[i1] = huajian(a,b);
                }
                else if(fuhao1 == 1&&ifYushu==1)
                {
                    //有负数
                    if(ifFushu==1){
                        System.out.print(y1+ " - " +x1+"    ");
                        a = b*c-a*d;
                        b = b*d;
                        
                        result1[i1] = yushu(a,b);
                    }
                }
                    //没有负数
                    if(ifFushu==2){
                    if(a/b<c/d)
                    {
                        System.out.print(y1+ " - " +x1+"    ");
                        a = b*c-a*d;
                        b = b*d;
                        
                        result1[i1] = yushu(a,b);
                    }
                    else
                    {
                        System.out.print(x1+ " - " +y1+"    ");
                        a = a*d-b*c;
                        b = b*d;
                        result1[i1] = yushu(a,b);
                    }
                    
                }
                else if(fuhao1 == 1&&ifYushu==2)
                {
                    //有负数
                    if(ifFushu==1){
                        System.out.print(y1+ " - " +x1+"    ");
                        a = b*c-a*d;
                        b = b*d;
                        
                        result1[i1] = huajian(a,b);
                    }
                    //没有负数
                    if(ifFushu==2){
                    if(a/b<c/d)
                    {
                        System.out.print(y1+ " - " +x1+"    ");
                        a = b*c-a*d;
                        b = b*d;
                        
                        result1[i1] = huajian(a,b);
                    }
                    else
                        System.out.print(x1+ " - " +y1+"    ");
                    a = a*d-b*c;
                    b = b*d;
                    
                    result1[i1] = huajian(a,b);}
                }
                else if(fuhao1 ==2&&ifYushu==1){
                    System.out.print(x1+ " * " +y1+"    ");
                    a = a*c;
                    b = b*d;
                    
                    result1[i1] = yushu(a,b);
                }
                else if(fuhao1 ==2&&ifYushu==2){
                    System.out.print(x1+ " * " +y1+"    ");
                    a = a*c;
                    b = b*d;
                    
                    result1[i1] = huajian(a,b);
                }
                else if(fuhao1 ==3&&ifYushu==1){
                    System.out.print(x1+ " / " +y1+"    ");
                    a = a*d;
                    b = b*c;
                    result1[i1] = yushu(a,b);
                }
                else if(fuhao1 ==3&&ifYushu==2){
                    System.out.print(x1+ " / " +y1+"    ");
                    a = a*d;
                    b = b*c;
                    result1[i1] = huajian(a,b);
                }    
                if((i1+1)%num==0)
                    System.out.print("
    ");
            }
            return result1;
        }
        //四则运算 fuhao_num控制乘除,num_max num_min控制数值范围,c控制是否有负数,d控制是否有余数
        public static void yunsuan(Scanner in,int n,int num,int fuhao_num,
                int num_max,int num_min,int ifFushu,int ifYushu,int if_kuohao) throws Exception{
            
            int result[] = new int[n];
            String result1[] = new String[n];
            String aq[] = new String[n];
            
            if(if_kuohao==2){
                //整数四则运算
                System.out.println("整数四则运算:");
                for(int i  = 0;i<n/2;i++){
                    //获得两个数
                    int x =1+ (int)(Math.random()*num_max);
                    int y = 1+(int)(Math.random()*num_max);
                    while(x<num_min){
                        x =1+ (int)(Math.random()*num_max); 
                    }
                    while(y<num_min)
                    {
                        y = 1+ (int)(Math.random()*num_max);
                    }
                    
                    //随机获得符号
                    int fuhao = 1+(int)(Math.random()*fuhao_num);
                    System.out.print(i+1+": ");
                    
                    //加法运算
                    if(fuhao == 0)
                    {
                        System.out.print(x+ " + " +y+"    ");
                        
                        result[i] = x+y;
                    }
                    //减法运算并检验结果无负数
                    else if(fuhao == 1&&ifFushu ==2)
                    {
                        if(x<y)
                        {
                            System.out.print(y+ " - " +x+"    ");
                            
                            result[i] = y-x;
                        }
                        else
                        {
                            System.out.print(x+ " - " +y+"    ");
                            
                            result[i] = x-y;
                        }
                    }
                    //减法运算结果有负数
                    else if(fuhao == 1&&ifFushu == 1)
                    {
                        System.out.print(x+ " - " +y+"    ");
                        
                        result[i] = x-y;
                    }
                    //乘法运算
                    else if(fuhao ==2){
                        System.out.print(x+ " * " +y+"    ");
                        
                        result[i] = x*y;
                    }
                    
                    //除法运算并判断结果有无余数
                    else
                    {
                    if(ifYushu == 2){
                        System.out.print(x+ " / " +y+"    ");
                        
                        result1[i] = huajian(x,y);
                    }
                    else
                    {
                        System.out.print(x+ " / " +y+"    ");
                        
                        result1[i] = x/y+"..."+x%y;
                    }
                    
                    
                }
                    //判断每行的题数
                    if((i+1)%num ==0)
                    {
                        System.out.println();
                    }
                }
                    
                    
                //分数四则运算
                aq=fenshu(n/2,num,num_max, num_min,fuhao_num, ifYushu, ifFushu);
                for(int j = n/2;j<n;j++){
                    result1[j] = aq[j-n/2]; 
                    
                }
                System.out.println("若做完,按1可查看答案");
                int number = in.nextInt();
                in.close();
                int a = 0;
                if(number == 1){
                for(int i1= 0;i1<n;i1++){
                    System.out.print(i1+1+":");
                    if(result1[i1]!=null){
                        System.out.print(result1[i1]+"   ");
                        }
                    else
                        System.out.print(result[i1]+"   ");
                    a++;
                    if(a%5==0)
                        System.out.print("
    ");
                }
            }
                }
            //带括号的运算
            if(if_kuohao==1){
                System.out.println("
    整数四则运算");
            for(int i  = 0;i<n/3;i++){
                //获得两个数
                int x =1+ (int)(Math.random()*num_max);
                int y = 1+(int)(Math.random()*num_max);
                while(x<num_min){
                    x =1+ (int)(Math.random()*num_max); 
                }
                while(y<num_min)
                {
                    y = 1+ (int)(Math.random()*num_max);
                }
                
                //随机获得符号
                int fuhao = 1+(int)(Math.random()*fuhao_num);
                System.out.print(i+1+": ");
                
                //加法运算
                if(fuhao == 0)
                {
                    System.out.print(x+ " + " +y+"    ");
                    
                    result[i] = x+y;
                }
                //减法运算并检验结果无负数
                else if(fuhao == 1&&ifFushu ==2)
                {
                    if(x<y)
                    {
                        System.out.print(y+ " - " +x+"    ");
                        
                        result[i] = y-x;
                    }
                    else
                    {
                        System.out.print(x+ " - " +y+"    ");
                        
                        result[i] = x-y;
                    }
                }
                //减法运算结果有负数
                else if(fuhao == 1&&ifFushu == 1)
                {
                    System.out.print(x+ " - " +y+"    ");
                    
                    result[i] = x-y;
                }
                //乘法运算
                else if(fuhao ==2){
                    System.out.print(x+ " * " +y+"    ");
                    
                    result[i] = x*y;
                }
                
                //除法运算并判断结果有无余数
                else
                {
                if(ifYushu == 2){
                    System.out.print(x+ " / " +y+"    ");
                    
                    result1[i] = huajian(x,y);
                }
                else
                {
                    System.out.print(x+ " / " +y+"    ");
                    
                    result1[i] = x/y+"..."+x%y;
                }
                
                
            }
                //判断每行的题数
                if((i+1)%num ==0)
                {
                    System.out.println();
                }
            }
                
                
            //分数四则运算
            aq=fenshu(n/3,num,num_max, num_min,fuhao_num, ifYushu, ifFushu);
            for(int j = n/3;j<n/3*2;j++){
                result1[j] = aq[j-n/3]; 
                
            }
            
            
            //括号四则运算
            float[] as = new float[n];
            System.out.println("
    括号四则运算:");
            for(int i = n/3*2;i<n;i++){
                System.out.print(i+1+":");
                as[i] = kuohao(num,num_max,num_min);
                if(i%num==0)
                    System.out.print("
    ");
                }
                
            System.out.println("若做完,按1可查看答案");
            int number1 = in.nextInt();
            in.close();
            int a1 = 0;
            if(number1 == 1){
            for(int i1= 0;i1<n;i1++){
                System.out.print(i1+1+":");
                if(result1[i1]!=null&&i1<n/3*2){
                    System.out.print(result1[i1]+"   ");
                    }
                else if(i1<n/3*2)
                    System.out.print(result[i1]+"   ");
                else if(i1>=n/3*2)
                    System.out.println(as[i1]);
                a1++;
                if(a1%5==0)
                    System.out.print("
    ");
            }
            }
        
    
    }
        }
        }
  • 相关阅读:
    EasyDarwin开源平台直播架构
    EasyDarwin开源平台直播架构
    ffmpeg推送,EasyDarwin转发,vlc播放 实现整个RTSP直播
    ffmpeg推送,EasyDarwin转发,vlc播放 实现整个RTSP直播
    开源G711(PCMA、PCMU)/G726转AAC项目EasyAACEncoder
    开源G711(PCMA、PCMU)/G726转AAC项目EasyAACEncoder
    RTSP/RTMP/HLS/HTTP流媒体播放器EasyPlayer
    开源流媒体播放器EasyPlayer
    vs添加对dll的引用
    vs添加对dll的引用
  • 原文地址:https://www.cnblogs.com/clueless/p/7025695.html
Copyright © 2020-2023  润新知