四则运算一

  1 import java.lang.*;
  2 import java.util.*;
  3 public class Shi2 {
  4 
  5     public static Boolean chachong1(Zhengshu x,Zhengshu y){
  6         if(x.a==y.a&&x.b==y.b&&x.c==y.c){
  7            return true;
  8         }
  9         else if(x.b==y.b&&(x.b==1||x.b==3)){
 10         if(x.a==y.c&&x.c==y.a) return true;
 11         else return false;
 12         }
 13         else{return false;}
 14         }
 15          public static Boolean chachong2(Fenshu x,Fenshu y){
 16           Fenshu xx=new Fenshu();
 17           Fenshu yy=new Fenshu();
 18           yy=yue(y);
 19           xx=yue(x);
 20           if(x.b==y.b){
 21            if(x.b==1||x.b==3){
 22             if(x.a==y.a&&x.c==y.c&&x.d==y.d&&x.e==y.e)return true;
 23             else if(x.a==y.d&&x.c==y.e&&x.d==y.a&&x.e==y.c)return true;
 24             else return false;
 25              }
 26            else {
 27             if(x.a==y.a&&x.c==y.c&&x.d==y.d&&x.e==y.e)return true;
 28             else return false;
 29             }
 30           }
 31           else {return false;}
 32     }
 33          public static int zuidayin(int a,int b){
 34           int c,d=1;
 35           if(a<b){c=a;}
 36           else{c=b;}
 37           for(int i=2;i<=c;i++){
 38            if(a%i==0&&b%i==0){
 39             d=i;
 40            }
 41           }
 42           return d;
 43          }
 44          public static void yuefen(int a,int b,int c,int d){
 45           int aa=zuidayin(a,b);
 46           c=a/aa;
 47           d=b/aa;
 48          }
 49     public static Fenshu yue(Fenshu x){
 50           Fenshu y=new Fenshu();
 51           int a=x.a;
 52           int b=x.b;
 53           int c=x.c;
 54           int d=x.d;
 55           int e=x.e;
 56           int a1=0,c1=0,d1=0,e1=0;
 57           yuefen(a,d,a1,d1);
 58           yuefen(c,e,c1,e1);
 59           y.fu(a1,b,c1,d1,e1);
 60           return y;
 61     }
 62      public static void main(String[] args) {
 63           int shu=0,q=0;
 64           Scanner input=new Scanner(System.in);
 65           System.out.println("输入1、整数2、分数");
 66           q=Integer.parseInt(input.next());
 67           System.out.println("输入数量:");
 68           shu=Integer.parseInt(input.next());
 69           if(q==1){
 70           Zhengshu cf[]=new Zhengshu [shu];
 71           for(int i=0;i<shu;i++){
 72            cf[i]=new Zhengshu();
 73            boolean aa=true;
 74            while(aa){
 75             cf[i].zheng();
 76             aa=false;
 77             for(int j=0;j<i;j++){
 78              if(chachong1(cf[i],cf[j])){
 79               aa=true;
 80               break;
 81              }
 82             }
 83            }
 84           }
 85           for(int i=0;i<shu;i++){
 86            cf[i].show();
 87           }
 88           }
 89           else if(q==2){
 90            Fenshu cf[]=new Fenshu [shu];
 91            for(int i=0;i<shu;i++){
 92             cf[i]=new Fenshu();
 93             boolean aa=true;
 94             while(aa){
 95              cf[i].fen();
 96              aa=false;
 97              for(int j=0;j<i;j++){
 98               if(chachong2(cf[i],cf[j])){
 99                aa=true;
100                break;
101               }
102              }
103             }
104            }
105            for(int i=0;i<shu;i++){
106             cf[i].show();
107            }
108           }
109           else System.out.println("输入有误");
110           
111         }
112         }
113 }
114 
115 class Zhengshu{
116     int a;
117     int b;
118     int c;
119     public void zheng(){
120       a=(int)(Math.random()*100);
121       b=(int)(Math.random()*4+1);
122       c=(int)(Math.random()*100);
123       if(b==4&&c==0){
124         zheng();
125       }
126     }
127     public void show(){
128        String q;
129        if(b==1)q="+";
130        else if(b==2)q="-";
131        else if(b==3)q="*";
132        else q="/";
133        System.out.println(a+q+c+"=");
134     }
135 }
136 
137 class Fenshu{
138     int a,b,c,d,e;
139     public void fu(int a1,int a2,int a3,int a4,int a5){
140          a=a1;
141          b=a2;
142          c=a3;
143          d=a4;
144          d=a5;
145     }
146     public void fen(){
147           a=(int)(Math.random()*100);
148           b=(int)(Math.random()*4+1);
149           c=(int)(Math.random()*100);
150           d=(int)(Math.random()*100);
151           e=(int)(Math.random()*100);
152           if(b==4&&c==0||d==0||e==0){
153                fen();
154           }
155      }
156  
157      public void show(){
158           String q;
159           if(b==1)q="+";
160           else if(b==2)q="-";
161           else if(b==3)q="*";
162           else q="/";
163           System.out.println("("+a+"/"+d+")"+q+"("+c+"/"+e+")=");
164      }
165 }
166         

验证截图:

posted @ 2017-11-25 19:41  messi2017  阅读(194)  评论(0编辑  收藏  举报