1 2 3 4

10.11

今天大概的实现四则运算的全部功能,但是其中仍然有很多不足的地方。

先来看看代码

package test;
import java.util.*;
import sizeyunsuan1.FormulaCalculator;
public class suan2 {
    public static String P[]=new String[100];//题目
    public int Q[]=new int[100];
    public static int J[]=new int[100];//错题
    public static int anwser[]=new int[1000];//答案
    public int Temp[]=new int[100];
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner cin=new Scanner(System.in);
        suan2 a=new suan2();
        int k=0;//错误题数
        int temp=1;
        int N=30;//出题个数
        
        while(temp==1) {
            System.out.println("****************************************\r\n"+
                    "1、参数设置\r\n" + 
                    "2、小学二年级口算题\r\n" + 
                    "3、小学三年级口算题\r\n" + 
                    "4、小学四年级口算题\r\n" + 
                    "5、错题集\r\n" + 
                    "请输入要操作的序号:\r\n"+
                    "****************************************");
            int num=cin.nextInt();
            switch(num) {
            case 1:
                System.out.println("请输入出题个数:");
                N=cin.nextInt();
                //System.out.println("输入q回到主菜单");
                //String ch=cin.next();
                break;
            case 2:
            case 3:
            case 4:
                for(int i=0;i<N;i++)
                {
                    String A=a.string(a,num,i);
                    
                    System.out.println(i+1+","+A+"=");
                    anwser[i]=FormulaCalculator.getResult(A);
                    int anwser1=cin.nextInt();
                    if(anwser1==anwser[i]) {
                        System.out.println("正确!!!");
                    }
                    else if(anwser1!=anwser[i]) {System.out.println("错误,已记录在错题本!");J[k]=i;++k;}
                }
                int trues=(N-k)/N;
                System.out.println("正确率:"+trues*100+"%");
                //System.out.println("输入q回到主菜单");
                //String ch1=cin.next();
                break;
            case 5:
                for(int i=0;i<k;i++) {
                    System.out.println(P[J[i]]);
                    int anwser2=cin.nextInt();
                    if(anwser2==anwser[i]) {
                        System.out.println("正确!!!");
                    }
                    else if(anwser2!=anwser[i]) {System.out.println("错误");}
                }
                }
            
            System.out.println("输入1继续运行,输入0退出系统");
            temp=cin.nextInt();
            
        }
            
            
        }
        
        
        
    
    
    
    
    
    
    
    
    
    
    
    public String string(suan2 a,int n,int i)//查重
    {
        String A;
        int Temp=0;
        for(;;)
        {
            A=a.suanf(n, i);
            for(int j=0;j<i;i++)
            {
                if(A.equals(a.P[j]))
                {
                    Temp=1;
                    break;
                }
            }
            if(Temp==0) break;
        }
        return A;
    }
    
    public  int random1(int a)//生成随机数
    {
        Random ran1 = new Random();
        return (ran1.nextInt(a)+1);
    }
    
    public  char random2()//生成随机符号
    {
        Random ran1 = new Random();
        int a=ran1.nextInt(4);
        char b;
        if(a==1) b='+';
        else if(a==2) b='-';
        else if(a==3) b='*';
        else b='/';
        return b;
    }
    
    /*public int answer1(int a,char b,int c)
    {
        int P;
        if(b=='+') P=a+b;
        else if(b=='-') P=a-b;
        else if(b=='*') P=a*b;
        else P=a/b;
        return P;
    }*/
    
    public String suanf(int AB,int AB1)//生成题目,AB为年级数,AB1为题数
    {
        int n;
        if(AB==2) n=100;
        else if(AB==3) n=1000;
        else n=1000;
        int a[]=new int[5];
        char b[]=new char[4];
        for(int i=0;i<5;i++)
        {
            a[i]=random1(n);
        }
        for(int i=0;i<4;i++)
        {
            b[i]=random2();
        }
        String A=String.valueOf(a[0])+String.valueOf(b[0]);
        String B=String.valueOf(a[1])+String.valueOf(b[1]);
        String C=String.valueOf(a[2])+String.valueOf(b[2]);
        String D=String.valueOf(a[3])+String.valueOf(b[3]);
        String E=String.valueOf(a[4]);
        
        int rand=random1(2);
        if(AB==2) P[AB1]=A+E;
        else if(AB==3) 
            if(rand==1) {
            P[AB1]= A+B+C+E;}
            else if(rand==2) {
                P[AB1]=A+B+E;
            }
        
        
        if(AB==4) 
        {
            int a1=random1(5);
            int a2=random1(5);
            while(a1==a2||a1==5||a2==1||(a1==1&&a2==5))
            {
                a1=random1(5);
                a2=random1(5);
            }
            if(a1>a2) {int a3=a2;a2=a1;a1=a3;}
            switch(a1)
            {
            case 1:A=String.valueOf('(')+A;break;
            case 2:B=String.valueOf('(')+B;break;
            case 3:C=String.valueOf('(')+C;break;
            case 4:D=String.valueOf('(')+D;break;
            }
            switch(a2)
            {
            case 2:B=String.valueOf(a[1])+String.valueOf(')')+String.valueOf(b[1]);break;
            case 3:C=String.valueOf(a[2])+String.valueOf(')')+String.valueOf(b[2]);break;
            case 4:D=String.valueOf(a[3])+String.valueOf(')')+String.valueOf(b[3]);break;
            case 5:E=String.valueOf(a[4])+String.valueOf(')');break;
            }
            
            
            P[AB1]=A+B+C+D+E;
            
            }
        
        return P[AB1];
    }
    
    
    
    //此下为栈计算
    private static boolean isRightFormat = true;
     
    public static int getResult(String formula){  
        int returnValue = 0;  
        try{  
            returnValue = (int) doAnalysis(formula);  
        }catch(NumberFormatException nfe){   
            System.out.println("公式格式有误,请检查:" + formula);  
        }catch(Exception e){   
            e.printStackTrace();  
        } 
        if(!isRightFormat){  
            System.out.println("公式格式有误,请检查:" + formula);  
        } 
        return returnValue;    
    }
 
    private static double doAnalysis(String formula){
        double returnValue = 0;  
        LinkedList<Integer> stack = new LinkedList<Integer>();  
        int curPos = 0;  
        String beforePart = ""; 
        String afterPart = "";  
        String calculator = ""; 
        isRightFormat = true; 
        while(isRightFormat&&(formula.indexOf('(') >= 0||formula.indexOf(')') >= 0)){            
            curPos = 0;  
            for(char s : formula.toCharArray()){   
                if(s == '('){     
                    stack.add(curPos);   
                }else if(s == ')'){     
                    if(stack.size() > 0){     
                        beforePart = formula.substring(0, stack.getLast());     
                        afterPart = formula.substring(curPos + 1);     
                        calculator = formula.substring(stack.getLast() + 1, curPos);      
                        formula = beforePart + doCalculation(calculator) + afterPart;     
                        stack.clear();     
                        break;     
                    }else{      
                        System.out.println("有未关闭的右括号!");     
                        isRightFormat = false;     
                    }   
                }    
                curPos++;   
            }   
            if(stack.size() > 0){    
                System.out.println("有未关闭的左括号!");   
                break;   
            } 
        }  
        if(isRightFormat){  
            returnValue = doCalculation(formula); 
        }  
        return returnValue; 
    }
     
    private static double doCalculation(String formula) {  
        ArrayList<Double> values = new ArrayList<Double>();  
        ArrayList<String> operators = new ArrayList<String>(); 
        int curPos = 0; 
        int prePos = 0;
        int minus = 0;        
        for (char s : formula.toCharArray()) { 
             if ((s == '+' || s == '-' || s == '*' || s == '/') && minus !=0 && minus !=2) {                                                
                 values.add(Double.parseDouble(formula.substring(prePos, curPos).trim()));                   
                 operators.add("" + s);                   
                 prePos = curPos + 1;                 
                 minus = minus +1;
             }else{                 
                  minus =1;                 
             }
             curPos++;         
        }  
        values.add(Double.parseDouble(formula.substring(prePos).trim())); 
        char op;  
        for (curPos = 0; curPos <= operators.size() - 1; curPos++) {                            
            op = operators.get(curPos).charAt(0);   
            switch (op) {  
            case '*':    
                values.add(curPos, values.get(curPos) * values.get(curPos + 1));   
                values.remove(curPos + 1);    
                values.remove(curPos + 1);   
                operators.remove(curPos);  
                curPos = -1;
                break;   
            case '/':   
                values.add(curPos, values.get(curPos) / values.get(curPos + 1));    
                values.remove(curPos + 1);    
                values.remove(curPos + 1);    
                operators.remove(curPos);  
                curPos = -1;
                break;   
            }  
        }  
        for (curPos = 0; curPos <= operators.size() - 1; curPos++) {   
            op = operators.get(curPos).charAt(0);  
            switch (op) {  
            case '+':    
                values.add(curPos, values.get(curPos) + values.get(curPos + 1));    
                values.remove(curPos + 1);    
                values.remove(curPos + 1);   
                operators.remove(curPos);  
                curPos = -1;
                break; 
            case '-':    
                values.add(curPos, values.get(curPos) - values.get(curPos + 1));   
                values.remove(curPos + 1); 
                values.remove(curPos + 1);   
                operators.remove(curPos);  
                curPos = -1;
                break;  
            } 
        }  
        return values.get(0).doubleValue();
    }
    
    }

其中设计到很多知识都是未曾接触过,所以这个小系统写下来真的很不容易。

posted @ 2020-10-11 23:12  小陈的太阳  阅读(67)  评论(0编辑  收藏  举报