欢迎来到天天文库
浏览记录
ID:51082578
大小:32.00 KB
页数:3页
时间:2020-03-18
《C#复数的加减乘运算.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespace_010921{classComplexNumber{privatedoublereal,image;publicComplexNumber()//无参构造函数{real=0;image=0;}publicComplexNumber(doubler)//只有实部的构造函数{real=r;image=0;}publicComplexNumber(doubler,doublei)//含有实部和
2、虚部的构造函数{real=r;image=i;}publicdoubleReal//实部{set{real=value;}get{returnreal;}}publicdoubleImage//虚部{set{image=value;}get{returnimage;}}publicComplexNumberAddComplex(ComplexNumberc1,ComplexNumberc2)//复数c1与c2相加{returnnewComplexNumber(c1.real+c2.real,c1.image+c2.image)
3、;}publicComplexNumberSubComplex(ComplexNumberc1,ComplexNumberc2)//复数c1与c2相减{returnnewComplexNumber(c1.real-c2.real,c1.image-c2.image);}publicComplexNumberMultiplyComplex(ComplexNumberc1,ComplexNumberc2)//复数c1与c2相乘{returnnewComplexNumber(c1.real*c2.real-c1.image*c2.i
4、mage,c1.image*c2.real+c1.real*c2.image);}publicstringTostring(ComplexNumberc){stringstr;if(c.Image==0){str=c.Real.ToString();}elseif(c.Real==0){str=c.Image.ToString()+"i";}elseif(c.Real!=0&&c.Image<0){str=c.Real.ToString()+c.Image.ToString()+"i";}else{str=c.Real.ToS
5、tring()+"+"+c.Image.ToString()+"i";}returnstr;}}classProgram{publicstaticvoidMain(string[]args){ComplexNumberc1=newComplexNumber();ComplexNumberc2=newComplexNumber();ComplexNumberc=newComplexNumber();Console.WriteLine("请输入第一个复数的实部:");c1.Real=double.Parse(Console.Rea
6、dLine());Console.WriteLine("请输入第一个复数的虚部:");c1.Image=double.Parse(Console.ReadLine());Console.WriteLine("请输入第二个复数的实部:");c2.Real=double.Parse(Console.ReadLine());Console.WriteLine("请输入第二个复数的虚部:");c2.Image=double.Parse(Console.ReadLine());Console.WriteLine("您输入的第一个复数c1
7、是:"+c.Tostring(c1));Console.WriteLine("您输入的第二个复数c2是:"+c.Tostring(c2));Console.WriteLine("c1+c2="+c.Tostring(c.AddComplex(c1,c2)));Console.WriteLine("c1-c2="+c.Tostring(c.SubComplex(c1,c2)));Console.WriteLine("c1*c2="+c.Tostring(c.MultiplyComplex(c1,c2)));Console.Rea
8、d();}}}
此文档下载收益归作者所有