C#中怎么通過(guò)運(yùn)算符重載實(shí)現(xiàn)復(fù)數(shù)運(yùn)算

今天就跟大家聊聊有關(guān)C#中怎么通過(guò)運(yùn)算符重載實(shí)現(xiàn)復(fù)數(shù)運(yùn)算,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的海門網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

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

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

int i=5;  int j=2;  int sum=i+j;

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

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

public struct Complex  {      public int real;      public int imaginary;       public Complex(int real, int imaginary)      {          this.real = real;          this.imaginary = imaginary;      }       //overload operator(+),added(two Complex objects) and return a Complex type      public static Complex operator +(Complex c1, Complex c2)      {          return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);      }       //overload operator(-)      public static Complex operator -(Complex c1, Complex c2)      {          return new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary);      }            //overload operator(*)      public static Complex operator *(Complex c1, Complex c2)      {          return new Complex(c1.real * c2.real - c1.imaginary * c2.imaginary,         c1.real * c2.imaginary + c1.imaginary * c2.real);      }       //overload operator(/)      public static Complex operator /(Complex c1, Complex c2)      {          return new Complex(-c1.real * c2.real +           c1.imaginary * c2.imaginary, -c1.real * c2.imaginary + c1.imaginary * c2.real);      }       // Override the ToString method to display an complex number in the suitable format:      public override string ToString()      {          return (String.Format("{0} + {1}i", real, imaginary));      }  }

C#運(yùn)算符重載之客戶端代碼:

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

C#運(yùn)算符重載實(shí)例運(yùn)行結(jié)果:

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

看完上述內(nèi)容,你們對(duì)C#中怎么通過(guò)運(yùn)算符重載實(shí)現(xiàn)復(fù)數(shù)運(yùn)算有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

文章名稱:C#中怎么通過(guò)運(yùn)算符重載實(shí)現(xiàn)復(fù)數(shù)運(yùn)算
網(wǎng)頁(yè)URL:http://bm7419.com/article48/jjdhep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司移動(dòng)網(wǎng)站建設(shè)、軟件開(kāi)發(fā)App開(kāi)發(fā)、響應(yīng)式網(wǎng)站網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司