广东工业大学C#实验题目和报告2.docx

上传人:b****6 文档编号:3743654 上传时间:2022-11-25 格式:DOCX 页数:20 大小:614.16KB
下载 相关 举报
广东工业大学C#实验题目和报告2.docx_第1页
第1页 / 共20页
广东工业大学C#实验题目和报告2.docx_第2页
第2页 / 共20页
广东工业大学C#实验题目和报告2.docx_第3页
第3页 / 共20页
广东工业大学C#实验题目和报告2.docx_第4页
第4页 / 共20页
广东工业大学C#实验题目和报告2.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

广东工业大学C#实验题目和报告2.docx

《广东工业大学C#实验题目和报告2.docx》由会员分享,可在线阅读,更多相关《广东工业大学C#实验题目和报告2.docx(20页珍藏版)》请在冰豆网上搜索。

广东工业大学C#实验题目和报告2.docx

广东工业大学C#实验题目和报告2

.NET程序设计

实验报告

 

班级:

计科7

姓名:

陈日燊

学号:

3110006131

教师:

 

广东工业大学

2013年4月20日

第二次实验:

内容:

完成下面的实验2.1和2.2。

地点:

工学一号馆413/410

时间:

2013-4-11

实验环境与工具:

(1)计算机及操作系统:

PC机,WindowsXP

(2)程序开发平台:

VisualStudio2005/2008/2010

实验2.1

一.实验目的

1.掌握System.IO类的文件流操作,会处理文件。

2.理解面向对象思想,会使用.NETFramework的类库。

3.熟悉VisualC#.NET的可视化界面,可以使用相关控件。

二.实验内容

1.编写一个C#应用程序,将一个包含多个子目录和文件的目录复制到另外一个指定的目录下。

a)欲复制的目录中包含的文件数和子目录层次未知,必须在程序执行时获得这些信息。

b)显示欲复制的目录的相关信息。

2.二战期间,英国情报人员获取德军的一机密电报,电报的内容为:

bzdzizusxgzdvslh,vpzgwoflshvsvwrhvhlsddlmpglmwrwgzyvsg.gflyzgstfzubvsggzsdhdmlpvmllm,hghzvywmzhwirymvvdgvyizdzhzdvivsg,ltztmlotmlO

情报人员已经知道,这段电报的加密方式为:

a.首先将字符串的顺序颠倒。

b.字母互换的规律为:

A->Z,B-Y,C-X...X->C,Y->B,Z-A;a->z,b->y,c-x...x->c,y->b,z->a.

c.非字母字符保持不变。

请编程帮助情报人员破译这份机密电报。

给出注释良好的源程序和程序运行后的结果。

三.源程序

程序一:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.IO;

namespaceWindowsFormsApplication2

{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

}

privatevoidForm1_Load(objectsender,EventArgse)

{

}

privatevoidbutton1_Click_1(objectsender,EventArgse)

{

DirectoryInfosourceDirectory=newDirectoryInfo("F:

\home\lzbm\zsbm_pic\2013");

DirectoryInfodestinationDirectoty=newDirectoryInfo("C:

\Users\acer\Desktop\xxoo");

CopyFile(textBox1.Text,textBox2.Text);

listBox1.Items.Add("复制完成");

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

FolderBrowserDialogfolder=newFolderBrowserDialog();

if(folder.ShowDialog()==DialogResult.OK){

textBox1.Text=folder.SelectedPath;

}

}

privatevoidbutton3_Click(objectsender,EventArgse)

{

FolderBrowserDialogfolder=newFolderBrowserDialog();

if(folder.ShowDialog()==DialogResult.OK)

{

textBox2.Text=folder.SelectedPath;

}

}

publicvoidCopyFile(StringsourceDirectory,StringdestinationDirectory){

Directory.CreateDirectory(destinationDirectory);

listBox1.Items.Add("已创建"+destinationDirectory+"\n");

if(!

Directory.Exists(sourceDirectory)){

MessageBox.Show("对不起你要拷贝的目录不存在");

return;

}

string[]directories=Directory.GetDirectories(sourceDirectory);

if(directories.Length>0){

foreach(stringdindirectories){

CopyFile(d,destinationDirectory+d.Substring(d.LastIndexOf("\\")));

}

}

String[]files=Directory.GetFiles(sourceDirectory);

if(files.Length>0){

foreach(Stringsinfiles){

File.Copy(s,destinationDirectory+s.Substring(s.LastIndexOf("\\")),true);

listBox1.Items.Add(s+"被复制\n");

}

}

}

privatevoidtextBox1_TextChanged(objectsender,EventArgse)

{

}

}

}

程序二:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespacepassword

{

classProgram

{

staticvoidMain(string[]args)

{

inti,len;

chartemp;

stringa;

a=Console.ReadLine();

char[]pass=a.ToCharArray();

len=a.Length;

for(i=0;i

temp=pass[i];

pass[i]=pass[len-1-i];

pass[len-1-i]=temp;

}

for(i=0;i

if(pass[i]>=97&&pass[i]<=122){

pass[i]=(char)(122-(pass[i]+25)%122);

}

elseif(pass[i]>=65&&pass[i]<=90){

pass[i]=(char)(90-(pass[i]+25)%90);

}

}

Console.WriteLine(pass);

Console.ReadLine();

}

}

}

四.调试和运行结果

程序一:

运行前目录一:

运行前目录二:

运行程序:

运行后的目录二:

实验二:

五.实验感想

 

实验二2.2

一.目的

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)

{

rtBox.Cut();

}

privatevoid复制ToolStripMenuItem_Click(objectsender,EventArgse)

{

rtBox.Copy();

}

privatevoid粘贴ToolStripMenuItem_Click(objectsender,EventArgse)

{

rtBox.Paste();

}

privatevoid撤销ToolStripMenuItem_Click(objectsender,EventArgse)

{

rtBox.Undo();

}

privatevoid恢复ToolStripMenuItem_Click(objectsender,EventArgse)

{

rtBox.Redo();

}

privatevoid字体ToolStripMenuItem_Click(objectsender,EventArgse)

{

FontDialogfg=newFontDialog();

if(fg.ShowDialog()==

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

当前位置:首页 > 高中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1