复数的编程题
⑴ 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(); //输出这个复数
}