net程序设计实验指导

上传人:仙*** 文档编号:28127152 上传时间:2021-08-23 格式:DOC 页数:45 大小:341KB
收藏 版权申诉 举报 下载
net程序设计实验指导_第1页
第1页 / 共45页
net程序设计实验指导_第2页
第2页 / 共45页
net程序设计实验指导_第3页
第3页 / 共45页
资源描述:

《net程序设计实验指导》由会员分享,可在线阅读,更多相关《net程序设计实验指导(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.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 vo

44、id 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 FileList.SelectedIndices )FileList

45、Show( FileList.ItemsListIndex.Text );private void menuItem6_Click(object sender, System.EventArgs e)this.Close ();private string SourceFileName;private void menuItem3_Click(object sender, System.EventArgs e)if (DirTree.Focused )SourceFileName = DirTree.SelectedNode .Text ;else if (FileList.Focused )

46、foreach (int ListIndex in FileList.SelectedIndices )SourceFileName = FileList.ItemsListIndex.Text ;MessageBox.Show (Copy File+SourceFileName);private void menuItem4_Click(object sender, System.EventArgs e)string CurPath = ;if (DirTree.Focused )CurPath = DirTree.SelectedNode .Text ;else if(FileList.F

47、ocused )CurPath = Directory.GetParent (FileList.Items0.Text ).FullName ;tryMessageBox.Show (Copy to + CurPath);File.Copy (SourceFileName,CurPath,true);catchprivate void FileList_SelectedIndexChanged(object sender, System.EventArgs e)5假设有文本文件1.txt和2.txt。编写程序,创建一个新的文本文件,将1.txt中的内容和2.txt中的内容重复两遍,交替写入新的

48、文本文件,并删除1.txt和2.txt。实验四 C#图形程序设计基础1使用图形方法,画出5条不同颜色的直线并形成一个多边形。using System;using System.Collections.Generic;using System.Text;namespace _01 struct Rectangle public int x, y; /矩形左上角的坐标 public int width, height; /矩形的宽和高 public Rectangle(int a, int b, int w, int h) x = a; y = b; width = w; height = h; class Program static void Main(string args) /声明一个Rectangle对象 Rectangle myRect; /初始化对象 myRect.x = 20; myRect.y = 30; myRect.width = 200; myRect.height = 300; /输出对象的值 Console.WriteLine(My Rectangle: ); Console.WriteLine(x = 0, y = 1, widt

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!