复数可以写成A+Bi的形式,其中A是实部,B是虚部。从键盘输入两个复数,做加法或乘法并输出结果。

例如

输入:4.2 2.5 2 -1.5 +

输出:6.2 1.0

输入:2.5 3 -1 2.4 *

输出:-9.7 3.0

输出运算结果,并保留1位小数。 

PS:复数的四则运算法则如下:

1. 加法:将两个复数的实部相加,虚部相加,得到结果的实部和虚部。 (a + bi) + (c + di) = (a + c) + (b + d)i。

2. 乘法:将两个复数的实部与虚部进行乘法运算,并将结果相加得到新的实部,然后将实部与两个复数的虚部进行乘法运算,将结果相加得到新的虚部。 (a + bi) * (c + di) = (ac - bd) + (ad + bc)i。

3. 除法:将除数乘以分母的共轭,得到新的分子,然后将分子的实部除以分母的实部作为新的实部,将分子的虚部除以分母的实部作为新的虚部。 (a + bi) / (c + di) = (a + bi) * (c - di) / (c + di) * (c - di) = (ac + bd) / (c^2 + d^2) + (bc - ad)i / (c^2 + d^2)。

#include<stdio.h>

int main(){
	float a1,a2;
	float b1,b2;
	float c1,c2;
	char opr;
	scanf("%f %f %f %f",&a1,&a2,&b1,&b2);
	getchar();
	scanf("%c",&opr);
	if(opr=='+')
	{
		c1=a1+b1;
		c2=a2+b2;
	}
	else if(opr=='*')
	{
		c1=a1*b1-a2*b2;
		c2=a1*b2+a2*b1;
	}
	printf("%.1f %.1f",c1,c2);
}

运行结果:

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 复数类complex可以包含以下属性和方法: 属性: - 部(real):表示复数部,类型为double。 - 部(imaginary):表示复数部,类型为double。 方法: - 构造方法(Complex):用于创建复数对象,可以接受部和部作为参数。 - 加法方法(add):用于将两个复数相加,返回一个新的复数对象。 - 减法方法(subtract):用于将两个复数相减,返回一个新的复数对象。 - 乘法方法(multiply):用于将两个复数相乘,返回一个新的复数对象。 - 除法方法(divide):用于将两个复数相除,返回一个新的复数对象。 - 模长方法(abs):用于计算复数的模长,返回一个double类型的值。 - toString方法:用于将复数对象转换为字符串表示。 下面是一个简单的复数类complex的现: public class Complex { private double real; private double imaginary; public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public Complex add(Complex other) { double real = this.real + other.real; double imaginary = this.imaginary + other.imaginary; return new Complex(real, imaginary); } public Complex subtract(Complex other) { double real = this.real - other.real; double imaginary = this.imaginary - other.imaginary; return new Complex(real, imaginary); } public Complex multiply(Complex other) { double real = this.real * other.real - this.imaginary * other.imaginary; double imaginary = this.real * other.imaginary + this.imaginary * other.real; return new Complex(real, imaginary); } public Complex divide(Complex other) { double denominator = other.real * other.real + other.imaginary * other.imaginary; double real = (this.real * other.real + this.imaginary * other.imaginary) / denominator; double imaginary = (this.imaginary * other.real - this.real * other.imaginary) / denominator; return new Complex(real, imaginary); } public double abs() { return Math.sqrt(real * real + imaginary * imaginary); } @Override public String toString() { return real + (imaginary < ? "-" : "+") + Math.abs(imaginary) + "i"; } } 使用示例: Complex c1 = new Complex(1, 2); Complex c2 = new Complex(3, -4); Complex sum = c1.add(c2); Complex difference = c1.subtract(c2); Complex product = c1.multiply(c2); Complex quotient = c1.divide(c2); System.out.println("c1 = " + c1); System.out.println("c2 = " + c2); System.out.println("c1 + c2 = " + sum); System.out.println("c1 - c2 = " + difference); System.out.println("c1 * c2 = " + product); System.out.println("c1 / c2 = " + quotient); System.out.println("|c1| = " + c1.abs()); System.out.println("|c2| = " + c2.abs()); ### 回答2: 复数是由数和数构的数字,可以用a+bi形式表示,其中a和b分别为数和数部分。Java中可以设计一个复数类Complex来表示复数,该类应包含以下员变量和方法。 员变量: - 数部分real,用double类型保存 - 数部分imaginary,用double类型保存 方法: - 构造方法Complex(double real, double imaginary):创建一个给定数和数部分的复数对象 - 无参构造方法Complex():创建一个值为0的复数对象 - getter和setter方法:获取和设置各员变量的值 - add方法:复数加法,将当前复数对象与给定复数相加并返回结果 - sub方法:复数减法,将当前复数对象与给定复数相减并返回结果 - mul方法:复数乘法,将当前复数对象与给定复数相乘并返回结果 - div方法:复数除法,将当前复数对象除以给定复数并返回结果 - abs方法:计算复数的模,即|a+bi| = √(a^2+b^2),返回double类型的结果 - toString方法:将复数对象转换字符串形式,例如"3.0+4.0i" 其中,加法、减法、乘法、除法的运算规则如下: - 加法:(a+bi)+(c+di) = (a+c)+(b+d)i - 减法:(a+bi)-(c+di) = (a-c)+(b-d)i - 乘法:(a+bi)*(c+di) = (ac-bd)+(ad+bc)i - 除法:(a+bi)/(c+di) = (ac+bd)/(c^2+d^2) + (bc-ad)/(c^2+d^2)i 复数类Complex可以提供一些基本的应用场景,例如: - 在图像处理中,使用复数表示频域中的信号,进行傅里叶变换等操作 - 在电路分析中,使用复数表示电压、电流等复杂的信号,并进行相位和幅度等计算 - 在计算物理学中,使用复数表示波包、波函数等量,进行量子力学计算等 总之,Java的复数类Complex可以用于各种需要表示复数的领域,具有广泛的应用价值。 ### 回答3: Java是一种非常流行的编程语言,因其独特的面向对象设计理念而备受欢迎。面向对象思想强调数据和操作的结合,将现世界中的概念和体抽象类(class),并通过类来描述对象(object)。在这种模式下,为了对复数进行计算,可以设计一个复数类。 复数是由部和部组的数,可以用双精度浮点数表示。我们可以首先定义复数类的员变量,包括部real和部imaginary: ``` public class Complex { private double real; private double imaginary; } ``` 接着,我们需要为复数类设计一些方法来现计算。对于复数的加、减、乘、除等操作,都是基于部和部的,因此我们需要考虑如何现这些操作。例如,对于两个复数加法,可以按照如下方式现: ``` public Complex add(Complex number) { double realSum = real + number.real; double imaginarySum = imaginary + number.imaginary; return new Complex(realSum, imaginarySum); } ``` 这里我们定义了一个add方法,输入为另一个复数对象number,返回一个新的复数对象,其部为两个部之和,部为两个部之和。 类似地,我们还可以设计subtract、multiply、divide等方法来复数的减法、乘法、除法等操作。总体设计如下: ``` public class Complex { private double real; private double imaginary; public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public Complex add(Complex number) { double realSum = real + number.real; double imaginarySum = imaginary + number.imaginary; return new Complex(realSum, imaginarySum); } public Complex subtract(Complex number) { double realDiff = real - number.real; double imaginaryDiff = imaginary - number.imaginary; return new Complex(realDiff, imaginaryDiff); } public Complex multiply(Complex number) { double realProd = real * number.real - imaginary * number.imaginary; double imaginaryProd = real * number.imaginary + imaginary * number.real; return new Complex(realProd, imaginaryProd); } public Complex divide(Complex number) { double denominator = number.real * number.real + number.imaginary * number.imaginary; double realQuot = (real * number.real + imaginary * number.imaginary) / denominator; double imaginaryQuot = (imaginary * number.real - real * number.imaginary) / denominator; return new Complex(realQuot, imaginaryQuot); } } ``` 以上是一个简单的复数类的设计,可以用于现常见的复数计算。总的来说,Java提供了丰富的面向对象的特性,可以让我们按照不同的需求设计出各种不同的类。复数类只是其中的一种,践中还有许多其他有趣的类的设计。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值