日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#——《C#语言程序设计》实验报告——泛型与集合——运算符重载

發布時間:2024/10/5 C# 104 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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#语言程序设计》实验报告——泛型与集合——运算符重载的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。