net程序设计实验指导.doc

上传人:西安人 文档编号:5015345 上传时间:2020-01-28 格式:DOC 页数:45 大小:341KB
返回 下载 相关 举报
net程序设计实验指导.doc_第1页
第1页 / 共45页
net程序设计实验指导.doc_第2页
第2页 / 共45页
net程序设计实验指导.doc_第3页
第3页 / 共45页
net程序设计实验指导.doc_第4页
第4页 / 共45页
net程序设计实验指导.doc_第5页
第5页 / 共45页
点击查看更多>>
资源描述

《net程序设计实验指导.doc》由会员分享,可在线阅读,更多相关《net程序设计实验指导.doc(45页珍藏版)》请在三一文库上搜索。

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.TabIndex = 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 =

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 研究报告 > 商业贸易


经营许可证编号:宁ICP备18001539号-1