您好,欢迎访问三七文档
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时,先将分数转换成点数,其转换方式如下:90~100对应点数为4.00,80~89对应点数为3.0,70~79对应点数为2.0,60~69对应点数为1.0,其他为0。以上5项成绩GPA为:常见算法GPA=(4×4+3×3+2×4+6×2+3×3)/(4+3+2+6+3)=3.00标准算法GPA=((92×4+80×3+98×2+70×6+89×3)×4)/(4+3+2+6+3)×100)=3.31要求将学生和课程分别设计成类Student和Course,计算一个学生GPA的输出结果如图5.31所示。(图见课本P140)程序:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceexperment5_1{classStudent//学生类{intsno;//学号stringsname;//姓名Course[]course;//Course类对象数组int[]score;//课程成绩数组doublesgpa1;//常见GPA值doublesgpa2;//标准GPA值publicintpsno//psno属性可读可写{get{returnsno;}set{sno=value;}}publicstringpsname//psname属性可读可写{get{returnsname;}set{sname=value;}}publicvoidsetcourse(paramsCourse[]course1)//设置课程{course=newCourse[course1.Length];for(inti=0;icourse1.Length;i++)course[i]=course1[i];}publicvoidsetscore(int[]score1)//设置分数{score=newint[score1.Length];for(inti=0;iscore1.Length;i++)score[i]=score1[i];}publicvoidcomputegpa()//根据课程的学分以及学生成绩计算GPA{inti;doubles,sumc=0,sumgpa1=0,sumgpa2=0;for(i=0;iscore.Length;i++){if(score[i]=90)s=4.0;//点数elseif(score[i]=80)s=3.0;elseif(score[i]=70)s=2.0;elseif(score[i]=60)s=1.0;elses=0.0;sumgpa1+=course[i].pcredits*s;sumgpa2+=course[i].pcredits*score[i];sumc+=course[i].pcredits;}sgpa1=sumgpa1/sumc;sgpa2=sumgpa2*4/sumc/100;}publicvoiddispstud()//输出学生信息{Console.WriteLine(学号:{0}\t姓名:{1},sno,sname);Console.WriteLine(课程名\t学分\t分数);for(inti=0;icourse.Length;i++)Console.WriteLine({0}\t{1}\t{2},course[i].pcname,course[i].pcredits,score[i]);}publicvoiddispgpa()//输出GPA{Console.WriteLine(常见算法GPA={0:n},标准算法GPA={1:n},sgpa1,sgpa2);}}classCourse//课程类{stringcname;//课程名intcredits;//课程学分publicCourse(){}publicCourse(stringname,intxf)//构造函数{cname=name;credits=xf;}publicstringpcname//pcname属性,课程名可读可写{get{returncname;}set{cname=value;}}publicintpcredits//pcredits属性,课程学分可读可写{get{returncredits;}set{credits=value;}}}classProgram{staticvoidMain(string[]args){Course[]course1=newCourse[]{newCourse(课程1,4),newCourse(课程2,3),newCourse(课程3,2),newCourse(课程4,6),newCourse(课程5,3)};int[]score1=newint[]{92,80,98,70,89};Students1=newStudent();s1.psno=1;s1.psname=王华;s1.setcourse(course1);s1.setscore(score1);s1.computegpa();s1.dispstud();s1.dispgpa();}}}(2)设计控制台应用程序项目experment5-2,用于模拟考试过程,其中有一个教师类Teacher和一个学生类Student。教师宣布开始考试,学生接收后开始答题,学生答题完毕引发答题完成事件,教师收卷。例如有五个学生考试,过程如图5.32所示。(图见课本P140)程序:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceexperment5_2{publicdelegatevoidEndExamType(DateTimeendtime,Studentstud);//声明完成考试委托类型publicdelegatevoidStartExamType(DateTimestarttime);//声明开始考试委托类型publicclassStudent//学生类{privatestringname;//学生姓名publiceventEndExamTypeEndExam;//定义完成考试事件publicStudent(stringname)//构造函数{this.name=name;}publicstringpname//学生姓名属性{get{returnname;}}publicvoidTesting(DateTimebegintime)//学生开始考试事件调用的方法{Console.WriteLine(学生{0}在{1}时开始答题...,name,begintime);}publicvoidHandIn()//学生交卷引发完成考试事件{EndExam(DateTime.Now,this);}}classTeacher//教师类{publiceventStartExamTypeStartExam;publicvoidNotifyBeginExam(){Console.WriteLine(教师宣布开始考试);StartExam(DateTime.Now);//引发开始考试事件}publicvoidAccept(DateTimeaccepttime,Studentstud){Console.WriteLine(学生+stud.pname+完成考试,老师收卷);}}classProgram{staticvoidMain(string[]args){Teachert=newTeacher();Student[]s=newStudent[5];s[0]=newStudent(张军);s[1]=newStudent(陈华);s[2]=newStudent(王丽);s[3]=newStudent(许源);s[4]=newStudent(刘畅);foreach(Studentstins){t.StartExam+=newStartExamType(st.Testing);//给每个学生订阅教师的开始考试事件st.EndExam+=newEndExamType(t.Accept);//给教师订阅每个学生的完成答卷事件}t.NotifyBeginExam();//教师宣布开始考试Console.WriteLine(经过一段时间...);s[1].HandIn();//一学生完成答题交卷Console.WriteLine(经过一段时间...);s[2].HandIn();//一学生完成答题交卷Console.WriteLine(经过一段时间...);s[4].HandIn();//一学生完成答题交卷Console.WriteLine(经过一段时间...);s[0].HandIn();//一学生完成答题交卷Console.WriteLine(经过一段时间...);s[3].HandIn();//一学生完成答题交卷}}}第六章、编写控制台应用程序项目experment6,假设图书馆的图书类B00k包含书名和编号和作者属性,读者类Reader包含姓名和借书证属性,每位读者最多可借5本书,设计他们的公共基类BClass。要求列出所有读者的借书情况,类似图6.15。(图见课本P176)程序:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceexperment6{publicclassBClass//基类{privatestringname;//名称privateintno;//编号publicBClass(stringna,intn)//构造函数{name=na;no=n;}publicvoidshow(){Console.Write({0}({1}),name,no);}}publicclassBook:BClass//图书类{stringauthor;//作者publicBook(stringna,intn,stringauth):base(na,n){author=auth;}publicvoidshowBook(){base.show();Console.Write(作者:{0},author);}}publicclassReader:BClass//读者类{Book[]rent;//所借图书inttop;publicReader(stringna,intn):base(na,n)//构造函数{rent=newBook[5];top=0;}publicvoidrentBook(refBookb){rent[top]=b;top++;}publicvoidshowReader(){Console.Write(读者:);base.show();Console.WriteLine(所借图书:);for(inti=0;itop;i++){Console.Write({0}:,i+1);//5个空格rent[i].show();Console.WriteLine();}}}classProgram{staticvoidMain(string[]args){Bookb1=newBook(C语言,100,潭浩强);Bookb2=newBook(数据结构,110,严蔚敏);Bookb3=newBook(软件工程,210,陈华);Bookb4=newBook(操作系统,208,张明);Readerr1=newReader(王华,1234);Readerr2=newRead
本文标题:C#重点代码题
链接地址:https://www.777doc.com/doc-4935795 .html