




已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
實驗三、運算符重載及自定義類型轉(zhuǎn)換一、實驗?zāi)康模?)掌握運算符重載;(2)掌握自定義數(shù)據(jù)類型的轉(zhuǎn)換。二、實驗內(nèi)容1、定義復(fù)數(shù)類,部分代碼已給出。(1)類名Complex,用double型變量real、image分別存儲復(fù)數(shù)的實部、虛部;class Complex double real; double imag; (2)定義復(fù)數(shù)類的構(gòu)造函數(shù)Complex(double real,double image);定義另一個構(gòu)造函數(shù)Complex(),將實部、虛部均置為0.0;public Complex() public Complex(double r, double i) this.real = r; this.imag = i; (3)用運算符重載的方法定義復(fù)數(shù)的基本運算:加(+)、減(-)、乘(*)、除(/);public static Complex operator +(Complex A, Complex B) double real; double imag; real = A.real + B.real; imag = A.real + B.imag; Complex c = new Complex(real, imag); return c; public static Complex operator -(Complex A, Complex B) double real; double imag; real = A.real - B.real; imag = A.real - B.imag; Complex c = new Complex(real, imag); return c; public static Complex operator *(Complex A, Complex B) double real; double imag; real = A.real * B.real - A.imag * B.imag; imag = A.real * B.imag + A.imag * B.real; Complex c = new Complex(real, imag); return c; public static Complex operator /(Complex A, Complex B) double real; double imag; real = (A.real * B.real + A.imag * B.imag) / (B.real * B.real + B.imag * B.imag); imag = (A.imag * B.real - A.real * B.imag) / (B.real * B.real + B.imag * B.imag); Complex c = new Complex(real, imag); return c; (4)用運算符重載的方法定義求復(fù)數(shù)的相反數(shù)的運算;如A=3+4i的相反數(shù)應(yīng)為-A=-3-4i public static Complex operator -(Complex D)/用運算符重載的方法定義求復(fù)數(shù)的相反數(shù)的運算 D.imag = -D.imag; D.real = -D.real; return D; (5) 實現(xiàn)實數(shù)類型到復(fù)數(shù)類型的隱式轉(zhuǎn)換;public static implicit operator Complex(double x) return (new Complex(x, 0); (6)定義從復(fù)數(shù)類型到實數(shù)類型的顯式變換,將復(fù)數(shù)轉(zhuǎn)化成其模(實部和虛部的平方和的平方根) public static explicit operator double (Complex x)/實現(xiàn)實數(shù)類型到復(fù)數(shù)類型的隱式轉(zhuǎn)換; double mou=Math.Sqrt ( x.imag *x .imag +x.real +x.real); string str1 = string.Format(0:F,mou); return ( mou );/定義從復(fù)數(shù)類型到實數(shù)類型的顯式變換,將復(fù)數(shù)轉(zhuǎn)化成其模(7) 定義一個將復(fù)數(shù)的實例信息輸出的函數(shù)ToString()。注意其原型為:public override string ToString().對各種復(fù)數(shù)均能有合乎習(xí)慣的輸出(可考慮如下幾種情況:1,3i,-3i,1+3i,1-3i等)(選做)public override string ToString() if (real = 0) return imag + i; else if (imag = 0) return real.ToString()+0i; else if (imag 0) return real + + +imag + i; else return real.ToString() + imag + i; 2、定義控制類 (1)類名Test,只包含如下Main()函數(shù); (2)Main()的定義 public static void Main() int i=1; Complex c1=new Complex(1,1); Complex c2=-c1; Complex c3=c1+c2; 輸出復(fù)數(shù)c3; c3=c1-c2; 輸出復(fù)數(shù)c3; c3=c1*c2; 輸出復(fù)數(shù)c3; c3=c1/c2;輸出復(fù)數(shù)c3;c3=c1-i; 輸出復(fù)數(shù)c3;Double j=(double)c3;/顯式轉(zhuǎn)換輸出復(fù)數(shù)c3的模j; class Test static void Main(string args) Complex a, b, he, cha, ji, shang, d, zh; a = new Complex(5, 3); b = new Complex(2, 2); d = new Complex(2, 2); double x = 9.1; double y; he = a + b; cha = a - b; ji = a * b; shang = a / b; zh = (Complex)x; y = (double)d; string str1 = string.Format(0:F, y); Console.WriteLine(復(fù)數(shù)a: + a.ToString(); Console.WriteLine(復(fù)數(shù)b: + b.ToString(); Console.WriteLine(復(fù)數(shù)求和: + he.ToString(); Console.WriteLine(復(fù)數(shù)求差: + cha.ToString(); Console.WriteLine(復(fù)數(shù)求積: + ji.ToString(); Console.WriteLine(復(fù)數(shù)求商: + shang.ToString(); Console.WriteLine(復(fù)數(shù)求反: + d.ToString(); Console.WriteLine(復(fù)數(shù)隱式轉(zhuǎn)換: + zh.ToString(); Console.WriteLine(復(fù)數(shù)顯式變換: + str1); Console.ReadLine(); 運行結(jié)果截圖2. 定義角度類Angle,包含3個int型分量信息度、分、秒。(1) 為其定義恰當(dāng)?shù)臉?gòu)造函數(shù)。class Angle int 度, 分, 秒; public Angle() public Angle(int d, int f, int m) this.度 = d; this.分 = f; this.秒 = m; (2)對兩個角度進行加、減運算的運算符進行重載。(注意:進位和借位)如:19度21分30秒+1度39分34秒=21度1分4秒public static Angle operator +(Angle A, Angle B)/兩個角度進行加運算的運算符進行重載 int d=0; int f=0; int m=0; m = A.秒 + B.秒 ; if (m = 60) f = f + m / 60; m = m - (m / 60)*60; f =f+ A.分 + B.分; if (f = 60) d = d + f / 60; f = f - (f / 60)*60 ; d = d+ A.度 + B.度 ; Angle c = new Angle(d,f,m); return c; public static Angle operator -(Angle A, Angle B)/兩個角度進行減運算的運算符進行重載 int d = 0; int f = 0; int m = 0; if (A.秒 B.秒 ) f = f -1; m = A.秒+60 - B.秒; if (A.分 B.分) d = d - 1; f = A.分 + 60 - B.分; d = A.度 - B.度; Angle c = new Angle(d, f, m); return c; (3) 定義從整型數(shù)到Angle的隱式轉(zhuǎn)換,整型數(shù)表示某角度的用秒作單位的值。如:4201=1度10分1秒 public override string ToString() return System.String.Format(0度1分2秒, 度, 分, 秒); (4)定義從Angle到int的顯式轉(zhuǎn)換public static implicit operator Angle(int x) int d = x / 3600; int f = (x - d * 3600) / 60; int m = x - d * 3600 - f * 60; return ( new Angle(d,f,m); (5)定義函數(shù)將某個角度值的信息輸出。 public override string ToString() return System.String.Format(0度1分2秒,度, 分, 秒); (6)控制類定義如下:定義控制類 (1)類名Test,只包含如下Main()函數(shù); (2)Main()的定義 public static void Main() int i=4201; Angle a1=new Angle(19,30,22); Angle a2=new Angle(11,34,39); Angle a3=c1+c2; 輸出a3; a3=a1-a2; 輸出a3; a3=a1+i; 輸出復(fù)數(shù)a3; int j=(int)a3; /注意顯式類型轉(zhuǎn)換與隱式類型轉(zhuǎn)換的區(qū)別輸出j;運行結(jié)果截圖 static void Main(string args) Angle a1 = new Angle(19, 30, 22); Angle a2 = new Angle(11, 34, 39); int M = 4261; Angle a3 = a1 + a2; Angle a4 = a1 - a2; Angle a5 = (Angle)M; int N = (int)a1 ; Console.WriteLine(a1度數(shù)為: + a1); Console.WriteLine(a2度數(shù)為: + a2); Console.WriteLine(相加的度數(shù)為:a3=a1+a2= + a3); Console.WriteLine(相減的度數(shù)為:a4=a1-a2= + a4); Console.WriteLine(整數(shù)轉(zhuǎn)化為角度a5為: + a5); Console.WriteLine(角度a1轉(zhuǎn)化為整數(shù)為: + N); Console.ReadLine(); 3 定義一個三維空間坐標(biāo)類,包含三個分量的值(x,y,z),如何對兩個坐標(biāo)點進行平移(也就是加運算),并增加你覺得合適的其他運算,如距離計算等class Point private int x, y, z; public Point() /*生成具有特定坐標(biāo)的點*/ public Point(int ox, int oy, int oz) setXYZ(ox, oy, oz); /設(shè)置三個點的坐標(biāo) public void setX(int x) this.x = x; public void setY(int y) this.y = y; public void setZ(int z) this.z = z; public void setXYZ(int x, int y, int z) setX(x); setY(y); setZ(z)public static Point operator +(Point A, Point B)/將p向x,y,z分別平移q的對應(yīng)坐標(biāo)個單位 int x, y, z; x = A.x + B.x; y = A.y + B.y; z = A.z + B.z; Point c = new Point(x, y, z); return c; /*該點到原點距離平方*/ public double getLeng() return Math.Sqrt(x * x + y * y + z * z
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 工業(yè)遺址改造為現(xiàn)代建筑的實踐案例
- 工業(yè)電氣自動化控制系統(tǒng)的應(yīng)用
- 工作坊組織與執(zhí)行要點
- 工作中的人性化情緒管理與壓力緩解方法探討
- 工作效率優(yōu)化與創(chuàng)新思維的融合
- 工作效率提升工具及技術(shù)應(yīng)用
- 工作場所中的性別平等意識培養(yǎng)
- 工程中的動態(tài)力學(xué)校準(zhǔn)技術(shù)
- 工作安全分析與改善策略
- 工廠安全管理與風(fēng)險控制
- 從“瑞幸會計造假案”談會計誠信問題
- 食品配送業(yè)食品安全培訓(xùn)
- 崗位之間工作銜接配合安全與職業(yè)衛(wèi)生事項課件
- 華為IPD流程管理
- 保險經(jīng)紀(jì)行業(yè)保險安全培訓(xùn)
- 監(jiān)理抽檢表 - 04路基土石方工程
- 計算機網(wǎng)絡(luò)謝希仁第七版全套課件
- 安徽省成人腎病綜合征分級診療指南(2016年版)
- 動物生物化學(xué)第3版高職全套教學(xué)課件
- 重慶地區(qū)小(1)型水庫分布區(qū)及大小
- 辦學(xué)許可證申請書
評論
0/150
提交評論