本文目錄一覽:
java中如何實現複數的加減?
不知道是不是 ~
//複數類。
public class Complex
{
private double real,im; //實部,虛部
public Complex(double real, double im) //構造方法
{
this.real = real;
this.im = im;
}
public Complex(double real) //構造方法重載
{
this(real,0);
}
public Complex()
{
this(0,0);
}
public Complex(Complex c) //拷貝構造方法
{
this(c.real,c.im);
}
public boolean equals(Complex c) //比較兩個對象是否相等
{
return this.real==c.real this.im==c.im;
}
public String toString()
{
return “(“+this.real+”+”+this.im+”i)”;
}
public void add(Complex c) //兩個對象相加
{ //改變當前對象,沒有返回新對象
this.real += c.real;
this.im += c.im;
}
public Complex plus(Complex c) //兩個對象相加,與add()方法參數一樣不能重載
{ //返回新創建對象,沒有改變當前對象
return new Complex(this.real+c.real, this.im+c.im);
}
public void subtract(Complex c) //兩個對象相減
{ //改變當前對象,沒有返回新對象
this.real -= c.real;
this.im -= c.im;
}
public Complex minus(Complex c) //兩個對象相減,與subtract()方法參數一樣不能重載
{ //返回新創建的對象,沒有改變當前對象
return new Complex(this.real-c.real, this.im-c.im);
}
}
class Complex__ex
{
public static void main(String args[])
{
Complex a = new Complex(1,2);
Complex b = new Complex(3,5);
Complex c = a.plus(b); //返回新創建對象
System.out.println(a+” + “+b+” = “+c);
}
}
/*
程序運行結果如下:
(1.0+2.0i) + (3.0+5.0i) = (40.0+7.0i)
*/
java 編寫一個可對複數進行加減運算的程序
1、real和image這兩個field前面的static去掉。
2、public Complex() 這個構造器去掉,如果要接受輸入的話,應該放到main方法里,這樣這個類更清晰。
3、靜態方法Complex_add和Complex_minus沒指定返回值類型,應該返回的是Complex。另外方法名字首字母應小寫。
4、參考這個:
1.運用java 編寫一個複數類,有實部和虛部,並實現複數的加減乘除運算?
public class ComplexNumber {
/**
* @param args
*/
int shi,xu;//複數的實部和虛部
public ComplexNumber(int n,int ni){
shi = n;
xu = ni;
}
public void ComplexShow(){
String output = “”;
output+=shi;
if(xu=0){
output+=”+”;
}
output+=xu;
output+=”i”;
System.out.println(output);
}
public void ComplexShow1(){//不要換行
String output = “”;
output+=shi;
if(xu=0){
output+=”+”;
}
output+=xu;
output+=”i”;
System.out.print(output);
}
public static void ComplexAdd(ComplexNumber x1,ComplexNumber x2){//實現兩個複數相加
ComplexNumber cn = new ComplexNumber(0, 0);//將兩個複數相加等於cn
cn.shi = x1.shi + x2.shi;
cn.xu = x1.xu + x2.xu;
cn.ComplexShow();
}
public static void ComplexMinus(ComplexNumber x1,ComplexNumber x2){//實現兩個複數相減,第一個數減第二個數
ComplexNumber cn = new ComplexNumber(0, 0);//將兩個複數相加等於cn
cn.shi = x1.shi – x2.shi;
cn.xu = x1.xu – x2.xu;
cn.ComplexShow();
}
public static void ComplexMultiply(ComplexNumber x1,ComplexNumber x2){//實現兩個複數相乘
ComplexNumber cn = new ComplexNumber(0, 0);//將兩個複數相加等於cn
cn.shi = x1.shi * x2.shi – x1.xu * x2.xu;
cn.xu = x1.xu * x2.shi + x2.xu * x1.shi;
cn.ComplexShow();
}
public static void ComplexDivide(ComplexNumber x1,ComplexNumber x2){//實現兩個複數相除,第一個數除以第二個數
ComplexNumber x2_gong = new ComplexNumber(x2.shi,0-x2.xu);//求被除數的共軛複數
ComplexNumber cn = new ComplexNumber(0, 0);//將兩個複數相加等於cn
cn.shi = x1.shi * x2_gong.shi – x1.xu * x2_gong.xu;//x1/x2,求分子實部
cn.xu = x1.xu * x2_gong.shi + x2_gong.xu * x1.shi;//x1/x2,求分子虛部
int fenMu = x2.shi * x2.shi + x2.xu * x2.xu;
if(fenMu!=0){
System.out.print(“(“);
cn.ComplexShow1();
System.out.print(“)”);
System.out.println(“/”+fenMu);
}
else
System.out.println(“分母為零,無法相除”);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ComplexNumber cn = new ComplexNumber(-1, -1);//初始化複數
cn.ComplexShow();//顯示複數
ComplexNumber c1 = new ComplexNumber(-1, -1);
ComplexNumber c2 = new ComplexNumber(1, 1);
System.out.print(“加:”);
ComplexAdd(c1, c2);
System.out.print(“減:”);
ComplexMinus(c1, c2);
System.out.print(“乘:”);
ComplexMultiply(c1, c2);
System.out.print(“除:”);
ComplexDivide(c1, c2);//自己化簡
}
}
java 中實現複數的加減
(1):具體代碼(附註釋)
複數類:
public class Complex {
private float shibu;
private float xubu;
Complex()
{this(0,0);
}
Complex(float shibu,float xubu){
this.shibu=shibu;
this.xubu=xubu;
}
public void Add(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu+p.shibu;
result.xubu=this.xubu+p.xubu;
System.out.print(“加法結果為:”+result.shibu+”+”+result.xubu+”i”);
}
public void Sub(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu-p.shibu;
result.xubu=this.xubu-p.xubu;
System.out.print(“加法結果為:”+result.shibu+”+”+result.xubu+”i”);
}
public void Mul(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu*p.shibu-this.xubu*p.xubu;
result.xubu=this.shibu*p.xubu+p.shibu*this.xubu;
System.out.print(“乘法結果為:”+result.shibu+”+”+result.xubu+”i”);
}
public static void main(String[] args) {
Complex fushu1=new Complex(1,2);
Complex fushu2=new Complex(3,4);
fushu1.Add(fushu2);
fushu1.Sub(fushu2);
fushu1.Mul(fushu2);
}
}
(2):提供一個例子:
源代碼:
import java.io.*;
public class Book{
double sb;
double xb;
Book(double x,double y){
this.sb=x;
this.xb=y;
}
Book(){
}
public static void main(String args[]){
System.out.println(“請輸入數據:”);
double a=0;
double b=0;
double c=0;
double d=0;
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“請輸入第一個複述的實部:”);
try{
s = in.readLine();
a=Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
System.out.println(“請輸入第一個複述的虛部:”);
try{
s = in.readLine();
b =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
System.out.println(“請輸入第二個複述的實部:”);
try{
s = in.readLine();
c =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
System.out.println(“請輸入第二個複述的虛部:”);
try{
s = in.readLine();
d =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
Book h;
h=new Book(a,b);
Book j;
j=new Book(c,d);
System.out.println(“您輸入的一個數為:”);
toString(h);
System.out.println(“您輸入的二個數為:”);
toString(j);
Book k;
k=new Book();
char z=’y’;
do{
System.out.println(“請選擇您要進行的計算:”);
System.out.println(“1 :進行加法運算”);
System.out.println(“2 :進行減法運算”);
System.out.println(“3 :進行修改”);
System.out.println(“4 :進行乘法運算”);
System.out.println(“5 :進行除法運算”);
System.out.println(“6 :查看修改結果”);
int i=0;
try{
i= Integer.parseInt(in.readLine());
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
switch(i)
{
case 1:
k.sb=jia(h.sb,j.sb);
k.xb=jia(h.xb,j.xb);
System.out.println(“計算結果的實部為:”+k.sb);
System.out.println(“計算結果的虛部為:”+k.xb);
toString(k);
break ;
case 2:
k.sb=jian(h.sb,j.sb);
k.xb=jian(h.xb,j.xb);
System.out.println(“計算結果的實部為:”+k.sb);
System.out.println(“計算結果的虛部為:”+k.xb);
toString(k);
break ;
case 3:
System.out.println(“請輸入您要修改哪個實數:”);
int l=0;
try{
l= Integer.parseInt(in.readLine());
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
if(l==1)
{
h.xiugais(h);
h.xiugaix(h);
}
else
{
xiugais(j);
xiugaix(j);
}
break ;
case 4:
double f=0;
double e=0;
f=cheng(h.sb,j.sb)+cheng(h.xb,j.xb);
e=cheng(h.sb,j.xb)+cheng(h.xb,j.sb);
k.sb=(double)(Math.round(f*100)/100.0);
k.xb=(double)(Math.round(e*100)/100.0);
System.out.println(“計算結果的實部為:”+k.sb);
System.out.println(“計算結果的虛部為:”+k.xb);
toString(k);
break ;
case 5:
double chushu=cheng(j.sb,j.sb)-cheng(j.xb,-j.xb);
double beichushus=jian(cheng(h.sb,j.sb),cheng(h.xb,-j.xb));
double beichushux=jia(cheng(h.sb,-j.xb),cheng(h.xb,j.sb));
k.sb=chu(beichushus,chushu);
k.xb=chu(beichushux,chushu);
System.out.println(“計算結果的實部為:”+k.sb);
System.out.println(“計算結果的虛部為:”+k.xb);
toString(k);
break ;
case 6:
System.out.println(“修改後的結果為:”);
System.out.println(“第一個複數:”+toString(h));
System.out.println(“第二個複數:”+toString(j));
break ;
}
System.out.println(“請問您是否還要繼續 y/n:”);
try{
z=(char)System.in.read();
System.in.skip(2); //忽略回車換行
}
catch(IOException e){}
} while(z==’y’);
}
public static double gets(Book a){
return a.sb;
}
public static double getx(Book b){
return b.xb;
}
public static double xiugais(Book a)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“請輸入您要修改的實部:”);
double m=0;
try{
m= Double.parseDouble(in.readLine());
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
a.sb=m;
System.out.println(“修改成功:”);
return 0;
}
public static double xiugaix(Book b)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“請輸入您要修改的虛部:”);
double n=0;
try{
n= Double.parseDouble(in.readLine());
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
b.xb=n;
System.out.println(“修改成功:”);
return 0;
}
public static double jia(double a,double b)//
{
double c=0;
c=a+b;
System.out.println(“加法成功:”);
return c ;
}
public static double jian(double a,double b)
{
double c=0;
c=a-b;
System.out.println(“減法成功:”);
return c;
}
public static double cheng(double a,double b)
{
double c=0;
c=a*b;
System.out.println(“乘法成功:”);
return c;
}
public static double chu(double a,double b)
{
double d=0;
double c=0;
d=a/b;
c=(double)(Math.round(d*100)/100.0);
System.out.println(“除法成功:”);
return c ;
}
public static double toString(Book a){
System.out.println(“結果為:”+a.sb+”+”+a.xb+”*i”);
return 0;
}
}
(3)測試結果截圖:
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/312775.html