资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
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); 计算GPAa两种,一种是常见算法GPA另一个是标准算法 GPA在计算常见算法 GPA时,先将分数转换成点数,其转换方式如下:90100对应点数为4.00,8089对应点数为3.0 , 7079对应点数为2.0,6069对应点数为1.0,其他为0。 以上5项成绩GPA为: 常见算法 GPA=( 4 X 4+3X 3+2 X 4+6X 2+3 X 3) / (4+3+2+6+3) =3.00 标准算法 GPA=(92X 4+80X 3+98X 2+70X 6+89X 3) X 4) / (4+3+2+6+3) X 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;/学号string sname; /姓名Course course; /Course类对象数组int score; /课程成绩数组double sgpa1;/常见 GPAdouble sgpa2;/标准 GPApublic int psno /psno属性可读可写 get return sno; set sno = value; public string psname /psname属性可读可写 get return sname; set( sname = value; public void setcourse(params Course coursel) /设置课程(course = 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, sumgpa2 = 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;elses = 0.0;sumgpa1 += coursei.pcredits * s;sumgpa2 += coursei.pcredits * scorei;sumc += coursei.pcredits; sgpa1 = sumgpa1 / sumc;sgpa2 = sumgpa2 * 4 / sumc / 100;public void dispstud() /(Console.WriteLine(Console.WriteLine(输出学生信息学号:0t 姓名:1”, sno, sname);课程名t学分t分数);for (int i = 0; i course.Length; i+)Console.WriteLine(0t1t2”,coursei.pcname,coursei.pcredits, scorei); public void dispgpa() /输出(Console.WriteLine(常见算法sgpa2); class Course/( string cname;/int credits;/public Course() ( public Course(string name, int xf) / ( cname = name; credits = xf; public string pcname ( get ( return cname; set ( cname = value; public int pcredits ( get ( return credits; set ( credits = value; class Program ( static void Main(string args) (Course course1 = new Course (new C 课程2”,3), new Course(课程 3,2),5,3);GPAGPA=(0:n,标准算法 GPA=(1:n”, sgpal,课程类课程名课程学分构造函数/pcname 属性,课程名可读可写/pcredits 属性,课程学分可读可写;e(课程 1”,4),new Course(Course( 课程 4,6),new Course( 课程int scorel = new int ( 92, 80, 98, 70, 89 ;Student si = new Student();sl.psno = 1; si.psname =王华;s1.setcourse(course1);sl.setscore(scorel);s1.computegpa();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 EndExamType(DateTime endtime, Student stud); /成考试委托类型public delegate void StartExamType(DateTime starttime); /始考试委托类型public class Student/private string name;/public event EndExamType EndExam; /public Student(string name) /this.name = name;public string pname/get return 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(StartExam(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 =new Student(许源);s4 =new Student(刘畅);foreach (Student st in s)(t.StartExam += newStartExamType(st.Testing);/给每个学生订阅教师的开始考试事件st.EndExam+= new EndExamType(t.Accept); /给教师订阅每个学生的完成答卷事件t.NotifyBeginExam(); /教师宣布开始考试Console.WriteLine(经过一段时间.”);s1.HandIn(); /一学生完成答题交卷Console.WriteLine(经过一段时间.”);s2.HandIn(); /一学生完成答题交卷Console.WriteLine(经过一段时间.”);s4.HandIn(); /一学生完成答题交卷Console.WriteLine(经过一段时间.”);s0.HandIn(); /一学生完成答题交卷Console.WriteLine(经过一段时间.”);s3.HandIn(); /一学生完成答题交卷第六章、编写控制台应用程序项目experment6,假设图书馆的图书类B00k包含书名和编号和作者属性,读者类 Reader包含姓名和借书证属性,每位读者最多可借5本书,设计他们的公共基类BClass。要求列出所有读者的借书情况,类似图 6.15。(图见课本P176)
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号