1.运用java 编写一个复数类,有实部和虚部,并实现复数的加减乘除运算?
1个回答
展开全部
public class ComplexNumber {
/**
* @param args
*/
int shi,xu;//复数的实部和虚部
public ComplexNumber(int n,int ni){
shi = n;
xu = ni;
}
public void ComplexShow(){
String output = "";
output+=shi;
if(xu>=0){
output+="+";
}
output+=xu;
output+="i";
System.out.println(output);
}
public void ComplexShow1(){//不要换行
String output = "";
output+=shi;
if(xu>=0){
output+="+";
}
output+=xu;
output+="i";
System.out.print(output);
}
public static void ComplexAdd(ComplexNumber x1,ComplexNumber x2){//实现两个复数相加
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi + x2.shi;
cn.xu = x1.xu + x2.xu;
cn.ComplexShow();
}
public static void ComplexMinus(ComplexNumber x1,ComplexNumber x2){//实现两个复数相减,第一个数减第二个数
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi - x2.shi;
cn.xu = x1.xu - x2.xu;
cn.ComplexShow();
}
public static void ComplexMultiply(ComplexNumber x1,ComplexNumber x2){//实现两个复数相乘
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi * x2.shi - x1.xu * x2.xu;
cn.xu = x1.xu * x2.shi + x2.xu * x1.shi;
cn.ComplexShow();
}
public static void ComplexDivide(ComplexNumber x1,ComplexNumber x2){//实现两个复数相除,第一个数除以第二个数
ComplexNumber x2_gong = new ComplexNumber(x2.shi,0-x2.xu);//求被除数的共轭复数
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi * x2_gong.shi - x1.xu * x2_gong.xu;//x1/x2,求分子实部
cn.xu = x1.xu * x2_gong.shi + x2_gong.xu * x1.shi;//x1/x2,求分子虚部
int fenMu = x2.shi * x2.shi + x2.xu * x2.xu;
if(fenMu!=0){
System.out.print("(");
cn.ComplexShow1();
System.out.print(")");
System.out.println("/"+fenMu);
}
else
System.out.println("分母为零,无法相除");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ComplexNumber cn = new ComplexNumber(-1, -1);//初始化复数
cn.ComplexShow();//显示复数
ComplexNumber c1 = new ComplexNumber(-1, -1);
ComplexNumber c2 = new ComplexNumber(1, 1);
System.out.print("加:");
ComplexAdd(c1, c2);
System.out.print("减:");
ComplexMinus(c1, c2);
System.out.print("乘:");
ComplexMultiply(c1, c2);
System.out.print("除:");
ComplexDivide(c1, c2);//自己化简
}
}
/**
* @param args
*/
int shi,xu;//复数的实部和虚部
public ComplexNumber(int n,int ni){
shi = n;
xu = ni;
}
public void ComplexShow(){
String output = "";
output+=shi;
if(xu>=0){
output+="+";
}
output+=xu;
output+="i";
System.out.println(output);
}
public void ComplexShow1(){//不要换行
String output = "";
output+=shi;
if(xu>=0){
output+="+";
}
output+=xu;
output+="i";
System.out.print(output);
}
public static void ComplexAdd(ComplexNumber x1,ComplexNumber x2){//实现两个复数相加
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi + x2.shi;
cn.xu = x1.xu + x2.xu;
cn.ComplexShow();
}
public static void ComplexMinus(ComplexNumber x1,ComplexNumber x2){//实现两个复数相减,第一个数减第二个数
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi - x2.shi;
cn.xu = x1.xu - x2.xu;
cn.ComplexShow();
}
public static void ComplexMultiply(ComplexNumber x1,ComplexNumber x2){//实现两个复数相乘
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi * x2.shi - x1.xu * x2.xu;
cn.xu = x1.xu * x2.shi + x2.xu * x1.shi;
cn.ComplexShow();
}
public static void ComplexDivide(ComplexNumber x1,ComplexNumber x2){//实现两个复数相除,第一个数除以第二个数
ComplexNumber x2_gong = new ComplexNumber(x2.shi,0-x2.xu);//求被除数的共轭复数
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi * x2_gong.shi - x1.xu * x2_gong.xu;//x1/x2,求分子实部
cn.xu = x1.xu * x2_gong.shi + x2_gong.xu * x1.shi;//x1/x2,求分子虚部
int fenMu = x2.shi * x2.shi + x2.xu * x2.xu;
if(fenMu!=0){
System.out.print("(");
cn.ComplexShow1();
System.out.print(")");
System.out.println("/"+fenMu);
}
else
System.out.println("分母为零,无法相除");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ComplexNumber cn = new ComplexNumber(-1, -1);//初始化复数
cn.ComplexShow();//显示复数
ComplexNumber c1 = new ComplexNumber(-1, -1);
ComplexNumber c2 = new ComplexNumber(1, 1);
System.out.print("加:");
ComplexAdd(c1, c2);
System.out.print("减:");
ComplexMinus(c1, c2);
System.out.print("乘:");
ComplexMultiply(c1, c2);
System.out.print("除:");
ComplexDivide(c1, c2);//自己化简
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询