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

内容发布更新时间 : 2024/12/22 23:21:00星期一 下面是文章的全部内容请认真阅读。

}

class Program

{ static void Main(string[] args) { List list = new List(); list.add(\); list.add(1.23); list.add(2); list.add('a');

Console.WriteLine(\元素序列:{0}\,list.disp()); Console.WriteLine(\元素个数:{0}\,list.getnum()); Console.WriteLine(\位置1的元素:{0}\,list.get(1)); Console.WriteLine(\删除位置2的元素\); list.delete(2);

Console.WriteLine(\元素序列:{0}\, list.disp()); } }

课本124页11 public class Student { 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); } }

class Program

{ const int Max = 100;

static void sort(int n, params Student[] p) { int i, j; bool exchange; Student tmp;

for (i = 0; i < n - 1; i++)

//采用冒泡排序法排序

{ exchange = false;

for (j = n - 2; j >= i; j--) if (p[j + 1].psum > p[j].psum) { tmp = p[j + 1]; p[j + 1] = p[j]; p[j] = tmp; exchange = true; }

if (exchange == false) break; } }

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

Student[] p = new Student[Max]; //定义对象引用数组 Console.Write(\);

n = int.Parse(Console.ReadLine());

for (i = 0; i < n; i++) //创建对象引用的实例 p[i] = new Student(); for (i = 0; i < n; i++)

{ Console.WriteLine(\输入第{0}个学生数据:\, i + 1); p[i].inscore(); }

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

Console.WriteLine(\姓名\\t英语\\t数学\\t总分\); for (i = 0; i < n; i++)

{ Console.Write(\序号{0}:\, i + 1); p[i].display(); }

sort(n, p); //按总降序排序 Console.WriteLine(\排序后:\);

Console.WriteLine(\姓名\\t英语\\t数学\\t总分\); for (i = 0; i < n; i++)

{ Console.Write(\第{0}名:\, i + 1); p[i].display(); } }

}

课本124上机实验6

class Student //学生类 { int sno; //学号 string sname; //姓名

Course[] course; //Course类对象数组 int[] score; //课程成绩数组

//p[j+1]<->p[j]

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(\, 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 { 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 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); s1.computegpa(); s1.dispstud();

//pcredits属性,课程学分可读可写 //pcname属性,课程名可读可写

s1.dispgpa(); }

} 课本157页7 using System;

using System.Collections.Generic; using System.Text;

namespace Proj7_15

{ public class Employee

{ private double bsalary= 1000; private double psalary; private int n; public int pn { get { return n; } set { n = value; } }

public double compsalary() { Console.Write(\工作年数:\);

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

public class UEmployee : Employee { new public double compsalary() { return 1.5 * 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()); } } }

课本157页8

public class Employee //普通职工类

{ private double bsalary = 1000; //基本工资 private double psalary; //实际工资 private int n; //工作年数 public int pn

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4 ceshi