C#综合实验报告.docx

上传人:b****5 文档编号:7823163 上传时间:2023-01-26 格式:DOCX 页数:14 大小:681.21KB
下载 相关 举报
C#综合实验报告.docx_第1页
第1页 / 共14页
C#综合实验报告.docx_第2页
第2页 / 共14页
C#综合实验报告.docx_第3页
第3页 / 共14页
C#综合实验报告.docx_第4页
第4页 / 共14页
C#综合实验报告.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

C#综合实验报告.docx

《C#综合实验报告.docx》由会员分享,可在线阅读,更多相关《C#综合实验报告.docx(14页珍藏版)》请在冰豆网上搜索。

C#综合实验报告.docx

C#综合实验报告

贵州大学实验报告

学院:

专业:

网络工程班级:

姓名

学号

实验组

实验时间

2012.6.7

指导教师

成绩

实验项目名称

综合实验

实验目的

1.了解文件流的概念。

2.学会文本文件的读写。

3.学会序列化及二进制随机文件的读写。

实验内容

1.设计如下窗体,A.要求用户点击添加按钮时将当前学生信息添加到指定文本文件中(如源文件中有内容,不覆盖)。

B.输入时,要求采用异常处理,针对输入学号、姓名为空,输入年龄、分数小于0的情况,弹出相应的出错对话框进行提示。

C.点击显示按钮,将当前文件中内容显示出来。

(提示:

异常处理时要自定义异常,具体方法见参考程序)

2.A.封装一个学生类对象,将以上程序学生信息采用B.序列化方式写入二进制文件,并可C.输入学号随机查找相应记录信息显示在文本框中,查找记录时,也要求用D.异常处理处理输入学号为空的情况。

实验环境

Pc机

VS2010环境(具体环境自己调整)

 

 

1、代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.IO;

namespaceProj_Zongheshiyan

{

publicpartialclassForm1:

Form

{

classExceptionSno:

Exception{}

classExceptionSname:

Exception{}

classExceptionSage:

Exception{}

classExceptionSgra:

Exception{}

stringpath="E:

\\StudentTest.bat";

structStudent

{

publicstringsno;

publicstringsname;

publicstringssex;

publicintsage;

publicintsgra;

}

publicForm1()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

Studentst=newStudent();

StreamWritersb=newStreamWriter(path,true);

st.sno=SNo.Text;

try

{

if(st.sno=="")

thrownewExceptionSno();

sb.Write(st.sno);

}

catch(ExceptionSno)

{MessageBox.Show("我自己定义的异常,学号不能为空!

");}

st.sname=SName.Text;

try

{

if(st.sname=="")

thrownewExceptionSname();

sb.Write(st.sname);

}

catch(ExceptionSname)

{MessageBox.Show("我自己定义的异常,姓名不能为空!

");}

st.ssex=SSex.Text;

st.sage=int.Parse(SAge.Text);

sb.Write(st.ssex);

try

{

if(st.sage<0)

thrownewExceptionSage();

sb.Write(st.sage);

}

catch(ExceptionSage)

{MessageBox.Show("我自己定义的异常,年龄不能小于0!

");}

st.sgra=int.Parse(SGra.Text);

try

{

if(st.sgra<0)

thrownewExceptionSgra();

sb.Write(st.sgra);

}

catch(ExceptionSgra)

{MessageBox.Show("我自己定义的异常,分数不能小于0!

");}

sb.WriteLine();

sb.Close();

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

stringstr="";

FileStreamfs=File.OpenRead(path);

StreamReadersr=newStreamReader(fs,Encoding.UTF8);

fs.Seek(0,SeekOrigin.Begin);

while(sr.Peek()>-1)

str=str+sr.ReadLine()+"\r\n";

sr.Close();

fs.Close();

textBox1.Text=str;

}

}

}

2、代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.IO;

usingSystem.Runtime.Serialization;

usingSystem.Runtime.Serialization.Formatters.Binary;

namespaceProj_Zongheshiyan2

{

publicpartialclassForm1:

Form

{

stringpath="E:

\\student.bat";

[Serializable]

publicclassStudent

{

privateintno;

privatestringname;

privatestringsex;

privateintage;

privateintgra;

publicintsno

{

get{returnno;}

set{no=value;}

}

publicstringsname

{

get{returnname;}

set{name=value;}

}

publicstringssex

{

get{returnsex;}

set{sex=value;}

}

publicintsage

{

get{returnage;}

set{age=value;}

}

publicintsgra

{

get{returngra;}

set{gra=value;}

}

}

publicForm1()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

Students=newStudent();

s.sno=int.Parse(Sno.Text);

s.sname=Sname.Text;

s.ssex=Ssex.Text;

s.sage=int.Parse(Sage.Text);

s.sgra=int.Parse(Sgra.Text);

FileStreamfs=newFileStream(path,FileMode.Create);

BinaryFormatterformatter=newBinaryFormatter();

formatter.Serialize(fs,s);

fs.Close();

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

intn,reclen,currp;

textBox1.Text="";

if(comboBox1.Text=="")

{

MessageBox.Show("请输入学号,再查找!

");

}

else

{

stringstr;

FileStreamfs=File.OpenRead(path);

BinaryReadersb=newBinaryReader(fs,Encoding.Default);

reclen=(int)(fs.Length/3);

n=int.Parse(comboBox1.Text);

currp=(n-1)*reclen;

fs.Seek(currp,SeekOrigin.Begin);

str=sb.ReadInt32()+"\t"+sb.ReadString()+"\t"+sb.ReadString()+"\t"+sb.ReadInt32()+"\t"+sb.ReadInt32();

sb.Close();

fs.Close();

textBox1.Text=str;

}

}

}

}

 

实验结果

分析

运行界面如下:

程序运行结果:

实验总结

此次试验花费了不少时间,但却在试验中整合了整合了不少知识点。

虽然我对编程没有什么天赋,但是通过了这学期实验,我学会了更多的解决问题的办法,比如很多问题我们都可以通过网络查询解决。

当然,书上的知识也能够对这个实验有很好的引道,通过对各个例子运行,渗透每个程序的真是含义

指导教师意见

 

签名:

年月日

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

当前位置:首页 > 农林牧渔 > 林学

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

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