欢迎来到天天文库
浏览记录
ID:9872345
大小:111.50 KB
页数:4页
时间:2018-05-13
《程序设计操作符的重载实验报告》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验报告二类和对象一、实验目的本实验的目的是熟练学习和掌握类的定义及操作符的重载以及Datatime结构。二、实验内容及结果(一)、第1个题目的要求:定义完整的ComplexNumber类,在其中重载加减乘除等基本操作符、相等和不等操作符、大小比较操作符以及自增自减操作符1、程序编程思路:首先定义一个ComplexNumber类,然后重载各种操作2、程序源码:staticvoidMain(string[]args){Console.WriteLine("请输入第一个复数c1:");ComplexNumberc1=newComplexNumber(dou
2、ble.Parse(Console.ReadLine()),double.Parse(Console.ReadLine()));Console.WriteLine("请输入第二个复数c2:");ComplexNumberc2=newComplexNumber(double.Parse(Console.ReadLine()),double.Parse(Console.ReadLine()));ComplexNumberc3=c1+c2;Console.WriteLine("这两个复数的和为{0}",c3);ComplexNumberc4=c1-c2;Co
3、nsole.WriteLine("这两个复数的差为{0}",c4);ComplexNumberc5=c1*c2;Console.WriteLine("这两个复数的乘积为{0}",c5);ComplexNumberc6=c1/c2;Console.WriteLine("这两个复数相除为{0}",c6);if(c1==c2){Console.WriteLine("这两个复数相等");}elseif(c1!=c2)Console.WriteLine("这两个复数不相等");{if(c1>c2)Console.WriteLine("c1>c2");elseCo
4、nsole.WriteLine("c15、){this.x=x;this.y=y;}publicstaticComplexNumberoperator+(ComplexNumberc1,ComplexNumberc2){returnnewComplexNumber(c1.x+c2.x,c1.y+c2.y);}publicstaticComplexNumberoperator-(ComplexNumberc1,ComplexNumberc2){returnnewComplexNumber(c1.x-c2.x,c1.y-c2.y);}publicstaticComplexNumberoperato6、r*(ComplexNumberc1,ComplexNumberc2){returnnewComplexNumber(c1.x*c2.x-c1.y*c2.y,c1.y*c2.x+c1.x*c2.y);}publicstaticComplexNumberoperator/(ComplexNumberc1,ComplexNumberc2){returnnewComplexNumber((c1.x*c2.x+c1.y*c2.y)/(c2.x*c2.x+c2.y*c2.y),(c1.y*c2.x-c1.x*c2.y)/(c2.x*c2.x+c2.y*c2.y)7、);}publicstaticbooloperator==(ComplexNumberc1,ComplexNumberc2){return(c1.x==c2.x)&&(c1.y==c2.y);}publicstaticbooloperator!=(ComplexNumberc1,ComplexNumberc2){return(c1.x!=c2.x)8、9、(c1.y!=c2.y);}publicstaticbooloperator>(ComplexNumberc1,ComplexNumberc2){doublea=c1.x*c1.x+c1.y*c1.y;d10、oubleb=c2.x*c2.x+c2.y*c2.y;returna>b;}publicsta
5、){this.x=x;this.y=y;}publicstaticComplexNumberoperator+(ComplexNumberc1,ComplexNumberc2){returnnewComplexNumber(c1.x+c2.x,c1.y+c2.y);}publicstaticComplexNumberoperator-(ComplexNumberc1,ComplexNumberc2){returnnewComplexNumber(c1.x-c2.x,c1.y-c2.y);}publicstaticComplexNumberoperato
6、r*(ComplexNumberc1,ComplexNumberc2){returnnewComplexNumber(c1.x*c2.x-c1.y*c2.y,c1.y*c2.x+c1.x*c2.y);}publicstaticComplexNumberoperator/(ComplexNumberc1,ComplexNumberc2){returnnewComplexNumber((c1.x*c2.x+c1.y*c2.y)/(c2.x*c2.x+c2.y*c2.y),(c1.y*c2.x-c1.x*c2.y)/(c2.x*c2.x+c2.y*c2.y)
7、);}publicstaticbooloperator==(ComplexNumberc1,ComplexNumberc2){return(c1.x==c2.x)&&(c1.y==c2.y);}publicstaticbooloperator!=(ComplexNumberc1,ComplexNumberc2){return(c1.x!=c2.x)
8、
9、(c1.y!=c2.y);}publicstaticbooloperator>(ComplexNumberc1,ComplexNumberc2){doublea=c1.x*c1.x+c1.y*c1.y;d
10、oubleb=c2.x*c2.x+c2.y*c2.y;returna>b;}publicsta
此文档下载收益归作者所有