第四版 c#程序设计教程(课后习题答案代码) 下载本文

{get { return n; } set { n = value; } }

public virtual double compsalary() //计算普通员工工资 { Console.Write(\工作年数:\);

pn = int.Parse(Console.ReadLine()); psalary = bsalary + 30 * pn; return psalary; } }

public class UEmployee : Employee //本科生职工类 { public override double compsalary() { return 1.5 * base.compsalary(); } }

public class GEmployee : Employee //研究生职工类 { public override double compsalary() { return 2 * base.compsalary(); } }

class Program

{ static void Main(string[] args) { Employee emp1 = new Employee();

Console.WriteLine(\该普通职工工资:{0}\, emp1.compsalary()); UEmployee emp2 = new UEmployee();

Console.WriteLine(\该本科生职工工资:{0}\, emp2.compsalary()); GEmployee emp3 = new GEmployee();

Console.WriteLine(\该研究生职工工资:{0}\, emp3.compsalary()); }

}

课本157页9

public class Person //人类 { private int no; //编号 private string name; //姓名 public void input()

{ Console.Write(\编号:\);

no = int.Parse(Console.ReadLine()); Console.Write(\姓名:\); name = Console.ReadLine(); }

public void disp()

{ Console.WriteLine(\编号:{0}\,no); Console.WriteLine(\姓名:{0}\,name); }

}

public class Student : Person //学生类 { private string sclass; //班号 private int degree; //成绩 public void input() { base.input();

Console.Write(\班号:\); sclass = Console.ReadLine(); Console.Write(\成绩:\);

degree = int.Parse(Console.ReadLine()); }

new public void disp() { base.disp();

Console.WriteLine(\班号:{0}\,sclass); Console.WriteLine(\成绩:{0}\,degree); } }

public class Teacher : Person //教师类 { private string prof; //职称 private string depart; //部门 public void input() { base.input();

Console.Write(\职称:\); prof = Console.ReadLine(); Console.Write(\部门:\); depart = Console.ReadLine(); }

new public void disp() { base.disp();

Console.WriteLine(\职称:{0}\, prof); Console.WriteLine(\部门:{0}\, depart); } }

class Program

{ static void Main(string[] args)

{ Student s1 = new Student(); Teacher t1 = new Teacher();

Console.WriteLine(\输入一个学生数据:\); s1.input(); Console.WriteLine(\输入一个教师数据:\); t1.input(); Console.WriteLine(\显示一个学生数据:\); s1.disp(); Console.WriteLine(\显示一个教师数据:\); t1.disp(); }

} 课本157页10

public class Student:IComparable { private string name; private int eng, math, sum; public int psum { get { return sum; } }

public void inscore() { Console.Write(\姓名:\); name = Console.ReadLine(); Console.Write(\英语:\);

eng = int.Parse(Console.ReadLine()); Console.Write(\数学:\);

math = int.Parse(Console.ReadLine()); sum = eng + math; }

public void display()

{ Console.WriteLine(\, name, eng, math, sum); }

public int CompareTo(object obj) //实现接口方法 { Student s = (Student)obj; //转换为Student实例 if (psum < s.psum) return 1; else if (psum == s.psum) return 0; else return -1; }} class Program

{static void Main(string[] args) { int n, i;

ArrayList myarr = new ArrayList();

Student p; //定义对象引用 Console.Write(\);

n = int.Parse(Console.ReadLine()); for (i = 0; i < n; i++)

{Console.WriteLine(\输入第{0}个学生数据:\, i + 1); p = new Student(); //创建对象引用实例 p.inscore(); myarr.Add(p); }

Console.WriteLine(\排序前:\);

Console.WriteLine(\姓名\\t英语\\t数学\\t总分\); i = 1;

foreach(Student s in myarr) { Console.Write(\序号{0}:\, i++); s.display();}

myarr.Sort(); //按总分降序排序

Console.WriteLine(\排序后:\);

Console.WriteLine(\姓名\\t英语\\t数学\\t总分\); i = 1;

foreach (Student s in myarr) { Console.Write(\第{0}名:\, i++); s.display();

} } } 课本157页上机实验7

public class BClass //基类 { private string name; //名称 private int no; //编号

public BClass(string na, int n) //构造函数 { name = na; no = n; }

public void show()

{ Console.Write(\, 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(\, i + 1); //5个空格

rent[i].show(); Console.WriteLine(); }} } class Program

{ static void Main(string[] args)

{ Book b1 = new Book(\语言\, 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();

}} 课本200页3

public partial class Form1 : Form

{ int n = 0; //保存用户错误输入的次数 public Form1()

{ InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == \ && textBox2.Text == \) { MessageBox.Show(\用户名/口令正确\); this.Close(); //调用其他程序 } else { if (n<3)

{ MessageBox.Show(\用户名/口令错误,再次输入\); n++; } else

{ MessageBox.Show(\已经错误输入三次,退出\); this.Close();} }}

课本200页4

public partial class Form2 : Form { public Form2()

{ InitializeComponent(); }