C#——《C#語言程序設計》實驗報告——泛型與集合——運算符重載

一、實驗目的

  1. 掌握運算符重載。
  2. 掌握索引符的編寫。
  3. 掌握常用非泛型集合類和集合類的使用;
  4. 掌握可空類型的使用

 

二、實驗內容

(實驗過程中編寫的程序複製到本文件中,下課整理後上交)

  1. 運算符重載

複數包含實部和虛部,現要求生成一個複數類,包含:

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);

        }
    }
}

運行結果 

三、實驗心得與體會

  1. 掌握運算符重載。
  2. 掌握索引符的編寫。
  3. 掌握常用非泛型集合類和集合類的使用;
  4. 掌握可空類型的使用

參考文章

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章