NET程序设计实验指导书.doc_第1页
NET程序设计实验指导书.doc_第2页
NET程序设计实验指导书.doc_第3页
NET程序设计实验指导书.doc_第4页
NET程序设计实验指导书.doc_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

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

文档简介

.NET 程序设计实验指导书 实验一 简单程序设计一实验目的1熟悉Visual Studio.NET的IDE界面,练习窗口的浮动、停靠,以及工具栏的定制。2学会使用帮助系统,在编写程序时使“动态帮助”始终打开,注意观察“动态帮助”窗口中内容的变化。二实验内容1模仿书中的例子,编写“Hello”程序。2在“帮助”菜单下选择“对帮助的帮助”,阅读其中的内容,学习帮助的使用。三源程序 创建Hello类,在Main中调用控制台WriteLine方法显示Hello,C# using System;using System.Collections.Generic;using System.Text;namespace _1 class Program static void Main(string args) Console.WriteLine(Hello,C#!); 四调试和运行结果 用Ctrl+F5快捷键执行程序可得到运行结果。实验二 C#.NET面向对象程序设计实验一实验目的1理解面向对象思想,体会面向对象思想在编程中的应用。2掌握Visual C#.NET类的创建(成员,方法,属性),类的继承,类的多态性及类的方法的重载。二实验内容1为某公司创建一个类来建立员工的人事记录:包括员工的姓名、性别、工资、到公司的日期、部门以及联系方式等信息。构建该类,并做出适当的测试。2从上面的类中派生出一个类,来记录公司干部的情况。包括职位、提职时间、管理的员工人数及姓名。3编写程序,使得一个大学书店可以用它来记录和确定教科书的零售价。所有计算应该用一个类TextBook的实例来完成。这个类应该具有属性Title(书名)、Author(作者)、Cost(批发费用)、Quantity(库存量)和Price(零售价)。同时假设零售价是批发价的1.25倍。4编写程序相加两个分数,并将它们的和以化简后的分数形式表现出来。程序使用类Fraction来存放分数的分子和分母,具有方法Reduce来化简结果。三源程序1. 题目1和题目2Using指令集:using System;using System.Collections.Generic;using System.Text;定义Employee类public class Employee/ protected string name; private string sexy; private int salary; private string date; private string department; private string telnum; public Employee() public Employee(string args) name = args 0; sexy = args 1; salary = Convert.ToInt32 (args 2); date = args 3; department = args4 ; telnum = args 5; /virtual关键字,方法可以重写 public virtual void Display() Console.WriteLine(Name:0, name); Console.WriteLine(Sexy:0, sexy); Console.WriteLine(Salary:0, salary); Console.WriteLine(Date:0, date); Console.WriteLine(Department:0, department); Console.WriteLine(Telephone:0, telnum); 定义Employer类,继承基类Employeepublic class Employer:Employee private string position; private string promote_date; private string number; private string Ename=new string 50; public Employer(string args): base(args) position = args6; promote_date = args7; number = args8; for (int i = 0; i Convert.ToInt32(number); i+) Enamei = args9 + i; /重写基类的Display的方法 public override void Display() base.Display(); Console.WriteLine(Position:0, position); Console.WriteLine(Promote_date:0, promote_date); Console.WriteLine(Number of employee:0, number); for (int i = 0; i = 1; i-) if (Molecule % i = 0) & (Denominator % i = 0) Molecule /= i; Denominator /= i; public void Display() Console .WriteLine (0/1,Molecule ,Denominator ); Main函数 static void Main(string args) Console.WriteLine(-软件051-孔令民-051023-); Console.WriteLine(Please input the molecule(分子) of the 1st pration:); int m = Convert.ToInt32(Console.ReadLine(); Console.WriteLine(Please input the denominator(分母) of the 1st pration:); int d = Convert.ToInt32(Console.ReadLine(); Praction newPraction=new Praction (m,d); Console.Write(您输入的第一个分数为:); newPraction.Display(); Console.WriteLine(Please input the molecule(分子) of the 2nd pration:); int m1 = Convert.ToInt32(Console.ReadLine(); Console.WriteLine(Please input the denominator(分母) of the 2nd pration:); int d1 = Convert.ToInt32(Console.ReadLine(); Praction newPraction1 = new Praction(m1, d1); Console.Write(您输入的第二个分数为:); newPraction1.Display(); Praction addPraction = new Praction(m*d1 + m1*d, d * d1); /化简分数 addPraction.Reduce(); Console.Write(两分数之和为:); addPraction.Display(); Console.WriteLine(- -); 题目4调试成功,运行结果如下所示:实验三 文本编辑器的设计一实验目的1熟悉VisualC#.NET的可视化界面,掌握控件的使用。2掌握System.IO类的文件流操作,会处理文件。二实验内容1假设有要排序的20个数存在文件Data.txt中。编写程序,打开该文件并将排好序的数重新写回该文件。2重新打开第1题创建的文件,在文件的结尾再添加10个随机数。3参考Windows的记事本程序,编写一个简单的文本编辑器程序。4编写程序,在用户选择了一个目录后,找出该目录及其子目录中所有后缀名为doc的文件。5假设有文本文件1.txt和2.txt。编写程序,创建一个新的文本文件,将1.txt中的内容和2.txt中的内容重复两遍,交替写入新的文本文件,并删除1.txt和2.txt。三源程序 Winform窗体主界面 主界面由“排序/添加随机数”,“文本编辑器”,“文件查找”,“文件合并”,“退出”,五个按钮构成。每个按钮实现内容的一个部分。主界面如下所示:1. 排序添加随机数源程序:/窗体载入事件,把文本文件Test.txt的内容读入RichTextBox1中private void Form2_Load(object sender, EventArgs e) FileStream fs = new FileStream(test.txt, FileMode.Open, FileAccess.ReadWrite); StreamReader sr = new StreamReader(fs); richTextBox1.Text = sr.ReadToEnd(); sr.Close(); fs.Close(); /将文件中的10个随机数用冒泡法排序,并在RichTextBox2中显示 private void button1_Click(object sender, EventArgs e) FileStream fs = new FileStream(test.txt, FileMode.Open, FileAccess.ReadWrite); StreamReader sr = new StreamReader(fs); string myDate = sr.ReadToEnd().Split(,); sr.Close(); fs.Close(); for (int i = 0; i myDate.Length; i+) for (int j = 0; j Convert.ToInt32(myDatej + 1) string t; t = myDatej; myDatej = myDatej + 1; myDatej + 1 = t; foreach (string s in myDate) Console.WriteLine(s); /将排好序的数写回到文件中 FileStream fs1 = new FileStream(test.txt, FileMode.Open, FileAccess.ReadWrite); StreamWriter sw = new StreamWriter(fs1); for (int i = 0; i myDate.Length - 1; i+) sw.Write(myDatei); sw.Write(,); sw.Write(myDatemyDate.Length - 1); sw.Close(); fs1.Close(); FileStream fs2 = new FileStream(test.txt, FileMode.Open, FileAccess.ReadWrite); StreamReader sr2 = new StreamReader(fs2); richTextBox2.Text = sr2.ReadToEnd(); sr2.Close(); fs2.Close(); /往文件中写入随机数 private void button2_Click(object sender, EventArgs e) Random newRandom = new Random();/声明产生随机数对象 FileInfo fi = new FileInfo(test.txt); using (StreamWriter sw = fi.AppendText() /写入随机数 for (int i = 0; i 10; i+) sw.Write(,); sw.Write(newRandom.Next(); sw.Write(newRandom.Next(); FileStream fs2 = new FileStream(test.txt, FileMode.Open, FileAccess.ReadWrite); StreamReader sr2 = new StreamReader(fs2); richTextBox2.Text = sr2.ReadToEnd(); sr2.Close(); fs2.Close(); 运行结果: 2.文本编辑器界面设计如下所示:主菜单栏设有文件,编辑,格式,帮助,返回主界面5个主菜单项,各主菜单的子菜单如下:(1)文件新建,打开,保存,退出(2)编辑剪切,粘贴,复制,撤销,恢复,查找(3)格式 字体,颜色 功能设计如下: 文件操作函数:(1)检查是否保存private void CheckSave() if (rtBox.Text != ) if (MessageBox.Show(是否保存当前文件?, 确认, MessageBoxButtons.OKCancel) = DialogResult.OK) SaveFile(); SaveInfo = true; else SaveInfo = false; (2)保存文件 private void SaveFile() saveFileDialog1.Filter = 所有文件(*.*)|*.*|文本文件(*.txt)|*.txt; saveFileDialog1.InitialDirectory = C:; if (saveFileDialog1.ShowDialog() = DialogResult.OK) rtBox.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); else return; (3)打开文件 private void OpenFile() CheckSave(); openFileDialog1.Filter = 所有文件(*.*)|*.*|文本文件(*.txt)|*.txt; openFileDialog1.InitialDirectory = C:; if (openFileDialog1.ShowDialog() = DialogResult.OK) rtBox.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText); else return; 文件菜单功能设计 (1)新建 private void 新建ToolStripMenuItem_Click(object sender, EventArgs e) CheckSave(); if(SaveInfo=true ) rtBox.Clear(); (2)打开 private void 打开OToolStripMenuItem_Click(object sender, EventArgs e) OpenFile(); (3)保存private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) SaveFile(); (4)剪切 private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e) rtBox.Cut(); (5)复制 private void 复制CToolStripMenuItem_Click(object sender, EventArgs e) rtBox.Copy(); (6)粘贴 private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e) rtBox.Paste(); (7)查找 private void 查找FToolStripMenuItem_Click(object sender, EventArgs e) rtBox.Undo(); (8)字体 private void 字体FToolStripMenuItem_Click(object sender, EventArgs e) FontDialog fg = new FontDialog(); if (fg.ShowDialog() = DialogResult.OK) rtBox.SelectionFont = fg.Font; else return; (9)恢复 private void 恢复ToolStripMenuItem_Click(object sender, EventArgs e) rtBox.Redo(); (10)颜色 private void 颜色CToolStripMenuItem_Click(object sender, EventArgs e) ColorDialog cg = new ColorDialog(); if (cg.ShowDialog() = DialogResult.OK) rtBox.SelectionColor = cg.Color; else return; (11)关于 private void 关ToolStripMenuItem_Click(object sender, EventArgs e) MessageBox.Show(开发语言:C# 作者:aight, 关于记事本, MessageBoxButtons.OK); 运行结果:3.文件查找源代码:public void FindFile(string dir) /参数为指定的目录 /在指定目录及子目录下查找文件,在listBox1中列出子目录及文件 DirectoryInfo Dir = new DirectoryInfo(dir); try foreach (DirectoryInfo d in Dir.GetDirectories() /查找子目录 FindFile(Dir + d.ToString() + ); foreach (FileInfo f in Dir.GetFiles(*.doc) /查找文件 listView1.Items.Add(Dir + f.ToString(); /listBox1中填加文件名 catch (Exception e) MessageBox.Show(e.Message); private void btnSearch_Click(object sender, EventArgs e) FindFile(textBox1 .Text ); 运行结果:4.文件合并 源代码:private void button4_Click(object sender, EventArgs e) FileStream fs1 = new FileStream(1.txt, FileMode.Open); StreamReader sr1 = new StreamReader(fs1); string text1 = sr1.ReadToEnd(); fs1.Close(); sr1.Close(); FileStream fs2 = new FileStream(2.txt, FileMode.Open); StreamReader sr2 = new StreamReader(fs2); string text2 = sr2.ReadToEnd(); fs2.Close(); sr2.Close(); using (StreamWriter sw = File.CreateText(3.txt) sw.WriteLine(text1); sw.WriteLine(text2); sw.WriteLine(text1); sw.WriteLine(text2); MessageBox.Show(文件添加完毕!, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); File.Delete(1.txt); File.Delete(2.txt); MessageBox.Show(文件删除完毕!, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); sw.Close(); 实验四 C#图形程序设计基础一实验目的1熟悉VisualC#.NET的图形基础知识,绘图的基本知识2学会GDI基础知识3.建立画笔,画刷4.画图的方法及使用二实验内容1使用图形方法,画出5条不同颜色的直线并形成一个多边形。2使用图形方法,画一条经过(200,200),(256,87),(87,9),(22,108)这4个点的曲线。3使用图形方法,画一个椭圆,并用纹理刷填充。4将前面3题组合在一起,并设计一个菜单来完成各项功能。三源程序 主界面设计如下所示: 源代码 (1).定义画笔P1 Pen p1=new Pen(Color.Red,2); (2)画多边形private void button1_Click(object sender, System.EventArgs e)p1.Color =Color.Red ; Graphics g=this.CreateGraphics();g.Clear(this.BackColor);g.DrawLine(p1,100,10,250,10);p1.Color=Color.Blue;g.DrawLine(p1,250,10,300,90);p1.Color=Color.Black; g.DrawLine(p1,300,90,175,180);p1.Color=Color.Pink;g.DrawLine(p1,175,180,50,90);p1.Color=Color.Yellow;g.DrawLine(p1,50,90,100,10); (3)画曲线private void button2_Click(object sender, System.EventArgs e)Graphics g=this.CreateGraphics();g.Clear(this.BackColor);g.DrawBezier(p1,200,200,256,87,87,9,22,108); (4)画椭圆并填充private void button3_Click(object sender,

温馨提示

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

评论

0/150

提交评论