C#课后习题复习题

更新时间:2024-05-02 21:48:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

P106:5-9、定义学生类,属性包括姓名、学号,c#、英语和数学成绩,方法包括设置姓名和学号、设置三门课的成绩和输出相关学生的信息,最后求出总成绩和平均成绩。 class student //创建学生类 {

public string name, stunumber; //姓名、学号属性 public double csharp,math,english;//三门成绩

public void setstudent(string name, string number) //设置姓名,学号 {

this.name = name;

this.stunumber = number; }

public void setscore(double csharp, double math, double english)//设置分数 {

this.csharp = csharp; this.math = math; this.english = english; }

public void show() //显示信息 {

Console.WriteLine(\the score of the student:\\nc#:{2},math:{3},english:{4}\ }

} /*创建学生类完成*/

static void Main(string[] args) //入口函数 { double fullscore; //总分 double avescore; //平均分

student s1 = new student(); //创建一个学生对象s1 s1.setstudent(\ s1.setscore(98,98,98.7); s1.show(); //现实学生信息

fullscore = s1.csharp + s1.math + s1.english; avescore = fullscore / 3; //算出平均分

Console.WriteLine(\student's fullscore :{0}\\naverage score :{1,6:f2} \ }

P106:5-10、定义一个人员类clsPerson,包括属性:姓名、编号、性别和用于输入输出地方法,在此基础上派生出学生类clsStudent(增加成绩)和教师类clsTeacher(增加教龄),并实现对学生和教师的信息的输入输出。 class clsPerson {

public string name,number,sex; //属性

public void input(string name, string number,string sex)//输入信息 {

this.name = name; this.number = number; this.sex = sex; }

public void output() {

Console.WriteLine(\输出信息 } }

class clsStudent:clsPerson//继承person {

public double score;

public void input(string name, string number, string sex, double score) {

base.input( name, number, sex);//继承基类输入 this.score = score;

}

public void output() {

base.output();//继承基类输出

Console.WriteLine(\ } }

class clsTeacher : clsPerson//继承person {

public int tchage;

public void input(string name, string number, string sex, int tchage) {

base.input(name, number, sex); this.tchage = tchage;

}

public void output() {

base.output();

Console.WriteLine(\ } }

static void Main(string[] args)//入口函数 {

clsStudent s1 = new clsStudent(); //创建一个学生类对象s1 clsTeacher t1 = new clsTeacher(); //创建一个教师类对象t1

s1.input(\范朦\ s1.output();

t1.input(\ t1.output(); }

(复习重点)P106:5-11、设有一个描述坐标点的clsPoint类,其私有变量x和y代表一个点的x,y坐标值。编写程序实现以下功能:利用构造函数传递参数,并设其默认参数值为60和75,利用方法display()输出这一默认的值;利用公有方法setPoint()将坐标值修改为(100,120),并利用方法输出修改后的坐标值。

class clsPoint//创建类 {

int x, y;//属于私有属性,此处private 省略

public clsPoint(int x, int y)//构造函数,就是对一个对象的初始化 {

this.x = x; this.y = y; }

public void setpoint(int x,int y)//公有方法设置坐标值 {

this.x=x; this.y=y; }

public void show()//显示坐标 {

Console.WriteLine(\ } }

static void Main(string[] args)//入口函数 {

clsPoint p1 = new clsPoint(60,75);//与int i=0作用相同,就是使对象有初始值

p1.show();

p1.setpoint(100,200);

Console.WriteLine(\ p1.show(); }

(复习重点)P106:5-12、把定义平面直角坐标系上的一个点的类clsPoint作为基类,派生出描述一条直线的类clsLine,再派生出一个矩形类clsRect。要求方法能求出两点间的距离,矩形的周长和面积等。设计一个测试程序,并构造出完整的程序。(考察get()set()和继承) class clsPoint //创建类 {

protected double x, y; public double X {

get //get\\set 的作用是将保护类型的属性转换为隐形可继承 用对象继承(考点) {

return this.x; } set {

this.x=value; } }

public double Y {

get {

return this.y; } set {

this.y= value; } }

public clsPoint() { }//定义空的构造函数,增加程序严谨性 public clsPoint(double x, double y) {

this.x = x; this.y = y; }

public void show() {

Console.WriteLine(\//输出点坐标 } }

class clsLine:clsPoint//创建clsLine类,继承clsPoint {

public clsPoint p = new clsPoint(); //定义一个点对象

public clsLine() { }

public clsLine(double x, double y, double x1, double y1) : base(x, y) {

p.X = x1; p.Y = y1; }

public double count1() //计算线段长度 {

double longth;

longth = Math.Sqrt((p.X - x) * (p.X - x) + (p.Y- y) * (p.Y - y)); return longth; }

public void show1() //输出相关信息 {

base.show();

Console.WriteLine(\

Console.WriteLine(\线段长度:{0,6:f2}\ } }

class clsRec : clsLine {

public clsRec(){}

public clsRec(double x, double y, double x1, double y1):base(x,y,x1,y1){}//继承线段构造函数

protected double getcs(out double s) //计算面积周长方法 {

double c;

c=2*(Math.Abs((x-base.p.X))+Math.Abs((y-base.p.Y))); //矩形周长 s=Math.Abs((x-base.p.X))*Math.Abs((y-base.p.Y)); //矩形面积 return c; }

public void show2() //输出相关信息 {

double s; base.show1();

Console.WriteLine(\周长:{0}\\n面积:{1}\ }

}

static void Main(string[] args) {

clsRec rec = new clsRec(2,3,4,6); //定义矩形对象 rec.show2(); //输出结果 } }

(复习重点)P106:5-13、设计一个项目,由程序随机产生12个数,并把12 个数按从大到小的顺序进行输出。(考察随机数与冒泡法排序) static void Main(string[] args) {

int[] a = new int[12]; //定义长度为12 的数组 int b; //b为暂存数 Random ran = new Random(); //定义随机数

Console.WriteLine(\未排序的数据:\ for (int j = 0; j < 12; j++) {

a[j] = (int)ran.Next(100) ; //给数组赋值 随机产生在0到99之间的整数 Console.Write(\ }

Console.WriteLine(\排序后的数据:\ for (int j = 0; j < 12; j++) //冒泡法排序 {

for (int i=0; i < 12-j-1; i++) //每次比较length-j-1次 比较范围内一个最大值放在最后,下一轮的长度是此轮长度减一 {

if (a[i] > a[i + 1]) {

b=a[i];

a[i] = a[i + 1];

a[i + 1] = b; } } }

foreach (int n in a) //用foreach 输出数据 {Console.WriteLine(\ }

P116:6-4、定义一个抽象类Cshape,包含抽象方法Area()(用来计算面积)和SetDate()(用来重设形状大小)。然后派生出三角形CTriangle类、矩形CRect类、圆形CCircle类,分别求其面积。最后定义一个CArea类,计算这几个形状的面积之和,各形状的数据通过CArea类构造函数或成员函数来设置。编写一个完整的程序。

public abstract class CShape

{

public abstract double Area();

public abstract void SetDate(params double[] d); }

public class Ctringle : CShape {

double a, b, c;//三角型三条边属性 public Ctringle() { }

public Ctringle(double a, double b, double c)//构造函数 {

this.a = a; this.b = b; this.c = c; }

public override void SetDate(params double[] d) {

f1: if (d[0] + d[1] > d[2] && d[1] + d[2] > d[0] && d[0] + d[2] > d[1])//判断三边是否构成三角形 {

this.a = d[0]; this.b = d[1]; this.c = d[2]; } else {

Console.WriteLine(\边长不能构成三角形,请重新设置!\ Console.WriteLine(\请输入三角形的三边边长\ for (int i = 0; i < 3; i++)

d[i] =double.Parse( Console.ReadLine());

goto f1;//对新设的三边回到判断 } }

public override double Area() {

double s;

double p=(this.a+this.b+this.c)/2.0;

s = System.Math.Sqrt(p*(p-this.a)*(p-this.b)*(p-this.c)); return s; } }

class CRect : CShape {

double a, b;

public CRect() { }

public CRect(double a,double b) {

this.a = a; this.b = b; }

public override void SetDate(params double[] d) {

this.a = d[0]; this.b = d[1]; }

public override double Area() {

return a * b; } }

class CCircle : CShape {

double r;

public CCircle() { } public CCircle(double r) {

this.r = r; }

public override void SetDate(params double[] d) {

this.r = d[0]; }

public override double Area() {

return r * r * 3.141; } }

class CArea {

CShape a, a1, a2;

public CArea() { }

public CArea(double ct1, double ct2, double ct3, double cr1, double cr2, double cc1)//构造函数 {

a = new Ctringle(ct1,ct2,ct3); a1 = new CRect(cr1,cr2); a2 = new CCircle(cc1); }

public double Alarea()//计算三种形状的面积和 {

return a.Area() + a1.Area() + a2.Area(); }

}

class Program {

static void Main(string[] args) {

double als=0;//计算总面积 CShape a;

a = new Ctringle(3,4,5);

als +=a.Area();//加上三角形面积 Console.WriteLine(\三角形面积:{0:f3}\输出地结果均保留3位小数 a.SetDate(3,4,5);

Console.WriteLine(\三角形面积:{0:f3}\

a = new CRect(4,5);

als+=a.Area();//加上矩形面积

Console.WriteLine(\矩形面积:{0:f3}\ a.SetDate(4, 6);

Console.WriteLine(\矩形面积:{0:f3}\

a = new CCircle(3);

als +=a.Area();//加上圆形面积

Console.WriteLine(\圆面积:{0:f3}\ a.SetDate(4);

Console.WriteLine(\圆面积:{0:f3}\

Console.WriteLine(\三种形状总面积:{0:f3}\

CArea c1=new CArea(3,4,5,4,5,3);

Console.WriteLine(\三种形状总面积:{0:f3}\

} }

(复习重点)P148:7-1、 输入一个字符串,统计其中有多少个单词?单词之间用空格分隔开。(加大难度 对单词进行排序与除重复) static void Main() {

string str;

string[] words = new string[100]; //用来存放分隔出来的数组 string[] newwords = new string[100];//用来存放无重复的数组 str = Console.ReadLine(); //输入字符串 string temb; //寄存字符串,用于排序 int n=0; //计数

words = str.Split(' '); //分隔字符串,用空格分离

for (int i = 0; i < words.Length; i++)//排序 for(int j=0;j

if((string.Compare(words[j],words[j+1]))==1) {

temb=words[j];

words[j]=words[j+1]; words[j+1]=temb; }

}

Console.WriteLine(\排序后字符串:\ for (int i=0; i < words.Length; i++) { Console.WriteLine(\ }

newwords[0] = words[0];//将排序后字符串首个赋给不重复

for (int i = 0; i < words.Length-1; i++) //除重复 {

if ((newwords[n].CompareTo(words[i])) != 0) //如果不相同,字符串赋值给下个单元

{

n++;

newwords[n] = words[i]; } }

Console.WriteLine(\除去重复的字符串:\ for (int i = 0; i < n+1; i++) {

Console.WriteLine(\ } }

本文来源:https://www.bwwdw.com/article/gymg.html

Top