资源描述
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)
程序:
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; //常见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 = new Course[course1.Length];
for (int i = 0; i < course1.Length; i++)
course[i] = course1[i];
}
public void setscore(int[] score1) //设置分数
{
score = new int[score1.Length];
for (int i = 0; i < score1.Length; i++)
score[i] = score1[i];
}
public void computegpa() //根据课程的学分以及学生成绩计算GPA
{
int i;
double s, sumc = 0, sumgpa1 = 0, sumgpa2 = 0;
for (i = 0; i < score.Length; i++)
{
if (score[i] >= 90)
s = 4.0; //点数
else if (score[i] >= 80)
s = 3.0;
else if (score[i] >= 70)
s = 2.0;
else if (score[i] >= 60)
s = 1.0;
else
s = 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;
}
public void dispstud() //输出学生信息
{
Console.WriteLine("学号:{0}\t姓名:{1}", sno, sname);
Console.WriteLine(" 课程名\t学分\t分数");
for (int i = 0; i < course.Length; i++)
Console.WriteLine(" {0}\t{1}\t{2}", course[i].pcname, course[i].pcredits, score[i]);
}
public void dispgpa() //输出GPA
{
Console.WriteLine("常见算法GPA={0:n},标准算法GPA={1:n}", sgpa1, sgpa2);
}
}
class 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; }
}
}
class 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(score1);
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 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 Student[5];
s[0] = new Student("张军");
s[1] = new Student("陈华");
s[2] = new Student("王丽");
s[3] = new Student("许源");
s[4] = new Student("刘畅");
foreach (Student st in s)
{
t.StartExam += new StartExamType(st.Testing); //给每个学生订阅教师的开始考试事件
st.EndExam += new EndExamType(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)
程序:
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 : BClass //图书类
{
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 Book[5];
top = 0;
}
public void rentBook(ref Book b)
{
rent[top] = b;
top++;
}
public void showReader()
{
Console.Write("读者:");
base.show();
Console.WriteLine("所借图书:");
for (int i = 0; i < top; i++)
{
Console.Write(" {0}:", i + 1); //5个空格
rent[i].show();
Console.WriteLine();
}
}
}
class Program
{
static void Main(string[] args)
{
Book b1 = new Book("C语言", 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();
}
}
}
第七章、编写控制台应用程序项目experment7,声明一个MyGen<T>泛型,包含一个List<T>类型的字段list,设计一个向list添加元素的add<T>方法和一个输出所有元素的Displist方法。在Main方法中,实例化为MyGen<int>和MyGen<string>,采用反射技术显示该泛型的所有方法,并用相关设计进行测试,类似于图7.7。(图见课本P194)
程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace experment7
{
class MyGen<T>
{
List<T> list = new List<T>();
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<int> obj1 = new MyGen<int>();
Console.WriteLine("向obj1对象中添加3个整数");
obj1.add(1);
obj1.add(2);
obj1.add(3);
Console.Write("obj1对象中的元素:");
obj1.Displist();
MyGen<string> obj2 = new MyGen<string>();
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();
Console.WriteLine("obj1对象的类型的方法个数:{0}",m.Length);
if (m.Length > 0)
{
Console.WriteLine("方法如下:");
foreach (MethodInfo item in m)
Console.WriteLine("\t{0} ", item.Name);
}
}
}
}
第八章、设计名称为experment8的控制台项目,声明一个学生类Student,包含学生学号、姓名和分数等成员。另外声明一个可枚举类MyClass,包含List<Student>对象list字段,向其中添加若干学生的成绩,使用get访问器(即属性方式)设计两个迭代器,分别用于迭代合格(分数大于、等于60)和不及格(分数小于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;
this.name = name;
this.score = score;
}
public bool pass()
{
return score >= 60;
}
public void Dispstud()
{
Console.WriteLine("[{0},{1},{2}]",no,name,score);
}
}
public class MyClass //声明可枚举类
{
List<Student> list = new List<Student>();
public MyClass()
{
list.Add(new Student(1, "王华", 90));
list.Add(new Student(2, "李明", 80));
list.Add(new Student(3, "程成", 58));
list.Add(new Student(4, "许冰", 70));
list.Add(new Student(5, "卢萍", 49));
}
public IEnumerable<Student> passlist //含yield return语句的属性
{
get
{
foreach(var s in list)
if (s.pass())
yield return s;
}
}
public IEnumerable<Student> notpasslist //含yield return语句的属性
{
get
{
foreach (var s in list)
if (!s.pass())
yield return s;
}
}
}
class Program
{
static void Main(string[] args)
{
MyClass obj = new MyClass();
Console.WriteLine("成绩合格的学生:");
foreach (Student item in obj.passlist)
item.Dispstud();
Console.WriteLine("成绩不合格的学生:");
foreach (Student item in obj.notpasslist)
item.Dispstud();
}
}
}
展开阅读全文