C程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第1頁(yè)
C程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第2頁(yè)
已閱讀5頁(yè),還剩49頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、實(shí) 驗(yàn) 報(bào) 告 書(shū) 寫(xiě) 要 求實(shí)驗(yàn)報(bào)告原則上要求學(xué)生手寫(xiě),要求書(shū)寫(xiě)工整。若因課程特點(diǎn)需 打印的,標(biāo)題采用四號(hào)黑體,正文采用小四號(hào)宋體,單倍行距。紙張 一律采用 A4A4 的紙張。實(shí)驗(yàn)報(bào)告書(shū)寫(xiě)說(shuō)明實(shí)驗(yàn)報(bào)告中實(shí)驗(yàn)?zāi)康暮鸵?、?shí)驗(yàn)儀器和設(shè)備、實(shí)驗(yàn)內(nèi)容與過(guò)程、實(shí)驗(yàn)結(jié)果與分析這四項(xiàng)內(nèi)容為必需項(xiàng)。 教師可根據(jù)學(xué)科特點(diǎn)和實(shí)驗(yàn)具 體要求增加項(xiàng)目。填寫(xiě)注意事項(xiàng)(1 1)細(xì)致觀察,及時(shí)、準(zhǔn)確、如實(shí)記錄。(2 2)準(zhǔn)確說(shuō)明,層次清晰。(3 3)盡量采用專用術(shù)語(yǔ)來(lái)說(shuō)明事物。(4 4)外文、符號(hào)、公式要準(zhǔn)確,應(yīng)使用統(tǒng)一規(guī)定的名詞和符號(hào)。(5 5)應(yīng)獨(dú)立完成實(shí)驗(yàn)報(bào)告的書(shū)寫(xiě),嚴(yán)禁抄襲、復(fù)印,一經(jīng)發(fā)現(xiàn),以零分論處。實(shí)驗(yàn)報(bào)告批

2、改說(shuō)明實(shí)驗(yàn)報(bào)告的批改要及時(shí)、認(rèn)真、仔細(xì),一律用紅色筆批改。實(shí)驗(yàn) 報(bào)告的批改成績(jī)采用五級(jí)記分制或百分制,按金陵科技學(xué)院課堂教學(xué)實(shí)施細(xì)則中作業(yè)批閱成績(jī)?cè)u(píng)定要求執(zhí)行。實(shí)驗(yàn)報(bào)告裝訂要求實(shí)驗(yàn)批改完畢后,任課老師將每門(mén)課程的每個(gè)實(shí)驗(yàn)項(xiàng)目的實(shí)驗(yàn)報(bào) 告以自然班為單位、按學(xué)號(hào)升序排列,裝訂成冊(cè),并附上一份該門(mén)課 程的實(shí)驗(yàn)大綱。實(shí)驗(yàn)項(xiàng)目名稱:C#C#基礎(chǔ)編程實(shí)驗(yàn)學(xué)時(shí):同組學(xué)生姓名:_實(shí)驗(yàn)地點(diǎn):實(shí)驗(yàn)日期:1010 月 5 5 日-10-10 月 1919 日 實(shí)驗(yàn)成績(jī):批改教師:_批改時(shí)間:13181318實(shí)驗(yàn) 1 C#基礎(chǔ)編程一、實(shí)驗(yàn)?zāi)康? 1、 熟悉 VisualVisual StudioStudio .NET

3、.NET 開(kāi)發(fā)環(huán)境。2 2、 掌握 C#C#應(yīng)用程序的基本操作過(guò)程。3 3、 掌握 C#C#的數(shù)據(jù)類型,運(yùn)算符以及表達(dá)式的使用。4 4、 掌握分支和循環(huán)語(yǔ)句的使用方法。5 5、 掌握一維數(shù)組,二維數(shù)組及數(shù)組型數(shù)組的使用。二、實(shí)驗(yàn)要求(1 1)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄(2 2)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。三、實(shí)驗(yàn)設(shè)備、環(huán)境安裝有 VisualVisual StudioStudio .NET.NET 軟件。四、實(shí)驗(yàn)步驟1 1、 分析題意。2 2、 根據(jù)題目要求,新建項(xiàng)目。3 3、 編寫(xiě)并輸入相關(guān)的程序代碼。5 5、 運(yùn)行與調(diào)試項(xiàng)目。6 6、 保存項(xiàng)目。五、實(shí)驗(yàn)內(nèi)容1 1

4、、 編寫(xiě)一個(gè)簡(jiǎn)單的控制臺(tái)應(yīng)用程序,打印一行文字(如你的姓名)。using System;using System.Collectio ns.Ge neric;using System 丄 inq;using System.Text;n amespace on e.firstclass Programstatic void Main( stri ng args)System.Co nsole.WriteLi ne(我叫王蕾!);2 2、編寫(xiě)一個(gè)簡(jiǎn)單的 WindowsWindows 應(yīng)用程序,在窗體 LoadLoad 事件中書(shū)寫(xiě)代碼,標(biāo)簽中顯示你 的姓名。using System;using Sy

5、stem.Collectio ns.Ge neric;using System.Comp onen tModel;using System.Data;using System.Drawi ng;using System 丄 inq;using System.Text;using System.Wi ndows.Forms;n amespace on e.sec ondpublic partial class Forml : Formpublic Form1()In itializeComp onen t();private void Form1_Load(object sen der, Eve

6、 ntArgs e)this.Text = Win dows 程序;Label lblShow = new Label();IblShow.Location = new Point(20, 30);IblShow.AutoSize = true;IblShow.Text =王蕾!; this.Co ntrols.Add(lblShow);3 3、 編寫(xiě)一個(gè)一個(gè)程序, 用來(lái)判斷輸入的是大寫(xiě)字母, 小寫(xiě)字母, 數(shù)字還是其他的字符。using System;using System.Collectio ns.Ge neric;using System.Text;n amespace on e.thi

7、rdclass Programstatic void Main( stri ng args)Con sole.WriteLi ne(請(qǐng)輸入一個(gè)字符:); char c = Convert.ToChar(C on sole.ReadL in e(); if(c=a&c=A&c=Z) Console.WriteLine(這是一個(gè)字母);if (char.IsDigit(c)Con sole .WriteLi ne(這是一個(gè)數(shù)字);4 4、分別用 whilewhile , do-whiledo-while , forfor 循環(huán)求 1 1 到 100100 的和。using Syst

8、em;using System.Collectio ns.Ge neric;using System.Text;n amespace on e.forth. oneclass Programstatic void Main( stri ng args)int i = 1, sum = 0;while (i = 100)sum = sum + i;i+;Console.WriteLine(1 到 100 的自然數(shù)之和為:using System;using System.Collectio ns.Ge neric;using System.Text;n amespace on e.forth.t

9、woclass Programstatic void Main( stri ng args)int i = 1, sum = 0;dosum = sum + i;i+;while (i = 100);Con sole .WriteLi ne(1 到 100 的自然數(shù)的和為:+ sum);+ sum );using System;using System.Collectio ns.Ge neric;using System.Text;n amespace on e.forth.threeclass Programstatic void Main( stri ng args)int i , sum

10、 = 0;for (i = 1; i = 100; i+)sum = sum + i;Console.WriteLine(1 到 100 的自然數(shù)的和為:+ sum);5 5、 定義一個(gè)一維數(shù)組,用隨機(jī)數(shù)為此賦值,用foreachforeach 循環(huán)輸出其中的內(nèi)容。using System;using System.Collectio ns.Ge neric;using System 丄 inq;using System.Text;n amespace first.fiveclass Programstatic void Main( stri ng args)in t a = 0,123,4;

11、foreach (int i in a)Co nsole.WriteL in e(ai);6 6、實(shí)現(xiàn)二維數(shù)組的輸入和輸出。using System;using System.Collectio ns.Ge neric;using System 丄 inq;using System.Text;n amespace first.six class Program static void Main( stri ng args) in t, a = new in t2, 3 1,2, 3 , 4, 5, 6 ; for (int i = 0; i 2; i+)for (i nt j = 0; j 3

12、; j+) Console.WriteLine(ai, j); 7 7、實(shí)現(xiàn)數(shù)組型數(shù)組的輸入和輸出。using System;using System.Collectio ns.Ge neric;using System 丄 inq;using System.Text;n amespace first.seve n class Program static void Main( stri ng args) in t a = new in t new in t 1,2, 3 , new in t 4, 5, 6 ; for(int i = 0; i a.Le ngth; i+)for (i nt

13、 j = 0; j ai.Le ngth; j+) Con sole.WriteLi ne(aij);剛開(kāi)始編程的時(shí)候覺(jué)得無(wú)從下手,盡管我們已經(jīng)學(xué)了好幾種高級(jí)編程語(yǔ)言,但每個(gè)都 六、實(shí)驗(yàn)體會(huì)(遇到問(wèn)題及解決辦法,編程后的心得體會(huì))有其獨(dú)特的地方,稍不留神就會(huì)混淆。通過(guò)這次實(shí)驗(yàn),我體會(huì)到課后復(fù)習(xí)鞏固的重要性。在編程的時(shí)候,很多內(nèi)容都不記得,需要去翻書(shū)。不得不說(shuō),實(shí)驗(yàn)是鞏固課程的好方法!本次實(shí)驗(yàn),我熟悉VisualVisual StudioStudio .NET.NET開(kāi)發(fā)環(huán)境;掌握了 C#C#應(yīng)用程序的基本操作過(guò)程;掌握了C#C#勺數(shù)據(jù)類型,運(yùn)算符以及表達(dá)式的使用;掌握了分支和循環(huán)語(yǔ)句的使用方法

14、以及一維數(shù)組,二維數(shù)組及數(shù)組型數(shù)組的使用。實(shí)驗(yàn)項(xiàng)目名稱:類與對(duì)象實(shí)驗(yàn)學(xué)時(shí):同組學(xué)生姓名:_ 實(shí)驗(yàn)地點(diǎn):實(shí)驗(yàn)日期:1010 月 2626 日-11-11 月 9 9 日 實(shí)驗(yàn)成績(jī):批改教師:_批改時(shí)間:13181318實(shí)驗(yàn) 2 類與對(duì)象一、實(shí)驗(yàn)?zāi)康摹⒁?1) 掌握類的定義和使用;(2) 掌握類的數(shù)據(jù)成員,屬性的定義和使用;(3) 掌握方法的定義,調(diào)用和重載以及方法參數(shù)的傳遞;(4) 掌握構(gòu)造函數(shù)的定義和使用。二、實(shí)驗(yàn)要求(1) 編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄;(2) 做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。三、實(shí)驗(yàn)設(shè)備、環(huán)境安裝有 VisualVisual StudioStudio

15、.NET.NET 軟件。四、實(shí)驗(yàn)步驟1 1、分析題意;2 2、根據(jù)題目要求,新建項(xiàng)目;3 3、編寫(xiě)并輸入相關(guān)的程序代碼;5 5、運(yùn)行與調(diào)試項(xiàng)目;6 6、保存項(xiàng)目。五、實(shí)驗(yàn)內(nèi)容1 1、定義一個(gè)方法,實(shí)現(xiàn)兩個(gè)數(shù)的交換(分別把參數(shù)按值傳遞和按引用傳遞)using System;using System.Collectio ns.Ge neric;using System.Text;n amespace sec ond.oneclass Programstatic void Main( stri ng args)Swaper s = new Swaper();Console.WriteLine(輸入

16、x 的值:);int a = Co nvert.Tol nt32(Co nsole.ReadL in e();Console.WriteLine(輸入 y 的值:);int b=Co nvert.Tol nt32(Co nsole.ReadL in e();Con sole.WriteLi ne(s.Swap(a, b);Con sole.WriteLi ne(s.Swap(ref a,ref b);class Swaperpublic stri ng Swap(i nt x, int y)int temp;temp = x;x = y;y = temp;returnstri ng.Forma

17、t(” 按值傳參交換之后:x=0,y=1,x,y);public stri ng Swap(ref int x, ref int y)int temp;temp = x;x = y;y = temp;return stri ng.Format(” 按引用傳參交換之后:x=0,y=1,x, y);2 2、定義一個(gè)方法,實(shí)現(xiàn)數(shù)組的排序。using System;using System.Collectio ns.Ge neric;using System.Text;n amespace sec on d.twoclass Programpublic class sortpublic void ch

18、a nge(i nt a)Con sole.WriteLi ne(排序前,數(shù)組順序?yàn)椋?;show(a);int i, j, m;for (i = 0; i = 0 & m aj)/判斷 i 下標(biāo)的數(shù)是否大于 j下標(biāo)的數(shù)aj + 1 = aj;/如果 i 下標(biāo)大于 j 把 j 往后移一個(gè)位j-;aj+1 = m; /當(dāng)不大于 j 的時(shí)候就把M的值放到 i 下標(biāo)下面 j+1是為了卜標(biāo)減到取前時(shí)考慮-1 + 1 還是卜標(biāo)的取前面Con sole.WriteLi ne(排序后,數(shù)組順序?yàn)椋?; show(a);void show(i nt a)int i;for (i = 0; i 10;

19、i+)Co nsole.Write(0 , ai);Con sole.WriteLi ne();static void Main( stri ng args)in t a = 4, 7, 1,2, 5, 8, 9, 10, 3, 6 ;sort s=new sort();s.cha nge(a);3 3、定義一個(gè)學(xué)生類,把學(xué)生類當(dāng)作對(duì)象來(lái)傳遞。using System;using System.Collectio ns.Ge neric;using System 丄 inq;using System.Text;n amespace sec on d.threeclass Programpubl

20、ic class stude ntpublic void st()int a = 999;public class stpublic void aa(stude nt s)Con sole.WriteL in e(s);static void Main( stri ng args)stude nt s=new stude nt();st si = new st();s1.aa(s);4 4、定義一個(gè)方法,求兩個(gè)數(shù)的和和差,通過(guò)參數(shù)把這兩個(gè)值帶回。using System;using System.Collectio ns.Ge neric;using System 丄 inq;using Sy

21、stem.Text;n amespace sec on d.fourclass Programpublic class sumpublic void ab(out int m, out int n ,i nt a, int b)m = a + b;n = a - b;static void Main( stri ng args)sum s = new sum();int a = 10;int b = 3;int m, n;s.ab(out m, out n, a, b);Co nsole.WriteL in e(0+1=2;0-1=3,a,b,m,n);5 5、用構(gòu)造函數(shù)重載,實(shí)現(xiàn)矩形的面積,

22、圓的面積,梯形的面積;using System;using System.Collectio ns.Ge neric;using System 丄 inq;using System.Text;n amespace sec on g.fiveclass Programpublic class squarepublic double area;public square() public square(double a)area = a * a * 3.14;public square(double a, double b)area = a * b;public square(double a,

23、double b, double h)area = (a + b) / 2 * h;static void Main( stri ng args)double a, b, h,area; a = 2; b = 5; h = 3;square s = new square(a,b);Console.WriteLine(求矩形面積,長(zhǎng)為 a=0,寬為 b=1,面 積area 二2,a,b,s.area);square i = new square(a);Console.WriteLine(求圓形面積,半徑 a=0,面積 area=1, a,i.area);square j = new square

24、(a, b, h);Console.WriteLine (求梯形面積,上底為 a=0, 下底為 b=1,高為 h=2面積 area=3, a, b,h, j.area);6 6、設(shè)計(jì)一個(gè) windowswindows 應(yīng)用程序,在該程序中定義一個(gè)學(xué)生類和班級(jí)類,以處理每個(gè)學(xué)生的學(xué)號(hào),姓名,語(yǔ)文,數(shù)學(xué)和英語(yǔ)成績(jī),要求:1 1)能查詢每個(gè)學(xué)生的總成績(jī)。2 2)能顯示全班前二名的名單。3 3)能顯示單科成績(jī)最高分和不及格的學(xué)生名單。4 4)能統(tǒng)計(jì)全班學(xué)生的平均成績(jī)。5 5)能顯示各科成績(jī)不同分?jǐn)?shù)段的學(xué)生人數(shù)的百分比。Stude nt 類:using System;using System.Colle

25、ctio ns.Ge neric;using System.Text;n amespace Test2_6public class Stude ntpublic stri ng stuNo;public stri ng n ame;public double chin ese;public double math;public double en glish;public double sumScoreget return chi nese + math + en glish; Stude ntList 類:using System;using System.Collectio ns.Ge n

26、eric;using System.Text;n amespace Test2_6public class Stude ntList:Stude ntint snu ms;public Student stu=new Student50;public Stude ntList()snums = 0;public void addstu(Stude nt s)stus nu ms = s;snu ms+;public int searchstu(stri ng n ame)int i;for (i = 0; i snu ms; i+)if (stui. name = n ame) break;i

27、f (i = snu ms) retur n -1; else return i;/給所有成績(jī)排序,用后面實(shí)現(xiàn)前三名的排名public void ProThree()for (int i = 0; i snu ms; i+)int k = i;for (int j = i + 1; j stuk.sumScore) k = j; if (k != i)Stude nt temp;temp = stuk;stuk = stui;stui = temp;顯示單科成績(jī)的最高分public int HighScore(i nt k)int p = 0;if (k = 0)for (int i = 1;

28、 i stup.math) p = i;else if (k = 1)for (int i = 1; i stup.chi nese) p = i;elsefor (int i = 1; i stup.chi nese) p = i;return p;顯示不及格名單public stri ng BuhgName(i nt k)stri ng n ame=;if (k = 0)for (int i = 0; i snu ms; i+)if (stui.math 60) name +=+n;else if (k = 1)for (int i = 0; i snu ms; i+)i

29、f (stui.ch in ese 60) n ame += stui. name + n;elsefor (int i = 0; i snu ms; i+)if (stui.english 60) name += + n;return n ame;public stri ng getHL()stri ng Maxer = , Loser =;Maxer +=單科數(shù)學(xué)最高:+ stuHighScore(0). name + n;Maxer +=單科語(yǔ)文最高:+ stuHighScore(1).name + n;Maxer +=單科英語(yǔ)最高:+ stuHighScore(2)

30、.name + n;Loser +=單科數(shù)學(xué)掛科名單:+BuhgName(0) + n;Loser +=單科語(yǔ)文掛科名單:+ BuhgName(1) + n;Loser +=單科英語(yǔ)掛科名單:+ BuhgName(2) + n;retur n Maxer + n + Loser;/全班的平均成績(jī)public stri ng SumScore()double sum = 0;double avg=0;for (int i = 0; i snu ms; i+)sum = sum + stui.sumScore;avg = sum / snu ms; return 班級(jí)總分平均分:+avg;各科成績(jī)

31、不同分?jǐn)?shù)段的學(xué)生百分比/英語(yǔ)成績(jī)各分?jǐn)?shù)段百分比public stri ng PerC()double per1, per2, per3, per4, per5;double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 =0;for (int i = 0; i 90) & (stui.chi nese = 100)sumC1+;else if (80 = stui.chinese) & (stui.chinese 90)sumC2+;else if(70=stui.chi nese )& (stui.chi nese

32、 80)sumC3+;else if(60=stui.chinese)&(stui.chinese 70)sumC4+;elsesumC5+;per1 = sumC1 / snu ms;per2 = sumC2 / snu ms;per3 = sumC3 / snu ms;per4 = sumC4 / snu ms;per5 = sumC5 / snu ms;return 語(yǔ)文成績(jī)百分比:+n+90100:+per1+ 8090:+per2+8070:+per3+7060:+per4+ 60 以下的:+per5;/數(shù)學(xué)成績(jī)各分?jǐn)?shù)段百分比public stri ng PerM()doub

33、le per1, per2, per3, per4, per5;double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 =0;for (int i = 0; i 90) &(stui.math = 100)sumC1+;else if (80 = stui.math) & (stui.math 90)sumC2+;else if (70 = stui.math) & (stui.math 80)sumC3+;else if (60 = stui.math) & (stui.math 70)sumC4+;e

34、lse sumC5+; perl = sumCI / snu ms;per2 = sumC2 / snu ms;per3 = sumC3 / snu ms;per4 = sumC4 / snu ms;per5 = sumC5 / snu ms;return stri ng.Format(” 數(shù)學(xué)成績(jī)百分比:+ n + 90T00: + per1 + 8090:+ per2 + 8070: + per3 + 7060: + per4 + 60 以下的:+ per5);/英語(yǔ)成績(jī)各分?jǐn)?shù)段百分比public stri ng PerE()double per1, per2, per3, per4, p

35、er5;double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 =0;for (int i = 0; i 90) & (stui.english = 100)sumC1+;else if (80 = stui.english) & (stui.english 90)sumC2+;else if (70 = stui.english) & (stui.english 80)sumC3+;else if (60 = stui.english) & (stui.english 70)sumC4+;else su

36、mC5+; per1 = sumC1 / snu ms;per2 = sumC2 / snu ms;per3 = sumC3 / snu ms;per4 = sumC4 / snu ms;per5 = sumC5 / snu ms;return stri ng.Format(” 數(shù)學(xué)成績(jī)百分比:+ n + 90100: + per1 + 8090:+ per2 + 8070: + per3 + 7060: + per4 + 60 以下的:+ per5);From 窗體代碼:using System;using System.Collectio ns.Ge neric;using System.

37、Comp onen tModel;using System.Data;using System.Drawi ng;using System.Text;using System.Wi ndows.Forms;n amespace Test2_6public partial class Forml : Formpublic Form1()In itializeComp onen t();public Stude ntList sl = new Stude ntList();private void btnAdd_Click(object sender, EventArgs e)Stude nt s

38、 = new Stude nt();s.stuNo = txtStuNo.Text;s.n ame = txtName.Text; qs.chi nese = Conv ert.ToDouble(txtCh in a.Text);s.math = Co nvert.ToDouble(txtMath.Text);s.e nglish = Conv ert.ToDouble(txtE ng.Text); sl.addstu(s);MessageBox.Show(添加成功);private void btnSearch_Click(object sender, EventArgs e)int pos

39、 = sl.searchstu(this.textBox1.Text);if (pos != -1)label7.Text = this.textBox1.Text + 的總成績(jī):+sl.stupos.sumScore;else MessageBox.Show(不 存在這個(gè)人! ); private void btnFinish_Click(object sender, EventArgs e)label7.Text =前 3 名:+n;for (int i = 0; i 3; i+)sl.ProThree();label7.Text+= sl.stui. name+n;label7.Text

40、 += sl.getHL()+n;label7.Text += Co nvert.ToStri ng(sl.SumScore()+n;label7.Text += sl.PerC()+n;label7.Text += sl.PerM()+n;label7.Text += sl.PerE()+n;六、實(shí)驗(yàn)體會(huì)(遇到問(wèn)題及解決辦法,編程后的心得體會(huì))通過(guò)本次實(shí)驗(yàn),我掌握了類的定義與使用;掌握了類的數(shù)據(jù)成員,屬性的定 義和使用;掌握了方法的定義,調(diào)用和重載以及方法參數(shù)的傳遞以及構(gòu)造函數(shù)的 定義和使用。值得注意的是:本次實(shí)驗(yàn)中 retur n 的使用以及所在的位置,類型 轉(zhuǎn)換時(shí)也經(jīng)常用到實(shí)驗(yàn)項(xiàng)目名稱:

41、繼承與多態(tài)實(shí)驗(yàn)學(xué)時(shí):同組學(xué)生姓名:_ 實(shí)驗(yàn)地點(diǎn):實(shí)驗(yàn)日期:1111 月 1616 日-11-11 月 3030 日 實(shí)驗(yàn)成績(jī):批改教師:_ 批改時(shí)間:13181318實(shí)驗(yàn) 3 繼承與多態(tài)、實(shí)驗(yàn)?zāi)康?、要?1) 掌握類的繼承性與多態(tài)性;(2) 掌握虛方法的定義以及如何使用虛方法實(shí)現(xiàn)多態(tài);(3) 掌握抽象類的定義以及如何使用抽象方法實(shí)現(xiàn)多態(tài);二、實(shí)驗(yàn)要求(1) 編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄;(2) 做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。三、實(shí)驗(yàn)設(shè)備、環(huán)境安裝有 VisualVisual StudioStudio .NET.NET 軟件。四、實(shí)驗(yàn)步驟1 1、分析題意;2 2、根據(jù)題目要

42、求,新建項(xiàng)目;3 3、編寫(xiě)并輸入相關(guān)的程序代碼;5 5、運(yùn)行與調(diào)試項(xiàng)目;6 6、保存項(xiàng)目。五、實(shí)驗(yàn)內(nèi)容1 1、設(shè)計(jì)一個(gè) WindowsWindows 應(yīng)用程序,在該程序中首先構(gòu)造一個(gè)學(xué)生基本類,再分別構(gòu)造小 學(xué)生、中學(xué)生、大學(xué)生派生類,當(dāng)輸入相關(guān)數(shù)據(jù),單擊不用的按鈕時(shí),將分別創(chuàng)建不同的學(xué) 生類對(duì)象,并輸出當(dāng)前學(xué)生的總?cè)藬?shù),該學(xué)生的姓名,學(xué)生類型,平均成績(jī)。Stude nt 類:using System;using System.Collectio ns.Ge neric;using System.Text;n amespace Test3_1public abstract class Stud

43、e ntprotected stri ng n ame;protected int age;public static int number;public Stude nt(stri ng n ame, int age)this. name = n ame;this.age = age;nu mber+;public stri ng Nameget retur n n ame; public abstract double Average。;public class Pupil : Stude ntprotected double chin ese;protected double math;

44、public Pupil(stri ng n ame, int age, double chin ese, double math):base( name, age)this.ch in ese = chin ese;this.math = math;public override double Average()retur n (chi nese + math) / 2;public class Middle : Stude ntprotected double chin ese;protected double math;protected double en glish;public M

45、iddle(string name, int age, double chinese, double math,double en glish):base( name, age) this.ch in ese = chin ese;this.math = math;this.e nglish = en glish;public override double Average。retur n (chi nese + math + en glish) / 3;public class College : Stude ntprotected double required;protected dou

46、ble elective;public College(stri ng n ame, int age, double required, doubleelective) :base( name, age)this.required = required; this.elective = elective;public override double Average()return (required + elective) / 2;Form 窗體內(nèi)的代碼:using System.Collectio ns.Ge neric;using System.Comp onen tModel;using

47、 System.Data;using System.Drawi ng;using System.Text;using System.Wi ndows.Forms;n amespace Test3_1public partial class Form1 : Formpublic Form1()In itializeComp onen t();private void bt nSmall_Click(object sen der, Eve ntArgs e)Pupilp=newPupil(txtName.Text,Co nvert.Tol nt32(txtAge.Text),Co nvert.To

48、Double(txtChinese.Text ),Co nvert.ToDouble(txtMath.Text);lblShow.Text +=總?cè)藬?shù):+Convert.ToString( Student.number) +, + 姓名:+ p.Name + , + 小學(xué)生+ , + 平均成績(jī)?yōu)椋? p.Average() +n;private void btnMiddle_Click(object sender, EventArgs e)Middle m = new Middle(txtName.Text,Con vert.Tol nt32(txtAge.Text),Con vert.ToD

49、ouble(txtChi nese.Text),Co nvert.ToDouble(txtMath.Text),Co nvert.ToDouble(TxtE nglish.Text);lblShow.Text +=總?cè)藬?shù):+ Convert.ToString(Student.number) +, + 姓名:+ m.Name + , + 中學(xué)生+ , + 平均成績(jī)?yōu)椋? m.Average() + n;private void btnBig_Click(object sender, EventArgs e)College c = new College(txtName.Text,Con vert

50、.Tol nt32(txtAge.Text),Con vert.ToDouble(txtChi nese.Text),Co nvert.ToDouble(txtMath.Text);lblShow.Text +=總?cè)藬?shù):+ Convert.ToString(Student.number) +, + 姓名:+ c.Name + , + 大學(xué)生+ , + 平均成績(jī)?yōu)椋? c.Average() + n;2 2、設(shè)計(jì)一個(gè) WindowsWindows 應(yīng)用程序,在該程序中定義平面圖形抽象類和派生類圓,矩形和 三角形。Figure 類代碼:using System;using System.Colle

51、ctio ns.Ge neric;using System.Text;n amespace Test3_2public abstract class Figurepublic abstract double Area();public class Circle:Figuredouble radius;public Circle(double r)radius = r;public override double Area()retur n radius * radius * 3.14;public class JUx in g:Figuredouble cha ng;double kua n;

52、public JUx in g(double c, double k) this.cha ng = c;this.kua n = k;public override double Area()retur n cha ng * kua n;public class San: Figuredouble bia n;double heigth;public San( double b, double h) this.bia n = b;this.heigth = h;public override double Area()retur n bia n * heigth / 2;Form 窗體代碼:u

53、sing System;using System.Collectio ns.Ge neric;using System.Comp onen tModel;using System.Data;using System.Drawi ng;using System.Text;using System.Wi ndows.Forms;n amespace Test3_2public partial class Forml : Formpublic Form1()In itializeComp onen t();private void btn Circle_Click(object sen der, E

54、ven tArgs e)Circle c=new Circle(Convert.Tolnt32(TxtChang.Text);IblShow.Text =圓的面積為:+ c.Area(); private void btnJu _Click(object sen der, Eve ntArgs e)JUx ingj=newJUx in g(Co nvert.Tol nt32(TxtCha ng.Text),Co nvert.Tol nt32(TxtHigh.Text);lblShow.Text =矩形的面積為:+ j.Area();private void btnSan_Click(objec

55、t sender, EventArgs e)San s = newSan (Co nvert.Tol nt32(TxtChang.Text),Co nvert.Tol nt32(TxtHigh.Text);lblShow.Text =三角形的面積為:+ s.Area();3 3、定義一個(gè) PersonPerson 類,包含姓名字段和一個(gè)方法,早上& 3030 學(xué)生開(kāi)始上課,教師開(kāi)始講課。分別用 newnew 關(guān)鍵字,虛方法,抽象類實(shí)現(xiàn)多態(tài)性。New 關(guān)鍵字:using System;using System.Collectio ns.Ge neric;using System.Text

56、;n amespace third.three class Programstatic void Main( stri ng args)Stude nt s=new Stude nt(學(xué)生); Teacher t=new Teacher(教 師);Con sole.WriteLi ne(s. name+s.work();Con sole.WriteLi ne(t. name+t.work();Con sole.ReadL in e();public class Pers onpublic stri ng n ame;public in terface method stri ng work()

57、;public class Stude nt:Pers onpublic Stude nt(stri ng n ame) this .n ame = n ame; public stri ng work() return 早上 8:30 開(kāi)始上課;public class Teacher:Pers onpublic Teacher (stri ng n ame) this .n ame = n ame; public stri ng work() return 開(kāi)始講課;虛方法:using System;using System.Collectio ns.Ge neric;using Syst

58、em.Text;n amespace third.three.twoclass Programstatic void Main( stri ng args)Student s = new Student(張三,學(xué)生);Pers on Work(s);Teacher t=n ew Teacher(李 斯,教師);Perso nWork(t);private static void Pers on Work(Pers on Pers on) Co nsole.WriteLi ne(Pers on .Work(); public class Pers onpublic stri ng n ame;p

59、ublic Pers on( stri ng n ame) this .n ame = n ame; public virtual stri ng Work() return string.Format(Person0:早上 8:30 開(kāi)始,name);public class Stude nt : Pers onprivate stri ng type;public Stude nt(stri ng n ame, stri ng type):base( name) this.type = type; public override stri ng Work()return string.Fo

60、rmat(Person0:早上 8:30 開(kāi)始上課,name);public class Teacher : Pers onprivate stri ng type;public Teacher(stri ng n ame, stri ng type):base( name) this.type = type; public override stri ng Work()return string.Format(Person0:開(kāi)始講課,name);抽象類:using System;using System.Collectio ns.Ge neric;using System.Text;n amespace third.three.threeclass Programstatic void Main( stri n

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論