net程序设计实验指导_第1页
net程序设计实验指导_第2页
net程序设计实验指导_第3页
net程序设计实验指导_第4页
net程序设计实验指导_第5页
已阅读5页,还剩40页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验一 简单程序设计1熟悉visual studio.net的ide界面,练习窗口的浮动、停靠,以及工具栏的定制。2模仿书中的例子,编写“hello”程序。3学会使用帮助系统,在编写程序时使“动态帮助”始终打开,注意观察“动态帮助”窗口中内容的变化。4在“帮助”菜单下选择“对帮助的帮助”,阅读其中的内容,学习帮助的使用。/ this is our first example: c# version hello world!using system;class helloworldpublic static void main()console.writeline(hello world!);实

2、验二 c#.net面向对象程序设计实验1为某公司创建一个类来建立员工的人事记录:包括员工的姓名、性别、工资、到公司的日期、部门以及联系方式等信息。构建该类,并做出适当的测试。2从上面的类中派生出一个类,来记录公司干部的情况。包括职位、提职时间、管理的员工人数及姓名。3编写程序,使得一个大学书店可以用它来记录和确定教科书的零售价。所有计算应该用一个类textbook的实例来完成。这个类应该具有属性title(书名)、author(作者)、cost(批发费用)、quantity(库存量)和price(零售价)。同时假设零售价是批发价的1.25倍。4编写程序相加两个分数,并将它们的和以化简后的分数形

3、式表现出来。程序使用类fraction来存放分数的分子和分母,具有方法reduce来化简结果。using system;namespace testclassapp/ / class1 的摘要说明。/ class class1/ / 应用程序的主入口点。/ stathreadstatic void main(string args)/ todo: 在此处添加代码以启动应用程序/double len = 2.5;double wid = 3.0;double rad = 4.1;rectangle arect = new rectangle();arect.length = len;arect.

4、width = wid;circle acirc = new circle (rad);console.writeline (area of rect is:0,arect.area ();console.writeline (area of circ is:0,acirc.area ();abstract class shape/抽象基类,不可实例化public const double pi=3.14;/常量protected double x, y;/私有,可继承变量public shape()/默认构造函数x=y=0;public shape(double x,double y)/带参

5、数构造函数this.x = x;this.y = y;public abstract double area();/抽象方法,需重载class rectangle: shapepublic rectangle():base()public rectangle(double x, double y): base(x,y)/使用基类构造函数public override double area()/函数重载return (x*y);public double length/属性:矩形长度getreturn x;setif (value0)x = value;public double width/

6、属性:矩形宽度getreturn y;setif (value0)y = value;class ellipse: shapepublic ellipse(double x, double y):base(x,y)/使用基类shape的构造函数public override double area()/函数重载return pi*x*y;class circle: ellipsepublic circle(double r):base(r,0)/使用基类ellipse的构造函数public override double area()/函数重载return pi*x*x;实验三 文本编辑器设计

7、1假设有要排序的20个数存在文件data.txt中。编写程序,打开该文件并将排好序的数重新写回该文件。2重新打开第1题创建的文件,在文件的结尾再添加10个随机数。3参考windows的记事本程序,编写一个简单的文本编辑器程序。using system;using system.drawing;using system.collections;using system.componentmodel;using system.windows.forms;using system.data;namespace editerapp/ / form1 的摘要说明。/ public class form1

8、 : system.windows.forms.formprivate system.windows.forms.mainmenu mainmenu1;private system.windows.forms.menuitem menuitem1;private system.windows.forms.menuitem menuitem2;private system.windows.forms.menuitem menuitem3;private system.windows.forms.menuitem menuitem4;private system.windows.forms.men

9、uitem menuitem5;private system.windows.forms.menuitem menuitem6;private system.windows.forms.menuitem menuitem7;private system.windows.forms.openfiledialog openfiledialog1;private system.windows.forms.savefiledialog savefiledialog1;private system.windows.forms.richtextbox myrtbox; private system.win

10、dows.forms.statusbar mystatus; private icontainer components;public form1()/ windows 窗体设计器支持所必需的/initializecomponent();/ todo: 在 initializecomponent 调用后添加任何构造函数代码/ / 清理所有正在使用的资源。/ protected override void dispose( bool disposing )if( disposing )if (components != null) components.dispose();base.dispos

11、e( disposing );#region windows form designer generated code/ / 设计器支持所需的方法 - 不要使用代码编辑器修改/ 此方法的内容。/ private void initializecomponent() ponents = new system.componentmodel.container(); this.myrtbox = new system.windows.forms.richtextbox(); this.mystatus = new system.windows.forms.statusbar(); this.main

12、menu1 = new system.windows.forms.mainmenu(ponents); this.menuitem1 = new system.windows.forms.menuitem(); this.menuitem3 = new system.windows.forms.menuitem(); this.menuitem2 = new system.windows.forms.menuitem(); this.menuitem4 = new system.windows.forms.menuitem(); this.menuitem5 = new system.wind

13、ows.forms.menuitem(); this.menuitem6 = new system.windows.forms.menuitem(); this.menuitem7 = new system.windows.forms.menuitem(); this.openfiledialog1 = new system.windows.forms.openfiledialog(); this.savefiledialog1 = new system.windows.forms.savefiledialog(); this.suspendlayout(); / / myrtbox / th

14、is.myrtbox.dock = system.windows.forms.dockstyle.fill; this.myrtbox.location = new system.drawing.point(0, 0); this.myrtbox.name = myrtbox; this.myrtbox.size = new system.drawing.size(292, 273); this.myrtbox.tabindex = 0; this.myrtbox.text = ; / / mystatus / this.mystatus.location = new system.drawi

15、ng.point(0, 251); this.mystatus.name = mystatus; this.mystatus.size = new system.drawing.size(292, 22); this.mystatus.tabindex = 1; this.mystatus.text = 新建文件; / / mainmenu1 / this.mainmenu1.menuitems.addrange(new system.windows.forms.menuitem this.menuitem1, this.menuitem7); / / menuitem1 / this.men

16、uitem1.index = 0; this.menuitem1.menuitems.addrange(new system.windows.forms.menuitem this.menuitem3, this.menuitem2, this.menuitem4, this.menuitem5, this.menuitem6); this.menuitem1.text = 文件(&f); / / menuitem3 / this.menuitem3.index = 0; this.menuitem3.text = 新建(&n); this.menuitem3.click += new sys

17、tem.eventhandler(this.menuitem3_click); / / menuitem2 / this.menuitem2.index = 1; this.menuitem2.text = 打开(&o); this.menuitem2.click += new system.eventhandler(this.menuitem2_click); / / menuitem4 / this.menuitem4.index = 2; this.menuitem4.text = 保存(&s); this.menuitem4.click += new system.eventhandl

18、er(this.menuitem4_click); / / menuitem5 / this.menuitem5.index = 3; this.menuitem5.text = -; / / menuitem6 / this.menuitem6.index = 4; this.menuitem6.text = 退出(&x); this.menuitem6.click += new system.eventhandler(this.menuitem6_click); / / menuitem7 / this.menuitem7.index = 1; this.menuitem7.text =

19、关于(&a); / / openfiledialog1 / this.openfiledialog1.filter = text files(*.txt)|*.txt|rich text format files(*.rtf)|*.rtf|all files(*.*)|*.*; / / savefiledialog1 / this.savefiledialog1.filename = doc1; this.savefiledialog1.filter = text files(*.txt)|*.txt|rich text format files(*.rtf)|*.rtf|all files(

20、*.*)|*.*; / / form1 / this.autoscalebasesize = new system.drawing.size(6, 14); this.clientsize = new system.drawing.size(292, 273); this.controls.add(this.mystatus); this.controls.add(this.myrtbox); this.menu = this.mainmenu1; this.name = form1; this.text = editorapp; this.resumelayout(false);#endre

21、gion/ / 应用程序的主入口点。/ stathreadstatic void main() application.run(new form1();private void checksave()if (myrtbox.text != )if (messagebox.show (是否保存当前文件?,确认 ,messageboxbuttons.okcancel ) = dialogresult.ok )mysavefile();private void mynewfile()checksave(); /检查是否保存当前文件myrtbox.clear ();mystatus.text = 新建

22、文件;private void mysavefile()mystatus.text = 保存文件;if (savefiledialog1.showdialog () = dialogresult.ok )myrtbox.savefile (savefiledialog1.filename );private void myopenfile()checksave(); /检查是否保存当前文件if (openfiledialog1.showdialog () = dialogresult.ok )myrtbox.loadfile (openfiledialog1.filename ,richtex

23、tboxstreamtype.plaintext );mystatus.text = 打开文件;private void myexit()checksave(); /检查是否保存当前文件application.exit ();private void menuitem3_click(object sender, system.eventargs e)mynewfile(); /新建文件private void menuitem2_click(object sender, system.eventargs e)myopenfile();/打开文件private void menuitem4_cl

24、ick(object sender, system.eventargs e)mysavefile();/保存文件private void menuitem6_click(object sender, system.eventargs e)myexit();/退出程序4编写程序,在用户选择了一个目录后,找出该目录及其子目录中所有后缀名为doc的文件。using system;using system.drawing;using system.collections;using system.componentmodel;using system.windows.forms;using syste

25、m.data;using system.io;namespace filemanagerapp/ / form1 的摘要说明。/ public class form1 : system.windows.forms.formprivate system.windows.forms.treeview dirtree;private system.windows.forms.splitter splitter1;private system.windows.forms.listview filelist;private system.windows.forms.mainmenu mainmenu1;

26、private system.windows.forms.menuitem menuitem1;private system.windows.forms.menuitem menuitem2;private system.windows.forms.menuitem menuitem3;private system.windows.forms.menuitem menuitem4;private system.windows.forms.menuitem menuitem5;private system.windows.forms.menuitem menuitem6;private syst

27、em.windows.forms.menuitem menuitem7;/ / 必需的设计器变量。/ private system.componentmodel.container components = null;public form1()/ windows 窗体设计器支持所必需的/initializecomponent();/ todo: 在initializecomponent 调用后添加任何构造函数代码/treenode mycomputernode = new treenode (我的电脑);dirtree.nodes.add (mycomputernode);filelists

28、how(mycomputernode);/初始化listview控件private void filelistshow(treenode adirnode)filelist.clear ();tryif (adirnode.parent = null)foreach (string drvname in directory.getlogicaldrives ()listviewitem aitem = new listviewitem(drvname);filelist.items.add (aitem);elseforeach(string dirname in directory.getd

29、irectories (string)adirnode.tag )listviewitem aitem = new listviewitem(dirname);filelist.items.add (aitem);foreach(string filename in directory.getfiles (string)adirnode.tag )listviewitem aitem = new listviewitem(filename);filelist.items.add (aitem);catchprivate void filelistshow(string adirname)fil

30、elist.clear ();tryforeach(string dirname in directory.getdirectories (adirname )listviewitem aitem = new listviewitem(dirname);filelist.items.add (aitem);foreach(string filename in directory.getfiles (adirname)listviewitem aitem = new listviewitem(filename);filelist.items.add (aitem);catchprivate vo

31、id dirtreeshow( treenode adirnode )tryif (adirnode.nodes.count = 0)if (adirnode.parent = null)foreach (string drvname in directory.getlogicaldrives ()treenode anode = new treenode(drvname);anode.tag = drvname;adirnode.nodes.add (anode);else foreach (string dirname in directory.getdirectories(string)

32、adirnode.tag )treenode anode = new treenode (dirname);anode.tag = dirname;adirnode.nodes.add (anode);catch/ / 清理所有正在使用的资源。/ protected override void dispose( bool disposing )if( disposing )if (components != null) components.dispose();base.dispose( disposing );#region windows form designer generated c

33、ode/ / 设计器支持所需的方法- 不要使用代码编辑器修改/ 此方法的内容。/ private void initializecomponent()this.dirtree = new system.windows.forms.treeview();this.splitter1 = new system.windows.forms.splitter();this.filelist = new system.windows.forms.listview();this.mainmenu1 = new system.windows.forms.mainmenu();this.menuitem1 =

34、 new system.windows.forms.menuitem();this.menuitem6 = new system.windows.forms.menuitem();this.menuitem2 = new system.windows.forms.menuitem();this.menuitem3 = new system.windows.forms.menuitem();this.menuitem4 = new system.windows.forms.menuitem();this.menuitem5 = new system.windows.forms.menuitem(

35、);this.menuitem7 = new system.windows.forms.menuitem();this.suspendlayout();/ / dirtree/ this.dirtree.dock = system.windows.forms.dockstyle.left;this.dirtree.imageindex = -1;this.dirtree.name = dirtree;this.dirtree.selectedimageindex = -1;this.dirtree.size = new system.drawing.size(121, 273);this.di

36、rtree.tabindex = 0;this.dirtree.afterselect += new system.windows.forms.treevieweventhandler(this.dirtree_afterselect);/ / splitter1/ this.splitter1.location = new system.drawing.point(121, 0);this.splitter1.name = splitter1;this.splitter1.size = new system.drawing.size(3, 273);this.splitter1.tabind

37、ex = 1;this.splitter1.tabstop = false;/ / filelist/ this.filelist.dock = system.windows.forms.dockstyle.fill;this.filelist.location = new system.drawing.point(124, 0);this.filelist.multiselect = false;this.filelist.name = filelist;this.filelist.size = new system.drawing.size(168, 273);this.filelist.

38、tabindex = 2;this.filelist.view = system.windows.forms.view.list;this.filelist.doubleclick += new system.eventhandler(this.filelist_doubleclick);this.filelist.selectedindexchanged += new system.eventhandler(this.filelist_selectedindexchanged);/ / mainmenu1/ this.mainmenu1.menuitems.addrange(new syst

39、em.windows.forms.menuitem this.menuitem1, this.menuitem2, this.menuitem7);/ / menuitem1/ this.menuitem1.index = 0;this.menuitem1.menuitems.addrange(new system.windows.forms.menuitem this.menuitem6);this.menuitem1.text = 文件(&f);/ / menuitem6/ this.menuitem6.index = 0;this.menuitem6.text = exit;this.m

40、enuitem6.click += new system.eventhandler(this.menuitem6_click);/ / menuitem2/ this.menuitem2.index = 1;this.menuitem2.menuitems.addrange(new system.windows.forms.menuitem this.menuitem3, this.menuitem4, this.menuitem5);this.menuitem2.text = 编辑(&e);/ / menuitem3/ this.menuitem3.index = 0;this.menuit

41、em3.shortcut = system.windows.forms.shortcut.ctrlc;this.menuitem3.text = 复制;this.menuitem3.click += new system.eventhandler(this.menuitem3_click);/ / menuitem4/ this.menuitem4.index = 1;this.menuitem4.shortcut = system.windows.forms.shortcut.ctrlv;this.menuitem4.text = 粘贴;this.menuitem4.click += new

42、 system.eventhandler(this.menuitem4_click);/ / menuitem5/ this.menuitem5.index = 2;this.menuitem5.shortcut = system.windows.forms.shortcut.ctrlx;this.menuitem5.text = 剪切;/ / menuitem7/ this.menuitem7.index = 2;this.menuitem7.text = 关于(&a);/ / form1/ this.autoscalebasesize = new system.drawing.size(6

43、, 14);this.clientsize = new system.drawing.size(292, 273);this.controls.addrange(new system.windows.forms.control this.filelist, this.splitter1, this.dirtree);this.menu = this.mainmenu1;this.name = form1;this.text = filemanagerapp;this.resumelayout(false);#endregion/ / 应用程序的主入口点。/ stathreadstatic void main() application.run(new form1();private void dirtree_afterselect(object sender, system.windows.forms.treevieweventargs e)filelistshow( e.node );dirtreeshow( e.node );private void filelist_doubleclick(object sender, system.eventargs e)foreach (int listindex in filelis

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论