C#程序设计实验报告

时间:2024.4.13

实验报告书写要求

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

{

class 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 class 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

{

class Program

{

static void Main(string[] args)

{

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

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

if ((c>='a'&&c<='z')||(c>='A'&&c<='Z'))

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

{

class Program

{

static void Main(string[] args)

{

int i = 1, sum = 0;

while (i <= 100)

{

sum = sum + i;

i++;

}

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

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace one.forth.two

{

class Program

{

static void Main(string[] args)

{

int i = 1, sum = 0;

do

{

sum = sum + i;

i++;

}

while (i <= 100);

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

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace one.forth.three

{

class Program

{

static void Main(string[] args)

{

int i , sum = 0;

for (i = 1; i <= 100; 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

{

class 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

{

class Program

{

static void Main(string[] args)

{

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

{

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 3; j++)

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

}

}

}

}

}

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace first.seven

{

class 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 < a.Length; i++)

{

for (int j = 0; j < a[i].Length; 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

{

class 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));

}

class Swaper

{

public string Swap(int x, int y)

{

int temp;

temp = x;

x = y;

y = temp;

return string.Format("按值传参交换之后: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

{

class Program

{

public class sort

{

public void change(int[] a)

{

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

show(a);

int i, j, m;

for (i = 0; i < 10; i++)

{

m = a[i];

j = i - 1; //a[j]为数组前一个值

while (j >= 0 && m > a[j])//判断i下标的数是否大于j下标的数

{

a[j + 1] = a[j];//如果i下标大于j把j往后移一个位

j--;

}

a[j+1] = m; //当不大于j的时候就把M的值放到i下标下面 j+1 是为了下标减到最前时考虑 -1 + 1 还是下标的最前面

}

Console.WriteLine("排序后,数组顺序为:");

show(a);

}

void show(int[] a)

{

int i;

for (i = 0; i < 10; i++)

{

Console.Write("{0} ", a[i]);

}

Console.WriteLine();

}

}

static void Main(string[] args)

{

int[] a ={ 4, 7, 1, 2, 5, 8, 9, 10, 3, 6 };

sort s=new sort();

s.change(a);

}

}

}

3、定义一个学生类,把学生类当作对象来传递。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace second.three

{

class Program

{

public class student

{

public void st()

{

int a = 999;

}

}

public class st

{

public void aa(student s)

{

Console.WriteLine(s);

}

}

static void Main(string[] args)

{

student s=new student();

st s1 = new st();

s1.aa(s);

}

}

}

4、定义一个方法,求两个数的和和差,通过参数把这两个值带回。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace second.four

{

class Program

{

public class sum

{

public void ab(out int m, out int n,int a, int b)

{

m = a + b;

n = a - b;

}

}

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

{

class Program

{

public class 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 class 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 class 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 < snums; i++)

{

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

}

if (i == snums) return -1;

else return i;

}

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

public void ProThree()

{

for (int i = 0; i < snums; i++)

{

int k = i;

for (int j = i + 1; j < snums; 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 < snums; i++)

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

}

else if (k == 1)

{

for (int i = 1; i < snums; i++)

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

}

else

{

for (int i = 1; i < snums; 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 < snums; i++)

if (stu[i].math < 60) name +=stu[i].name+"\n";

}

else if (k == 1)

{

for (int i = 0; i < snums; i++)

if (stu[i].chinese < 60) name += stu[i].name + "\n";

}

else

{

for (int i = 0; i < snums; i++)

if (stu[i].english < 60) name += stu[i].name + "\n";

}

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 < snums; 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 < snums; i++)

{

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

{

sumC1++;

}

else if ((80 <= stu[i].chinese) && (stu[i].chinese < 90))

{

sumC2++;

}

else if((70<=stu[i].chinese)&& (stu[i].chinese < 80))

{

sumC3++;

}

else if((60<=stu[i].chinese)&&(stu[i].chinese < 70))

{

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 < snums; i++)

{

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

{

sumC1++;

}

else if ((80 <= stu[i].math) && (stu[i].math < 90))

{

sumC2++;

}

else if ((70 <= stu[i].math) && (stu[i].math < 80))

{

sumC3++;

}

else if ((60 <= stu[i].math) && (stu[i].math < 70))

{

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 < snums; i++)

{

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

{

sumC1++;

}

else if ((80 <= stu[i].english) && (stu[i].english < 90))

{

sumC2++;

}

else if ((70 <= stu[i].english) && (stu[i].english < 80))

{

sumC3++;

}

else if ((60 <= stu[i].english) && (stu[i].english < 70))

{

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 class 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);

MessageBox.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 { MessageBox.Show("不存在这个人!"); }

}

private void btnFinish_Click(object sender, EventArgs e)

{

label7.Text = "前3名:"+"\n";

for (int i = 0; i < 3; 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 class 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 class 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 class 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 class 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 class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void btnSmall_Click(object sender, EventArgs e)

{

Pupil p = new Pupil(txtName.Text,Convert.ToInt32(txtAge.Text),Convert.ToDouble(txtChinese.Text),Convert.ToDouble(txtMath.Text));

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

}

private void btnMiddle_Click(object sender, EventArgs e)

{

Middle m = new Middle(txtName.Text, Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtChinese.Text), Convert.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 c = new College(txtName.Text, Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtChinese.Text), Convert.ToDouble(txtMath.Text));

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

}

}

}

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

Figure类代码:

using System;

using System.Collections.Generic;

using System.Text;

namespace Test3_2

{

public abstract class Figure

{

public abstract double Area();

}

public class Circle:Figure

{

double radius;

public Circle(double r)

{

radius = r;

}

public override double Area()

{

return radius * radius * 3.14;

}

}

public class 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 class 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 class 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

{

class 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 class Person

{

public string name;

public interface method

{ string work();}

}

public class Student:Person

{

public Student(string name)

{ this.name = name; }

public string work()

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

}

public class 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

{

class 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 class Person

{

public string name;

public Person(string name)

{ this.name = name; }

public virtual string Work()

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

}

public class 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 class 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

{

class 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 class Person

{

public string name;

public Person(string name)

{ this.name = name; }

public abstract string Work();

}

public class 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 class 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

{

class Program

{

static void Main(string[] args)

{

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

Console.WriteLine(s.Work());

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

Console.WriteLine(t.Work());

}

public abstract class Person

{

public string name;

public Person(string name)

{

this.name = name;

}

}

interface IPerson

{

string type { get;}

string Work();

}

public class 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 class 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 class 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 class 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 class 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 class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void btnSave_Click(object sender, EventArgs e)

{

saveFileDialog1.ShowDialog();

StreamWriter sw = new 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 = new 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;

namespace Test4_4

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void btnSave_Click(object sender, EventArgs e)

{

FileStream fs = new FileStream(@"d:\Data\student.dat",FileMode.Append,FileAccess.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 = new FileStream(@"d:\Data\student.dat",FileMode.Open,FileAccess.Read);

BinaryReader br = new BinaryReader(fs);

fs.Position = 0;

while (fs.Position != fs.Length)

{

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 class 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 class StudentList

{

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

public Student this[int index]

{

get

{

if (index < 0 || index >= 100)

return list[0];

else

return list[index];

}

set

{

if (!(index < 0 || 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 class 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:\data\student.dat";

Stream stream = new FileStream(file,FileMode.OpenOrCreate,FileAccess.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;

else

isMale = false;

Student student = new Student(sno, TxtName.Text, isMale);

list[i] = student;

i++;

}

private void btnShow_Click(object sender, EventArgs e)

{

lstShow.Items.Clear();

lstShow.Items.Add("学号\t姓名\t性别");

string file = @"d:\data\student.dat";

Stream stream = new FileStream(file,FileMode.Open,FileAccess.Read);

BinaryFormatter bf = new BinaryFormatter();

StudentList students = (StudentList)bf.Deserialize(stream);

int k = 0;

while (students[k] != null)

{

int s_no = students[k].sno;

string name = students[k].name;

bool isMale = students[k].sex;

string sex = "";

if (isMale)

sex = "男";

else

sex = "女";

string result = String.Format("{0}\t{1}\t{2}", s_no, name, sex);

lstShow.Items.Add(result);

k++;

}

stream.Close();

}

}

}

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

通过本次实验我掌握接口的定义及使用方法;掌握流,序列化和反序列化的概念和使用方法;掌握流文件的读写操作类及其使用方法;掌握OpenFileDialog,SaveFileDialog等控件的使用。

更多相关推荐:
C++程序设计实验报告

C++程序设计实验报告学号:姓名:班级:指导老师:实验一、字符和格式的输出实验一,实验目的1、重点把握各种内部数据类型、数值和逻辑运算,各种表达式、函数声明、定义和调用。2、掌握过程控制编程方法,正确编制多重循…

程序设计实验报告模板

C语言程序设计实验报告1实验目的(1)掌握函数的定义方法、调用方法、参数说明以及返回值;(2)掌握实参与形参的对应关系,以及参数之间的值传递的方式;(3)掌握函数的嵌套调用及递归调用的设计方法;(4)在编程过程…

Java程序设计实验报告

学生实验报告册(理工类)课程名称:JAVA程序设计实验专业班级:M11计算机科学与技术II学生学号:学生姓名:所属院部:XX学院指导教师:20XX20XX学年第二学期金陵科技学院教务处制实验报告书写要求实验报告…

程序设计综合实验报告册

西南交通大学程序设计综合实验实验地点实验学期学生班级学生学号学生姓名任课教师龚勋信息科学与技术学院20xx年4月实验一ATM机用户操作界面业务逻辑编写实验目的及内容1根据ATM的工作流程编写用户界面掌握文本用户...

C程序设计实验报告

C语言程序设计实验报告学号不告诉你哦班级信管一班姓名你猜猜哈哈一实验题目一编程实验猜数问题输入两个整数并求这两个整数的和输入所猜的结果如果输入数比正确的结果要大提示猜大了如果输入数比正确的结果要小提示猜小了当猜...

C程序设计实验报告5

工学院工业设计专业10级20xx至20xx学年度第1学期学号2104021638姓名杨芹芹20xx年9月22日第34节综合楼325教室

C程序设计实验报告2

院专业20xx级20xx至20xx学年度第1学期学号姓名20xx年9月22日第34节综合楼325教室

程序设计实验报告

兰州商学院陇桥学院工学系课程设计报告设计题目:迷宫与栈问题系别:工学系专业(方向):年级、班:学生姓名:学生学号:指导教师:目录一、系统开发的背景...1二、系统分析与设计...1(一)系统的分析...1(二)…

程序设计实践 实验报告

程序设计基础课程设计通讯录管理系统院系:计算机学院班级:信息工程1班姓名:学号:合作者:指导教师:20##年5月3日目录摘要.11.研究背景及意义...1第一章系统设计说明以及功能分析.21.1系统简述...2…

实验三最简单的C程序设计实验报告

实验三最简单的程序设计一实验目的1掌握语言中使用最多的一种语句赋值语句的使用方法2掌握各种类型数据的输入输出方法能正确使用各种格式输出符二实验内容和步骤掌握各种格式输出符的使用方法includeltstdioh...

面向对象程序设计实验报告

综合实验报告面向对象程序设计专业名称班级姓名学号信息科学与工程学院二零一三年七月信息科学与工程学院面向对象程序设计综合实验报告目录第一题111题目概述112设计思路113设计说明与调试分析14编码实现15程序运...

c++程序设计实验报告

实验要求对大纲中列出的四个实验要求1以面向对象的程序设计思想编程2熟悉面向对象程序设计语言VC编程环境3在计算机上快速完成程序编写调试运行分别写出实验报告三页以上要求详尽描述根据实验内容要求自己设计的上机编程源...

程序设计实验报告(37篇)