C#重点代码题_第1页
C#重点代码题_第2页
C#重点代码题_第3页
C#重点代码题_第4页
C#重点代码题_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.C#重点代码题第五章、(1)设计控制台应用程序项目experment5-1,用于求学生的GPA。GPA是英文平均分的简称,美国大学的GPA满分是4分。例如某学生的5门课程的学分和成绩为: 课程1有4个学分,成绩92(A); 课程2有3个学分,成绩80(B); 课程3有2个学分,成绩98(A); 课程4有6个学分,成绩70(C); 课程5有3个学分,成绩89(B); 计算GPA有两种,一种是常见算法GPA,另一个是标准算法GPA。在计算常见算法GPA时,先将分数转换成点数,其转换方式如下: 90100对应点数为4.00,8089对应点数为3.0,7079对应点数为2.0,6069对应点数为1.

2、0,其他为0。 以上5项成绩GPA为: 常见算法GPA=(44+33+24+62+33)/(4+3+2+6+3)=3.00 标准算法GPA=(924+803+982+706+893)4)/(4+3+2+6+3)100)=3.31 要求将学生和课程分别设计成类Student和Course,计算一个学生GPA的输出结果如图5.31所示。(图见课本P140)程序:using System;using System.Collections.Generic;using System.Text;namespace experment5_1 class Student /学生类 int sno; /学号 s

3、tring sname; /姓名 Course course; /Course类对象数组 int score; /课程成绩数组 double sgpa1; /常见GPA值 double sgpa2; /标准GPA值 public int psno /psno属性可读可写 get return sno; set sno = value; public string psname /psname属性可读可写 get return sname; set sname = value; public void setcourse(params Course course1) /设置课程 course =

4、 new Coursecourse1.Length; for (int i = 0; i course1.Length; i+) coursei = course1i; public void setscore(int score1) /设置分数 score = new intscore1.Length; for (int i = 0; i score1.Length; i+) scorei = score1i; public void computegpa() /根据课程的学分以及学生成绩计算GPA int i; double s, sumc = 0, sumgpa1 = 0, sumgpa

5、2 = 0; for (i = 0; i = 90) s = 4.0; /点数 else if (scorei = 80) s = 3.0; else if (scorei = 70) s = 2.0; else if (scorei = 60) s = 1.0; else s = 0.0; sumgpa1 += coursei.pcredits * s; sumgpa2 += coursei.pcredits * scorei; sumc += coursei.pcredits; sgpa1 = sumgpa1 / sumc; sgpa2 = sumgpa2 * 4 / sumc / 100

6、; public void dispstud() /输出学生信息 Console.WriteLine(学号:0t姓名:1, sno, sname); Console.WriteLine( 课程名t学分t分数); for (int i = 0; i course.Length; i+) Console.WriteLine( 0t1t2, coursei.pcname, coursei.pcredits, scorei); public void dispgpa() /输出GPA Console.WriteLine(常见算法GPA=0:n,标准算法GPA=1:n, sgpa1, sgpa2); c

7、lass Course /课程类 string cname; /课程名 int credits; /课程学分 public Course() public Course(string name, int xf) /构造函数 cname = name; credits = xf; public string pcname /pcname属性,课程名可读可写 get return cname; set cname = value; public int pcredits /pcredits属性,课程学分可读可写 get return credits; set credits = value; cl

8、ass Program static void Main(string args) Course course1 = new Course new Course(课程1,4),new Course(课程2,3), new Course(课程3,2),new Course(课程4,6),new Course(课程5,3); int score1 = new int 92, 80, 98, 70, 89 ; Student s1 = new Student(); s1.psno = 1; s1.psname = 王华; s1.setcourse(course1); s1.setscore(scor

9、e1); putegpa(); s1.dispstud(); s1.dispgpa(); (2)设计控制台应用程序项目experment5-2,用于模拟考试过程,其中有一个教师类Teacher和一个学生类Student。教师宣布开始考试,学生接收后开始答题,学生答题完毕引发答题完成事件,教师收卷。例如有五个学生考试,过程如图5.32所示。(图见课本P140)程序:using System;using System.Collections.Generic;using System.Text;namespace experment5_2 public delegate void EndExamTy

10、pe(DateTime endtime, Student stud); /声明完成考试委托类型 public delegate void StartExamType(DateTime starttime); /声明开始考试委托类型 public class Student /学生类 private string name; /学生姓名 public event EndExamType EndExam; /定义完成考试事件 public Student(string name) /构造函数 = name; public string pname /学生姓名属性 get ret

11、urn name; public void Testing(DateTime begintime) /学生开始考试事件调用的方法 Console.WriteLine(学生0在1时开始答题., name, begintime); public void HandIn() /学生交卷引发完成考试事件 EndExam(DateTime.Now, this); class Teacher /教师类 public event StartExamType StartExam; public void NotifyBeginExam() Console.WriteLine(教师宣布开始考试); StartE

12、xam(DateTime.Now); /引发开始考试事件 public void Accept(DateTime accepttime, Student stud) Console.WriteLine( 学生 + stud.pname + 完成考试,老师收卷); class Program static void Main(string args) Teacher t = new Teacher(); Student s = new Student5; s0 = new Student(张军); s1 = new Student(陈华); s2 = new Student(王丽); s3 =

13、new Student(许源); s4 = new Student(刘畅); foreach (Student st in s) t.StartExam += new StartExamType(st.Testing); /给每个学生订阅教师的开始考试事件 st.EndExam += new EndExamType(t.Accept); /给教师订阅每个学生的完成答卷事件 t.NotifyBeginExam(); /教师宣布开始考试 Console.WriteLine(经过一段时间.); s1.HandIn(); /一学生完成答题交卷 Console.WriteLine(经过一段时间.); s

14、2.HandIn(); /一学生完成答题交卷 Console.WriteLine(经过一段时间.); s4.HandIn(); /一学生完成答题交卷 Console.WriteLine(经过一段时间.); s0.HandIn(); /一学生完成答题交卷 Console.WriteLine(经过一段时间.); s3.HandIn(); /一学生完成答题交卷 第六章、编写控制台应用程序项目experment6,假设图书馆的图书类B00k包含书名和编号和作者属性,读者类Reader包含姓名和借书证属性,每位读者最多可借5本书,设计他们的公共基类BClass。要求列出所有读者的借书情况,类似图6.15

15、。(图见课本P176)程序:using System;using System.Collections.Generic;using System.Text;namespace experment6 public class BClass /基类 private string name; /名称 private int no; /编号 public BClass(string na, int n) /构造函数 name = na; no = n; public void show() Console.Write(0(1), name, no); public class Book : BClas

16、s /图书类 string author; /作者 public Book(string na, int n, string auth) : base(na, n) author = auth; public void showBook() base.show(); Console.Write(作者:0, author); public class Reader : BClass /读者类 Book rent; /所借图书 int top; public Reader(string na, int n) : base(na, n) /构造函数 rent = new Book5; top = 0

17、; public void rentBook(ref Book b) renttop = b; top+; public void showReader() Console.Write(读者:); base.show(); Console.WriteLine(所借图书:); for (int i = 0; i top; i+) Console.Write( 0:, i + 1); /5个空格 renti.show(); Console.WriteLine(); class Program static void Main(string args) Book b1 = new Book(C语言,

18、 100, 潭浩强); Book b2 = new Book(数据结构, 110, 严蔚敏); Book b3 = new Book(软件工程, 210, 陈华); Book b4 = new Book(操作系统, 208, 张明); Reader r1 = new Reader(王华, 1234); Reader r2 = new Reader(李兵, 2600); r1.rentBook(ref b1); r1.rentBook(ref b2); r1.rentBook(ref b3); r2.rentBook(ref b4); r1.showReader(); r2.showReader

19、(); 第七章、编写控制台应用程序项目experment7,声明一个MyGen泛型,包含一个List类型的字段list,设计一个向list添加元素的add方法和一个输出所有元素的Displist方法。在Main方法中,实例化为MyGen和MyGen,采用反射技术显示该泛型的所有方法,并用相关设计进行测试,类似于图7.7。(图见课本P194)程序:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Re

20、flection;namespace experment7 class MyGen List list = new List(); public void add(T t) list.Add(t); public void Displist() foreach (var i in list) Console.Write(0 ,i); Console.WriteLine(); class Program static void Main(string args) MyGen obj1 = new MyGen(); Console.WriteLine(向obj1对象中添加3个整数); obj1.a

21、dd(1); obj1.add(2); obj1.add(3); Console.Write(obj1对象中的元素:); obj1.Displist(); MyGen obj2 = new MyGen(); Console.WriteLine(向obj2对象中添加4个字符串); obj2.add(C+); obj2.add(Java); obj2.add(VB); obj2.add(C#); Console.Write(obj2对象中的元素:); obj2.Displist(); Type t = obj1.GetType(); MethodInfo m = t.GetMethods(); C

22、onsole.WriteLine(obj1对象的类型的方法个数:0,m.Length); if (m.Length 0) Console.WriteLine(方法如下:); foreach (MethodInfo item in m) Console.WriteLine(t0 , item.Name); 第八章、设计名称为experment8的控制台项目,声明一个学生类Student,包含学生学号、姓名和分数等成员。另外声明一个可枚举类MyClass,包含List对象list字段,向其中添加若干学生的成绩,使用get访问器(即属性方式)设计两个迭代器,分别用于迭代合格(分数大于、等于60)和不

23、及格(分数小于60)的学生记录,在Main方法中输出这两个迭代器的学生记录,并采用相关数据进行测试,其执行界面类似于图8.8。(图见课本P207)程序:using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace experment8 public class Student int no; /学号 string name; /姓名 int score; /分数 public Student(int no, string name, int score) this.no = no; = name; this.score = score; public bool pas

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论