NET程序设计实验指导书.docx
- 文档编号:18010347
- 上传时间:2023-08-05
- 格式:DOCX
- 页数:58
- 大小:403.19KB
NET程序设计实验指导书.docx
《NET程序设计实验指导书.docx》由会员分享,可在线阅读,更多相关《NET程序设计实验指导书.docx(58页珍藏版)》请在冰点文库上搜索。
NET程序设计实验指导书
.NET程序设计
实验指导书
实验一简单程序设计
一.实验目的
1.熟悉VisualStudio.NET的IDE界面,练习窗口的浮动、停靠,以及工具栏的定制。
2.学会使用帮助系统,在编写程序时使“动态帮助”始终打开,注意观察“动态帮助”窗口中内容的变化。
二.实验内容
1.模仿书中的例子,编写“Hello”程序。
2.在“帮助”菜单下选择“对‘帮助’的帮助”,阅读其中的内容,学习帮助的使用。
三.源程序
创建Hello类,在Main中调用控制台WriteLine方法显示Hello,C#
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespace__1
{
classProgram
{
staticvoidMain(string[]args)
{
Console.WriteLine("Hello,C#!
");
}
}
}
四.调试和运行结果
用Ctrl+F5快捷键执行程序可得到运行结果。
实验二C#.NET面向对象程序设计实验
一.实验目的
1.理解面向对象思想,体会面向对象思想在编程中的应用。
2.掌握VisualC#.NET类的创建(成员,方法,属性),类的继承,类的多态性及类的方法的重载。
二.实验内容
1.为某公司创建一个类来建立员工的人事记录:
包括员工的姓名、性别、工资、到公司的日期、部门以及联系方式等信息。
构建该类,并做出适当的测试。
2.从上面的类中派生出一个类,来记录公司干部的情况。
包括职位、提职时间、管理的员工人数及姓名。
3.编写程序,使得一个大学书店可以用它来记录和确定教科书的零售价。
所有计算应该用一个类TextBook的实例来完成。
这个类应该具有属性Title(书名)、Author(作者)、Cost(批发费用)、Quantity(库存量)和Price(零售价)。
同时假设零售价是批发价的1.25倍。
4.编写程序相加两个分数,并将它们的和以化简后的分数形式表现出来。
程序使用类Fraction来存放分数的分子和分母,具有方法Reduce来化简结果。
三.源程序
1.题目1和题目2
Using指令集:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
定义Employee类
publicclassEmployee
{
//
protectedstringname;
privatestringsexy;
privateintsalary;
privatestringdate;
privatestringdepartment;
privatestringtelnum;
publicEmployee()
{}
publicEmployee(string[]args)
{
name=args[0];
sexy=args[1];
salary=Convert.ToInt32(args[2]);
date=args[3];
department=args[4];
telnum=args[5];
}
//virtual关键字,方法可以重写
publicvirtualvoidDisplay()
{
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类,继承基类Employee
publicclassEmployer:
Employee
{
privatestringposition;
privatestringpromote_date;
privatestringnumber;
privatestring[]Ename=newstring[50];
publicEmployer(string[]args):
base(args)
{
position=args[6];
promote_date=args[7];
number=args[8];
for(inti=0;i { Ename[i]=args[9+i]; } } //重写基类的Display的方法 publicoverridevoidDisplay() { base.Display(); Console.WriteLine("Position: {0}",position); Console.WriteLine("Promote_date: {0}",promote_date); Console.WriteLine("Numberofemployee: {0}",number); for(inti=0;i Console.WriteLine("Nameofemployee{0}: {1}",i+1,Ename[i]); } } Main函数调用Employee和Employer的构造函数和方法并显示: publicclassClass1 { staticvoidMain(string[]args) { Console.WriteLine("----------------软件051------孔令民------051023----------------------"); string[]employee1=newstring[50]; Console.WriteLine("Pleaseinputimformationofemployee: "); Console.WriteLine("(name,sexy,salary,date,department,telephonenumber)"); employee1=Console.ReadLine().Split(''); //调用Employee构造函数 EmployeenewEmployee=newEmployee(employee1); Console.WriteLine("-----------------Thisistheimfomationofemployee: -------------------"); //调用EmployeeDisplay()方法显示数据 newEmployee.Display(); Console.WriteLine("-----------------------------------------------------------------------------"); Console.WriteLine("Pleaseinputimformationofemployer: "); Console.WriteLine("(name,sexy,salary,date,department,telephonenumber,position,promote_date,numberofemployees,nameofemployees(name1name2name3...))"); employee1=Console.ReadLine().Split(''); //调用Employer构造函数 EmployernewEmployer=newEmployer(employee1); Console.WriteLine("------------------Thisistheimfomationofemployee: -------------------"); //调用EmployerDisplay()方法显示数据 newEmployer.Display(); Console.WriteLine("----------------------------------------------------------------------------"); } } 题目1和题目2调试通过运行结果如下所示: 2.题目3 定义类TextBook TextBook类包括Title,Author,Cost,Quantity,Price属性和Display方法 publicclassTextBook { publicstringTitle; publicstringAuthor; publicdoubleCost; publicintQuantity; publicdoublePrice; publicTextBook(stringt,stringa,doublec,intq) { Title=t; Author=a; Cost=c; Quantity=q; Price=1.25*c; } publicvoidDisplay() { Console.WriteLine("Title: {0}",Title); Console.WriteLine("Author: {0}",Author); Console.WriteLine("Cost: {0}",Cost); Console.WriteLine("Quantity: {0}",Quantity); Console.WriteLine("Price: {0}",Price); } } Main函数调用Textbook的构造函数和方法并显示: staticvoidMain(string[]args) { Console.WriteLine("---------------软件051------孔令民------051023--------------------"); Console.WriteLine("Pleaseinputinformationofthebook: "); Console.WriteLine("Title,Author,Cost,Quantity"); string[]info=Console.ReadLine().Split('');//不用初始化 stringt=info[0]; stringa=info[1]; doublec=Convert.ToDouble(info[2]); intq=Convert.ToInt32(info[3]); TextBooknewTextBook=newTextBook(t,a,c,q); Console.WriteLine("-----------------Thisistheimfomationofbook: -------------------"); newTextBook.Display(); Console.WriteLine("------------------------------------------------------------------------"); } 题目3调试通过运行结果如下所示: 输入C#BeginnerWaston8050 计算得Price=Cost×1.25=100 3.题目4 定义了Faction类用来计算两分数相加 Faction类包括分子和分母两个属性,以及Reduce()和Display()两个方法 classPraction { publicintDenominator;//分母 publicintMolecule;//分子 publicPraction(intm,intd) { Denominator=d; Molecule=m; } publicvoidReduce() { intt=Molecule; for(inti=t;i>=1;i--) { if((Molecule%i==0)&&(Denominator%i==0)) { Molecule/=i; Denominator/=i; } } } publicvoidDisplay() { Console.WriteLine("{0}/{1}",Molecule,Denominator); } } Main函数 staticvoidMain(string[]args) { Console.WriteLine("-------------软件051------孔令民------051023--------------------"); Console.WriteLine("Pleaseinputthemolecule(分子)ofthe1stpration: "); intm=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Pleaseinputthedenominator(分母)ofthe1stpration: "); intd=Convert.ToInt32(Console.ReadLine()); PractionnewPraction=newPraction(m,d); Console.Write("您输入的第一个分数为: "); newPraction.Display(); Console.WriteLine("Pleaseinputthemolecule(分子)ofthe2ndpration: "); intm1=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Pleaseinputthedenominator(分母)ofthe2ndpration: "); intd1=Convert.ToInt32(Console.ReadLine()); PractionnewPraction1=newPraction(m1,d1); Console.Write("您输入的第二个分数为: "); newPraction1.Display(); PractionaddPraction=newPraction(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中 privatevoidForm2_Load(objectsender,EventArgse) { FileStreamfs=newFileStream("test.txt",FileMode.Open,FileAccess.ReadWrite); StreamReadersr=newStreamReader(fs); richTextBox1.Text=sr.ReadToEnd(); sr.Close(); fs.Close(); } //将文件中的10个随机数用冒泡法排序,并在RichTextBox2中显示 privatevoidbutton1_Click(objectsender,EventArgse) { FileStreamfs=newFileStream("test.txt",FileMode.Open,FileAccess.ReadWrite); StreamReadersr=newStreamReader(fs); string[]myDate=sr.ReadToEnd().Split(','); sr.Close(); fs.Close(); for(inti=0;i { for(intj=0;j { if(Convert.ToInt32(myDate[j])>Convert.ToInt32(myDate[j+1])) { stringt; t=myDate[j]; myDate[j]=myDate[j+1]; myDate[j+1]=t; } } } foreach(stringsinmyDate) { Console.WriteLine(s); } //将排好序的数写回到文件中 FileStreamfs1=newFileStream("test.txt",FileMode.Open,FileAccess.ReadWrite); StreamWritersw=newStreamWriter(fs1); for(inti=0;i { sw.Write(myDate[i]); sw.Write(","); } sw.Write(myDate[myDate.Length-1]); sw.Close(); fs1.Close(); FileStreamfs2=newFileStream("test.txt",FileMode.Open,FileAccess.ReadWrite); StreamReadersr2=newStreamReader(fs2); richTextBox2.Text=sr2.ReadToEnd(); sr2.Close(); fs2.Close(); } //往文件中写入随机数 privatevoidbutton2_Click(objectsender,EventArgse) { RandomnewRandom=newRandom();//声明产生随机数对象 FileInfofi=newFileInfo("test.txt"); using(StreamWritersw=fi.AppendText()) { //写入随机数 for(inti=0;i<10;i++) { sw.Write(','); sw.Write(newRandom.Next()); } sw.Write(newRandom.Next()); FileStreamfs2=newFileStream("test.txt",FileMode.Open,FileAccess.ReadWrite); StreamReadersr2=newStreamReader(fs2); richTextBox2.Text=sr2.ReadToEnd(); sr2.Close(); fs2.Close(); } } 运行结果: 2.文本编辑器 界面设计如下所示: 主菜单栏设有文件,编辑,格式,帮助,返回主界面5个主菜单项,各主菜单的子菜单如下: (1)文件 新建,打开,保存,退出 (2)编辑 剪切,粘贴,复制,撤销,恢复,查找 (3)格式 字体,颜色 功能设计如下: 文件操作函数: (1)检查是否保存 privatevoidCheckSave() { if(rtBox.Text! ="") { if((MessageBox.Show("是否保存当前文件? ","确认",MessageBoxButtons.OKCancel)==DialogResult.OK)) { SaveFile(); SaveInfo=true; } else SaveInfo=false; } } (2)保存文件 privatevoidSaveFile() { saveFileDialog1.Filter="所有文件(*.*)|*.*|文本文件(*.txt)|*.txt"; saveFileDialog1.InitialDirectory="C: \\"; if(saveFileDialog1.ShowDialog()==DialogResult.OK) { rtBox.SaveFile(saveFileDialog1.FileN
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- NET 程序设计 实验 指导书
![提示](https://static.bingdoc.com/images/bang_tan.gif)