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());
}
}
public class Complex {
private Integer realPart;
private Integer imaginPart;
public Complex(){
this.realPart = 0;
this.imaginPart = 0;
}
public Complex(int r,int i){
this.realPart = r;
this.imaginPart = i;
}
public Complex complexAdd(Complex a){
Complex c = new Complex();
c.setImaginPart((this.imaginPart+a.imaginPart)%10);
c.setRealPart(this.realPart+a.realPart+(this.imaginPart+a.imaginPart)/10);
return c;
}
public String toString(){
return this.realPart+"+"+this.imaginPart+"i";
}
public Integer getRealPart() {
return realPart;
}
public void setRealPart(Integer realPart) {
this.realPart = realPart;
}
public Integer getImaginPart() {
return imaginPart;
}
public void setImaginPart(Integer imaginPart) {
this.imaginPart = imaginPart;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Complex c1 = new Complex(1,20); //1+2i
Complex c2 = new Complex(3,40); //3+4i
System.out.println(c1.toString()+" + "+c2.toString()+" = "+c1.complexAdd(c2).toString());
}
}
public class Complex {
protected int a;
protected int b;
public Complex(int a, int b) {
this.a = a;
this.b = b;
}
public String toString() {
return this.a + " + " + this.b + "i";
}
public static Complex addition(Complex complex1, Complex complex2) {
int a = complex1.a + complex2.a;
int b = complex1.b + complex2.b;
return new Complex(a, b);
}
public static Complex subtract(Complex complex1, Complex complex2) {
int a = complex1.a - complex2.a;
int b = complex1.b - complex2.b;
return new Complex(a, b);
}
public static Complex multiplication(Complex complex1, Complex complex2) {
int a = complex1.a * complex2.a - complex1.b * complex2.b;
int b = complex1.b * complex2.a + complex1.a * complex2.b;
return new Complex(a, b);
}
public static Complex division(Complex complex1, Complex complex2) throws Exception {
if (complex2.a == 0) {
throw new Exception("complex2.a is 0");
}
if (complex2.b == 0) {
throw new Exception("complex2.b is 0");
}
int a = (complex1.a * complex2.a + complex1.b * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
int b = (complex1.b * complex2.a - complex1.a * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
return new Complex(a, b);
}
}
//測試用的類
public class ComplexTest {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Complex complex1 = new Complex(1, 2);
Complex complex2 = new Complex(3, 4);
System.out.println(complex1 + " + " + complex2 + " = " + Complex.addition(complex1, complex2));
System.out.println(complex1 + " - " + complex2 + " = " + Complex.subtract(complex1, complex2));
System.out.println(complex1 + " x " + complex2 + " = " + Complex.multiplication(complex1, complex2));
System.out.println(complex1 + " / " + complex2 + " = " + Complex.division(complex1, complex2));
}
}
import java.util.*;
public class ComplexTest{
static class ComplexNumber{
private double real,image;
public ComplexNumber(){
this(0.0,0.0);}
public ComplexNumber(double a,double b){
real=a;image=b;
}
public ComplexNumber add(ComplexNumber x){
return new ComplexNumber(real+x.real,image+x.image);}
public ComplexNumber sub(ComplexNumber x){
return new ComplexNumber(real-x.real,image-x.image); }
public ComplexNumber mul(ComplexNumber x){
return new ComplexNumber(real*x.real-image*x.image,real*x.image+image*x.real);}
public ComplexNumber div(ComplexNumber x){
if(x.real==0x.image==0){
System.out.println("無法進行除法!");
return new ComplexNumber();}
else return new ComplexNumber((real*x.real+image*x.image)/(x.real*x.real+x.image*x.image)
,(image*x.real-real*x.image)/(x.real*x.real+x.image*x.image));}
public double getReal (){return real;}
public double getImage (){return image;}
public void show(){System.out.println(this.toString());}
public String toString(){
if(image0)return ""+real+image+"i";
else return ""+real+"+"+image+"i";
}
}
static class Test{
public Test(){
Scanner sr=new Scanner(System.in);
ComplexNumber a,b,c;
try{System.out.println("請輸入第一個實部和虛部:");
a=new ComplexNumber(sr.nextDouble(),sr.nextDouble());
System.out.println("請輸入第二個實部和虛部:");
b=new ComplexNumber(sr.nextDouble(),sr.nextDouble());
System.out.print("第一個復數:");a.show();
System.out.print("第二個復數:");b.show();
c=a.add(b);
System.out.print("兩個復數的和:");c.show();
c=a.sub(b);
System.out.print("兩個復數的差:");c.show();
c=a.mul(b);
System.out.print("兩個復數的積:");c.show();
c=a.div(b);
System.out.print("兩個復數的商:");c.show();
}
catch(InputMismatchException e){
System.out.println("輸入有誤!");}
}
}
public static void main(String[] args){
new Test();
}
}
網站題目:java編寫復數的代碼 java編寫復數類
瀏覽地址:http://m.newbst.com/article22/doojgcc.html
成都網站建設公司_創新互聯,為您提供微信小程序、企業建站、App開發、營銷型網站建設、品牌網站制作、關鍵詞優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯