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

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

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

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

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

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

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

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

  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#運(yùn)算符重載之客戶端代碼:

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

【編輯推薦】

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

2009-09-04 13:18:10

C#允許運(yùn)算符重載

2009-08-12 10:27:12

C#運(yùn)算符重載運(yùn)算符重載實(shí)例

2009-08-12 10:56:47

C#運(yùn)算符重載C#運(yùn)算符重載實(shí)例

2009-08-14 10:16:57

C#運(yùn)算符重載

2009-08-12 12:46:11

C#運(yùn)算符重載

2009-08-11 15:51:08

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

2009-08-12 10:37:13

C#運(yùn)算符重載

2009-08-12 09:30:10

C#??運(yùn)算符

2009-08-12 15:20:18

C#賦值運(yùn)算符復(fù)合賦值運(yùn)算符

2009-08-12 15:02:49

C#賦值運(yùn)算符簡(jiǎn)單賦值運(yùn)算符

2009-08-12 11:20:51

C#運(yùn)算符重載

2009-08-12 14:49:33

C#移位運(yùn)算符

2009-08-11 14:16:38

C# New運(yùn)算符

2009-08-12 13:35:22

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

2009-09-01 10:08:57

C#運(yùn)算符

2009-08-12 14:29:32

C#條件運(yùn)算符

2022-09-19 08:10:37

運(yùn)算符函數(shù)語(yǔ)言

2021-12-15 10:25:57

C++運(yùn)算符重載

2011-07-15 01:34:36

C++重載運(yùn)算符

2009-08-12 14:23:09

C#邏輯運(yùn)算符
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 视频在线观看亚洲 | 日本精品一区二区三区在线观看视频 | 九色国产 | 中文字字幕一区二区三区四区五区 | www.色综合 | 国产精品伦理一区二区三区 | 羞羞视频在线观免费观看 | 免费看啪啪网站 | 北条麻妃国产九九九精品小说 | 欧美精品网站 | 国产乱码精品一区二区三区中文 | 色婷婷亚洲国产女人的天堂 | 久久久久久久久久久久亚洲 | 亚洲成人动漫在线观看 | 国产成人高清成人av片在线看 | 一本一道久久a久久精品综合蜜臀 | 91视频精选| 91色在线 | 久久美女视频 | 欧美日韩在线一区二区 | 一区二区三区四区免费视频 | 欧美精品v| 最新超碰| 色婷婷久久久亚洲一区二区三区 | 99精品国自产在线观看 | 久久一区视频 | 三级免费毛片 | 欧美黑人一级爽快片淫片高清 | 精久久久 | 欧美网站一区 | 九色视频网站 | 热re99久久精品国99热观看 | 97精品一区二区 | 欧美a区 | 色呦呦网站 | 亚洲国产成人精品在线 | 亚洲精品久久久一区二区三区 | 午夜免费网| 一级毛片成人免费看a | 国产精品视频播放 | 综合二区|