一、实验目的
- 掌握运算符重载。
- 掌握索引符的编写。
- 掌握常用非泛型集合类和集合类的使用;
- 掌握可空类型的使用
二、实验内容
(实验过程中编写的程序复制到本文件中,下课整理后上交)
- 运算符重载
复数包含实部和虚部,现要求生成一个复数类,包含:
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