3. 根据下面的要求编程实现复数类ComplexNumber。 (1)复数类的属性包括实数部分和虚数部分 (2)复数类

.根据下面的要求编程实现复数类ComplexNumber。(1)复数类的属性包括实数部分和虚数部分(2)复数类的方法有加法add,减法subtract并编程验证以上的复数... . 根据下面的要求编程实现复数类ComplexNumber。
(1)复数类的属性包括实数部分和虚数部分
(2)复数类的方法有加法add,减法subtract
并编程验证以上的复数类。接收用户输入的复数的实部和虚部,计算复数与复数、复数与实数的加、减的操作。
展开
 我来答
怒飞
推荐于2017-12-16 · 超过11用户采纳过TA的回答
知道答主
回答量:103
采纳率:100%
帮助的人:22万
展开全部
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" + ")";
}
}
bestlanzi
2011-05-01
知道答主
回答量:3
采纳率:0%
帮助的人:0
展开全部
用c++语言的代码表示如下(visual studio 2008编译成功):
#include<iostream>
using namespace std;

class complex_number
{
public:
complex_number(void);
double result_add;
double result_sub;
void Function_add(complex_number);
void Function_sub(complex_number);

double shibu;
double xubu;
};
complex_number::complex_number()
{
result_add=0;
result_sub=0;
shibu=0;
xubu=0;
}
void complex_number::Function_add(complex_number numberB)
{
cout<<"+结果是:"<<shibu+numberB.shibu<<" "<<xubu+numberB.xubu<<"i"<<endl;
}
void complex_number::Function_sub(complex_number numberB)
{
cout<<"—结果是:"<<shibu-numberB.shibu<<" "<<xubu-numberB.xubu<<"i"<<endl;
}

int main()
{
complex_number number1;
complex_number number2;
cout<<"请输入虚数1的实部";
double a;
cin>>a;
number1.shibu=a;

cout<<"请输入虚数1的虚部";
double b;
cin>>b;
number1.xubu=b;

cout<<"请输入虚数2的实部";
double c;
cin>>c;
number2.shibu=c;

cout<<"请输入虚数2的虚部";
double d;
cin>>d;
number2.xubu=d;

number1.Function_add (number2);
number1.Function_sub (number2);

return 0;
}
追问
用java怎么写呢?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
13573437306
2011-05-04
知道答主
回答量:9
采纳率:0%
帮助的人:0
展开全部
I went there on sunday.I spent four hours there.I saw lots of books.Where di
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
小贝格
2011-04-30 · TA获得超过664个赞
知道答主
回答量:429
采纳率:0%
帮助的人:132万
展开全部
1
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式