重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
public class ComplexNumber {
我们提供的服务有:成都网站建设、做网站、微信公众号开发、网站优化、网站认证、太谷ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的太谷网站制作公司
/**
* @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);//自己化简
}
}
1、real和image这两个field前面的static去掉。
2、public Complex() 这个构造器去掉,如果要接受输入的话,应该放到main方法里,这样这个类更清晰。
3、静态方法Complex_add和Complex_minus没指定返回值类型,应该返回的是Complex。另外方法名字首字母应小写。
4、参考这个:
public class Complex {
/**成员属性定义实部和虚部
*@param real_part 实部
*@param imagin_part 虚部
*/
private double real_part;
private double imagin_part;
//构造方法,初始化
public Complex() {
this(0.0,0.0);
}
//构造方法的重载
public Complex(double real_part, double imagin_part) {
this.real_part = real_part;
this.imagin_part = imagin_part;
}
//取得实部
public void getReal_part(double real_part) {
this.real_part = real_part;
}
//取得虚部
public void getImagin_part() {
this.imagin_part = imagin_part;
}
//复数自加
public Complex addComplex(Complex c) {
this.real_part += c.real_part;
this.imagin_part += c.imagin_part;
return this;
}
//复数自减
public Complex decComplex(Complex c) {
this.real_part -= c.real_part;
this.imagin_part -= c.imagin_part;
return this;
}
//定义输出格式
public String toString() {
//按照虚部正负,不同显示
if(this.imagin_part =0)
return this.real_part + "+" + this.imagin_part + "i";
return this.real_part + "-" + this.imagin_part + "i";
}
}
//测试类
class TestComplex {
public static void main(String[] args) {
//一个c1复数对象
Complex c1 = new Complex(1.0,1.0);
//输出自加结果
c1.addComplex(c1);
System.out.println(c1.toString());
//输出自减结果
c1.decComplex(c1);
System.out.println(c1.toString());
}
}
(1):具体代码(附注释)
复数类:
public class Complex {
private float shibu;
private float xubu;
Complex()
{this(0,0);
}
Complex(float shibu,float xubu){
this.shibu=shibu;
this.xubu=xubu;
}
public void Add(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu+p.shibu;
result.xubu=this.xubu+p.xubu;
System.out.print("加法结果为:"+result.shibu+"+"+result.xubu+"i");
}
public void Sub(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu-p.shibu;
result.xubu=this.xubu-p.xubu;
System.out.print("加法结果为:"+result.shibu+"+"+result.xubu+"i");
}
public void Mul(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu*p.shibu-this.xubu*p.xubu;
result.xubu=this.shibu*p.xubu+p.shibu*this.xubu;
System.out.print("乘法结果为:"+result.shibu+"+"+result.xubu+"i");
}
public static void main(String[] args) {
Complex fushu1=new Complex(1,2);
Complex fushu2=new Complex(3,4);
fushu1.Add(fushu2);
fushu1.Sub(fushu2);
fushu1.Mul(fushu2);
}
}
(2):提供一个例子:
源代码:
import java.io.*;
public class Book{
double sb;
double xb;
Book(double x,double y){
this.sb=x;
this.xb=y;
}
Book(){
}
public static void main(String args[]){
System.out.println("请输入数据:");
double a=0;
double b=0;
double c=0;
double d=0;
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入第一个复述的实部:");
try{
s = in.readLine();
a=Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第一个复述的虚部:");
try{
s = in.readLine();
b =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第二个复述的实部:");
try{
s = in.readLine();
c =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第二个复述的虚部:");
try{
s = in.readLine();
d =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
Book h;
h=new Book(a,b);
Book j;
j=new Book(c,d);
System.out.println("您输入的一个数为:");
toString(h);
System.out.println("您输入的二个数为:");
toString(j);
Book k;
k=new Book();
char z='y';
do{
System.out.println("请选择您要进行的计算:");
System.out.println("1 :进行加法运算");
System.out.println("2 :进行减法运算");
System.out.println("3 :进行修改");
System.out.println("4 :进行乘法运算");
System.out.println("5 :进行除法运算");
System.out.println("6 :查看修改结果");
int i=0;
try{
i= Integer.parseInt(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
switch(i)
{
case 1:
k.sb=jia(h.sb,j.sb);
k.xb=jia(h.xb,j.xb);
System.out.println("计算结果的实部为:"+k.sb);
System.out.println("计算结果的虚部为:"+k.xb);
toString(k);
break ;
case 2:
k.sb=jian(h.sb,j.sb);
k.xb=jian(h.xb,j.xb);
System.out.println("计算结果的实部为:"+k.sb);
System.out.println("计算结果的虚部为:"+k.xb);
toString(k);
break ;
case 3:
System.out.println("请输入您要修改哪个实数:");
int l=0;
try{
l= Integer.parseInt(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
if(l==1)
{
h.xiugais(h);
h.xiugaix(h);
}
else
{
xiugais(j);
xiugaix(j);
}
break ;
case 4:
double f=0;
double e=0;
f=cheng(h.sb,j.sb)+cheng(h.xb,j.xb);
e=cheng(h.sb,j.xb)+cheng(h.xb,j.sb);
k.sb=(double)(Math.round(f*100)/100.0);
k.xb=(double)(Math.round(e*100)/100.0);
System.out.println("计算结果的实部为:"+k.sb);
System.out.println("计算结果的虚部为:"+k.xb);
toString(k);
break ;
case 5:
double chushu=cheng(j.sb,j.sb)-cheng(j.xb,-j.xb);
double beichushus=jian(cheng(h.sb,j.sb),cheng(h.xb,-j.xb));
double beichushux=jia(cheng(h.sb,-j.xb),cheng(h.xb,j.sb));
k.sb=chu(beichushus,chushu);
k.xb=chu(beichushux,chushu);
System.out.println("计算结果的实部为:"+k.sb);
System.out.println("计算结果的虚部为:"+k.xb);
toString(k);
break ;
case 6:
System.out.println("修改后的结果为:");
System.out.println("第一个复数:"+toString(h));
System.out.println("第二个复数:"+toString(j));
break ;
}
System.out.println("请问您是否还要继续 y/n:");
try{
z=(char)System.in.read();
System.in.skip(2); //忽略回车换行
}
catch(IOException e){}
} while(z=='y');
}
public static double gets(Book a){
return a.sb;
}
public static double getx(Book b){
return b.xb;
}
public static double xiugais(Book a)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入您要修改的实部:");
double m=0;
try{
m= Double.parseDouble(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
a.sb=m;
System.out.println("修改成功:");
return 0;
}
public static double xiugaix(Book b)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入您要修改的虚部:");
double n=0;
try{
n= Double.parseDouble(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
b.xb=n;
System.out.println("修改成功:");
return 0;
}
public static double jia(double a,double b)//
{
double c=0;
c=a+b;
System.out.println("加法成功:");
return c ;
}
public static double jian(double a,double b)
{
double c=0;
c=a-b;
System.out.println("减法成功:");
return c;
}
public static double cheng(double a,double b)
{
double c=0;
c=a*b;
System.out.println("乘法成功:");
return c;
}
public static double chu(double a,double b)
{
double d=0;
double c=0;
d=a/b;
c=(double)(Math.round(d*100)/100.0);
System.out.println("除法成功:");
return c ;
}
public static double toString(Book a){
System.out.println("结果为:"+a.sb+"+"+a.xb+"*i");
return 0;
}
}
(3)测试结果截图:
例如:ai+b ci+d
加法:int a,b,c,d;int x,y; x=a+c; y=b+d
结果就为:xi+y 这很简单 再输出的时候要变为字符串型 这样你就可以再x后加个i 形成xi的形式了
减法: int a,b,c,d;int x,y; x=a-c; y=b-d
结果就为:xi+y 这很简单 再输出的时候要变为字符串型 这样你就可以再x后加个i 形成xi的形式了
乘法: int a,b,c,d;int x,y;
x=a*d+b*c y=b*d-a*c
结果就为:xi+y 这很简单 再输出的时候要变为字符串型 这样你就可以再x后加个i 形成xi的形式了