c#综合实验报告_第1页
c#综合实验报告_第2页
c#综合实验报告_第3页
c#综合实验报告_第4页
c#综合实验报告_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

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

文档简介

C#综合实验实验报告 专 业: _网 络 工 程 _ 班 级:_091 班 _ 学 号:_080712110235_ 学生姓名:_姬 世 海 指导教师:_罗 为 _ 贵州大学计算机科学与信息学院 2012 年 6 月 13 号 贵州大学实验报告 学院:计算机科学与信息工程学院 专业:网络工程 班级:091 姓名 姬世海 学号 080712110235 实验组 6 实验 时间 2012.6.7 指导教师 罗 为 成绩 实验项目 名称 C # 综 合 程 序 设 计 实 验 目 的 1. 了解文件流的概念。 2. 学会文本文件的读写。 3. 学会序列化及二进制随机文件的读写。 实 验 内 容 1.设计如下窗体,A.要求用户点击添加按钮时将当前学生信息添加到指定文本文件 中(如源文件中有内容,不覆盖)。B.输入时,要求采用异常处理,针对输入学号、姓 名为空,输入年龄、分数小于 0 的情况,弹出相应的出错对话框进行提示。C.点击显示 按钮,将当前文件中内容显示出来。(提示:异常处理时要自定义异常,具体方法见参 考程序) 2. A.封装一个学生类对象,将以上程序学生信息采用 B.序列化方式写入二进制文件, 并可 C.输入学号随机查找相应记录信息显示在文本框中,查找记录时,也要求用 D.异常 处理处理输入学号为空的情况。 第 3 页 共 22 页 实 验 环 境 Pc 机; VC+ 6.0 环境(具体环境自己调整) 实 验 步 骤 参考书 P.27, windows 窗体应用程序的实验步骤。 序列化、二进制文件的读写及随机记录的读取参考书 p.270 实 验 程 序 实验内容1程序: Form1 代码如下: using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Test_end public partial class Form1 : Form public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) StreamWriter f; string path = “G:myfile.txt“; f = new StreamWriter(path); f.WriteLine(textBox1.Text); f.Close(); private void textBox1_TextChanged(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e) Form2 代码如下: using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Test_end public partial class Form2 : Form public Form2() InitializeComponent(); private void button1_Click(object sender, EventArgs e) string path = “H:myfile.txt“; StreamReader f; f = new StreamReader(path); textBox1.Text = f.ReadToEnd(); f.Close(); private void Form2_Load(object sender, EventArgs e) 第 5 页 共 22 页 Form3 代码如下: using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Test_end public partial class Form3 : Form public Form3() InitializeComponent(); private void button1_Click(object sender, EventArgs e) string path = “G:myfile.dat“; FileStream f = File.OpenWrite(path); BinaryWriter sb = new BinaryWriter(f); int i; string name = new string3“王华“, “孙强“, “成功“; float score = new float378.5f, 88.6f, 98.5f; for (i = 0; i -1) for (i = 0; i -1) mystr = mystr + sr.ReadLine() + “rn“; sr.Close(); fs.Close(); textBox1.Text = mystr; Form3 代码如下: using System; using System.Collections.Generic; using System.IO; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Test_end2 public partial class Form3 : Form string path = “GMyTest1.txt“; /文件名path作为Form3类的字段 public Form3() InitializeComponent(); private void button1_Click(object sender, EventArgs e) if (File.Exists(path) File.Delete(path); else FileStream fs = File.OpenWrite(path); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(textBox1.Text); sw.Close(); fs.Close(); button2.Enabled = true; private void button2_Click(object sender, EventArgs e) string mystr = “; FileStream fs = File.OpenRead(path); StreamReader sr = new StreamReader(fs); while (sr.Peek() -1) mystr = mystr + sr.ReadLine() + “rn“; sr.Close(); fs.Close(); textBox2.Text = mystr; private void Form3_Load(object sender, EventArgs e) textBox1.Text = “; textBox2.Text = “; button1.Enabled = true; button2.Enabled = false; 第 13 页 共 22 页 Form4 代码如下: using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Test_end2 public partial class Form4 : Form string path = “GMyTest.dat“; const int stnum = 4; /学生人数常量 struct Student public int sno; public string sname; public double score; Student st = new Studentstnum; public Form4() InitializeComponent(); private void button1_Click(object sender, EventArgs e) st0.sno = 1; st0.sname = “王 华“; st0.score = 90.2; st1.sno = 3; st1.sname = “陈红兵“; st1.score = 87.5; st2.sno = 4; st2.sname = “刘 英“; st2.score = 72.5; st3.sno = 2; st3.sname = “张晓华“; st3.score = 88.5; int i; if (File.Exists(path) File.Delete(path); FileStream fs = File.OpenWrite(path); BinaryWriter sb = new BinaryWriter(fs, Encoding.Default); for (i = 0; i -1) mystr = mystr + sb.ReadInt32() + “t“ + sb.ReadString() + “t“ + sb.ReadDouble() + “rn“; sb.Close(); fs.Close(); textBox1.Text = mystr; 第 15 页 共 22 页 Form6 代码如下: using System; using System.Collections.Generic; using System.IO; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Test_end2 public partial class Form6 : Form string path = “H:MyTest.dat“; public Form6() InitializeComponent(); private void Form6_Load(object sender, EventArgs e) comboBox1.Items.Add(1); comboBox1.Items.Add(2); comboBox1.Items.Add(3); comboBox1.Items.Add(4); comboBox1.Text = “; textBox1.Text = “; private void button1_Click(object sender, EventArgs e) int n,reclen,currp; textBox1.Text = “; if (comboBox1.Text != “) string mystr; FileStream fs = File.OpenRead(path); BinaryReader sb = new BinaryReader(fs, Encoding.Default); reclen = (int)(fs.Length / 4); /每个记录的长度 n = Convert.ToInt16(comboBox1.Text); currp = (n - 1) * reclen; fs.Seek(currp, SeekOrigin.Begin); mystr = sb.ReadInt32() + “t“ + sb.ReadString() + “t“ + sb.ReadDouble(); sb.Close(); fs.Close(); textBox1.Text = mystr; Form7 代码如下: using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Collections; using System.Windows.Forms; namespace Test_end2 public partial class Form7 : Form Serializable public class Student private int no; /学号 private string name; public int pno get return no; set no = value; public string pname get return name; set name = value; public Form7() InitializeComponent(); private void button1_Click(object sender, EventArgs e) 第 17 页 共 22 页 ArrayList st = new ArrayList(); Student s1 = new Student(); s1.pno = 1; s1.pname = “王华“; Student s2 = new Student(); s2.pno = 8; s2.pname = “李明“; st.Add(s1); st.Add(s2); FileStream f = new FileStream(“H:C#2005ch11studnt.dat“,FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(f,st); f.Close(); f = new FileStream(“H:C#2005ch11studnt.dat“, FileMode.Open); st.Clear(); st = (ArrayList)formatter.Deserialize(f); f.Close(); textBox1.Text = “学号t姓名“ + “rn“; foreach (Student s in st) textBox1.Text = textBox1.Text + string.Format(“0t1“, s.pno, s.pname) + “rn“; Proj_CS: using System; using System.Collections.Generic; using System.Windows.Forms; namespace Test_end2 static class Program / / 应用程序的主入口点。 / STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form7(); 实 验 结 果 及 分 析 实验内容1相关窗体如下: Form1 Form2 Form3 Form4 第 19 页 共 22 页 Form5 程序调试后效果: 结果分析: 使用 C#窗体设计,制作出可以存储效果的窗体,在代码编写前,其必须有对应的存储路径,即 “G:myfile.txt“,所以在进行代码编写前,需要在自己的计算机盘目录中建立文档文件,这样就可以 为下步的读取有相应的准备,参看对 Form1 的分析,其方式即在代码的编写中,要选择存储盘,在 进入最后的 Form5 编写时,相应的代码链接联系到之前的 Form

温馨提示

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

评论

0/150

提交评论