成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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-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#運算符算術運算符

2009-08-12 10:37:13

C#運算符重載

2009-08-12 09:30:10

C#??運算符

2009-08-12 15:20:18

C#賦值運算符復合賦值運算符

2009-08-12 15:02:49

C#賦值運算符簡單賦值運算符

2009-08-12 11:20:51

C#運算符重載

2009-08-11 14:16:38

C# New運算符

2009-08-12 13:35:22

C#關系運算符

2009-09-01 10:08:57

C#運算符

2009-08-12 14:29:32

C#條件運算符

2009-08-12 14:49:33

C#移位運算符

2021-12-15 10:25:57

C++運算符重載

2022-09-19 08:10:37

運算符函數語言

2011-07-15 01:34:36

C++重載運算符

2009-08-12 10:07:51

C#運算符
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 免费三级网 | 欧美日韩一区二区在线观看 | 久久大陆| 成人综合视频在线 | 欧美一区免费 | 欧美日韩视频一区二区 | 国产日韩欧美激情 | 国产精品一区二区在线免费观看 | 久久成人精品视频 | 久久精品视频9 | 毛片一级电影 | 国产精品性做久久久久久 | 日本精品一区二区三区视频 | 亚洲二区在线 | 精品久久久久久亚洲国产800 | 精品国产99| 成人免费在线 | 久久久网| 午夜日韩精品 | 日批的视频| 91精品国产91久久久久久最新 | 亚洲成人三级 | 午夜视频网站 | 亚洲精品1区 | 欧美亚州 | 精品毛片在线观看 | 国产精品美女久久久 | 亚洲国产成人精品女人久久久 | 成人深夜福利 | 精品在线播放 | 成人二区| 日日操夜夜操天天操 | 久久成人精品视频 | 亚洲欧美日韩精品久久亚洲区 | 亚洲一区二区三区在线播放 | 日韩在线日韩 | 欧美一区二区免费 | 在线观看 亚洲 | 一区二区三区在线免费观看视频 | 亚洲欧美另类在线 | 国产成人高清在线观看 |