问题描述
写一个复数类,实现(1)构造函数重载(三种:缺省,实部+虚部,幅角+模)(2)实现+,-,×,/运算,尤其是注意/运算分母不等于零,包含和整数,和浮点数,和复数相乘的情况。(3)定义>,<,==,!=等(4)实现实数到复数的转换(显式,隐式),实现复数到实数的转换(显式,隐式),如果可以的话,如果不可以请显示错误信息。请各位大侠,帮帮忙!!!
解决方案
解决方案二:
精华区有一个
解决方案三:
谢谢过楼上的了,去看了一下,不过都是用VB编的,我以前没有学过VB,不知道用C#怎么编写?
解决方案四:
publicstructComplex{publicintreal;publicintimaginary;publicComplex(intreal,intimaginary)//constructor{this.real=real;this.imaginary=imaginary;}//Declarewhichoperatortooverload(+),//thetypesthatcanbeadded(twoComplexobjects),//andthereturntype(Complex):publicstaticComplexoperator+(Complexc1,Complexc2){returnnewComplex(c1.real+c2.real,c1.imaginary+c2.imaginary);}//OverridetheToString()methodtodisplayacomplexnumberinthetraditionalformat:publicoverridestringToString(){return(System.String.Format("{0}+{1}i",real,imaginary));}}classTestComplex{staticvoidMain(){Complexnum1=newComplex(2,3);Complexnum2=newComplex(3,4);//AddtwoComplexobjectsthroughtheoverloadedplusoperator:Complexsum=num1+num2;//PrintthenumbersandthesumusingtheoverridenToStringmethod:System.Console.WriteLine("Firstcomplexnumber:{0}",num1);System.Console.WriteLine("Secondcomplexnumber:{0}",num2);System.Console.WriteLine("Thesumofthetwonumbers:{0}",sum);}}//一个简单的复数类。。
解决方案五:
帮顶一下,这个应该不难,不过得花点时间,我现在做项目遇到问题了暂时没空.如果问题解决了帮你写出来
解决方案六:
学习
解决方案七:
(4)实现实数到复数的转换(显式,隐式),实现复数到实数的转换(显式,隐式),如果可以的话,如果不可以请显示错误信息。这一步怎么实现呢?还是不会啊
解决方案八:
好贴支持ing....
解决方案九:
还有大侠在指点一下吗?
解决方案十:
usingSystem;usingSystem.Collections.Generic;usingSystem.Windows.Forms;namespace复数{publicstructComplex{publicdoublereal;publicdoubleimaginary;publicComplex(doublereal,doubleimaginary){this.real=real;this.imaginary=imaginary;}publicstaticComplexoperator+(Complexc1,Complexc2)//重载"+"运算符{returnnewComplex(c1.real+c2.real,c1.imaginary+c2.imaginary);}publicstaticComplexoperator-(Complexc1,Complexc2)//重载"-"运算符{returnnewComplex(c1.real-c2.real,c1.imaginary-c2.imaginary);}publicstaticComplexoperator*(Complexc1,Complexc2)//重载"*"运算符{returnnewComplex((c1.real*c2.real-c1.imaginary*c2.imaginary),(c1.real*c2.imaginary+c2.real*c1.imaginary));}publicstaticComplexoperator/(Complexc1,Complexc2)//重载"/"运算符{returnnewComplex((c1.real*c2.real+c1.imaginary*c2.imaginary)/(c2.real*c2.real+c2.imaginary*c2.imaginary),(c2.real*c1.imaginary-c1.real*c2.imaginary)/(c2.real*c2.real+c2.imaginary*c2.imaginary));}publicstaticbooloperator>(Complexc1,Complexc2)//重载">"运算符{if(c1.real>c2.real)returntrue;elsereturnfalse;}publicstaticbooloperator<(Complexc1,Complexc2){if(c1.real<c2.real)returntrue;elsereturnfalse;}publicstaticbooloperator==(Complexc1,Complexc2)//重载"<"运算符{if((c1.real==c2.real)&&(c1.imaginary==c2.imaginary))returntrue;elsereturnfalse;}publicstaticbooloperator!=(Complexc1,Complexc2){if((c1.real==c2.real)&&(c1.imaginary==c2.imaginary))returnfalse;elsereturntrue;}publicoverridestringToString(){return(String.Format("{0}+{1}I",real,imaginary));}///<summary>///应用程序的主入口点。///</summary>publicstaticvoidMain(){Complexvalue1=newComplex(4,3);Complexvalue2=newComplex(5,4);Complexsum=value1+value2;Console.WriteLine("第一个复数是:{0}",value1);Console.WriteLine("第二个复数是:{0}",value2);Console.WriteLine("两个复数的和是:{0}",sum);}}}这是自己编的,怎么也运行不出来,不知道问题出在哪里??????????
解决方案十一:
publicstructComplex{publicintreal;publicintimaginary;publicComplex(intreal,intimaginary)//constructor{this.real=real;this.imaginary=imaginary;}publicstaticComplexoperator+(Complexc1,Complexc2){returnnewComplex(c1.real+c2.real,c1.imaginary+c2.imaginary);}//OverridetheToString()methodtodisplayacomplexnumberinthetraditionalformat:publicoverridestringToString(){return(System.String.Format("{0}+{1}i",real,imaginary));}}classTestComplex{staticvoidMain(){Complexnum1=newComplex(2,3);Complexnum2=newComplex(3,4);//AddtwoComplexobjectsthroughtheoverloadedplusoperator:Complexsum=num1+num2;//PrintthenumbersandthesumusingtheoverridenToStringmethod:System.Console.WriteLine("Firstcomplexnumber:{0}",num1);System.Console.WriteLine("Secondcomplexnumber:{0}",num2);System.Console.WriteLine("Thesumofthetwonumbers:{0}",sum);}}
解决方案十二:
实现实数到复数的转换(显式,隐式),实现复数到实数的转换(显式,隐式),如果可以的话,如果不可以请显示错误信息。这一步怎么实现呢?