重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.util.ArrayList;
成都创新互联公司-专业网站定制、快速模板网站建设、高性价比子洲网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式子洲网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖子洲地区。费用合理售后完善,10年实体公司更值得信赖。
import java.util.List;
import java.util.Scanner;
/**
* @author young
* @Description: TODO
* @date 2016年5月21日下午5:03:16
*/
interface MyComparable {
public int myCompareTo(Complex c);
}
public class Complex implements MyComparable {
private double re;// 实部
private double im;// 虚部
private double mc;// 模长
public Complex() {
super();
}
public Complex(double re, double im) {
super();
this.re = re;
this.im = im;
}
public double getRe() {
return re;
}
public void setRe(double re) {
this.re = re;
}
public double getIm() {
return im;
}
public void setIm(double im) {
this.im = im;
}
//根据实部和虚部计算出复数的模长
public double getMod() {
return Double.valueOf(String.format("%.6f", Math.sqrt(this.re * this.re + this.im * this.im)));
}
public double getMc() {
return mc;
}
public void setMc(double mc) {
this.mc = mc;
}
@Override
//实现接口中的方法,根据模长来比较复数
public int myCompareTo(Complex c) {
// TODO Auto-generated method stub
if (this.mc c.mc) {
return -1;
} else if (this.mc c.mc) {
return 1;
} else {
return 0;
}
}
public String toString(){
return String.valueOf(this.re) + "+" + String.valueOf(this.im) + "i" + " =模长: " + this.getMod();
}
//测试结果
public static void main(String[] args) {
ListComplex lists = new ArrayListComplex();
double[] mod = new double[5];
Scanner scanner = new Scanner(System.in);
int number = 0;
double m ;
while(number 5){
Complex c = new Complex();
System.out.print("输入第["+ (number + 1) +"]个数的实部:");
c.setRe(scanner.nextDouble());
System.out.print("输入第["+ (number + 1) +"]个数的虚部:");
c.setIm(scanner.nextDouble());
m = c.getMod();
c.setMc(m);
mod[number] = m;
lists.add(c);
number++;
}
System.out.println("\n冒泡排序(从大到小): ");
for (int k = 0; k mod.length - 1; k++) {
for (int j = k + 1; j mod.length; j++) { // 升序把改成
if (mod[k] mod[j]) {
double temp = mod[k];
mod[k] = mod[j];
mod[j] = temp;
}
}
}
//排序后输出
for(int i = 0; i mod.length; i++){
for(int j = 0; j lists.size(); j++){
if(mod[i] == lists.get(j).getMc()){
System.out.println(lists.get(j).toString());
}
}
}
}
}
import java.util.*;
public class ComplexTest{
static class ComplexNumber{
private double real,image;
public ComplexNumber(){
this(0.0,0.0);}
public ComplexNumber(double a,double b){
real=a;image=b;
}
public ComplexNumber add(ComplexNumber x){
return new ComplexNumber(real+x.real,image+x.image);}
public ComplexNumber sub(ComplexNumber x){
return new ComplexNumber(real-x.real,image-x.image); }
public ComplexNumber mul(ComplexNumber x){
return new ComplexNumber(real*x.real-image*x.image,real*x.image+image*x.real);}
public ComplexNumber div(ComplexNumber x){
if(x.real==0x.image==0){
System.out.println("无法进行除法!");
return new ComplexNumber();}
else return new ComplexNumber((real*x.real+image*x.image)/(x.real*x.real+x.image*x.image)
,(image*x.real-real*x.image)/(x.real*x.real+x.image*x.image));}
public double getReal (){return real;}
public double getImage (){return image;}
public void show(){System.out.println(this.toString());}
public String toString(){
if(image0)return ""+real+image+"i";
else return ""+real+"+"+image+"i";
}
}
static class Test{
public Test(){
Scanner sr=new Scanner(System.in);
ComplexNumber a,b,c;
try{System.out.println("请输入第一个实部和虚部:");
a=new ComplexNumber(sr.nextDouble(),sr.nextDouble());
System.out.println("请输入第二个实部和虚部:");
b=new ComplexNumber(sr.nextDouble(),sr.nextDouble());
System.out.print("第一个复数:");a.show();
System.out.print("第二个复数:");b.show();
c=a.add(b);
System.out.print("两个复数的和:");c.show();
c=a.sub(b);
System.out.print("两个复数的差:");c.show();
c=a.mul(b);
System.out.print("两个复数的积:");c.show();
c=a.div(b);
System.out.print("两个复数的商:");c.show();
}
catch(InputMismatchException e){
System.out.println("输入有误!");}
}
}
public static void main(String[] args){
new Test();
}
}
publicclassComplexDemo{//main方法publicstaticvoidmain(String[]a){Complexb=newComplex(2,5);Complexc=newComplex(3,-4);System.out.println(b+"+"+c+"="+b.add(c));System.out.println(b+"-"+c+"="+b.minus(c));System.out.println(b+"*"+c+"="+b.multiply(c));System.out.println(b+"/"+c+"="+b.divide(c));}}//Complex类classComplex{privatedoublem;//实部privatedoublen;//虚部publicComplex(doublem,doublen){this.m=m;this.n=n;}//addpublicComplexadd(Complexc){returnnewComplex(m+c.m,n+c.n);}//minuspublicComplexminus(Complexc){returnnewComplex(m-c.m,n-c.n);}//multiplypublicComplexmultiply(Complexc){returnnewComplex(m*c.m-n*c.n,m*c.n+n*c.m);}//dividepublicComplexdivide(Complexc){doubled=Math.sqrt(c.m*c.m)+Math.sqrt(c.n*c.n);returnnewComplex((m*c.m+n*c.n)/d,Math.round((m*c.n-n*c.m)/d));}publicStringtoString(){Stringrtr_str="";if(n0)rtr_str="("+m+"+"+n+"i"+")";if(n==0)rtr_str="("+m+")";if(n0)rtr_str="("+m+n+"i"+")";returnrtr_str;}}