1、C#实验综合设计贵州大学计算机科学与信息学院C#综合设计题 目 姓 名 学 号 班 级 完成 时间 综合设计学院:计算机科学与信息工程学院 专业:网络工程 班级:网络091姓名学号设计组设计时间2012-6-10指导教师成绩设计项目名称综合设计 设计 目的1. 了解文件流的概念。2. 学会文本文件的读写。3. 学会序列化及二进制随机文件的读写。设计内容1.设计如下窗体,A.要求用户点击添加按钮时将当前学生信息添加到指定文本文件中(如源文件中有内容,不覆盖)。B.输入时,要求采用异常处理,针对输入学号、姓名为空,输入年龄、分数小于0的情况 ,弹出相应的出错对话框进行提示。C.点击显示按钮,将当前
2、文件中内容显示出来。(提示:异常处理时要自定义异常,具体方法见参考程序) 2. A.封装一个学生类对象,将以上程序学生信息采用序列化方式写入二进制文件,并可输入学号随机查找相应记录信息显示在文本框中,查找记录时,也要求用异常处理处理输入学号为空的情况。 设计环境Pc机Visual studio2005设计步骤1. 编写程序2. 运行程序3. 得出实验结果设计程序1.using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace StudentLast sta
3、tic class Program / / 应用程序的主入口点。 / STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(); using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using
4、System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace StudentLast public partial class Form1 : Form string path = G:C#实验StudentLastStudentLastStudentLast.txt; public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) string txt = textBo
5、x1.Text + + textBox2.Text + + textBox3.Text + + textBox4.Text + + textBox5.Text+rn; File.AppendAllText(path, txt); 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 +
6、 sr.ReadLine() + rn; sr.Close(); fs.Close(); richTextBox1.Text = mystr; private void textBox1_TextChanged(object sender, EventArgs e) if (textBox1.Text = ) MessageBox.Show(学号为空,请重新输入!); private void textBox2_TextChanged(object sender, EventArgs e) if (textBox2.Text = ) MessageBox.Show(姓名为空,请重新输入!);
7、private void textBox3_TextChanged(object sender, EventArgs e) if (textBox3.Text != 男 & textBox3.Text != 女) MessageBox.Show(性别输入有误,请重新输入!); private void textBox4_TextChanged(object sender, EventArgs e) int age = Convert.ToInt32(textBox4.Text); if (age 1) MessageBox.Show(年龄输入有误,请重新输入!); private void t
8、extBox5_TextChanged(object sender, EventArgs e) int score = 0; if (textBox5.Text != ) if (textBox5.Text != -) score = Convert.ToInt32(textBox5.Text); if (score100) MessageBox.Show(分数输入有误,请重新输入!); private void button3_Click(object sender, EventArgs e) 2.using System;using System.Collections.Generic;u
9、sing System.Linq;using System.Windows.Forms;namespace zhsj static class Program / / 应用程序的主入口点。 / STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(); using System;using System.Collections.Generic;using Syste
10、m.Linq;using System.Text;namespace zhsj class NULL1 : Exception class NULL2 : Exception class NOT1 : Exception class NOT2 : Exception class SN : Exception class NULL3 : Exception class yccl public static bool yc(string sa, string sb, string sc, string sd) if(sa =) throw new NULL1 (); else if(sb=) th
11、row new NULL2(); else if(Convert .ToInt32 (sc)0) throw new NOT1(); else if (Convert.ToDouble(sd) 100) throw new NOT2(); else return true; public static bool yd(string se) if (se = ) throw new NULL3(); else return true; using System;using System.Collections.Generic;using System.Linq;using System.Text
12、;namespace zhsj Serializable class stud public int sno, sage; public string sname, sex; public double score; public stud() public stud(int psno, string psname, string psex, int psage, double pscore) sno = psno; sname = psname; sex = psex; sage = psage; score = pscore; public int no get return sno; s
13、et sno = value; public string name get return sname; set sname = value; public string ssex get return sex; set sex = value; public int age get return sage; set sage = value; ; public double sscore get return score; set score = value; using System;using System.Collections;using System.Collections.Gen
14、eric;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary;using System.IO;namespace zhsj public partial class Form1 : Form public Form1
15、() InitializeComponent(); ArrayList st = new ArrayList(); private void Form1_Load(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e) try yccl.yc(textBox1.Text, textBox2.Text, textBox4.Text, textBox5.Text); stud s = new stud(Convert.ToInt32(textBox1.Text), textBox2.Tex
16、t, textBox3.Text, Convert.ToInt32(textBox4.Text), Convert.ToDouble(textBox5.Text); st.Add(s); FileStream f = new FileStream(I:C#student.dat, FileMode.Create); BinaryFormatter b = new BinaryFormatter(); b.Serialize(f, st); f.Close(); MessageBox.Show(学生信息添加成功!); if (!comboBox1.Items.Contains(textBox1.
17、Text)&!comboBox1.Items.Contains(s.no) comboBox1.Items.Add(textBox1.Text); catch (NULL1) MessageBox.Show(请输入的学号不能为空!); catch (NULL2) MessageBox.Show(请输入的姓名不能为空!); catch (NOT1) MessageBox.Show(请输入的年龄不能小于0!); catch (NOT2) MessageBox.Show(请输入的分数不能小于0或大于100!); private void button2_Click(object sender, Ev
18、entArgs e) try if(!File .Exists(I:C#student.dat) MessageBox.Show(无学生信息记录,请先添加!); else FileStream f = new FileStream(I:C#student.dat, FileMode.Open); BinaryFormatter b = new BinaryFormatter(); st.Clear(); st = (ArrayList)b.Deserialize(f); f.Close(); textBox6.Text = 学号t姓名t性别t年龄t分数 + rn; foreach (stud
19、s1 in st) textBox6.Text = textBox6.Text + string.Format(0t1t2t3t4, s1.no, s1.name, s1.ssex, s1.age, s1.sscore) + rn; if (!comboBox1.Items.Contains(s1.no)&!comboBox1.Items.Contains(textBox1.Text) comboBox1.Items.Add(s1.no); catch(Exception ex) MessageBox.Show(ex.Message); private void button3_Click(o
20、bject sender, EventArgs e) try yccl.yd(comboBox1.Text); ArrayList stu = new ArrayList(); stud s2 = new stud(); FileStream f = new FileStream(I:C#student.dat, FileMode.Open); BinaryFormatter b = new BinaryFormatter(); stu.Clear(); stu = (ArrayList)b.Deserialize(f); f.Close(); textBox6.Text = 学号t姓名t性别
21、t年龄t分数 + rn; foreach (stud s6 in stu) if (s6.no = Convert.ToInt32(comboBox1.Text) textBox6.Text = textBox6.Text + string.Format(0t1t2t3t4, s6.no, s6.name, s6.ssex, s6.age, s6.sscore) + rn; break; catch (NULL3) MessageBox.Show(学号为空,请先选择学号或正确添加信息!); namespace zhsj partial class Form1 / / 必需的设计器变量。 / p
22、rivate System.ComponentModel.IContainer components = null; / / 清理所有正在使用的资源。 / / 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) if (disposing & (components != null) components.Dispose(); base.Dispose(disposing); #region Windows 窗体设计器生成的代码 / / 设计器支持所需的方法 - 不要 / 使用代码编辑器修改此方
23、法的内容。 / private void InitializeComponent() this.label1 = new System.Windows.Forms.Label(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.button1 = new System.Windows.Forms.Button(); this.textBox5 = new System.Windows.Forms.TextBox(); this.textBox4 = new System.Windows.Forms.TextBox(); t
24、his.textBox3 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); t
25、his.label2 = new System.Windows.Forms.Label(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.button2 = new System.Windows.Forms.Button(); this.textBox6 = new System.Windows.Forms.TextBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.button3 = new System.Windows.Forms.But
26、ton(); boBox1 = new System.Windows.Forms.ComboBox(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.SuspendLayout(); / / label1 / this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(15, 34); this.label1.Name = label1; thi
27、s.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 0; this.label1.Text = 学号:; / / groupBox1 / this.groupBox1.Controls.Add(this.button1); this.groupBox1.Controls.Add(this.textBox5); this.groupBox1.Controls.Add(this.textBox4); this.groupBox1.Controls.Add(this.textBox3); this.groupBox1.Controls.Add(this.textBox2); this.groupBox1.Controls.Add(this.textBox1); this.groupBox1.Controls.Add(this.label5); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label1);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1