C#——《C#语言程序设计》实验报告——泛型与集合——运算符重载
一、實驗目的
?
二、實驗內(nèi)容
(實驗過程中編寫的程序復制到本文件中,下課整理后上交)
復數(shù)包含實部和虛部,現(xiàn)要求生成一個復數(shù)類,包含:
1)屬性
2)構(gòu)造方法
3)重載ToString方法
4)重載兩個運算符+和-
5)編寫索引器操作double this[int index],當index為0時,可讀寫實部;當index為1時,可讀寫虛部。
5)編寫靜態(tài)方法static bool TryParse(string s, out Complex complex),將一個字符串解析為一個復數(shù)并輸出,返回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;/** 空參構(gòu)造*/public Complex(){}/** 含參構(gòu)造*/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" + ")";}/* 復數(shù)的加法 */public static Complex operator +(Complex b, Complex c){return new Complex(b.Real+c.Real,b.Image+c.Image);}/* 復數(shù)的減法 */public static Complex operator -(Complex b, Complex c){return new Complex(b.Real - c.Real, b.Image - c.Image);}/* 復數(shù)的乘法 */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);//兩個虛部相乘是負數(shù)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){//這兩行代碼必須執(zhí)行通過Complex c = new Complex(2, 3);c[0] = 2 * c[1];Test(c);//選做:優(yōu)化代碼,使得以下代碼順利執(zhí)行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);}} }運行結(jié)果?
三、實驗心得與體會
參考文章
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
總結(jié)
以上是生活随笔為你收集整理的C#——《C#语言程序设计》实验报告——泛型与集合——运算符重载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#——《C#语言程序设计》实验报告——
- 下一篇: C#——《C#语言程序设计》实验报告——