广东工业大学C#实验题目和报告2.doc_第1页
广东工业大学C#实验题目和报告2.doc_第2页
广东工业大学C#实验题目和报告2.doc_第3页
广东工业大学C#实验题目和报告2.doc_第4页
广东工业大学C#实验题目和报告2.doc_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

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

文档简介

.NET 程序设计实验报告 班级: 计科7 姓名: 陈日燊 学号: 3110006131 教师: 广东工业大学2013年 4 月 20 日第二次实验:内容:完成下面的实验2.1和2.2。地点:工学一号馆413/410时间:2013-4-11实验环境与工具:(1)计算机及操作系统:PC机,WindowsXP(2)程序开发平台:Visual Studio 2005/2008/2010实验2.1一实验目的1掌握System.IO类的文件流操作,会处理文件。2. 理解面向对象思想,会使用.NET Framework的类库。3. 熟悉VisualC#.NET的可视化界面,可以使用相关控件。二实验内容1. 编写一个C#应用程序,将一个包含多个子目录和文件的目录复制到另外一个指定的目录下。a)欲复制的目录中包含的文件数和子目录层次未知,必须在程序执行时获得这些信息。b)显示欲复制的目录的相关信息。2. 二战期间,英国情报人员获取德军的一机密电报,电报的内容为:bzdz izu sxgzd vs lh ,vpzg woflsh vs vwrh vhlsddlmp glm wrw gzy vsg .gflyz gstfzu bvsg gzsd hdmlp vml lm ,hghzvy wmz hwiry mvvdgvy izd z hzd vivsg ,ltz tmlo tmlO情报人员已经知道,这段电报的加密方式为: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. 非字母字符保持不变。请编程帮助情报人员破译这份机密电报。给出注释良好的源程序和程序运行后的结果。三源程序程序一:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace WindowsFormsApplication2 public partial class Form1 : Form public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) private void button1_Click_1(object sender, EventArgs e) DirectoryInfo sourceDirectory = new DirectoryInfo( F:homelzbmzsbm_pic2013); DirectoryInfo destinationDirectoty = new DirectoryInfo(C:UsersacerDesktopxxoo); CopyFile(textBox1.Text,textBox2.Text); listBox1.Items.Add(复制完成); private void button2_Click(object sender, EventArgs e) FolderBrowserDialog folder = new FolderBrowserDialog(); if (folder.ShowDialog() = DialogResult.OK) textBox1.Text = folder.SelectedPath; private void button3_Click(object sender, EventArgs e) FolderBrowserDialog folder = new FolderBrowserDialog(); if (folder.ShowDialog() = DialogResult.OK) textBox2.Text = folder.SelectedPath; public void CopyFile(String sourceDirectory, String destinationDirectory) Directory.CreateDirectory(destinationDirectory); listBox1.Items.Add(已创建 + destinationDirectory + n); if(!Directory.Exists(sourceDirectory) MessageBox.Show(对不起你要拷贝的目录不存在); return; string directories = Directory.GetDirectories(sourceDirectory); if(directories.Length0) foreach(string d in directories) CopyFile(d,destinationDirectory+d.Substring(d.LastIndexOf(); String files = Directory.GetFiles(sourceDirectory); if(files.Length0) foreach(String s in files) File.Copy(s, destinationDirectory + s.Substring(s.LastIndexOf(), true); listBox1.Items.Add(s + 被复制n); private void textBox1_TextChanged(object sender, EventArgs e) 程序二:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace password class Program static void Main(string args) int i,len; char temp; string a; a = Console.ReadLine(); char pass= a.ToCharArray(); len=a.Length; for(i=0;ilen/2-1;i+) temp=passi; passi=passlen-1-i; passlen-1-i=temp; for (i = 0; i =97&passi=65&passi=90) passi = (char)(90 - (passi + 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。三源程序主窗体部分:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace _011_shiyan3 public partial class Form1 : Form public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) Form2 f = new Form2(); f.Show(); private void button2_Click(object sender, EventArgs e) Form3 f = new Form3(); f.Show(); private void button3_Click(object sender, EventArgs e) Form4 f = new Form4(); f.Show(); private void button5_Click(object sender, EventArgs e) this.Close(); 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(); 排序/添加随机数:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace _011_shiyan3 public partial class Form2 : Form public Form2() InitializeComponent(); FileStream fs = new FileStream(test.txt, FileMode.Open, FileAccess.ReadWrite); StreamReader sr = new StreamReader(fs); /MessageBox.Show(sr.ReadToEnd(); /richTextBox1.Text = ddddddddd; richTextBox1.Text = sr.ReadToEnd(); sr.Close(); fs.Close(); 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()%30); 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 button3_Click(object sender, EventArgs e) this.Close(); 结果图: (修改后的内容是添加过随机数的)文本编辑器:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace _011_shiyan3 public partial class Form3 : Form public Form3() InitializeComponent(); private bool SaveInfo; private void CheckSave() /检查文件是否保存 if (rtBox.Text != ) if (MessageBox.Show(是否保存当前文件?, 确认, MessageBoxButtons.OKCancel) = DialogResult.OK) SaveFile(); SaveInfo = true; else SaveInfo = false; private void SaveFile() /保存文件 SaveFileDialog saveFileDialog1=new SaveFileDialog(); saveFileDialog1.Filter = 所有文件(*.*)|*.*|文本文件(*.txt)|*.txt; saveFileDialog1.InitialDirectory = C:; if (saveFileDialog1.ShowDialog() = DialogResult.OK) rtBox.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); else return; private void OpenFile() /打开文件 CheckSave(); OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = 所有文件(*.*)|*.*|文本文件(*.txt)|*.txt; openFileDialog1.InitialDirectory = C:; if (openFileDialog1.ShowDialog() = DialogResult.OK) rtBox.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText); else return; private void 新建ToolStripMenuItem_Click(object sender, EventArgs e) CheckSave(); if (SaveInfo = true) rtBox.Clear(); private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) OpenFile(); private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) SaveFile(); private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) this.Close(); private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e) rtBox.Cut(); private void 复制ToolStripMenuItem_Click(object sender, EventArgs e) rtBox.Copy(); private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e) rtBox.Paste(); private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e) rtBox.Undo(); private void 恢复ToolStripMenuItem_Click(object sender, EventArgs e) rtBox.Redo(); private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) FontDialog fg = new FontDialog(); if(fg.ShowDialog() = DialogResult.OK) rtBox.SelectionFont = fg.Font; else return; private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e) ColorDialog cg = new ColorDialog(); if (cg.Sh

温馨提示

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

评论

0/150

提交评论