package sizeyunsuan2;
import java.util.Random;
import java.util.Scanner;
public class suan {
    static int sum = 0;//记录目前式子个数
    static double cuoti = 0;//错题个数
    static int rr = 0;//也是错题个数,因为要计算错题率所以一个double型,一个int型
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.println("请输入式子个数:");
        int shizi = sc.nextInt();
        String [] arr = new String[shizi];//存运算式
        double [] result = new double[shizi];//存结果
        
        System.out.println("请输入操作数个数:");
        int c = sc.nextInt();//操作数个数
        
        System.out.println("是否需要加入括号,0:不需要 1:需要");
        int kuohao = sc.nextInt();
        
        System.out.println("请输入最小的值");
        int min = sc.nextInt();
        System.out.println("请输入最大的值");
        int max = sc.nextInt();
    
        double [] a = new double[c];
        char [] b = new char[c-1];
        int []temp = new int[shizi];//错题下标
        
        for(int i=0;i<shizi;i++)//生成式子同时判断有无相同,无则存
        {
            String s = chuti(c,a,b,result,max,min,kuohao);
            while(!panduan(arr,s))
            {
                s=chuti(c,a,b,result,max,min,kuohao);
            }
            System.out.print(s);
            dati(result,temp);
            arr[sum] = s;
            sum++;
        }
        gailu(shizi);
        cuotiji(temp,arr);
        System.out.println("是否需要重新计算错题,0:不需要    1:需要");
        int o = sc.nextInt();
        if(o==1)
        {
            System.out.println("------------------------");
            chonglian(temp,arr,result);
        }
    }
    
    public static boolean panduan(String arr[],String b)//判断是否重复
    {
        for(int i=0;i<arr.length;i++)
        {
            if(b.equals(arr[i]))
            {
                System.out.println("重复");
                return false;
            }
        }
        return true;
    }
    
    public static String chuti(int c,double []a,char []b,double []result,int max,int min,int kuohao)//随机生成式子同时计算结果储存到result数组中
    {
        String s="";
        double re=0;
        char [] r = {'+','-','*','/'}; 
        Random ra = new Random();
        Random rb = new Random();
        
        if(kuohao==0)//不要括号
        {
            for(int i=0;i<c;i++)    //5
            {
                if(i<c-1)
                {
                    a[i]=rb.nextInt(max-min+1)+min;//将随机整数存到a中
                    s+=a[i];
                    int t = ra.nextInt(4);
                    b[i]=r[t];//将字符串存到b中
                    s+=r[t];
                }else
                {
                    a[i]=ra.nextInt(max-min+1)+min;
                    s+=a[i];
                }
            }
            //12+45/4*3    a[]={12,45,4,3} b[]={+,/,*}
            
            for(int i=0;i<b.length;i++)//i<7
            {
                if(b[i]=='*')
                {
                    a[i+1]=a[i]*a[i+1];
                    a[i]=0;
                }
                if(b[i]=='/')
                {
                    a[i+1]=a[i]/a[i+1];
                    a[i]=0;
                }
                if(b[i]=='-')//1+2-3
                {
                    a[i+1]=-a[i+1];
                }
            }
            for(int i=0;i<a.length;i++)
            {
                re+=a[i];
            }
            result[sum]=re;
        }else//需要括号
        {
            int zuokuo = ra.nextInt(c-1);
            int youkuo = ra.nextInt(c-zuokuo-1) + zuokuo + 1;
            for(int i=0;i<c;i++)    //5
            {
                if(i<c-1)
                {
                    if(zuokuo==i) s+='(';
                    a[i]=rb.nextInt(max-min+1)+min;//将随机整数存到a中
                    s+=a[i];
                    int t = ra.nextInt(4);
                    b[i]=r[t];//将字符串存到b中
                    if(youkuo==i) s+=')';
                    s+=r[t];
                }else
                {
                    a[i]=ra.nextInt(max-min+1)+min;
                    s+=a[i];
                    if(youkuo==i) s+=')';
                }
            }
            //12+45/4*3    a[]={12,45,4,3} b[]={+,/,*}
            
            for(int j=zuokuo;j<youkuo;j++)//计算括号里的值
            {
                if(b[j]=='*')
                {
                    a[j+1]=a[j]*a[j+1];
                    a[j]=0;
                }
                if(b[j]=='/')
                {
                    a[j+1]=a[j]/a[j+1];
                    a[j]=0;
                }
                if(b[j]=='-')//1+2-3
                {
                    a[j+1]=-a[j+1];
                }
            }
            for(int j=zuokuo;j<=youkuo;j++)
            {
                re+=a[j];
            }
            
             a[zuokuo] = re;//括号内计算结果

             double[] temp = new double[c - (youkuo - zuokuo)];//存去掉括号后的操作数
             char[] tempB = new char[c - (youkuo - zuokuo) - 1];//存括号外的操作符

             temp[zuokuo] = re;
             if (youkuo != c - 1) {
                 tempB[zuokuo] = b[youkuo];
             }

             for (int i = 0; i < zuokuo; i++) {
                 temp[i] = a[i];
                 tempB[i] = b[i];
             }
             for (int i = youkuo + 1, index = zuokuo + 1; i < c; i++, index++) {
                 temp[index] = a[i];
             }
             for (int i =youkuo + 1, index = zuokuo + 1; i < c - 1; i++, index++) {
                 tempB[index] = b[i];
             }

             //继续计算
             for (int j = 0; j < c - (youkuo-zuokuo) - 1; j++) {
                 if (tempB[j] == '-') {
                     temp[j + 1] = -temp[j + 1];
                 }
                 if (tempB[j] == '*') {
                     temp[j + 1] = temp[j] * temp[j + 1];
                     temp[j] = 0;
                 }
                 if (tempB[j] == '/') {
                     temp[j + 1] = temp[j] / temp[j + 1];
                     temp[j] = 0; 
                 }
             }
             re = 0;
             for (int j = 0; j < c - (youkuo-zuokuo); j++) {
                re += temp[j];
             }
            result[sum]=re;
        }
        s+="=";
        return s;
    }
    
    public static void dati(double []result,int []temp)//答题
    {
        Scanner sc = new Scanner(System.in);
            double t = sc.nextDouble();//输入你的答案
            if(t!=result[sum]) 
            {
                cuoti++;
                temp[rr] = sum;
                rr++;
                System.out.println("答案错误");
            }else
            {
                System.out.println("答案正确");
            }
    }
    
    public static void gailu(int shizi)
    {
        double p = (cuoti/shizi)*100;
        System.out.print("你的错误率为:");
        System.out.printf("%.2f",p);
        System.out.println("%");
    }
    
    public static void cuotiji(int []temp,String []arr)
    {
        System.out.println("你的错题集如下:");
        for(int i=0;i<rr;i++)
        {
            System.out.println(arr[temp[i]]);
        }
    }
    
    public static void chonglian(int []temp,String []arr,double []result)
    {
        Scanner sc = new Scanner(System.in);
        for(int i=0;i<rr;i++)
        {
            System.out.print(arr[temp[i]]);
            double t = sc.nextDouble();
            if(t==result[temp[i]])
                System.out.println("再次计算正确");
            else
                System.out.println("再次计算错误");
        }
    }
}

今天重新调试了算法四则运算