复数的类封装

编程练习:Ch4 复数的类封装

参考P111 4.18.1 有理数的类封装 完成 复数加减乘除四则运算的类封装,主类Main输入两个复数输出其四则运算后的结果。
如:输入 1 2 1 -2
输出 复数: 1.0+2.0i 与复数: 1.0-2.0i
相加得: 2.0
相减得: 4.0i
相乘得: 5.0
相除得: -0.6+0.8i

import java.util.Scanner;
class Fushu {//复数类,成员变量shibu,xubu分别存放复数实部和虚部的值
double shibu,xubu;
 String string;
public Fushu(double shibu,double xubu){//含参构造方法,指定复数的实部和虚部
	this.shibu=shibu;
	this.xubu=xubu;
}
public Fushu(){	
}
 
double getShibu() {
	return shibu;
}
double getXubu() {
	return xubu;
}

void shuChu() {
	if(shibu==0&&xubu!=0) 
		System.out.print(xubu+"i");	
	else if(xubu==0)
		System.out.print(shibu);	
	else if(xubu<0)
		System.out.print(shibu+""+xubu+"i");	
	else
	System.out.print(shibu+"+"+xubu+"i");
}

public String toStr() {
	if(shibu==0&&xubu!=0) 
		string=String.valueOf(xubu)+"i";	
	else if(xubu==0)
		string=String.valueOf(shibu);	
	else if(xubu<0)
		string=String.valueOf(shibu)+String.valueOf(xubu)+"i";	
	else
		string=String.valueOf(shibu)+"+"+String.valueOf(xubu)+"i";
	return string;
}

public Fushu add(Fushu r) {//加法运算
	double x=r.getShibu();
	double y=r.getXubu();
	double newShibu=shibu+x;
	double newXubu=xubu+y;
	Fushu result=new Fushu(newShibu,newXubu);
	return result;
}

public Fushu sub(Fushu r) {//减法运算
	double x=r.getShibu();
	double y=r.getXubu();
	double newShibu=shibu-x;
	double newXubu=xubu-y;
	Fushu result=new Fushu(newShibu,newXubu);
	return result;
}

public Fushu muti(Fushu r) {//乘法运算
	double x=r.getShibu();
	double y=r.getXubu();
	double newShibu=shibu*x-xubu*y;
	double newXubu=xubu*x+shibu*y;
	Fushu result=new Fushu(newShibu,newXubu);
	return result;
}

public Fushu div(Fushu r) {//除法运算
	double x=r.getShibu();
	double y=r.getXubu();
	double newShibu=(shibu*x+xubu*y)/(x*x+y*y);
	double newXubu=(xubu*x-shibu*y)/(x*x+y*y);
	Fushu result=new Fushu(newShibu,newXubu);
	return result;
}

}


public class Main {
	public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);    
		Fushu r1=new Fushu(sc.nextDouble(),sc.nextDouble());
		Fushu r2=new Fushu(sc.nextDouble(),sc.nextDouble());
    System.out.print("复数: ");
    r1.shuChu();
    System.out.print(" 与复数: ");
    r2.shuChu();
		System.out.print("\n相加得: ");
		Fushu Result=r1.add(r2);
		Result.shuChu();
		System.out.print("\n相减得: ");
		Result=r1.sub(r2);
		Result.shuChu();
		System.out.print("\n相乘得: ");
		Result=r1.muti(r2);
		Result.shuChu();
		System.out.print("\n相除得: ");
		Result=r1.div(r2);
		Result.shuChu();
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,以下是一个简单的Complex的定义和实现,可以完成基本的复数运算和出结果的封装: ```python class Complex: def __init__(self, real, imag): self.real = real self.imag = imag def __add__(self, other): return Complex(self.real + other.real, self.imag + other.imag) def __sub__(self, other): return Complex(self.real - other.real, self.imag - other.imag) def __mul__(self, other): return Complex(self.real * other.real - self.imag * other.imag, self.real * other.imag + self.imag * other.real) def __truediv__(self, other): denominator = other.real**2 + other.imag**2 return Complex((self.real*other.real + self.imag*other.imag)/denominator, (self.imag*other.real - self.real*other.imag)/denominator) def __str__(self): if self.imag >= 0: return f'{self.real}+{self.imag}i' else: return f'{self.real}{self.imag}i' ``` 这个有一个构造方法`__init__`,用来初始化复数的实部和虚部。还有四个运算符重载方法:`__add__`用来实现复数加法,`__sub__`用来实现复数减法,`__mul__`用来实现复数乘法,`__truediv__`用来实现复数除法。 最后,还有一个`__str__`方法,用来将复数出为字符串格式。该方法利用了Python中的f-string,可以方便地自定义出格式。 使用示例: ```python a = Complex(1, 2) b = Complex(2, -3) print(a + b) # 出:3-i print(a - b) # 出:-1+5i print(a * b) # 出:8-1i print(a / b) # 出:-0.4+0.8i ``` 希望这个例子能够帮到你!
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值