已阅读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.异常处理处理输入学号为空的情况。实验环境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_endpublic partial class Form1 : Formpublic 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) 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) stringpath = 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 = 2; i+) sb.Write(namei); sb.Write(scorei); sb.Close(); f.Close(); private void Form3_Load(object sender, EventArgs e) 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_end public partial class Form4 : Form public Form4() InitializeComponent(); private void button1_Click(object sender, EventArgs e) stringpath = G:myfile.dat; if (!File.Exists(path) textBox1.Text = 指定的文件不存在; else FileStream f = File.OpenRead(path); BinaryReader sb = new BinaryReader(f); string mystr = ; string name = ; float score = 0; int i; float s = 0; for (i = 0; i -1) for (i = 0; i 5; i+) mystr = mystr + sr.ReadLine() + t; mystr = mystr + rn; sr.Close(); fs.Close(); textBox6.Text = mystr; private void textBox1_TextChanged(object sender, EventArgs e) private void groupBox2_Enter(object sender, EventArgs e) private void Form5_Load(object sender, EventArgs e) Proj_Test_end:using System;using System.Collections.Generic;using System.Windows.Forms;namespace Test_end static class Program STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form5(); 实验内容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_end2 public partial class Form1 : Form public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) int i; string filen; string filea; listBox1.Items.Clear(); if (!Directory.Exists(textBox1.Text) MessageBox.Show(textBox1.Text + 文件夹不存在,信息提示,MessageBoxButtons.OK); else filen = Directory.GetFiles(textBox1.Text); 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; 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; 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) 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相关窗体如下: For
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 皮肤管理师岗前生产安全培训考核试卷含答案
- 客服培训资料
- 井下支护工岗前基础技能考核试卷含答案
- 轧钢精整工创新思维测试考核试卷含答案
- 工业炉燃料系统装配工岗前安全素养考核试卷含答案
- 未来五年层绞式光缆企业数字化转型与智慧升级战略分析研究报告
- 未来五年引出端耐焊接热试验设备行业直播电商战略分析研究报告
- 未来五年支撑软件企业ESG实践与创新战略分析研究报告
- 未来五年马棘类灌木种子行业跨境出海战略分析研究报告
- 2025年考驾照气味测试题及答案
- 肾癌病人教育知识培训课件
- 雅马哈DTX430K电子鼓中文说明书
- 2024内蒙古公务员考试行测真题(行政执法类)
- 40个团体心理辅导游戏
- 处方规范书写培训课件
- 咯血病人的护理小讲课
- 科创板开户知识测试题及答案
- 3 1 2 电离平衡常数 练习 人教版(2019)高中化学选择性必修一
- Python图像处理课件
- 勘察设计进度计划及风险防控措施
- 2025秋国开《形势与政策》形考大作业答案:如何理解“作风建设永远在路上永远没有休止符”?我们应如何加强作风建设
评论
0/150
提交评论