當前位置:首頁 » 編程軟體 » 復數的編程題

復數的編程題

發布時間: 2022-07-31 14:58:44

⑴ VC++編程:復數的四則運算

#include <iostream.h>

class complex
{
public:
complex() { real=imag=0; }
complex(double r, double i)
{
real = r, imag = i;
}
complex operator +(const complex &c);
complex operator -(const complex &c);
complex operator *(const complex &c);
complex operator /(const complex &c);
friend void print(const complex &c);
private:
double real, imag;
};

inline complex complex::operator +(const complex &c)
{
return complex(real + c.real, imag + c.imag);
}

inline complex complex::operator -(const complex &c)
{
return complex(real - c.real, imag - c.imag);
}

inline complex complex::operator *(const complex &c)
{
return complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}

inline complex complex::operator /(const complex &c)
{
return complex((real * c.real + imag + c.imag) / (c.real * c.real + c.imag * c.imag),
(imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));
}

void print(const complex &c)
{
if(c.imag<0)
cout<<c.real<<c.imag<<'i';
else
cout<<c.real<<'+'<<c.imag<<'i';
}

void main()
{
complex c1(2.0, 3.0), c2(4.0, -2.0), c3;
c3 = c1 + c2;
cout<<"\nc1+c2=";
print(c3);
c3 = c1 - c2;
cout<<"\nc1-c2=";
print(c3);
c3 = c1 * c2;
cout<<"\nc1*c2=";
print(c3);
c3 = c1 / c2;
cout<<"\nc1/c2=";
print(c3);
c3 = (c1+c2) * (c1-c2) * c2/c1;
cout<<"\n(c1+c2)*(c1-c2)*c2/c1=";
print(c3);
cout<<endl;
}

⑵ C++復數類編程題

別懶了,這么簡單的,自己寫吧

⑶ 3. 根據下面的要求編程實現復數類ComplexNumber。 (1)復數類的屬性包括實數部分和虛數部分 (2)復數類

public class Test
{
public static void main(String args[])
{

ComplexNumber cNumber_1 =
new ComplexNumber(3,-5);
ComplexNumber
cNumber_2 =
new ComplexNumber(2,2);
double d = 10.0;

System.out.println(cNumber_1.toString() + " 加 "
+ cNumber_2.toString() + "
等於 "
+ cNumber_1.complexAdd(cNumber_2).toString());

System.out.println(cNumber_1.toString() + " 加 "
+ d + " 等於 "
+
cNumber_1.complexAdd(d).toString());
System.out.println();

System.out.println(cNumber_1.toString() + " 減 "
+ cNumber_2.toString() + "
等於 "
+ cNumber_1.complexMinus(cNumber_2).toString());

System.out.println(cNumber_1.toString() + " 減 "
+ d + " 等於 "
+
cNumber_1.complexMinus(d).toString());
System.out.println();

System.out.println(cNumber_1.toString() + " 乘 "
+ cNumber_2.toString() + "
等於 "
+ cNumber_1.complexMulti(cNumber_2).toString());

System.out.println(cNumber_1.toString() + " 乘 "
+ d + " 等於 "
+
cNumber_1.complexMulti(d).toString());
System.out.println();

System.out.println(cNumber_1.toString() + " 除 "
+ cNumber_2.toString() +
"等於"
+ cNumber_1.complexDivision(cNumber_2).toString());

System.out.println(cNumber_1.toString() + " 除 "
+ d + " 等於 "
+
cNumber_1.complexDivision(d).toString());
}
}
class
ComplexNumber
{
//域
private double m_dRealPart;
private double
m_dImaginPart;
//構造方法
ComplexNumber()
{
m_dRealPart = 0.0;

m_dImaginPart = 0.0;
}
ComplexNumber(double r,double i)
{

m_dRealPart = r;
m_dImaginPart = i;
}
ComplexNumber(ComplexNumber
c)
{
m_dRealPart = c.getRealPart();
m_dImaginPart =
c.getImaginaryPart();
}
//get,set方法
double getRealPart()
{

return m_dRealPart;
}
double getImaginaryPart()
{
return
m_dImaginPart;
}
void setRealPart(double d)
{
m_dRealPart =
d;
}
void setImaginaryPart(double d)
{
m_dImaginPart =
d;
}
//復數運算方法
//加
ComplexNumber complexAdd(ComplexNumber
c)
{
return new ComplexNumber(
this.m_dRealPart +
c.getRealPart(),
this.m_dImaginPart +
c.getImaginaryPart());
}
ComplexNumber complexAdd(double c)
{

return new ComplexNumber(
this.m_dRealPart + c,
this.m_dImaginPart);
}
//減
ComplexNumber complexMinus(ComplexNumber
c)
{
return new ComplexNumber(
this.m_dRealPart -
c.getRealPart(),
this.m_dImaginPart -
c.getImaginaryPart());
}
ComplexNumber complexMinus(double c)
{

return new ComplexNumber(
this.m_dRealPart - c,
this.m_dImaginPart);
}
//乘
ComplexNumber complexMulti(ComplexNumber
c)
{
return new ComplexNumber(
this.m_dRealPart * c.getRealPart()

- this.m_dImaginPart * c.getImaginaryPart(),
this.m_dRealPart *
c.getImaginaryPart()
+ this.m_dImaginPart *
c.getRealPart());
}
ComplexNumber complexMulti(double c)
{
return
new ComplexNumber(
this.m_dRealPart * c, this.m_dImaginPart *
c);
}
//除
ComplexNumber complexDivision(ComplexNumber c){
return
new ComplexNumber((this.m_dRealPart*c.getRealPart()

+this.m_dImaginPart*c.getImaginaryPart())

/(c.getRealPart()*c.getRealPart()+c.getImaginaryPart()*c.getImaginaryPart()),

(this.m_dImaginPart*c.getRealPart()

-this.m_dRealPart*c.getImaginaryPart())

/(c.getRealPart()*c.getRealPart()+c.getImaginaryPart()*c.getImaginaryPart()));

}
ComplexNumber complexDivision(double c){
return new
ComplexNumber(this.m_dRealPart/c,this.m_dImaginPart/c);
}
//toString()
public
String toString()
{
return "(" + m_dRealPart + " + "
+
m_dImaginPart + " i" + ")";
}
}

⑷ c++程序設計題(復數類設計與實現)

好麻煩的
第一章 緒論

1-4.什麼是抽象數據類型?試用C++的類聲明定義「復數」的抽象數據類型。要求
(1) 在復數內部用浮點數定義它的實部和虛部。
(2) 實現3個構造函數:預設的構造函數沒有參數;第二個構造函數將雙精度浮點數賦給復數的實部,虛部置為0;第三個構造函數將兩個雙精度浮點數分別賦給復數的實部和虛部。
(3) 定義獲取和修改復數的實部和虛部,以及+、-、*、/等運算的成員函數。
(4) 定義重載的流函數來輸出一個復數。
【解答】
抽象數據類型通常是指由用戶定義,用以表示應用問題的數據模型。抽象數據類型由基本的數據類型構成,並包括一組相關的服務。

//在頭文件complex.h中定義的復數類
#ifndef _complex_h_
#define _complex_h_
#include <iostream.h>

class comlex {
public:
complex ( ){ Re = Im = 0; } //不帶參數的構造函數
complex ( double r ) { Re = r; Im = 0; } //只置實部的構造函數
complex ( double r, double i ) { Re = r; Im = i; } //分別置實部、虛部的構造函數
double getReal ( ) { return Re; } //取復數實部
double getImag ( ) { return Im; } //取復數虛部
void setReal ( double r ) { Re = r; } //修改復數實部
void setImag ( double i ) { Im = i; } //修改復數虛部
complex & operator = ( complex & ob) { Re = ob.Re; Im = ob.Im; } //復數賦值
complex & operator + ( complex & ob ); //重載函數:復數四則運算
complex & operator – ( complex & ob );
complex & operator * ( complex & ob );
complex & operator / ( complex & ob );
friend ostream & operator << ( ostream & os, complex & c ); //友元函數:重載<<
private:
double Re, Im; //復數的實部與虛部
};
#endif

//復數類complex的相關服務的實現放在C++源文件complex.cpp中
#include <iostream.h>
#include <math.h>
#include 「complex.h」
complex & complex :: operator + ( complex & ob ) {
//重載函數:復數加法運算。
complex * result = new complex ( Re + ob.Re, Im + ob.Im );
return *result;
}
complex & complex :: operator – ( complex & ob ) {
//重載函數:復數減法運算
complex *result = new complex ( Re – ob.Re, Im – ob.Im );
return * result;
}
complex & complex :: operator * ( complex & ob ) {
//重載函數:復數乘法運算
complex *result =
new complex ( Re * ob.Re – Im * ob.Im, Im * ob.Re + Re * ob.Im );
return *result;
}
complex & complex :: operator / ( complex & ) {
//重載函數:復數除法運算
double d = ob.Re * ob.Re + ob.Im * ob.Im;
complex * result = new complex ( ( Re * ob.Re + Im * ob.Im ) / d,
( Im * ob. Re – Re * ob.Im ) / d );
return * result;
}
friend ostream & operator << ( ostream & os, complex & ob ) {
//友元函數:重載<<,將復數ob輸出到輸出流對象os中。
return os << ob.Re << ( ob.Im >= 0.0 ) ? 「+」 : 「-」 << fabs ( ob.Im ) << 「i」;
}

⑸ c++習題編程 1.定義和實現一個復數類ClsComplex,並完成如下的功能要求:

第1題.

#include<iostream>
usingnamespacestd;

classClsComplex{
private:
doublereal;
doubleimg;
public:
ClsComplex(){
real=0;
img=0;
}
ClsComplex(doubler,doublei){
real=r;
img=i;
}
voidshow(){
cout<<real<<''<<img<<endl;
}
};

第二題:

#include<iostream>
usingnamespacestd;

classClsComplex{
private:
doublereal;
doubleimg;
public:
ClsComplex(){
real=0;
img=0;
}
ClsComplex(doubler,doublei){
real=r;
img=i;
}
voidshow(){
cout<<real<<''<<img<<endl;
}
frienddoubleabs(ClsComplexa,ClsComplexb);
};
doubleabs(ClsComplexa,ClsComplexb){
returnsqrt((a.real-b.real)*(a.real-b.real)+(a.img-b.img)*(a.img-b.img));
}
voidmain(){
ClsComplexc1,c2(3,4);
cout<<abs(c1,c2);

}
第三題:
#include<iostream>
usingnamespacestd;
classShape{
public:
virtualfloatperi()=0;
};
classRect:publicShape{
private:
floatwidth;
floatlength;
public:
floatperi(){
return2*(width+length);
}
Rect(floatw,floatl){
width=w;
length=l;
}
};
classCircle:publicShape{
private:
floatr;
public:
floatperi(){
return2*(3.1415926*r);
}
};
voidmain(){
Rectr(1,1);
Shape*p=&r;
cout<<p->peri();
}

⑹ 如果對的我追加200分,一個關於Java復數類的編程題

稍微改了你下,你寫得不錯

public class Number {
private double a; // 實部
private double b; // 虛部

public Number() {
}

// 可以看成僅為實數
// 因為它只有虛部為0
public Number(double a) {
this.a = a;
this.b = 0.0;// 其實不寫也OK
}

public Number(double a, double b) {
this.a = a;
this.b = b;
}

public double getA() // 獲取屬性
{
return a;
}

public double getB() {
return b;
}

public void setA(double a) // 修改屬性
{
this.a = a;
}

public void setB(double b) {
this.b = b;
}

public Number add(Number num) // 相加
{
// 如果你在此定義一個新對象,為何不它返回呢?
Number n = new Number();
n.a = a + num.a;
n.b = b + num.b;
return n;
}

public Number subtract(Number num) // 相減
{
Number n = new Number();
n.a = a - num.a;
n.b = b - num.b;
return n;
}

/**
* 重寫toString方法,省得你列印時還要寫那麼多
*
*/
public String toString() {
if (b == 0) {
return String.valueOf(a);
} else if (a == 0) {
return String.valueOf(b) + "i";
} else {
return a + " + " + b + "i";
}
}

public static void main(String[] args) {
Number n1 = new Number(1, 1);
Number n2 = new Number(1, 1);
System.out.println("n1為:\t" + n1);
System.out.println("n2為:\t" + n2);

System.out.println("相加結果為:" + n1.add(n2));
System.out.println("相減結果為:" + n1.subtract(n2));
}
}

c語言編程題 求復數相乘

#include<stdio.h>
typedef
struct
complex
{
float
re;
float
im;
}comp;
comp
mut
(comp,comp);
void
print_out(comp);
void
main()
{comp
x,y,z;
printf("輸入2個復數
\n");
printf("輸入第一個復數
a+bi:");
scanf("%f+%fi",&x.re,&x.im);
printf("輸入第二個復數
x+yi:");
scanf("%f+%fi",&y.re,&y.im);
z=mut
(x,y);
print_out(z);
}
comp
mut
(comp
x,comp
y)
//多了分號,下面的函數也是
{
comp
z;
z.re=x.re*y.re-x.im*y.im;
z.im=x.re*y.im+x.im*y.re;
return
z;
}
void
print_out(comp
z)
{
printf("%.2f",z.re);
if(z.im>0)printf("+%.2fi\n",z.im);
else
printf("-%.2fi\n",-z.im);
}

⑻ C++編程題 定義並實現一個復數類Complex,使得下面的代碼能夠工作

#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
public:
Complex();
Complex(float,float);
//Complex(Complex &Complex); //這個拷貝構造函數你下面沒有定義 對象名不能與類名相同,改成小寫
Complex(Complex &complex);
void put_real1(float); //獲得復數的實部
void put_imaginary1(float); //獲得復數的虛部 put應該是用於輸出函數名的,嚴格來說應用Set_,你自己改吧
void out_real2(); //輸出復數的實部 你的函數名都有同名的,編譯器怎麼區分?加個1和2
void out_imaginary2(); //輸出復數的虛部
void out_size(); //輸出復數的模
void out_view(); //輸出復數的輻角
void out_it(); //輸出這個復數
private:
float real,imaginary;
};Complex::Complex()
{
real=0;
imaginary=0;
}Complex::Complex(float a,float b)
{
//a=real;
//b=imaginary; 賦值錯誤,改為:
real=a;
imaginary=b;
}Complex::Complex(Complex &complex) //拷貝構造函數定義
{
real=complex.real;
imaginary=complex.imaginary;
}
/*void Complex::put_real()
{
cout<<"請輸入復數的實部"<<endl;

cin>>real;}void Complex::put_imaginary(){
cout<<"請輸入復數的虛部"<<endl;

cin>>imaginary;
}
void Complex::out_real()
{
cout<<"復數的實部為:"<<real<<endl;
} void Complex::out_imaginary()
{
cout<<"復數的虛部為:"<<imaginary<<endl;
}*/ //這4個函數跟下面的重復了,是多餘的

void Complex::put_real1(float a)
{
//cout<<"請輸入復數的實部"<<endl;
//cin>>a>>endl; // cin不需要換行符endl
real=a; //用戶的輸入可以放到主函數比較簡單,這里只需要獲得輸入就行了
} void Complex::put_imaginary1(float b)
{
//cout<<"請輸入復數的實部"<<endl;
//cin>>b>>endl; // cin不需要換行符endl
imaginary=b; //用戶的輸入可以放到主函數比較簡單,這里只需要獲得輸入就行了
} //這兩個成員函數跟你在類里聲明的不一樣,你聲明的是無參的,定義就不能有參數,類里幫你改成有參數的了 void Complex::out_real2(){ //不需要參數,類里你聲明的就是無參的
cout<<"復數的實部為:"<<real<<endl;
} void Complex::out_imaginary2() //不需要參數
{
cout<<"復數的虛部為:"<<imaginary<<endl;
}void Complex::out_size()
{ cout<<"該復數的模為:"<<sqrt(real*real+imaginary*imaginary)<<endl;
}void Complex::out_view()
{
cout<<"該復數的輻角為:"<<atan(real/imaginary)<<endl; //修改後的

}//void Complex::out_it(float a,float b) 這句也是一樣,定義與聲明不同
void Complex::out_it()
{
cout<<"復數="<<real<<"+"<<imaginary<<"i"<<endl;
} void main()
{
//float a=0,b=0; 不需要賦值
float a,b;
//Complex(); 沒有對象
Complex Com1; //定義對象Com1的同時自動調用構造函數
cout<<"初始化的復數1為:";
Com1.out_it();
//Complex(float,float); 實參都沒有,怎麼傳值給形參。實參不需要數據類型,只要一個具體的數
Complex Com2(1.0,2.0); //調用帶參數的構造函數
cout<<"初始化的復數2為:";
Com2.out_it();
//Complex( &Complex); 你應該給一個具體的對象讓它拷貝
Complex Com3(Com2); //拷貝對象Com2
cout<<"拷貝復數2的復數為:";
Com3.out_it();
//Complex Com;Complex
cout<<"請您輸入一個復數的實部和虛部:"<<endl;
cin>>a>>b;
Complex Com4;
cout<<"您輸入的復數信息如下:"<<endl;
Com4.put_real1(a); //取得復數的實部
Com4.put_imaginary1(b); //取得復數的虛部
Com4.out_real2(); //輸出復數的實部
Com4.out_imaginary2(); //輸出復數的虛部
Com4.out_size(); //輸出復數的模
Com4.out_view(); //輸出復數的輻角
Com4.out_it(); //輸出這個復數
}

熱點內容
艾莫迅plc編程電纜 發布:2025-03-15 20:44:05 瀏覽:301
妖妖靈腳本 發布:2025-03-15 20:36:56 瀏覽:254
公司自己搭建ftp 發布:2025-03-15 20:36:07 瀏覽:61
如何增加配置使半袖變得不單調 發布:2025-03-15 20:33:37 瀏覽:349
linux顯示目錄 發布:2025-03-15 20:30:42 瀏覽:660
素數演算法表示 發布:2025-03-15 20:24:02 瀏覽:842
大話西遊手游怎麼看伺服器等級 發布:2025-03-15 20:21:53 瀏覽:221
rsa加密c源代碼 發布:2025-03-15 19:53:55 瀏覽:693
linux解壓bin 發布:2025-03-15 19:40:25 瀏覽:384
存儲數據為什麼只能使用兩種狀態 發布:2025-03-15 19:40:21 瀏覽:264