首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >复数的运算 (类和对象) ( SDUT3336 )

复数的运算 (类和对象) ( SDUT3336 )

作者头像
Lokinli
发布2023-03-09 19:05:36
发布2023-03-09 19:05:36
28500
代码可运行
举报
文章被收录于专栏:以终为始以终为始
运行总次数:0
代码可运行

 说明:没有测试,仅仅用于复习。

复数的运算(类和对象)

Problem Description

设计一个类Complex,用于封装对复数的下列操作:

成员变量:实部real,虚部image,均为整数变量;

构造方法:无参构造方法、有参构造方法(参数2个)

成员方法:含两个复数的加、减、乘操作。

    复数相加举例: (1+2i)+(3+4i)= 4 + 6i

    复数相减举例: (1+2i)-(3+4i)= -2 - 2i

    复数相乘举例: (1+2i)*(3+4i)= -5 + 10i

要求:对复数进行连环运算。


代码语言:javascript
代码运行次数:0
运行
复制
class Complex{
	int real,image;
	Complex(){
		
	}
	Complex(int real, int image){
		this.real = real;
		this.image = image;
	}
	public int getReal(){
		return real;
	}
	public int getImage(){
		return image;
	}
	public Complex getSum(Complex a){
		int R = a.getReal();
		int I = a.getImage();
		int newR = real + R;
		int newI = image + I;
		Complex res = new Complex(newR, newI);
		return res;
	}
	public Complex getSub(Complex a){
		int R = a.getReal();
		int I = a.getImage();
		int newR = real + R;
		int newI = real + I;
		Complex res = new Complex(newR,newI);
		return res;
	}
	public Complex getMul(Complex a){
		int R = a.getReal();
		int I = a.getImage();
		int newR = real * R - I * image;
		int newI = image * R + real * I;
		Complex res = new Complex(newR,newI);
		return res;
	}
	public String toString(){
		DecimalFormat ze = new DecimalFormat("#0");
		String str;
		if(image > 0){
			str = ze.format(real) + "+" + ze.format(image) + "i";
		}
		else if(image < 0) {
			str = ze.format(real)+ "" + ze.format(image) + "i";
		}
		else {
			str = ze.format(real);
		}
		return str;
	}
}

Input

输入有多行。 第一行有两个整数,代表复数X的实部和虚部。 后续各行的第一个和第二个数表示复数Y的实部和虚部,第三个数表示操作符op: 1——复数X和Y相加;2——复数X和Y相减;3——复数X和Y相乘。

当输入0 0 0时,结束运算,输出结果。

Output

输出一行。

第一行有两个整数,代表复数的实部和虚部。

Sample Input

代码语言:javascript
代码运行次数:0
运行
复制
1 1
3 4 2
5 2 1
2 -1 3
0 2 2
0 0 0

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
5 -7

代码语言:javascript
代码运行次数:0
运行
复制
package aa;

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int x,y,z;
		x = sc.nextInt();
		y = sc.nextInt();
		Complex com1 = new Complex(x,y);
		while(sc.hasNext()){
			x = sc.nextInt();
			y = sc.nextInt();
			z = sc.nextInt();
			if(x == 0 && y == 0 && z == 0){
				String str = com1.toString();
				System.out.println(str);
			}
			Complex com2 = new Complex(x,y);
			if(z == 1){
				com1 = com1.getSum(com2);
			}else if(z==2){
				com1 = com1.getSub(com2);
			}else if(z==3){
				com1 = com1.getMul(com2);
			}
		}
	}
}

class Complex{
	int real,image;
	Complex(){
		
	}
	Complex(int real, int image){
		this.real = real;
		this.image = image;
	}
	public int getReal(){
		return real;
	}
	public int getImage(){
		return image;
	}
	public Complex getSum(Complex a){
		int R = a.getReal();
		int I = a.getImage();
		int newR = real + R;
		int newI = image + I;
		Complex res = new Complex(newR, newI);
		return res;
	}
	public Complex getSub(Complex a){
		int R = a.getReal();
		int I = a.getImage();
		int newR = real + R;
		int newI = real + I;
		Complex res = new Complex(newR,newI);
		return res;
	}
	public Complex getMul(Complex a){
		int R = a.getReal();
		int I = a.getImage();
		int newR = real * R - I * image;
		int newI = image * R + real * I;
		Complex res = new Complex(newR,newI);
		return res;
	}
	public String toString(){
		DecimalFormat ze = new DecimalFormat("#0");
		String str;
		if(image > 0){
			str = ze.format(real) + "+" + ze.format(image) + "i";
		}
		else if(image < 0) {
			str = ze.format(real)+ "" + ze.format(image) + "i";
		}
		else {
			str = ze.format(real);
		}
		return str;
	}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 复数的运算(类和对象)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档