四则运算(二)

设计思路:

  先出出来题(String型),上周已经实现,再写方法计算结果,加入控制有无乘除法,范围,参与计算数,出题数,答案对错判断等简单功能,有无括号和分数的计算还没有实现,正在努力

代码:

  1 package ketang;
  2 
  3 import java.util.Scanner;
  4 
  5 public class text {
  6     static Scanner scanner=new Scanner(System.in);
  7     public static void main(String [] ags){
  8         show();
  9         int n=scanner.nextInt();
 10         while(n!=0){
 11         System.out.println("有无乘除法:1.有   0.无");
 12         int cc=scanner.nextInt();
 13         System.out.println("范围    0~?");
 14         int fw=scanner.nextInt();
 15         System.out.println("输入参与计算数:");
 16         int num=scanner.nextInt();
 17         System.out.println("输入出题数量:");
 18         int i=scanner.nextInt();
 19         
 20         String []a=new String [i];
 21         int dui=0;int cuo=0;
 22         for(int j=0;j<i;j++){
 23             if(n==1)
 24                 a[j]=putong(num,cc,fw);
 25             else if(n==2)
 26                 a[j]=zhenfenshu(num,cc);
 27             else if(n==3){
 28                 int m=(int)(Math.random()*2+1);
 29                 if(m==1)
 30                     a[j]=putong(num,cc,fw);
 31                 else if(m==2)
 32                     a[j]=zhenfenshu(num,cc);
 33             }
 34             
 35             for(int k=0;k<j;k++){
 36                 if(a[k].equals(a[j]))
 37                     {j--;break;}
 38                 else
 39                     continue;
 40                  }
 41             System.out.println((j+1)+". "+a[j]);
 42             if(n==1){
 43             String daan=String.valueOf(Zhengshujisuan.fun1(a[j]));
 44             System.out.println("输入答案:");
 45             String result=scanner.next();
 46             if(result.equals(daan)||(result+".0").equals(daan))
 47                 {daan+="	"+"√";dui++;}
 48             else
 49                 {daan+="	"+"×";cuo++;}
 50             System.out.println("你的结果:"+result+"	"+"答案:"+daan);
 51             }
 52         }
 53         
 54         System.out.println("共"+i+"道题"+"	√:"+dui+"	×:"+cuo);
 55             
 56         show();
 57         n=scanner.nextInt();
 58         }
 59     }
 60     public static String putong(int num,int n,int fw){
 61         char [] a=new char[4];int chu=4;
 62         a[0]='+';a[1]='-';a[2]='×';a[3]='÷';
 63         if(n==1){
 64             chu=4;
 65             if(num>2)
 66                 chu=3;
 67         }
 68         if(n==0){
 69             chu=2;
 70         }
 71         
 72         String s="";
 73         for(int i=0;i<num;i++){
 74             s+=(int)(Math.random()*fw+1);
 75             if(i<num-1){
 76                 s+=a[(int)(Math.random()*chu+0)];
 77             }
 78             else
 79                 break;
 80         }
 81     
 82 
 83         return s;
 84     }
 85     public static String zhenfenshu(int num,int n){
 86         char [] a=new char[4];String m,s="";int chu=4;
 87         a[0]='+';a[1]='-';a[2]='×';a[3]='÷';
 88         if(n==1){
 89             chu=4;
 90         }
 91         if(n==0){
 92             chu=2;
 93         }
 94         for(int i=0;i<num;i++){
 95             int num1=(int)(Math.random()*10+1);
 96             int num2=(int)(Math.random()*10+1);
 97             for(;num1==num2;num1=(int)(Math.random()*10+1),num2=(int)(Math.random()*10+1)){}
 98                 if(num1<num2){
 99                     m=num1+"/"+num2;
100                 }
101                 else
102                     m=num2+"/"+num1;
103             s+=m;
104             if(i<num-1){
105                 s+=a[(int)(Math.random()*chu+0)];
106             }
107             else
108                 break;
109         }
110         
111     
112         return s;
113     }
114 
115 
116     
117     public static void show(){
118         System.out.println("1.整数"+"	"+"2.真分数"+"	"+"3.都有"+"	"+"0.退出(暂不支持分数计算,分数只能出题,尽请谅解)");
119     }
120 }

 整数计算方法:

  1 package ketang;
  2 
  3 
  4 import java.util.HashMap;
  5 import java.util.Map;
  6 import java.util.Stack;
  7 public class Zhengshujisuan {
  8     public static Map pro=new HashMap();
  9     public static void init()
 10     {
 11     pro.put('+', 1);
 12     pro.put('-', 1);
 13     pro.put('×', 2);
 14     pro.put('÷', 2);
 15     }
 16     public static int getIndex(String str)
 17     {
 18     int index1=(str.indexOf('+')==-1?str.length():str.indexOf('+'));
 19        int index2=(str.indexOf('-')==-1?str.length():str.indexOf('-'));
 20        int index3=(str.indexOf('×')==-1?str.length():str.indexOf('×'));
 21        int index4=(str.indexOf('÷')==-1?str.length():str.indexOf('÷'));
 22        int index=index1<index2?index1:index2;
 23        index=index<index3?index:index3;
 24        index=index<index4?index:index4;
 25        return index;
 26     }
 27     public static double cal(char op,double num1,double num2)
 28     {
 29     switch(op)
 30     {
 31       case '+':
 32       return num1+num2;
 33       case '-':
 34       return num1-num2;
 35       case '×':
 36       return num1*num2;
 37       default:
 38           return num1/num2;
 39     }
 40     }
 41     public static double fun1(String str)
 42     {
 43     init();
 44     Stack st1=new Stack();
 45     Stack st2=new Stack();
 46     int fop=0;
 47     while(str.length()>0)
 48     {
 49     int index=getIndex(str);
 50     st1.push(Double.parseDouble(str.substring(0,index)));
 51     if(index!=str.length())
 52     {
 53     char op=str.charAt(index);
 54     str=str.substring(index+1);
 55     while(true)
 56     {
 57     if((int)pro.get(op)>fop)
 58     {
 59     st2.push(op);
 60     fop=(int)pro.get(op);
 61     break;
 62     }
 63     else
 64     {
 65     double num2= (double) st1.pop();
 66     double num1=(double) st1.pop();
 67     double result=cal((char)st2.pop(),num1,num2);
 68     st1.push(result);
 69     if(st2.size()==0)
 70     {
 71     st2.push(op);
 72     fop=(int)pro.get(op);
 73     break;
 74     }
 75     char cop=(char) st2.pop();
 76     fop=(int)pro.get(cop);
 77     st2.push(cop);
 78     }
 79     }
 80        }
 81     else
 82     {
 83     break;
 84     }
 85     }
 86     while(st2.size()!=0)
 87     {
 88     double num2=(double) st1.pop();
 89     double num1=(double) st1.pop();
 90     char op=(char) st2.pop();
 91     st1.push(cal(op,num1,num2));
 92     }
 93     double result=(double) st1.pop();
 94     return result;
 95     } 
 96     public static double fun2(String str)
 97     {
 98     while(str.indexOf('(')!=-1)
 99     {
100     int left=0;
101     int right=str.length();
102     char op;
103     for(int i=0;i<str.length();i++)
104     {
105     if(str.charAt(i)=='(')
106     {
107     left=i;
108     }
109     if(str.charAt(i)==')')
110     {
111     right=i;
112     break;
113     }
114     }
115     str=str.substring(0,left)+fun1(str.substring(left+1,right))+str.substring(right+1);
116     }
117 
118     return fun1(str);
119 
120 }
121     }

截图:

  

 

总结:

   分数计算很麻烦,还有小括号的加入,还在想。

原文地址:https://www.cnblogs.com/ghs1065248758/p/6531430.html