程序设计操作符的重载实验报告
实验报告二 类和对象一、实验目的
本实验的U的是熟练学习和掌握类的定义及操作符的重载以及Datatime结 构。二、实验内容及结果
(一)、第1个题訂的要求:定义完整的ComplexNumber类,在其中重载加减乘 除等基
本操作符、相等和不等操作符、大小比较操作符以及自增自减操作符
1、 程序编程思路:首先定义一个ComplexNumber类,然后重载各种操作
2、 程序源码:
static void Main(stringargs)
{
Console. WriteLine(z/请输入第一个复数 cl: ”);
ComplexNumber cl = new
ComplexNumber(double? Parse(Console? ReadLine()),
double? Parse(Console? ReadLine()));
Console. WriteLine ("请输入笫二个复数 c2: ”);
ComplexNumber c2 = new
ComplexNumber(double? Parse(Console? ReadLine()),
double? Parse(Console. ReadLine()));
ComplexNumber c3 = cl + c2;
Console. WriteLine C这两个复数的和为{0}", c3);
ComplexNumber c4 二 cl 一 c2;
Console. WriteLine ("这两个复数的差为{0} "、c4);
Comp1exNumber co = cl * c2;
Console. WriteLine C这两个复数的乘积为{0}", c5);
Comp1exNumber c6 = cl / c2;
Console. WriteLine C这两个复数相除为{0}", c6);
if (cl = c2)
{
Console. WriteLine C这两个复数相等");
}
else if (cl != c2)
Console. WriteLine C这两个复数不相等");
{
if (cl > c2)
Console. WriteLine("cl>c2");
else
Console. WriteLine("cl〈c2");
}
Console. WriteLine Cc 1 自增结果为{0}", cl++);
Console. WriteLineCc 1 自减结果为{0}", cl--);
Console. ReadLine ();
}
public class Comp1exNumber
{
private double x, y;
public double X
get { return x; }
set { x = value; }
}
public double Y
{
get { return y; }
set { y = value; }
}
public Comp1exNumber(double x, double y)
{
this? x = x;
this? y 二 y;
}
public static Comp1exNumber operator +(Comp1exNumber cl,
Comp1exNumber c2)
{
return new Comp 1 exNumber (cl?x + c2? x, cl? y + c2? y);
}
public static Comp1exNumber operator -(Comp1exNumber cl,
Comp1exNumber c2)
return new Comp 1 exNumber (cl.x 一 c2? x, cl? y 一 c2? y);
}
public static Comp1exNumber operator *(Comp1exNumber cl,
Comp1exNumber c2)
{
return new Comp 1 exNumber (cl ?x * c2 ?x 一 cl? y * c2? y, cl? y * c2 ?x + cl. x * c2. y);
}
public static Comp1exNumber operator /(Comp1exNumber cl,
Comp1exNumber c2)
{
return new Comp 1 exNumber((cl?x * c2?x + cl? y * c2? y) / (c2?x * c2?
+ c2. y * c2. y), (cl. y * c2. x - cl. x * c2. y) / (c2. x * c2. x + c2. y * c2. y));
}
public static bool operator ==(Comp1exNumber cl, Comp1exNumber c2)
{
return (cl?x == c2? x) && (cl?y == c2? y);
}
public static bool operator !=(Comp1exNumber cl, Comp1exNumber c2)
{
return (cl?x != c2? x) I i (cl?y !二 c2? y);
}
public static bool operator >(Comp1exNumber cl, Comp1exNumber c2)
double a = cl?x * cl?x + cl. y * cl.y;
3
3、实验结果:
double b = c2. x * c2. x + c2. y * c2. y;
return a > b;
}
public static bool operator <(Comp1exNumber cl, Comp1exNumber c2)
double a = cl?为
:* cl. X
+ cl? y * cl. y;
double b = c2? y
:* c2. x
+ c2. y * c2. y;
return a < b;
}
public static Comp1exNumber operator ++(Comp1exNumber cl)
{
return new Comp1exNumber(cl? x++, cl. y++);
}
public static Comp1exNumber operator --(Comp1exNumber cl)
{
return new Comp1exNumber(cl? x一一, cl? y一一);
}
public override string ToStringO
{
return string? Format C {0} + {l} i,z, this? x, this? y);
}
}
(二)、第2个题目的要求:知道一个人的生日,如何计算他的年龄(严格按照周 岁计算)
1、 程序编程思路:
首先考虑的是怎么来计算年龄,想到用当前日期减去生日日期,用年减去年, 月减去月,日减去日,然后思考,各种情况下的年龄讣算方法,画出程序的流程 图,根据流程图写出源程序;接着就想到如何让操作者方便的一次性将生日的年月 日输入,就考虑用字符数组,然后用string类的split方法将输入的字符串劈 开。
2、 程序源码:
static void Main (string [Z args)
{
int a, b, c, d, e, f;
DateTime s = DateTime.Today;
d = s? Year;
e = s. Month:
f = s. Day;
Console. WriteLine ("0000-00-00");请输入您的生日(格式为)
string g = Console?ReadLine();
char[] sepl 二{'」};
string [J h= g. Split (sepl);
int[] x = new int[h.Length];
for (int i = 0; i < h. Length; i++)
x[i[ = int. Parse(h[i]);
TOC \o "1-5" \h \z a = d - x[0];
b = e - x[l];
c = f - x[2];
辻((a < 0) I I (a = 0 && b < 0) I i (a == 0 && b == 0 && c < 0))
Console. WriteLine ;您输入的生日有误?
else
if (a = 0) Console. WriteLine(,/,?);您不满一岁?
else if (a > 0)
{
if (b > 0) Console. WriteLine (" {0} ", a);您今年岁?
else if (b < 0) Console. WriteLine C {0}z/, a - 1);您今年岁?
else if (b = 0)
{
if (c<0) Console. WriteLine C {0}z/, aT);您今年岁?
else Console. WriteLine (" {0} ", a);您今年岁?
}
}
Console. ReadLine ();
}
3、实验结果:
三、实验效果
对第一个题II,要熟记操作符重载的方法与格式,掌握被重载的操作符必须
被声明为共有的和静态的并通过operator后跟操作符来声明。
对笫二个题LI,将程序的流程图画出后,更多的考虑的是如何让用户方便的
输入生日数据,能够一次性将生日的年月日输入,考虑如何得到系统现在的日 期。自我感觉这次上机实验,我收获了很多,能够考虑到如何让自己的程序更加
完善,能更多的考虑到如何让用户更加方便的输入生日数据,但以后要更加加
对临界情况下的调试,使得程序更加符合题口的要求。