C#程序设计实验报告_c

其他范文 时间:2020-02-28 11:20:42 收藏本文下载本文
【www.daodoc.com - 其他范文】

C#程序设计实验报告由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“c”。

实验报告书写要求

实验报告原则上要求学生手写,要求书写工整。若因课程特点需打印的,标题采用四号黑体,正文采用小四号宋体,单倍行距。纸张一律采用A4的纸张。

实验报告书写说明

实验报告中实验目的和要求、实验仪器和设备、实验内容与过程、实验结果与分析这四项内容为必需项。教师可根据学科特点和实验具体要求增加项目。

填写注意事项

(1)细致观察,及时、准确、如实记录。(2)准确说明,层次清晰。

(3)尽量采用专用术语来说明事物。

(4)外文、符号、公式要准确,应使用统一规定的名词和符号。(5)应独立完成实验报告的书写,严禁抄袭、复印,一经发现,以零分论处。

实验报告批改说明

实验报告的批改要及时、认真、仔细,一律用红色笔批改。实验报告的批改成绩采用五级记分制或百分制,按《金陵科技学院课堂教学实施细则》中作业批阅成绩评定要求执行。

实验报告装订要求

实验批改完毕后,任课老师将每门课程的每个实验项目的实验报告以自然班为单位、按学号升序排列,装订成册,并附上一份该门课程的实验大纲。

金陵科技学院实验报告

实验项目名称: C#基础编程 实验学时: 6 同组学生姓名: 实验地点: 1318 实验日期: 10月5日-10月19日 实验成绩: 批改教师: 批改时间:

金陵科技学院实验报告

实验1 C#基础编程

一、实验目的1、熟悉Visual Studio.NET开发环境。

2、掌握C#应用程序的基本操作过程。

3、掌握C#的数据类型,运算符以及表达式的使用。

4、掌握分支和循环语句的使用方法。

5、掌握一维数组,二维数组及数组型数组的使用。

二、实验要求

(1)编写程序要规范、正确,上机调试过程和结果要有记录(2)做完实验后给出本实验的实验报告。

三、实验设备、环境

安装有Visual Studio.NET软件。

四、实验步骤

1、分析题意。

2、根据题目要求,新建项目。

3、编写并输入相关的程序代码。

5、运行与调试项目。

6、保存项目。

五、实验内容

1、编写一个简单的控制台应用程序,打印一行文字(如你的姓名)。

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace one.first {

cla Program

{

static void Main(string[] args)

{

System.Console.WriteLine(“我叫王蕾!”);

}

} }

2、编写一个简单的Windows应用程序,在窗体Load事件中书写代码,标签中显示你的姓名。

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;

namespace one.second {

public partial cla Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

this.Text = “Windows 程序”;

Label lblShow = new Label();

lblShow.Location = new Point(20, 30);

lblShow.AutoSize = true;

lblShow.Text = “王蕾!”;

this.Controls.Add(lblShow);

}

} }

3、编写一个一个程序,用来判断输入的是大写字母,小写字母,数字还是其他的字符。

using System;using System.Collections.Generic;using System.Text;

namespace one.third {

cla Program

{

static void Main(string[] args)

{

Console.WriteLine(“请输入一个字符:”);

char c = Convert.ToChar(Console.ReadLine());

if((c>='a'&&c='A'&&c

Console.WriteLine(“这是一个字母”);

if(char.IsDigit(c))

Console.WriteLine(“这是一个数字”);

金陵科技学院实验报告

}

} }

4、分别用while,do-while,for循环求1到100的和。

using System;using System.Collections.Generic;using System.Text;

namespace one.forth.one {

cla Program

{

static void Main(string[] args)

{

int i = 1, sum = 0;

while(i

{

sum = sum + i;

i++;

}

Console.WriteLine(“1到100的自然数之和为:” + sum);

}

} } using System;using System.Collections.Generic;using System.Text;

namespace one.forth.two {

cla Program

{

static void Main(string[] args)

{

int i = 1, sum = 0;

do

{

sum = sum + i;

i++;

}

while(i

Console.WriteLine(“1到100的自然数的和为:” + sum);

}

}

金陵科技学院实验报告

} using System;using System.Collections.Generic;using System.Text;

namespace one.forth.three {

cla Program

{

static void Main(string[] args)

{

int i , sum = 0;

for(i = 1;i

{

sum = sum + i;

}

Console.WriteLine(“1到100的自然数的和为:” + sum);

}

} }

5、定义一个一维数组,用随机数为此赋值,用foreach循环输出其中的内容。

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace first.five {

cla Program

{

static void Main(string[] args)

{

int[] a = {0,1,2,3,4};

foreach(int i in a)

{

Console.WriteLine(a[i]);

}

}

} }

6、实现二维数组的输入和输出。

using System;using System.Collections.Generic;using System.Linq;

金陵科技学院实验报告

using System.Text;

namespace first.six {

cla Program

{

static void Main(string[] args)

{

int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

{

for(int i = 0;i

{

for(int j = 0;j

{ Console.WriteLine(a[i, j]);}

}

}

}

} }

7、实现数组型数组的输入和输出。

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace first.seven {

cla Program

{

static void Main(string[] args)

{

int[][] a = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };

for(int i = 0;i

{

for(int j = 0;j

{

Console.WriteLine(a[i][j]);

}

}

}

} }

六、实验体会(遇到问题及解决办法,编程后的心得体会)

刚开始编程的时候觉得无从下手,尽管我们已经学了好几种高级编程语言,但每个都

金陵科技学院实验报告

有其独特的地方,稍不留神就会混淆。

通过这次实验,我体会到课后复习巩固的重要性。在编程的时候,很多内容都不记得,需要去翻书。不得不说,实验是巩固课程的好方法!本次实验,我熟悉Visual Studio.NET开发环境;掌握了C#应用程序的基本操作过程;掌握了C#的数据类型,运算符以及表达式的使用;掌握了分支和循环语句的使用方法以及一维数组,二维数组及数组型数组的使用。

金陵科技学院实验报告

实验项目名称: 类与对象 实验学时: 6 同组学生姓名: 实验地点: 1318 实验日期: 10月26日-11月9日 实验成绩: 批改教师: 批改时间:

金陵科技学院实验报告

实验2 类与对象

一、实验目的、要求

(1)掌握类的定义和使用;

(2)掌握类的数据成员,属性的定义和使用;

(3)掌握方法的定义,调用和重载以及方法参数的传递;(4)掌握构造函数的定义和使用。

二、实验要求

(1)编写程序要规范、正确,上机调试过程和结果要有记录;(2)做完实验后给出本实验的实验报告。

三、实验设备、环境

安装有Visual Studio.NET软件。

四、实验步骤

1、分析题意;

2、根据题目要求,新建项目;

3、编写并输入相关的程序代码;

5、运行与调试项目;

6、保存项目。

五、实验内容

1、定义一个方法,实现两个数的交换(分别把参数按值传递和按引用传递)。

using System;using System.Collections.Generic;using System.Text;

namespace second.one {

cla Program

{

static void Main(string[] args)

{

Swaper s = new Swaper();

Console.WriteLine(“输入x的值:”);

int a = Convert.ToInt32(Console.ReadLine());

Console.WriteLine(“输入y的值:”);

int b=Convert.ToInt32(Console.ReadLine());

金陵科技学院实验报告

Console.WriteLine(s.Swap(a, b));

Console.WriteLine(s.Swap(ref a,ref b));

}

cla Swaper

{

public string Swap(int x, int y)

{

int temp;

temp = x;

x = y;

y = temp;

return 后:x={0},y={1}“,x,y);

}

public string Swap(ref int x, ref int y)

{

int temp;

temp = x;

x = y;

y = temp;

return string.Format(”按引用传参交换之后:x={0},y={1}“, x, y);

}

}

} }

2、定义一个方法,实现数组的排序。using System;using System.Collections.Generic;using System.Text;

namespace second.two {

cla Program

{

string.Format(”

金陵科技学院实验报告

public cla sort

{

public void change(int[] a)

{

Console.WriteLine(“排序前,数组顺序为:”);

show(a);

int i, j, m;

for(i = 0;i

{

m = a[i];

j = ib;

}

}

static void Main(string[] args)

{

sum s = new sum();

int a = 10;

int b = 3;

int m, n;

s.ab(out m, out n, a, b);

Console.WriteLine(“{0}+{1}={2};{0}-{1}={3}”,a,b,m,n);

金陵科技学院实验报告

}

} }

5、用构造函数重载,实现矩形的面积,圆的面积,梯形的面积;

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace secong.five {

cla Program

{

public cla square

{

public double area;

public square(){ }

public square(double a)

{

area = a * a * 3.14;

}

public square(double a, double b)

{

area = a * b;

}

public square(double a, double b, double h)

{

area =(a + b)/ 2 * h;

}

}

static void Main(string[] args)

{

double a, b, h,area;

a = 2;b = 5;h = 3;

金陵科技学院实验报告

square s = new square(a,b);

Console.WriteLine(“求矩形面积,长为a={0},宽为b={1},面积area={2}”,a,b,s.area);

square i = new square(a);

Console.WriteLine(“求圆形面积,半径a={0},面积area={1}”, a, i.area);

square j = new square(a, b, h);

Console.WriteLine(“求梯形面积,上底为a={0},下底为b={1},高为h={2}面积area={3}”, a, b,h, j.area);

}

} }

6、设计一个windows应用程序,在该程序中定义一个学生类和班级类,以处理每个学生的学号,姓名,语文,数学和英语成绩,要求:

1)能查询每个学生的总成绩。2)能显示全班前三名的名单。

3)能显示单科成绩最高分和不及格的学生名单。4)能统计全班学生的平均成绩。

5)能显示各科成绩不同分数段的学生人数的百分比。

Student类: using System;using System.Collections.Generic;using System.Text;namespace Test2_6 {

public cla Student

{

public string stuNo;

public string name;

public double chinese;

public double math;

public double english;

public double sumScore

{

金陵科技学院实验报告

get { return chinese + math + english;}

}

} } StudentList类: using System;using System.Collections.Generic;using System.Text;namespace Test2_6 {

public cla StudentList:Student

{

int snums;

public Student[] stu=new Student[50];

public StudentList()

{

snums = 0;

}

public void addstu(Student s)

{

stu[snums] = s;

snums++;

}

public int searchstu(string name)

{

int i;

for(i = 0;i

{

if(stu[i].name == name)break;

}

if(i == snums)return-1;

else return i;

}

//给所有成绩排序,用后面实现前三名的排名

金陵科技学院实验报告

public void ProThree()

{

for(int i = 0;i

{

int k = i;

for(int j = i + 1;j

if(stu[j].sumScore > stu[k].sumScore)k = j;

if(k!= i)

{

Student temp;

temp = stu[k];

stu[k] = stu[i];

stu[i] = temp;

}

}

}

//显示单科成绩的最高分

public int HighScore(int k)

{

int p = 0;

if(k == 0)

{

for(int i = 1;i

if(stu[i].math > stu[p].math)p = i;

}

else if(k == 1)

{

for(int i = 1;i

if(stu[i].chinese > stu[p].chinese)p = i;

}

else

{

for(int i = 1;i

if(stu[i].chinese > stu[p].chinese)p = i;

金陵科技学院实验报告

}

return p;

}

//显示不及格名单

public string BuhgName(int k)

{

string name=“ ”;

if(k == 0)

{

for(int i = 0;i

if(stu[i].math

}

else if(k == 1)

{

for(int i = 0;i

if(stu[i].chinese

}

else

{

for(int i = 0;i

if(stu[i].english

}

return name;

}

public string getHL()

{

string Maxer = “ ”, Loser = “ ”;

Maxer += “单科数学最高:” + stu[HighScore(0)].name + “n”;

Maxer += “ 单科语文最高:” + stu[HighScore(1)].name + “n”;

Maxer += “ 单科英语最高:” + stu[HighScore(2)].name + “n”;

Loser += “单科数学挂科名单:” +BuhgName(0)+ “n”;

Loser += “单科语文挂科名单:” + BuhgName(1)+ “n”;

Loser += “单科英语挂科名单:” + BuhgName(2)+ “n”;

金陵科技学院实验报告

return Maxer + “n” + Loser;

}

//全班的平均成绩

public string SumScore()

{

double sum = 0;

double avg=0;

for(int i = 0;i

{

sum = sum + stu[i].sumScore;

}

avg = sum / snums;

return “班级总分平均分:”+avg;

}

//各科成绩不同分数段的学生百分比

//英语成绩各分数段百分比

public string PerC()

{

double per1, per2, per3, per4, per5;

double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;

for(int i = 0;i

{

if((stu[i].chinese > 90)&&(stu[i].chinese

{

sumC1++;

}

else if((80

{

sumC2++;

}

金陵科技学院实验报告

else if((70

{

sumC3++;

}

else if((60

{

sumC4++;

}

else

{sumC5++;}

}

per1 = sumC1 / snums;

per2 = sumC2 / snums;

per3 = sumC3 / snums;

per4 = sumC4 / snums;

per5 = sumC5 / snums;

return “语文成绩百分比:”+“n”+“90~100:”+per1+“ 80~90:”+per2+“ 80~70:”+per3+“ 70~60:”+per4+“ 60以下的:”+per5;

}

//数学成绩各分数段百分比

public string PerM()

{

double per1, per2, per3, per4, per5;

double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;

for(int i = 0;i

{

if((stu[i].math> 90)&&(stu[i].math

{

sumC1++;

}

else if((80

{

金陵科技学院实验报告

sumC2++;

}

else if((70

{

sumC3++;

}

else if((60

{

sumC4++;

}

else

{ sumC5++;}

}

per1 = sumC1 / snums;

per2 = sumC2 / snums;

per3 = sumC3 / snums;

per4 = sumC4 / snums;

per5 = sumC5 / snums;

return string.Format(“数学成绩百分比:” + “n” + “90~100:” + per1 + “ 80~90:” + per2 + “ 80~70:” + per3 + “ 70~60:” + per4 + “ 60以下的:” + per5);

}

//英语成绩各分数段百分比

public string PerE()

{

double per1, per2, per3, per4, per5;

double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;

for(int i = 0;i

{

if((stu[i].english > 90)&&(stu[i].english

{

sumC1++;

金陵科技学院实验报告

}

else if((80

{

sumC2++;

}

else if((70

{

sumC3++;

}

else if((60

{

sumC4++;

}

else

{ sumC5++;}

}

per1 = sumC1 / snums;

per2 = sumC2 / snums;

per3 = sumC3 / snums;

per4 = sumC4 / snums;

per5 = sumC5 / snums;

return string.Format(“数学成绩百分比:” + “n” + “90~100:” + per1 + “ 80~90:” + per2 + “ 80~70:” + per3 + “ 70~60:” + per4 + “ 60以下的:” + per5);

}

} } From窗体代码: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;

金陵科技学院实验报告

using System.Windows.Forms;namespace Test2_6 {

public partial cla Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public StudentList sl = new StudentList();

private void btnAdd_Click(object sender, EventArgs e)

{

Student s = new Student();

s.stuNo = txtStuNo.Text;

s.name = txtName.Text;

s.chinese = Convert.ToDouble(txtChina.Text);

s.math = Convert.ToDouble(txtMath.Text);

s.english = Convert.ToDouble(txtEng.Text);

sl.addstu(s);

MeageBox.Show(“添加成功”);

}

private void btnSearch_Click(object sender, EventArgs e)

{

int pos = sl.searchstu(this.textBox1.Text);

if(pos!=-1)

{

label7.Text = this.textBox1.Text + “的总成绩:sl.stu[pos].sumScore;

}

else { MeageBox.Show(”不存在这个人!“);}

}

private void btnFinish_Click(object sender, EventArgs e)

{

label7.Text = ”前3名:“+”n“;

” + 金陵科技学院实验报告

for(int i = 0;i

{

sl.ProThree();

label7.Text+= sl.stu[i].name+“n”;

}

label7.Text += sl.getHL()+“n”;

label7.Text += Convert.ToString(sl.SumScore())+“n”;

label7.Text += sl.PerC()+“n”;

label7.Text += sl.PerM()+“n”;

label7.Text += sl.PerE()+“n”;

}

} }

六、实验体会(遇到问题及解决办法,编程后的心得体会)

通过本次实验,我掌握了类的定义与使用;掌握了类的数据成员,属性的定义和使用;掌握了方法的定义,调用和重载以及方法参数的传递以及构造函数的定义和使用。值得注意的是:本次实验中return的使用以及所在的位置,类型转换时也经常用到

金陵科技学院实验报告

实验项目名称: 继承与多态 实验学时: 6 同组学生姓名: 实验地点: 1318 实验日期: 11月16日-11月30日 实验成绩: 批改教师: 批改时间:

金陵科技学院实验报告

实验3 继承与多态

一、实验目的、要求

(1)掌握类的继承性与多态性;

(2)掌握虚方法的定义以及如何使用虚方法实现多态;(3)掌握抽象类的定义以及如何使用抽象方法实现多态;

二、实验要求

(1)编写程序要规范、正确,上机调试过程和结果要有记录;(2)做完实验后给出本实验的实验报告。

三、实验设备、环境

安装有Visual Studio.NET软件。

四、实验步骤

1、分析题意;

2、根据题目要求,新建项目;

3、编写并输入相关的程序代码;

5、运行与调试项目;

6、保存项目。

五、实验内容

1、设计一个Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生派生类,当输入相关数据,单击不用的按钮时,将分别创建不同的学生类对象,并输出当前学生的总人数,该学生的姓名,学生类型,平均成绩。

Student类: using System;using System.Collections.Generic;using System.Text;namespace Test3_1 {

public abstract cla Student

{

protected string name;

protected int age;

public static int number;

public Student(string name, int age)

{

this.name = name;

this.age = age;

金陵科技学院实验报告

number++;

}

public string Name

{

get { return name;}

}

public abstract double Average();

}

public cla Pupil : Student

{

protected double chinese;

protected double math;

public Pupil(string name, int age, double chinese, double math)

: base(name, age)

{

this.chinese = chinese;

this.math = math;

}

public override double Average()

{

return(chinese + math)/ 2;

}

}

public cla Middle : Student

{

protected double chinese;

protected double math;

protected double english;

public Middle(string name, int age, double chinese, double math, double english)

: base(name, age)

{

this.chinese = chinese;

this.math = math;

金陵科技学院实验报告

this.english = english;

}

public override double Average()

{

return(chinese + math + english)/ 3;

}

}

public cla College : Student

{

protected double required;

protected double elective;

public College(string name, int age, double required, double elective)

: base(name, age)

{

this.required = required;

this.elective = elective;

}

public override double Average()

{

return(required + elective)/ 2;

}

} } Form窗体内的代码:

using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Test3_1 {

public partial cla Form1 : Form

{

金陵科技学院实验报告

public Form1()

{

InitializeComponent();

}

private void btnSmall_Click(object sender, EventArgs e)

{

Pupil),Convert.ToDouble(txtMath.Text));

lblShow.Text += “总人数:” +Convert.ToString(Student.number)+ “,” + “姓名:” + p.Name + “,” + “小学生” + “,” + “平均成绩为:” + p.Average()+“n”;

}

private void btnMiddle_Click(object sender, EventArgs e)

{

Middle Convert.ToInt32(txtAge.Text),m

=

new

Middle(txtName.Text,Convert.ToDouble(txtChinese.Text), p

=

new Pupil(txtName.Text,Convert.ToInt32(txtAge.Text),Convert.ToDouble(txtChinese.TextConvert.ToDouble(txtMath.Text),Convert.ToDouble(TxtEnglish.Text));

lblShow.Text += “总人数:” + Convert.ToString(Student.number)+ “,” + “姓名:” + m.Name + “,” + “中学生” + “,” + “平均成绩为:” + m.Average()+ “n”;

}

private void btnBig_Click(object sender, EventArgs e)

{

College Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtMath.Text));

lblShow.Text += “总人数:” + Convert.ToString(Student.number)+ “,” + “姓名:” + c.Name + “,” + “大学生” + “,” + “平均成绩为:” + c.Average()+ “n”;

}

}

c

=

new

College(txtName.Text,Convert.ToDouble(txtChinese.Text),28

金陵科技学院实验报告

}

2、设计一个Windows应用程序,在该程序中定义平面图形抽象类和派生类圆,矩形和三角形。

Figure类代码: using System;using System.Collections.Generic;using System.Text;namespace Test3_2 {

public abstract cla Figure

{

public abstract double Area();

}

public cla Circle:Figure

{

double radius;

public Circle(double r)

{

radius = r;

}

public override double Area()

{

return radius * radius * 3.14;

}

}

public cla JUxing:Figure

{

double chang;

double kuan;

public JUxing(double c, double k)

{

this.chang = c;

this.kuan = k;

}

金陵科技学院实验报告

public override double Area()

{

return chang * kuan;

}

}

public cla San:Figure

{

double bian;

double heigth;

public San(double b, double h)

{

this.bian = b;

this.heigth = h;

}

public override double Area()

{

return bian * heigth / 2;

}

} } Form窗体代码: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Test3_2 {

public partial cla Form1 : Form

{

public Form1()

{

金陵科技学院实验报告

InitializeComponent();

}

private void btnCircle_Click(object sender, EventArgs e)

{

Circle c=new Circle(Convert.ToInt32(TxtChang.Text));

lblShow.Text = “圆的面积为:” + c.Area();

}

private void btnJu_Click(object sender, EventArgs e)

{

JUxing

j

=

new JUxing(Convert.ToInt32(TxtChang.Text),Convert.ToInt32(TxtHigh.Text));

lblShow.Text = “矩形的面积为:” + j.Area();

}

private void btnSan_Click(object sender, EventArgs e)

{

San

s

=

new

San(Convert.ToInt32(TxtChang.Text), Convert.ToInt32(TxtHigh.Text));

lblShow.Text = “三角形的面积为:” + s.Area();

}

} }

3、定义一个Person类,包含姓名字段和一个方法,早上8:30学生开始上课,教师开始讲课。分别用new关键字,虚方法,抽象类实现多态性。

New关键字: using System;using System.Collections.Generic;using System.Text;

namespace third.three {

cla Program

{

static void Main(string[] args)

金陵科技学院实验报告

{

Student s=new Student(“学生”);

Teacher t=new Teacher(“教师”);

Console.WriteLine(s.name+s.work());

Console.WriteLine(t.name+t.work());

Console.ReadLine();

}

}

public cla Person

{

public string name;

public interface method

{ string work();}

}

public cla Student:Person

{

public Student(string name)

{ this.name = name;}

public string work()

{ return “早上8:30开始上课”;}

}

public cla Teacher:Person

{

public Teacher(string name)

{ this.name = name;}

public string work()

{ return “开始讲课”;}

} } 虚方法: using System;using System.Collections.Generic;using System.Text;

金陵科技学院实验报告

namespace third.three.two {

cla Program

{

static void Main(string[] args)

{

Student s = new Student(“张三”,“学生”);

PersonWork(s);

Teacher t=new Teacher(“李斯”,“教师”);

PersonWork(t);

}

private static void PersonWork(Person Person)

{ Console.WriteLine(Person.Work());}

}

public cla Person

{

public string name;

public Person(string name)

{ this.name = name;}

public virtual string Work()

{ return string.Format(“Person{0}:早上8:30开始”,name);}

}

public cla Student : Person

{

private string type;

public Student(string name, string type)

: base(name)

{ this.type = type;}

public override string Work()

{

return string.Format(“Person{0}:早上8:30开始上课”, name);

}

}

public cla Teacher : Person

金陵科技学院实验报告

{

private string type;

public Teacher(string name, string type)

: base(name)

{ this.type = type;}

public override string Work()

{

return string.Format(“Person{0}:开始讲课”, name);

}

} }

抽象类: using System;using System.Collections.Generic;using System.Text;

namespace third.three.three {

cla Program

{

static void Main(string[] args)

{

Student s = new Student(“张三”, “学生”);

PersonWork(s);

Teacher t = new Teacher(“李斯”, “教师”);

PersonWork(t);

}

private static void PersonWork(Person person)

{

Console.WriteLine(person.Work());

}

}

public abstract cla Person

金陵科技学院实验报告

{

public string name;

public Person(string name)

{ this.name = name;}

public abstract string Work();

}

public cla Student : Person

{

private string type;

public Student(string name, string type)

: base(name)

{

this.type = type;

}

public override string Work()

{

return string.Format(“Person{0}:早上8:30开始上课”, name);

}

}

public cla Teacher : Person

{

private string type;

public Teacher(string name, string type)

: base(name)

{

this.type = type;

}

public override string Work()

{

return string.Format(“Person{0}:开始讲课”, name);

}

} }

金陵科技学院实验报告

六、实验体会(遇到问题及解决办法,编程后的心得体会)

通过本次实验,我理解了类的继承性与多态性;掌握了虚方法的定义以及如何用虚方法来实现多态;掌握了抽象类的定义以及如何用抽象方法来实现多态。这次实验与前两次不同,采用Windows应用程序,既涉及到代码段也涉及到界面的设计。所以,勉强通过实验。

金陵科技学院实验报告

实验项目名称: 接口、文件和流 实验学时: 6 同组学生姓名: 实验地点: A205 实验日期: 12月7日-12月21日 实验成绩: 批改教师: 批改时间:

金陵科技学院实验报告

实验4 接口、文件和流

一、实验目的(1)掌握接口的定义及使用方法;

(2)掌握流,序列化和反序列化的概念和使用方法;(3)掌握流文件的读写操作类及其使用方法;

(4)掌握OpenFileDialog,SaveFileDialog等控件的使用。

二、实验要求

(1)编写程序要规范、正确,上机调试过程和结果要有记录;(2)做完实验后给出本实验的实验报告。

三、实验设备、环境

安装有Visual Studio.NET软件。

四、实验步骤

1、分析题意;

2、根据题目要求,新建项目;

3、编写并输入相关的程序代码;

5、运行与调试项目;

6、保存项目。

五、实验内容

1、定义一个Person类,包含姓名字段和一个方法,早上8:30学生开始上课,教师开始讲课。用接口来实现。

using System;using System.Collections.Generic;using System.Text;namespace Test4_1 {

cla Program

{

static void Main(string[] args)

{

Student s = new Student(“张三”,“学生”);

Console.WriteLine(s.Work());

Teacher t = new Teacher(“李四”,“老师”);

Console.WriteLine(t.Work());

}

金陵科技学院实验报告

public abstract cla Person

{

public string name;

public Person(string name)

{

this.name = name;

}

}

interface IPerson

{

string type { get;}

string Work();

}

public cla Student :Person, IPerson

{

public string type

{

get { return string.Format(“老师”);}

}

public Student(string name, string type)

: base(name)

{

this.name=name;

}

public string Work()

{

return string.Format(“Person{0}:早上8:30开始上课”, name);

}

}

public cla Teacher :Person, IPerson

{

public string type

{

金陵科技学院实验报告

get { return string.Format(“学生”);}

}

public Teacher(string name, string type)

: base(name)

{

this.name = name;

}

public string Work()

{

return string.Format(“Person{0}:早上8:30开始讲课”, name);

}

}

} }

2、声明一个接口Iplayer,包含5个接口方法:播放,停止,暂停,上一首和下一首。在该程序中定义一个MP3播放器类和一个AVI播放器类,以实现该接口。

MP3类(包含Iplayer接口,AVI类): using System;using System.Collections.Generic;using System.Text;namespace Test4_2 {

interface IPlayer

{

string Play();

string Stop();

string Pause();

string Pre();

string Next();

}

public cla MP3:IPlayer

{

public string Play()

金陵科技学院实验报告

{

return “正在播放MP3歌曲!”;

}

public string Stop()

{

return “停止播放MP3歌曲!”;

}

public string Pause()

{

return “暂停播放MP3歌曲!”;

}

public string Pre()

{

return “播放上一首MP3歌曲!”;

}

public string Next()

{

return “播放下一首MP3歌曲!”;

}

}

public cla AVI : IPlayer

{

public string Play()

{

return “正在播放AVI歌曲!”;

}

public string Stop()

{

return “停止播放AVI歌曲!”;

}

public string Pause()

{

return “暂停播放AVI歌曲!”;

}

金陵科技学院实验报告

public string Pre()

{

return “播放上一首AVI歌曲!”;

}

public string Next()

{

return “播放下一首AVI歌曲!”;

}

} } Form窗体里代码: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Test4_2 {

public partial cla Form1 : Form

{

IPlayer iplayer;

MP3 mp3;

AVI avi;

public Form1()

{

InitializeComponent();

}

private void btnMp3_Click(object sender, EventArgs e)

{

mp3 = new MP3();

iplayer =(IPlayer)mp3;

}

金陵科技学院实验报告

private void btnPlay_Click(object sender, EventArgs e)

{

label1.Text = iplayer.Play();

}

private void btnUp_Click(object sender, EventArgs e)

{

label1.Text = iplayer.Pre();

}

private void btnStop_Click(object sender, EventArgs e)

{

label1.Text = iplayer.Stop();

}

private void btnPause_Click(object sender, EventArgs e)

{

label1.Text = iplayer.Pause();

}

private void btnNext_Click(object sender, EventArgs e)

{

label1.Text = iplayer.Next();

}

private void btnAvi_Click(object sender, EventArgs e)

{

avi = new AVI();

iplayer =(IPlayer)avi;

}

} }

3、实现对文本文件的读写操作,用文件操作控件打开和保存文件。

using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace Test4_3

金陵科技学院实验报告

{

public partial cla Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void btnSave_Click(object sender, EventArgs e)

{

saveFileDialog1.ShowDialog();

StreamWriter

sw = StreamWriter(saveFileDialog1.FileName,true);

sw.WriteLine(DateTime.Now.ToString());

sw.WriteLine(txtSource.Text);

sw.Close();

}

private void btnShow_Click(object sender, EventArgs e)

{

StreamReader sr = StreamReader(saveFileDialog1.FileName);

txtShow.Text = sr.ReadToEnd();

sr.Close();

}

} }

4、实现对二进制文件的读写操作。

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;

new

new

金陵科技学院实验报告

namespace Test4_4 {

public partial cla Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void btnSave_Click(object sender, EventArgs e)

{

FileStream

fs

= FileStream(@“d:Datastudent.dat”,FileMode.Append,FileAcce.Write);

BinaryWriter bw = new BinaryWriter(fs);

bw.Write(Int32.Parse(txtStuNo.Text));

bw.Write(TxtName.Text);

bool isMale;

if(rdoMan.Checked)

isMale = true;

else

isMale = false;

bw.Write(isMale);

fs.Close();

bw.Close();

}

private void btnShow_Click(object sender, EventArgs e)

{

lstShow.Items.Clear();

lstShow.Items.Add(“学号t姓名t性别”);

FileStream

fs

= FileStream(@“d:Datastudent.dat”,FileMode.Open,FileAcce.Read);

BinaryReader br = new BinaryReader(fs);

fs.Position = 0;

while(fs.Position!= fs.Length)

{

new

new

金陵科技学院实验报告

int s_no = br.ReadInt32();

string name = br.ReadString();

string sex = “”;

if(br.ReadBoolean())

sex = “男”;

else

sex = “女”;

string

result String.Format(“{0}t{1}t{2}”,s_no,name,sex);

lstShow.Items.Add(result);

}

br.Close();

fs.Close();

}

} }

5、实现对象序列化和反序化。

Student类: using System;using System.Collections.Generic;using System.Text;namespace Test4_5 {

[Serializable]

public cla Student

{

public int sno;

public string name;

public bool sex;

public Student(int s_no, string name, bool isMale)

{

this.sno = s_no;

this.name = name;

this.sex = isMale;

=

金陵科技学院实验报告

}

} } StudentList类: using System;using System.Collections.Generic;using System.Text;namespace Test4_5 {

[Serializable]

public cla StudentList

{

private Student[] list = new Student[100];

public Student this[int index]

{

get

{

if(index = 100)

return list[0];

else

return list[index];

}

set

{

if(!(index =100))

list[index] = value;

}

}

} } Form下的代码: 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;using System.Runtime.Serialization.Formatters.Binary;namespace Test4_5 {

public partial cla Test9_32 : Form

{

private StudentList list = new StudentList();

private int i = 0;

public Test9_32()

{

InitializeComponent();

}

private void Test9_32_Load(object sender, EventArgs e)

{

}

private void btnSave_Click(object sender, EventArgs e)

{

string file = @“d:datastudent.dat”;

Stream

stream

= FileStream(file,FileMode.OpenOrCreate,FileAcce.Write);

BinaryFormatter bf = new BinaryFormatter();

bf.Serialize(stream,list);

stream.Close();

}

private void btnAdd_Click(object sender, EventArgs e)

{

int sno = Int32.Parse(txtStuNo.Text);

bool isMale;

if(rdoMan.Checked)

isMale = true;

new

C#程序设计案例评语

C#程序设计案例评语:优秀:该生在课程实习过程中,能够按照实习任务书的要求,完整的开发记事本及计算器程序,设计过程中能主动查阅资料,独立完成程序开发,软件设计界面良好,功能完整,实......

C程序设计实验报告

浙江理工大学信息学院C程序设计实验报告实验名称: 学时安排:实验类别: 实验要求:1人1组  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄一、实验目的二、实验设备介绍软件需求: Visual C++ 6.0或CFree3.5以上版本硬件需求:......

《C++程序设计》实验报告

注意:以下蓝色文字部分根据情况增删实验1熟悉VC++6.0集成开发环境一、实验时间:2011年9月20日二、实验地点:第7机房三、指导教师:邓谱四、实验目的:1.了解VC++6.0的主要特点;2.熟......

C程序设计综合性实验报告

《C程序设计》综合性实验实验报告题目: 学生成绩管理姓名: 班级: 学号: 指导教师: 完成时间:一、实验题目 学生成绩管理二、实验目的1.掌握一维数组、二维数组的使用方法。 2.掌握......

c语言程序设计实验报告

c语言程序设计实验报告(合集12篇)由网友“芒果树”投稿提供,下面小编给大家整理c语言程序设计实验报告,希望大家喜欢!篇1:c语言程序设计实验报告 一、实验目的:1、掌握C语言的数据......

下载C#程序设计实验报告word格式文档
下载C#程序设计实验报告.doc
将本文档下载到自己电脑,方便修改和收藏。
点此处下载文档

文档为doc格式

热门文章
点击下载本文