编程题,新手上路,求指教……
1、设计一个日期类,其中含 以下方法,a、已知从1980年1月1日以来的天数,求该天的年月日;b、在某天比如 10月22日后的多少天比如 1000天应该是哪个年月日;2、设计一个复数类,含两个复数的加、减、乘、除方法,以及复数与实数的加、减、乘、除方法。
我刚学,这是老师出的题目,我了解一下前辈是怎么想的
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test_Complex { // 自定义复数类 public class Complex { private Double _real; // 实部 private Double _imaginary; // 虚部 // 构造函数 public Complex(Double real, Double imaginary) { _real = real; _imaginary = imaginary; } // 模 public Double Size() { return Math.Sqrt(_real * _real + _imaginary * _imaginary); } // 定义加法运算 public static Complex operator +(Complex c1, Complex c2) { return new Complex(c1._real + c2._real, c1._imaginary + c2._imaginary); } // 定义减法运算 public static Complex operator -(Complex c1, Complex c2) { return new Complex(c1._real - c2._real, c1._imaginary - c2._imaginary); } // 定义乘法运算 public static Complex operator *(Complex c1, Complex c2) { Double x = c1._real * c2._real - c1._imaginary * c2._imaginary; Double y = c1._real * c2._imaginary + c2._real * c1._imaginary; return new Complex(x, y); } // 定义除法运算 public static Complex operator /(Complex c1, Complex c2) { Double x = 0; Double y = 0; if (c2.Size() != 0) { x = (c1._real * c2._real + c1._imaginary * c2._imaginary) / (c2._real * c2._real + c2._imaginary * c2._imaginary); y = (c2._real * c1._imaginary - c1._real * c2._imaginary) / (c2._real * c2._real + c2._imaginary * c2._imaginary); } return new Complex(x, y); } public override String ToString() { return String.Format("({0:N})+i({1:N})", _real, _imaginary); } }; class Program { static void Main(String[] args) { Complex c1 = new Complex(1, 0); Complex c2 = new Complex(0, 1); Console.WriteLine("c1 = {0}", c1); Console.WriteLine("c2 = {0}", c2); Console.WriteLine("c1 + c2 = {0}", c1 + c2); Console.WriteLine("c1 - c2 = {0}", c1 - c2); Console.WriteLine("c1 * c2 = {0}", c1 * c2); Console.WriteLine("c1 / c2 = {0}", c1 / c2); Console.ReadLine(); } } }