日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
C#運(yùn)算符重載實(shí)現(xiàn)復(fù)數(shù)運(yùn)算

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

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

 
 
 
  1. int i=5;
  2. int j=2;
  3. 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)算

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

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.     //Add two Complex objects (num1 and num2) through the overloaded plus operator:
  6.     Complex sum_Add = num1 + num2;
  7.     Complex sum_Minus = num1 - num2;
  8.     Complex sum_Product = num1 * num2;
  9.     Complex sum_Divide = num1 / num2;
  10.     //Print the numbers and the Result using the overriden ToString method:
  11.     Console.WriteLine("First complex number:  {0}", num1);
  12.     Console.WriteLine("Second complex number: {0}", num2);
  13.     Console.WriteLine("The sum of the two numbers: {0}", sum_Add);
  14.     Console.WriteLine("The Minus of the two numbers: {0}", sum_Minus);
  15.     Console.WriteLine("The Product of the two numbers: {0}", sum_Product);
  16.     Console.WriteLine("The Divide of the two numbers: {0}", sum_Divide);
  17.     Console.ReadLine();
  18. }

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)算符重載有所幫助。


名稱欄目:C#運(yùn)算符重載實(shí)現(xiàn)復(fù)數(shù)運(yùn)算
當(dāng)前URL:http://m.5511xx.com/article/dpcesid.html