c#实验报告1.docx
《c#实验报告1.docx》由会员分享,可在线阅读,更多相关《c#实验报告1.docx(39页珍藏版)》请在冰点文库上搜索。
c#实验报告1
实验一简单程序设计
一.实验目的
1.熟悉VisualStudio.NET的IDE界面,练习窗口的浮动、停靠,以及工具栏的定制。
2.学会使用帮助系统,在编写程序时使“动态帮助”始终打开,注意观察“动态帮助”窗口中内容的变化。
二.实验内容
1.模仿书中的例子,编写“Hello”程序。
2.在“帮助”菜单下选择“对‘帮助’的帮助”,阅读其中的内容,学习帮助的使用。
三.源程序
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespace_011_shyan1
{
classProgram
{
staticvoidMain(string[]args)
{
Console.WriteLine("Hello,C#!
");
}
}
}
4.实验结果
实验二C#.NET面向对象程序设计实验
一.实验目的
1.理解面向对象思想,体会面向对象思想在编程中的应用。
2.掌握VisualC#.NET类的创建(成员,方法,属性),类的继承,类的多态性及类的方法的重载。
二.实验内容
1.为某公司创建一个类来建立员工的人事记录:
包括员工的姓名、性别、工资、到公司的日期、部门以及联系方式等信息。
构建该类,并做出适当的测试。
2.从上面的类中派生出一个类,来记录公司干部的情况。
包括职位、提职时间、管理的员工人数及姓名。
3.编写程序,使得一个大学书店可以用它来记录和确定教科书的零售价。
所有计算应该用一个类TextBook的实例来完成。
这个类应该具有属性Title(书名)、Author(作者)、Cost(批发费用)、Quantity(库存量)和Price(零售价)。
同时假设零售价是批发价的1.25倍。
4.编写程序相加两个分数,并将它们的和以化简后的分数形式表现出来。
程序使用类Fraction来存放分数的分子和分母,具有方法Reduce来化简结果。
题目1:
源程序:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
publicclassEmployee
{
protectedstringname;
privatestringsexy;
privatestringsalary;
privatestringdate;
privatestringdepartment;
privatestringtelnum;
publicEmployee(stringname,stringsexy,stringsalary,stringdate,stringdepartment,stringtelnum)
{
this.name=name;
this.sexy=sexy;
this.salary=salary;
this.date=date;
this.department=department;
this.telnum=telnum;
}
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);
}
}
publicclassClass1
{
staticvoidMain()
{
Console.WriteLine("----------------软件091------郭帅------092598----------------------");
Console.WriteLine("Pleaseinputimformationofemployee:
");
Console.WriteLine("(name,sexy,salary,date,department,telephonenumber)");
string[]employee=Console.ReadLine().Split('');
Employeenewemployee=newEmployee(employee[0],employee[1],employee[2],employee[3],employee[4],employee[5]);
Console.WriteLine("-----------------Thisistheimfomationofemployee:
-------------------");
newemployee.Display();
Console.WriteLine("-----------------------------------------------------------------------------");
}
}
结果图:
题目2:
源程序:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
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);
}
staticvoidMain(string[]args)
{
Console.WriteLine("-------------软件091------郭帅------092598--------------------");
Console.WriteLine("Pleaseinputthemolecule(分子)ofthe1stpration:
");
intm=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Pleaseinputthedenominator(分母)ofthe1stpration:
(0toend)");
intd=Convert.ToInt32(Console.ReadLine());
PractionnewPraction=newPraction(m,d);
Console.Write("您输入的第一个分数为:
");
newPraction.Reduce();
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.Reduce();
newPraction1.Display();
PractionaddPraction=newPraction(m*d1+m1*d,d*d1);
//化简分数
addPraction.Reduce();
Console.Write("两分数之和为:
");
addPraction.Display();
Console.WriteLine("----------------------------------------------------------------------------");
}
}
结果图:
实验三文本编辑器的设计
一.实验目的
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。
三.源程序
主窗体部分:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.IO;
namespace_011_shiyan3
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
Form2f=newForm2();
f.Show();
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
Form3f=newForm3();
f.Show();
}
privatevoidbutton3_Click(objectsender,EventArgse)
{
Form4f=newForm4();
f.Show();
}
privatevoidbutton5_Click(objectsender,EventArgse)
{
this.Close();
}
privatevoidbutton4_Click(objectsender,EventArgse)
{
FileStreamfs1=newFileStream("1.txt",FileMode.Open);
StreamReadersr1=newStreamReader(fs1);
stringtext1=sr1.ReadToEnd();
fs1.Close();
sr1.Close();
FileStreamfs2=newFileStream("2.txt",FileMode.Open);
StreamReadersr2=newStreamReader(fs2);
stringtext2=sr2.ReadToEnd();
fs2.Close();
sr2.Close();
using(StreamWritersw=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();
}
}
}
}
结果图:
排序/添加随机数:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.IO;
namespace_011_shiyan3
{
publicpartialclassForm2:
Form
{
publicForm2()
{
InitializeComponent();
FileStreamfs=newFileStream("test.txt",FileMode.Open,FileAccess.ReadWrite);
StreamReadersr=newStreamReader(fs);
//MessageBox.Show(sr.ReadToEnd());
//richTextBox1.Text="ddddddddd";
richTextBox1.Text=sr.ReadToEnd();
sr.Close();
fs.Close();
}
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()%30);
}
}
FileStreamfs2=newFileStream("test.txt",FileMode.Open,FileAccess.ReadWrite);
StreamReadersr2=newStreamReader(fs2);
richTextBox2.Text=sr2.ReadToEnd();
sr2.Close();
fs2.Close();
}
privatevoidbutton3_Click(objectsender,EventArgse)
{
this.Close();
}
}
}
结果图:
(修改后的内容是添加过随机数的)
文本编辑器:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
namespace_011_shiyan3
{
publicpartialclassForm3:
Form
{
publicForm3()
{
InitializeComponent();
}
privateboolSaveInfo;
privatevoidCheckSave()//检查文件是否保存
{
if(rtBox.Text!
="")
{
if((MessageBox.Show("是否保存当前文件?
","确认",MessageBoxButtons.OKCancel)==DialogResult.OK))
{
SaveFile();
SaveInfo=true;
}
else
SaveInfo=false;
}
}
privatevoidSaveFile()//保存文件
{
SaveFileDialogsaveFileDialog1=newSaveFileDialog();
saveFileDialog1.Filter="所有文件(*.*)|*.*|文本文件(*.txt)|*.txt";
saveFileDialog1.InitialDirectory="C:
\\";
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
rtBox.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);
}
else
return;
}
privatevoidOpenFile()//打开文件
{
CheckSave();
OpenFileDialogopenFileDialog1=newOpenFileDialog();
openFileDialog1.Filter="所有文件(*.*)|*.*|文本文件(*.txt)|*.txt";
openFileDialog1.InitialDirectory="C:
\\";
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
rtBox.LoadFile(openFileDialog1.FileName,RichTextBoxStreamType.PlainText);
}
else
return;
}
privatevoid新建ToolStripMenuItem_Click(objectsender,EventArgse)
{
CheckSave();
if(SaveInfo==true)
rtBox.Clear();
}
privatevoid打开ToolStripMenuItem_Click(objectsender,EventArgse)
{
OpenFile();
}
privatevoid保存ToolStripMenuItem_Click(objectsender,EventArgse)
{
SaveFile();
}
privatevoid退出ToolStripMenuItem_Click(objectsender,EventArgse)
{
this.Close();
}
privatevoid剪切ToolStripMenuItem_Click(objectsender,EventArgse)
{
rtB