《Visualc#程序》課程設計報告_第1頁
《Visualc#程序》課程設計報告_第2頁
《Visualc#程序》課程設計報告_第3頁
《Visualc#程序》課程設計報告_第4頁
《Visualc#程序》課程設計報告_第5頁
已閱讀5頁,還剩31頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、瓊州學院電子信息工程學院visual c# 2010程序課程設計報告項目名稱: .net程序設計實驗課程建設 專 業: 計算機科學與技術 年 級: 2010級 姓 名: 李欣 徐嘉 學 號: 10237058 10237070 指導老師: 熊志斌 2013年6月實 驗實驗1 c#語法基礎實驗目的:(1)掌握visual studio 2010 集成開發環境的使用方法;(2)掌握c#應用程序的基本結構、開發過程和方法;(3)掌握c#語言的基礎知識及使用。實驗要求:編寫簡單c#控制臺應用程序:(1)定義學生成績的數組;(2)對學生成績排序;(3)按照從高分到低分順序輸出,并判斷等級。實驗準備和說明

2、:(1)具備知識:數組定義,排序算法,選擇結構的使用。(2)準備本次上機所需要的程序。(3)創建本次實驗項目文件夾“visual studio 2010projects實驗1”實驗內容和步驟:1啟動visual studio 2010打開計算機,啟動microsoft visual studio2010系統。2創建工程并添加代碼 選擇“文件”“新建項目”菜單命令,顯示出“新建項目”對話框。單擊“已安裝的模版”選項,在左邊的列表框中選擇visual c#語言,在右邊的列表框中選擇“控制臺應用程序”在項目名稱框中輸入text。單擊瀏覽按鈕.將項目定位到文件夾 “ visual studio 201

3、0projects實驗3”。 using system; using system.collections.generic; using system.linq; using system.text; namespace test class program static void main(string args) int i, j, temp = 0; int sco = new int8 56, 77, 90, 76, 88, 57, 94, 81 ; /定義、初始化成績的數組 string grade = new string8; /定義成績等級數組 for (i = 0; i <

4、; sco.length; i+) for (j = i + 1; j < sco.length; j+) if (scoi < scoj) temp=scoi; scoi = scoj; /對學生成績排序 scoj = temp; for (i = 0; i < sco.length; i+) /判斷等級 int n = scoi / 10; switch(n) case 10:gradei = "優秀" break; case 9:gradei = "優秀"break; case 8:gradei = "良好"

5、break; case 7: gradei = "中等" break; case 6: gradei = "及格" break; default: gradei = "不及格" break; console.writeline("排序后的分數及等級"); for (i = 0; i < sco.length; i+) console.writeline("num0:1-2",i+1,scoi,gradei); console.readline(); 3運行并測試運行程序,結果如圖1-1所示

6、。圖1-1學生成績排序圖4寫出實驗報告實驗2 繼承與多態實驗目的:(1)初步掌握面向對象程序設計封裝的概念;(2)掌握類的設計、屬性和運算符重載;(3)了解屬性和運算符重載的作用。實驗要求:設計并編程實現一個復數類,要求:(1)封裝類中的實部和虛部,通過屬性提供對外訪問接口;(2)重載加、減、乘、除四則運算;(3)設計一個類測試復數類;實驗準備和說明:(1)具備知識:c#封裝、屬性、重載。(2)準備本次上機所需要的程序。(3)創建本次實驗項目文件夾“visual studio 2010projects實驗2”實驗內容和步驟:1啟動visual studio 2010打開計算機,啟動micros

7、oft visual studio2010系統。2創建工程并添加代碼 選擇“文件”“新建項目”菜單命令,顯示出“新建項目”對話框。單擊“已安裝的模版”選項,在左邊的列表框中選擇visual c#語言,在右邊的列表框中選擇“控制臺應用程序”在項目名稱框中輸入consolecomplex。單擊瀏覽按鈕.將項目定位到文件夾 “ visual studio 2010projects實驗2”。 using system; using system.collections.generic; using system.linq; using system.text; namespace consolecom

8、plex class complex private double real, image; /聲明私有域real, image public double real /定義公有屬性real封裝類中的實部 set real = value; get return real; public double image /定義公有屬性image封裝類中的虛部 set image = value; get return image; public static complex operator +(complex c1, complex c2) /加號運算符重載 complex c = new com

9、plex(); c.real = c1.real + c2.real; c.image = c1.image + c2.image; return c; public static complex operator - (complex c1, complex c2) /減號運算符重載 complex c = new complex(); c.real = c1.real - c2.real; c.image = c1.image - c2.image; return c; public static complex operator * (complex c1, complex c2) /乘

10、號運算符重載 complex c = new complex(); c.real = c1.real * c2.real - c1.image * c2.image; c.image = c1.real * c2.image + c1.image * c2.real; return c; public static complex operator / (complex c1, complex c2) /除號運算符重載 complex c = new complex(); double r, a; r = math.sqrt(c1.real * c1.real + c1.image * c1.

11、image) / (c2.real * c2.real + c2.image * c2.image); a = math.atan(c1.image / c1.real) - math.atan(c2.image / c2.real); c.real = r * math.cos(a); c.image = r * math.sin(a); return c; class program static void main(string args) complex c = new complex(), c1 = new complex(), c2 = new complex(); c1.real

12、 = 1; c1.image = 2; c2.real = 3; c2.image = 4; console.writeline("c1= "+c1.real.tostring() + " + " + c1.image.tostring() + "i"); console.writeline("c2= " + c2.real.tostring() + " + " + c2.image.tostring() + "i"); console.writeline(">

13、;>>>>>>><<<<<<<<<"); c = c1 + c2; console.writeline(c.real.tostring() + " + " + c.image.tostring() + "i"); c = c1 - c2; console.writeline(c.real.tostring() + " + " + c.image.tostring() + "i"); c = c1 * c2; c

14、onsole.writeline(c.real.tostring() + " + " + c.image.tostring() + "i"); c = c1 / c2; console.writeline(c.real.tostring() + " + " + c.image.tostring() + "i"); console.readline(); 3運行并測試運行程序,結果如圖2-1所示。圖2-1復數的加減乘除示例圖實驗3 繼承與多態實驗目的:(1)初步掌握面向對象程序設計繼承和多態的概念;(2)掌握繼承和多

15、態的c#實現方法;(3)了解繼承和多態的作用。實驗要求:設計基本幾何圖形的繼承層次結構,并編程實現其中的類, 封裝類中的所有字段,通過屬性提供對外的接口;使用繼承實現幾何圖形的層次結構(3層);計算各種圖形的面積和周長;實驗準備和說明:(1)具備知識:c#繼承、接口、抽象方法、虛方法、多態。(2)準備本次上機所需要的程序。(3)創建本次實驗項目文件夾“visual studio 2010projects實驗3”實驗內容和步驟:1啟動visual studio 2010打開計算機,啟動microsoft visual studio2010系統。2創建工程并添加代碼 選擇“文件”“新建項目”菜單命

16、令,顯示出“新建項目”對話框。單擊“已安裝的模版”選項,在左邊的列表框中選擇visual c#語言,在右邊的列表框中選擇“控制臺應用程序”在項目名稱框中輸入ji_cheng。單擊瀏覽按鈕.將項目定位到文件夾 “ visual studio 2010projects實驗3”。 在打開的文檔窗口中輸入下面的代碼: using system; using system.collections.generic; using system.linq; using system.text; namespace ji_cheng class program public abstract class sha

17、pe /抽象類 public abstract double area(); public abstract double length(); public class circle : shape /圓繼承shape類 public double r; /半徑 public double r / 屬性r get return r; set r = value; public override double area() /面積 return math.pi * math.pow(r, 2); public override double length() /周長 return math.pi

18、 * 2 * r; public circle(double r) this.r = r; public class square : shape /正方形繼承shape類 public double edge; /邊長 public double edge /屬性edge get return edge; set edge = value; public override double area() /重載area方法 return math.pow(edge, 2); public override double length() /重載length方法 return 4 * edge;

19、public square(double edge) this.edge = edge; class test static void main(string args) console.writeline("請輸入圓的半徑:"); double r = double.parse(console.readline(); shape p = new circle(r); console.writeline("圓的周長:0n圓的面積:1", p.length(), p.area(); console.writeline("請輸入正方形的邊長:&qu

20、ot;); double edge = double.parse(console.readline(); shape e = new square(edge); console.writeline("正方形的周長:0n正方形的面積:1", e.length(), e.area(); console.readline(); 3運行并測試編譯并運行程序,在提示命令符下輸入圓的半徑為5,按enter鍵,輸入正方形的邊長為4,按enter鍵,結果如圖3-1所示。圖3-1圓和正方形的周長及面積示例圖4寫出實驗報告 35實驗4 事件編程實驗目的:(1)掌握事件的聲明;(2)掌握事件處理

21、程序的原理及應用;(3)定義事件處理函數。實驗要求:設計一個賬戶類,包括賬號,姓名,余額及密碼等變量;定義成員變量的讀寫訪問函數;定義一個事件,當余額發生改變時觸發事件;設計一個類測試賬戶類中的事件。實驗準備和說明:(1)具備知識:方法的重載、事件的聲明與訂閱。(2)準備本次上機所需要的程序。(3)創建本次實驗項目文件夾“visual studio 2010projects實驗4”實驗內容和步驟:1啟動visual studio 2010打開計算機,啟動microsoft visual studio2010系統。2創建工程并添加代碼 選擇“文件”“新建項目”菜單命令,顯示出“新建項目”對話框。

22、在左邊的列表框中選擇visual c#語言,在右邊的列表框中選擇“控制臺應用程序”在項目名稱框中輸入changed。單擊瀏覽按鈕.將項目定位到文件夾 “ visual studio 2010projects實驗4”。在打開的文檔窗口中輸入下面的代碼:using system;using system.collections.generic;using system.linq;using system.text;namespace changed class account private string number; /定義成員變量域 private string name; private

23、double balanced; private string password; public delegate void changedeventhander(object sender, eventargs e); /定義委托代表類型 public event changedeventhander changed; /聲明事件 protected virtual void onchanged(eventargs e) /用以觸發事件 if (this.changed != null) this.changed(this, e); public double balance /定義bala

24、nce屬性 get return this.balanced; set this.balanced = value; /當balance屬性被修改時,觸發changed事件 this.onchanged(new eventargs(); public void account1(string number, string name, double balanced, string password) /存款 this.number = number; this.name = name; this.balanced += balanced; this.password = password; p

25、ublic void account2(string number, double balanced, string password) /取款 this.number = number; this.balanced -= balanced; this.password = password; public void outb() console.writeline("帳號:0", number); console.writeline("姓名:0", name); console.writeline("密碼:0", password)

26、; console.writeline("餘額:0", balanced); class test public static void main(string args) account d = new account(); /將事件處理程序添加到事件的調用列表中即訂閱事件 d.changed += new account.changedeventhander(d_changed); d.account1("12345678567887112", "陳好", 2000.0, "543221"); d.outb()

27、; d.account2("12345678567887112", 300.0, "543221"); d.outb(); string str = "" while (str != "0") console.writeline("please enter a string:"); str = console.readline(); d.balance = system.convert.todouble(str); d.outb(); /事件處理函數 private static void d_

28、changed(object sender, eventargs e) console.writeline("此人的余額已改變為:0n", (account)sender).balance); 3運行并測試編譯并運行程序,在提示命令符中輸入5000,顯示結果如圖4-1所示。圖4-1 賬戶信息變動圖實驗5 文本編輯器的設計與實現實驗目的要求目的:掌握面向對象程序設計方法以及winform開發技術。要求:設計并實現一個文本編輯器(1)實現新建文件、打開文件、保存文件、選擇字體、設置顏色功能;(2)添加下拉菜單和彈出菜單,用于選擇不同復制、粘貼、查找等文本編輯功能;(3)添加工具

29、欄,工具欄設置若干按鈕,用于選擇不同的文本編輯功能;(4)添加狀態欄,顯示狀態提示信息。實驗準備和說明(1)具備知識:winform的應用、菜單、工具欄、狀態欄。(2)準備本次上機所需要的程序。(3)創建本次實驗項目文件夾“visual studio 2010projects實驗5”實驗內容和步驟1啟動visual studio 2010打開計算機,啟動microsoft visual studio2010系統。2創建一個默認的對話框用用程序form1選擇“文件”“新建項目”菜單命令,顯示出“新建項目”對話框。在左邊的列表框中選擇visual c#語言,在右邊的列表框中選擇“windows窗體

30、應用程序”,在項目名稱框中輸入wen_ben。單擊瀏覽按鈕.將項目定位到文件夾 “ visual studio 2010projects實驗5”。3添加菜單在左邊的“工具箱”中選擇,在form1窗體上單擊即可顯示出菜單欄,在menustrip1屬性窗口中選擇“items”屬性,點擊打開“項集合編輯器”,在左上方下拉條中選擇menuitem,點擊“添加”,然后選中被添加的toolstripmenuitem1,將其text屬性更改為“文件(f)”。在項集合編輯器中按照上述步驟一次添加“編輯(e)”、“格式(o)”、“查看(v)”、“幫助(h)”菜單項,顯示效果如圖下圖。選中“文件”,單擊dropd

31、ownitems屬性,打開項集合編輯器,在左上方選擇menuitem,單擊“添加”,在成員列表中出現“toolstripmenuitem1”,在右邊的屬性列表中選擇text屬性,將toolstripmenuitem1改為“新建”,在shortcutkeys屬性中添加ctrl+n快捷鍵。根據表5-1所示菜單項,參照圖5-1設計“文件”菜單。表5-1 “文件”菜單添加的成員成員nametextshortcutkeys新建(n)新建toolstripmenuitem新建ctrl+n打開(o)打開otoolstripmenuitem打開ctrl+o保存(s)保存stoolstripmenuitem保存

32、ctri+s另存為另存為toolstripmenuitem另存為頁面屬性頁面屬性toolstripmenuitem頁面屬性打印(p)打印ptoolstripmenuitem打印退出退出toolstripmenuitem退出ctrl+e根據表5-2所示菜單項,參照圖5-2設計“編輯”菜單。表5.2 “編輯”菜單添加的成員成員nametextshortcutkeys撤銷(u)退出toolstripmenuitem撤銷ctri+u復制(c)復制ctoolstripmenuitem復制ctrl+c剪切(x)剪貼xtoolstripmenuitem剪貼ctrl+x粘貼(p)粘貼toolstripmenu

33、item粘貼ctrl+p刪除(l)刪除ltoolstripmenuitem刪除delete查找(f)查找toolstripmenuitem查找ctrl+f替換(r)替換toolstripmenuitem替換ctrl+h全選(a)全選atoolstripmenuitem全選ctrl+a時間/日期(d)時間/日期toolstripmenuitem時間/日期f5根據表5-3所示菜單項,參照圖5-3設計“格式”菜單。表5.3 “格式”菜單添加的成員成員nametextshortcutkeys自動換行(w)自動換行toolstripmenuitem自動換行字體(f)字體toolstripmenuitem

34、字體顏色顏色toolstripmenuitem顏色根據表5-4所示菜單項,參照圖5-4設計“查看”菜單。表5.4 “查看”菜單添加的成員成員nametextshortcutkeys狀態欄狀態欄toolstripmenuitem狀態欄根據表5-5所示菜單項,參照圖5-5設計“幫助”菜單。表5.5 “幫助”菜單添加的成員成員nametextshortcutkeys關于記事本(a)關于記事本toolstripmenuitem關于記事本 圖5-1文件菜單項 圖5-2 編輯菜單項 圖5-3 格式菜單項 圖5-4 查看菜單項 圖5-5 幫助菜單項4添加文本框在左邊的“工具箱”中選擇,在form1窗體上單擊

35、即可顯示出來。5添加窗體 在form1窗體中添加一個窗體命名為formfind,text屬性為“查找”,參看圖5-6的控件布局,為formfind對話框添加表5-6的一些控件圖5-6 “查看”對話框布局添加的控件nametextlabel1label1查找內容:textbox1textbox1button1button1查找button2button2取消表5.6 “查看”對話框添加的控件a. 在formfind中添加如下代碼public partial class formfind : form public formfind() initializecomponent(); public

36、richtextbox rtb; int start = 0;/查找的起始位置? string nr = ""/查找的內容 richtextboxfinds f = 0;b. 雙擊“查找”按鈕,添加如下代碼private void button1_click(object sender, eventargs e) nr = this.textbox1.text; /查找 start = rtb.find(nr, start, f); if (start = -1) messagebox.show("對不起查找不到與"+ nr +"相符的內容&q

37、uot;, "(記事本)查找提示", messageboxbuttons.ok, messageboxicon.asterisk); start = 0; else start = start + nr.length;/找到后從找到位置之后開始下一次 rtb.focus(); /給予焦點 c. 雙擊“取消”按鈕,添加如下代碼private void button2_click(object sender, eventargs e) close(); 為form1窗體添加一個窗體,命名為tihuan,text屬性為“替換”,參看圖5-7的控件布局,為tihuan對話框添加表5

38、.7的一些控件。圖5-7 “替換”對話框添加的控件nametextlabel1label1查找內容:textbox1textbox1label2label2替換為:textbox2textbox2button1button1查找下一個button2button2替換button3button3全部替換button4button4取消表5.7 “替換”對話框添加的控件a. 為tihuan對話框添加如下代碼public partial class tihuan : form public tihuan() initializecomponent(); public richtextbox rtb;

39、 int start = 0;/開始位置 string nr = ""/定義內容字符串 string th = ""/定義轉換字符串 richtextboxfinds f = 0; int i = 0;b. 雙擊“查找下一個”按鈕,添加下列代碼private void button1_click(object sender, eventargs e) nr = this.textbox1.text;/將textbox1中的內容賦值給 start = rtb.find(nr, start, f);/開始的位置是找到的內容 if (start = -1)/找

40、不到啊 messagebox.show("對不起查找不到與"+ nr +"內容相匹配的信息","記事本查找提示信息", messageboxbuttons.ok, messageboxicon.asterisk); start = 0;/返回開始位置 else start = start + nr.length;/找到后從找到位置之后開始下一次 rtb.focus(); /給予焦點,此處不解 c. 雙擊“替換”按鈕,添加下列代碼private void button2_click(object sender, eventargs e)

41、 nr = this.textbox1.text;/找的內容 th = this.textbox2.text;/替換的內容 / rtb.selectedtext = th; /start = rtb.find(nr, start, f); if (start = -1) messagebox.show("對不起查找不到與"+ nr + "內容相匹配的信息","記事本查找提示信息", messageboxbuttons.ok, messageboxicon.asterisk); else rtb.selectedtext = th; / start = start

溫馨提示

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

評論

0/150

提交評論