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

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

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

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

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#运算符重载有所帮助。

【编辑推荐】

  1. C#运算符之??浅析
  2. C#运算符种类简析
  3. C#位运算符种类及使用浅析
  4. C#运算符重载实例浅析
  5. C#运算符重载概念及应用详解
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-12 10:27:12

C#运算符重载运算符重载实例

2009-09-04 13:18:10

C#允许运算符重载

2009-08-12 10:56:47

C#运算符重载C#运算符重载实例

2009-08-14 10:16:57

C#运算符重载

2009-08-12 12:46:11

C#运算符重载

2009-08-11 15:51:08

C#运算符算术运算符

2009-08-12 10:37:13

C#运算符重载

2009-08-12 15:02:49

C#赋值运算符简单赋值运算符

2009-08-12 09:30:10

C#??运算符

2009-08-12 15:20:18

C#赋值运算符复合赋值运算符

2009-08-12 11:20:51

C#运算符重载

2009-09-01 10:08:57

C#运算符

2009-08-12 14:29:32

C#条件运算符

2009-08-12 13:35:22

C#关系运算符

2009-08-11 14:16:38

C# New运算符

2022-09-19 08:10:37

运算符函数语言

2021-12-15 10:25:57

C++运算符重载

2009-08-12 14:49:33

C#移位运算符

2011-07-15 01:34:36

C++重载运算符

2021-12-16 10:40:11

C++运算符重载
点赞
收藏

51CTO技术栈公众号