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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Starzkg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值