c#——《c#语言程序设计》实验报告——泛型与集合——运算符重载 -尊龙游戏旗舰厅官网
尊龙游戏旗舰厅官网
收集整理的这篇文章主要介绍了
c#——《c#语言程序设计》实验报告——泛型与集合——运算符重载
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
(实验过程中编写的程序复制到本文件中,下课整理后上交)
复数包含实部和虚部,现要求生成一个复数类,包含:
1)属性
2)构造方法
3)重载tostring方法
4)重载两个运算符 和-
5)编写索引器操作double this[int index],当index为0时,可读写实部;当index为1时,可读写虚部。
5)编写静态方法static bool tryparse(string s, out complex complex),将一个字符串解析为一个复数并输出,返回true。如果解析不成功,返回false。
提示:可使用double.tryparse(string s, out double value)方法;可使用string的indexof(char c)来搜索某个字符在字符串中的位置;可使用string的substring(int start, int length)来提取子串。
源代码
using system; using system.diagnostics;namespace homework17 {class complex {private double real;private double image;/** 空参构造*/public complex(){}/** 含参构造*/public complex(double real, double image){this.real = real;this.image = image;}public double real{get { return real; }set { real = value; }}public double image{get { return image; }set { image = value; }}public double this[int index] {get {if (index == 0){return real;}else {return image;}}set {if (index == 0){real=value;}else{image = value;}}}/*** 重写tostring方法,输出容易看的懂,方便*/public override string tostring(){return "(" real " " image "i" ")";}/* 复数的加法 */public static complex operator (complex b, complex c){return new complex(b.real c.real,b.image c.image);}/* 复数的减法 */public static complex operator -(complex b, complex c){return new complex(b.real - c.real, b.image - c.image);}/* 复数的乘法 */public static complex operator *(complex b, complex c){double real1;double image1;if (b.image != 0 && c.image != 0){//虚部不为0时real1 = (b.real * c.real) - (b.image * c.image);//两个虚部相乘是负数image1 = (b.real * c.image) (b.image* c.real);}else{//当有其中一个虚部为0时real1 = (b.real * c.real);image1 = (b.real * c.image) (b.image * c.real);}return new complex(real1, image1); }public static bool tryparse(string s, out complex complex) {complex = new complex();try {double real;if (!double.tryparse(s.substring(1, s.indexof(' ') - 1), out real)){console.writeline(real);return false;}double image;if (!double.tryparse(s.substring(s.indexof(' ') 1, s.indexof('i')- s.indexof(' ')-1), out image)){return false;}complex = new complex(real, image);}catch(argumentoutofrangeexception e){return false;}return true;}public void printcomplex(double real, double image){console.writeline(new complex(real, image));}}class program{static void main(string[] args){//这两行代码必须执行通过complex c = new complex(2, 3);c[0] = 2 * c[1];test(c);//选做:优化代码,使得以下代码顺利执行c = new complex(2, -3);test(c);c = new complex(2, 0);test(c);c = new complex(0, 2);test(c);}public static void test(complex c){console.writeline(c);complex result;bool ok = complex.tryparse(c.tostring(), out result);if (!ok)console.writeline("错了");console.writeline(result);complex c2 = c result;console.writeline(c2);debug.assert(c2.real == c.real * 2);debug.assert(c2.image == c.image * 2);}} }运行结果
https://www.cnblogs.com/vssure/p/8024802.html
https://blog.csdn.net/w15977858408/article/details/100783654
https://www.runoob.com/csharp/csharp-operator-overloading.html
总结
以上是尊龙游戏旗舰厅官网为你收集整理的c#——《c#语言程序设计》实验报告——泛型与集合——运算符重载的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: c#——《c#语言程序设计》实验报告——
- 下一篇: c#——《c#语言程序设计》实验报告——