自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#運算符重載實現(xiàn)復(fù)數(shù)運算

開發(fā) 后端
C#運算符重載實現(xiàn)復(fù)數(shù)運算是如何辦到的呢?本文就向你詳細(xì)介紹這方面的內(nèi)容。

C#運算符重載實現(xiàn)復(fù)數(shù)運算的由來:函數(shù)的重載——同名函數(shù),不同的參數(shù)(包括參數(shù)個數(shù)不同和參數(shù)個數(shù)相同但個數(shù)不同)

將其引申,像如下的代碼:

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

如果沒有自定義的C#運算符重載,像+,-,*,/這樣的運算符只能用于預(yù)定義的數(shù)據(jù)類型,編譯器認(rèn)為所有常見的運算符都是用于這些數(shù)據(jù)類型的。
問題來了,如果我要對兩個復(fù)數(shù)或矩陣進(jìn)行四則運算,就需要我們自己擴(kuò)展運算符重載函數(shù)了。

C#運算符重載之示例:復(fù)數(shù)的四則運算

  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#運算符重載實例運行結(jié)果:
 

  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#運算符重載實現(xiàn)復(fù)數(shù)運算的實例講解就到這里,希望對你學(xué)習(xí)C#運算符重載有所幫助。

【編輯推薦】

  1. C#運算符之??淺析
  2. C#運算符種類簡析
  3. C#位運算符種類及使用淺析
  4. C#運算符重載實例淺析
  5. C#運算符重載概念及應(yīng)用詳解
責(zé)任編輯:仲衡 來源: 百度空間
相關(guān)推薦

2009-09-04 13:18:10

C#允許運算符重載

2009-08-12 10:27:12

C#運算符重載運算符重載實例

2009-08-14 10:16:57

C#運算符重載

2009-08-12 10:56:47

C#運算符重載C#運算符重載實例

2009-08-12 12:46:11

C#運算符重載

2009-08-11 15:51:08

C#運算符算術(shù)運算符

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#賦值運算符復(fù)合賦值運算符

2009-08-12 11:20:51

C#運算符重載

2009-09-01 10:08:57

C#運算符

2009-08-12 13:35:22

C#關(guān)系運算符

2009-08-12 14:29:32

C#條件運算符

2009-08-11 14:16:38

C# New運算符

2009-08-12 14:49:33

C#移位運算符

2022-09-19 08:10:37

運算符函數(shù)語言

2021-12-15 10:25:57

C++運算符重載

2011-07-15 01:34:36

C++重載運算符

2009-08-12 10:07:51

C#運算符
點贊
收藏

51CTO技術(shù)棧公眾號