C#运算符重载实现复数运算

C#运算符重载实现复数运算的由来:函数的重载——同名函数,不同的参数(包括参数个数不同和参数个数相同但个数不同)

将其引申,像如下的代码:

 
 
 
  1. int i=5;
  2. int j=2;
  3. int sum=i+j;

如果没有自定义的C#运算符重载,像+,-,*,/这样的运算符只能用于预定义的数据类型,编译器认为所有常见的运算符都是用于这些数据类型的。
问题来了,如果我要对两个复数或矩阵进行四则运算,就需要我们自己扩展运算符重载函数了。

C#运算符重载之示例:复数的四则运算

 
 
 
  1. public struct Complex
  2. {
  3.     public int real;
  4.     public int imaginary;
  5.     public Complex(int real, int imaginary)
  6.     {
  7.         this.real = real;
  8.         this.imaginary = imaginary;
  9.     }
  10.     //overload operator(+),added(two Complex objects) and return a Complex type
  11.     public static Complex operator +(Complex c1, Complex c2)
  12.     {
  13.         return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
  14.     }
  15.     //overload operator(-)
  16.     public static Complex operator -(Complex c1, Complex c2)
  17.     {
  18.         return new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary);
  19.     }
  20.     
  21.     //overload operator(*)
  22.     public static Complex operator *(Complex c1, Complex c2)
  23.     {
  24.         return new Complex(c1.real * c2.real - c1.imaginary * c2.imaginary,
  25.  c1.real * c2.imaginary + c1.imaginary * c2.real);
  26.     }
  27.     //overload operator(/)
  28.     public static Complex operator /(Complex c1, Complex c2)
  29.     {
  30.         return new Complex(-c1.real * c2.real +
  31.  c1.imaginary * c2.imaginary, -c1.real * c2.imaginary + c1.imaginary * c2.real);
  32.     }
  33.     // Override the ToString method to display an complex number in the suitable format:
  34.     public override string ToString()
  35.     {
  36.         return (String.Format("{0} + {1}i", real, imaginary));
  37.     }
  38. }

C#运算符重载之客户端代码:

 
 
 
  1. static void Main(string[] args)
  2. {
  3.     Complex num1 = new Complex(2, 3);
  4.     Complex num2 = new Complex(3, 4);
  5.     //Add two Complex objects (num1 and num2) through the overloaded plus operator:
  6.     Complex sum_Add = num1 + num2;
  7.     Complex sum_Minus = num1 - num2;
  8.     Complex sum_Product = num1 * num2;
  9.     Complex sum_Divide = num1 / num2;
  10.     //Print the numbers and the Result using the overriden ToString method:
  11.     Console.WriteLine("First complex number:  {0}", num1);
  12.     Console.WriteLine("Second complex number: {0}", num2);
  13.     Console.WriteLine("The sum of the two numbers: {0}", sum_Add);
  14.     Console.WriteLine("The Minus of the two numbers: {0}", sum_Minus);
  15.     Console.WriteLine("The Product of the two numbers: {0}", sum_Product);
  16.     Console.WriteLine("The Divide of the two numbers: {0}", sum_Divide);
  17.     Console.ReadLine();
  18. }

C#运算符重载实例运行结果:

 
 
 
  1. First complex number:  2 + 3i
  2. Second complex number: 3 + 4i
  3. The sum of the two numbers: 5 + 7i
  4. The Minus of the two numbers: -1 + -1i
  5. The Product of the two numbers: -6 + 17i
  6. The Divide of the two numbers: 6 + 1i

C#运算符重载实现复数运算的实例讲解就到这里,希望对你学习C#运算符重载有所帮助。

网页名称:C#运算符重载实现复数运算
URL网址:http://www.hantingmc.com/qtweb/news2/343152.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联