创建一个复数类并实现加减乘除运算

package exe01;

import java.util.Scanner;
/*name:Excise1                   功能:创建一个复数类并实现加减乘除运算
 */
public class first1 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		//创建类
		class Complex {
			//定义成员变量
			float RealPart;
			float ImaginaryPart;
			//构造函数
			Complex(float dbRealPart,float dbImaginaryPart) {                     
				this.RealPart=dbRealPart;
				this.ImaginaryPart=dbImaginaryPart;
			}
			
			
			
			private void Complex(float dbRealPart, float dbImaginaryPart) {       
				this.RealPart=dbRealPart;
				this.ImaginaryPart=dbImaginaryPart;
			}
			Complex() {                                                   
				Scanner sc=new Scanner(System.in );
				float RealPart=sc.nextFloat();
				float ImaginaryPart=sc.nextFloat();
				Complex(RealPart,ImaginaryPart);
			}
			
			
			public float getRealPart() {
				return RealPart;
			}
			public void setRealPart(float RealPart) {
				this.RealPart=RealPart;
			}
			public float getImaginaryPart() {
				return ImaginaryPart;
			}
			public void setImaginaryPart(float ImaginaryPart) {
				this.ImaginaryPart=ImaginaryPart;
			}
			
			Complex Add(Complex a) {                                               //加法运算
				float RealPart1=a.getRealPart();
				float ImaginaryPart1=a.getImaginaryPart();
				float newRealPart = RealPart+RealPart1;
				float newImaginaryPart = ImaginaryPart+ImaginaryPart1;
				Complex result = new Complex(newRealPart,newImaginaryPart);
				return result;
			}
			
			Complex Reduce(Complex a) {                                            //减法运算
				float RealPart1=a.getRealPart();
				float ImaginaryPart1=a.getImaginaryPart();
				float newRealPart = RealPart-RealPart1;
				float newImaginaryPart = ImaginaryPart-ImaginaryPart1;
				Complex result = new Complex(newRealPart,newImaginaryPart);
				return result;
			}
			Complex Multiply(Complex a) {                                               //乘法运算
				float RealPart1=a.getRealPart();
				float ImaginaryPart1=a.getImaginaryPart();
				float newRealPart = RealPart*RealPart1-ImaginaryPart*ImaginaryPart1;
				float newImaginaryPart = RealPart*ImaginaryPart1+ImaginaryPart*RealPart1;
				Complex result = new Complex(newRealPart,newImaginaryPart);
				return result;
			}
			Complex Divide(Complex a) {                                               //除法运算
				float RealPart1=a.getRealPart();
				float ImaginaryPart1=a.getImaginaryPart();
				float newRealPart = (RealPart*RealPart1+ImaginaryPart*ImaginaryPart1)/(RealPart1*RealPart1+ImaginaryPart1*ImaginaryPart1);
				float newImaginaryPart = (ImaginaryPart*RealPart1-RealPart*ImaginaryPart1)/(RealPart1*RealPart1+ImaginaryPart1*ImaginaryPart1);
				Complex result = new Complex(newRealPart,newImaginaryPart);
				return result;
			}
			public void printf() {                                                    //输出结果
				System.out.println("("+RealPart+","+ImaginaryPart+")");
			}
			
		}
		
		
		
		
		
		
		
		      System.out.println("请输入第一个复数的实部和虚部:");
              Complex first = new Complex();
              System.out.println("请输入第二个复数的实部和虚部:");
              Complex second = new Complex();
              
              Complex result_Add = first.Add(second);                    //是第一个对第二个的操作
              Complex result_Reduce = first.Reduce(second);
              Complex result_Multiply = first.Multiply(second);
              Complex result_Divide = first.Divide(second);
              
              System.out.println("两个复数相加后:");
              result_Add.printf();
              System.out.println("两个复数相减后:");
              result_Reduce.printf();
              System.out.println("两个复数相乘后:");
              result_Multiply.printf();
              System.out.println("两个复数相除后:");
              result_Divide.printf();
	}

}/*
 * 法二:
 * Complex Add(Complex a,Complex b) {                                               //加法运算
	//float RealPart1=a.getRealPart();
	//float ImaginaryPart1=a.getImaginaryPart();
	float newRealPart = a.RealPart+b.RealPart;
	float newImaginaryPart = a.ImaginaryPart+b.ImaginaryPart;
	Complex result = new Complex(newRealPart,newImaginaryPart);
	return result;
}
 * 
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值