标签:
整数运算
1 public class Zhengshu { 2 int zs1,zs2,sum=1;int fh; 3 Zhengshu(){ 4 5 } 6 Zhengshu(int Zs1,int Fh,int Zs2){ 7 zs1=Zs1; 8 fh=Fh; 9 zs2=Zs2; 10 } 11 public void setJiaf(int Zs1,int Zs2){ 12 sum=Zs1+Zs2; 13 } 14 public void setJianf(int Zs1,int Zs2){ 15 sum=Zs1-Zs2; 16 } 17 public void setChengf(int Zs1,int Zs2){ 18 sum=Zs1*Zs2; 19 } 20 public void setChuf(int Zs1,int Zs2){ 21 sum=Zs1/Zs2; 22 } 23 public String getJiaf(){ 24 return zs1+"+"+zs2; 25 } 26 public String getJianf(){ 27 return zs1+"-"+zs2; 28 } 29 public String getChengf(){ 30 return zs1+"*"+zs2; 31 } 32 public String getChuf(){ 33 return zs1+"/"+zs2; 34 } 35 public int getSum(){ 36 return sum; 37 } 38 public String getsum(int zs1,int zs2){ 39 return zs1+"/"+zs2; 40 } 41 }
分数运算
1 import java.util.Random; 2 3 4 public class Fenshu { 5 private int fz,fm;//用户输入分子、分母 6 private int fz0,fm0;//已约分正确分子、分母 7 private int fz1=1,fm1=1;//未约分正确分子、分母 8 private int jlfz,jlfm;//操作数 9 private int temp,ys;//操作数 10 private char ch; 11 private char fh[]={‘+‘,‘-‘,‘*‘,‘/‘}; 12 Fenshu(){} 13 public String suanshi(int a,int b,int c,int d,int z){ 14 if(a>b)//确保分母大于分子 15 { 16 temp=b; 17 b=a; 18 a=temp; 19 } 20 if(c>d)//确保分母大于分子 21 { 22 temp=d; 23 d=c; 24 c=temp; 25 } 26 // z=new Random().nextInt(cc*2-1); 27 return a+"/"+b+" "+fh[z]+" "+c+"/"+d; 28 29 } 30 public String answer(int a,int b,int c,int d,int z){ 31 int shu []=new int [4]; 32 shu[0]=a; 33 shu[1]=b; 34 shu[2]=c; 35 shu[3]=d; 36 switch(z) 37 { 38 case 0: 39 fz1=shu[0]*shu[3]+shu[1]*shu[2]; 40 fm1=shu[1]*shu[3]; 41 break; 42 case 1: 43 fz1=shu[0]*shu[3]-shu[1]*shu[2]; 44 fm1=shu[1]*shu[3]; 45 break; 46 case 2: 47 fz1=shu[0]*shu[2]; 48 fm1=shu[1]*shu[3]; 49 break; 50 case 3: 51 fz1=shu[0]*shu[3]; 52 fm1=shu[1]*shu[2]; 53 break; 54 default: 55 break; 56 } 57 jlfz=fz1;//约分 58 jlfm=fm1; 59 while(fm1!=0) 60 { 61 ys=fz1%fm1; 62 fz1=fm1; 63 fm1=ys; 64 } 65 fz0=jlfz/fz1; 66 fm0=jlfm/fz1; 67 if(fm0<0) 68 { 69 fz0=fz0*-1; 70 fm0=fm0*-1; 71 } 72 return fz0+"/"+fm0; 73 } 74 75 }
阶乘运算
1 public class Jiecheng { 2 private int jc1; 3 private int sum=1; 4 private int n; 5 Jiecheng(){ 6 7 } 8 Jiecheng(int jc){ 9 jc1=jc; 10 11 } 12 public int getJieguo() 13 { 14 if(jc1==0) 15 sum=1; 16 else{ 17 for(n=1;n<=jc1;n++) 18 { 19 sum=sum*n; 20 } 21 } 22 return sum; 23 } 24 public int getsum() 25 { 26 return sum; 27 } 28 }
标签:
原文地址:http://www.cnblogs.com/linhaixin/p/4468613.html