C#程序设计类与对象02.docx

上传人:b****3 文档编号:3837593 上传时间:2022-11-25 格式:DOCX 页数:11 大小:44.30KB
下载 相关 举报
C#程序设计类与对象02.docx_第1页
第1页 / 共11页
C#程序设计类与对象02.docx_第2页
第2页 / 共11页
C#程序设计类与对象02.docx_第3页
第3页 / 共11页
C#程序设计类与对象02.docx_第4页
第4页 / 共11页
C#程序设计类与对象02.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

C#程序设计类与对象02.docx

《C#程序设计类与对象02.docx》由会员分享,可在线阅读,更多相关《C#程序设计类与对象02.docx(11页珍藏版)》请在冰豆网上搜索。

C#程序设计类与对象02.docx

C#程序设计类与对象02

 

福建农林大学计算机与信息学院

计算机类

 

课程设计报告

课程名称:

VisualC#

设计题目:

类与对象

姓名:

系:

计算机科学与技术

专业:

计算机科学与技术

年级:

2009级

学号:

指导教师:

职称:

讲师

 

2011年11月28日

福建农林大学计算机与信息学院计算机类

课程设计报告结果评定

评语:

成绩:

指导教师签字:

评定日期:

设计题目名称

1.设计的目的和任务

1、掌握类的成员设计。

2、掌握属性的定义及使用。

3、掌握索引器的定义及使用。

2.设计地点

田家炳实验楼513

3.主要仪器设备(实验用的软硬件环境)

VisualStdio2008

4.实验内容

1.创建5个学生对象给一个学生数组赋值,每个学生的成员变量包括:

学号、姓名、年龄。

请根据【模版一】,将【代码1】到【代码6】替换为C#程序代码,以实现如下要求:

(1)将学生按学号排序输出;

(2)给所有学生年龄加1;

(3)统计大于20岁的学生人数。

(4)输出结果,如图1所示:

图1

【模版一】

classStudent{

//【代码1】定义Student类的成员变量

//【代码2】重载Student类的构造方法

publicoverridestringToString(){

/*【代码3】定义ToString()方法,输出一个对象时,

会自动调用对象的toString()方法。

*/

}

/*Output方法输出学生数组的所有元素*/

publicstaticvoidOutput(Student[]s){

for(inti=0;i

Console.WriteLine(s[i]);

}

publicstaticvoidMain(string[]args)

{

Students1=newStudent(3,18,"Lily");

Students2=newStudent(1,21,"Suan");

Students3=newStudent(33,20,"John");

Students4=newStudent(13,20,"Lucy");

Students5=newStudent(8,17,"Jack");

Student[]s={s1,s2,s3,s4,s5};

Console.WriteLine("班级学生名单如下:

");

Output(s);//第1次调用output方法输出数组

//【代码4】将学生按照学号排序

Console.WriteLine("按学号由小到大排序...");

Output(s);//第2次调用output方法输出数组

//【代码5】将所有学生的年龄加1

Console.WriteLine("所有学生年龄加1后...");

Output(s);//第3次调用output方法输出数组

//【代码6】统计大于20岁的学生个数

Console.WriteLine("大于20岁人数是:

"+count);

}

}

2.已知链表的每个结点定义如下:

    Node{

       intdata;

       Nodenext;

    }

请根据【模版二】,将【代码1】到【代码5】替换为C#代码,以实现如下要求:

(1)创建一个初始为10个结点的单向链表,结点数据用随机函数产生。

(2)输出该链表:

(3)在链表的首部增加一个数据为50的结点,并输出链表。

(4)在链表的尾部增加一个数据为50的结点,并输出链表。

(5)输出结果,如图2所示:

图2

usingSystem;

classNode{

publicintdata;//数据域

publicNodenext;//链域,存放下一个结点的引用

}

publicclassLink{

Nodeheader;

/*构建含n个结点的链表链表的头用header指示*/

publicLink(intn){

Nodep,q;//用于链表的推进,p为链表中当前最后结点,q为要新加的结点

header=newNode();//创建首结点

Randomrnd=newRandom();

header.data=rnd.Next(1,10);//使用随机函数,给首结点的data域赋值

p=header;

for(intk=1;k<=n-1;k++)

{

//【代码1】创建除了首节点之外的其他节点

}

}

voidOutputLink()

{

//【代码2】输出链表中所有元素的数据

}

publicstaticvoidMain(String[]a)

{

//【代码3】创建含10个元素的链表

x.OutputLink();//输出链表

 

    //【代码4】创建一个数据为50的新结点,放到链表的头部

x.OutputLink(); 

//【代码5】至链表的尾部,在尾部增加一个data值为50的新结点

x.OutputLink(); 

}

}

 

5.代码

第一题:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespaceConsoleApplication1

{

classStudent

{

privateintnum;

privatestringname;

privateintage;

publicStudent(intnum,intage,stringname)

{

this.num=num;

this.name=name;

this.age=age;

}

publicoverridestringToString()

{

return("学号:

"+this.num+",姓名:

"+this.name+",年龄:

"+this.age);

}

publicstaticvoidoutput(Student[]s)

{

for(inti=0;i

{

Console.WriteLine(s[i]);

}

}

staticvoidMain(string[]args)

{

inti,j;

intpeoplesum=0;

Students1=newStudent(3,18,"Lily");

Students2=newStudent(1,21,"Suan");

Students3=newStudent(33,20,"John");

Students4=newStudent(13,20,"Lucy");

Students5=newStudent(8,17,"Jack");

Student[]s={s1,s2,s3,s4,s5};

Studentmin;

Console.WriteLine("该学生名单是:

");

output(s);

for(i=0;i

{

for(j=i;j

{

if(s[i].num>s[j].num)

{

min=s[i];

s[i]=s[j];

s[j]=min;

}

}

}

Console.WriteLine("学生按学号顺序为...");

output(s);

for(i=0;i

{

s[i].age=s[i].age+1;

}

Console.WriteLine("所有学生年龄加1后...");

output(s);//第3次调用output方法输出数组

for(i=0;i

{

if(s[i].age>20)

{

peoplesum++;

}

}

Console.WriteLine("年龄大于20岁人数为:

"+peoplesum);

Console.ReadKey();

}

}

}

 

实验截图:

第二题:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespaceConsoleApplication1

{

classProgram

{

staticvoidMain(string[]args)

{

Linkx=newLink(10);

x.OutputLink();

Nodeno=newNode();

no.data=50;

no.next=x.header;

x.header=no;

x.OutputLink();

Nodeno2=newNode();

no2.data=50;

x.last.next=no2;

x.OutputLink();

Console.ReadKey();

}

}

publicclassNode

{

publicintdata;

publicNodenext;

}

publicclassLink

{

publicNodeheader;

publicNodep,q;

publicNodelast;

publicLink(intn)

{

header=newNode();

Randomrnd=newRandom();

header.data=rnd.Next(1,10);

p=header;

for(intk=1;k<=n-1;k++)

{

Nodenode=newNode();

node.data=rnd.Next(1,10);

p.next=node;

p=p.next;

}

last=p;

}

publicvoidOutputLink()

{

p=header;

q=p.next;

while(q!

=null)

{

Console.Write(p.data);

Console.Write("-->");

p=q;

q=p.next;

 

}

Console.WriteLine();

}

}

}

实验截图

参考文献

《sualC#2008大学教程第三版》………P.J.DeitelH.M.Deitel

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

当前位置:首页 > 农林牧渔 > 畜牧兽医

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

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