c#程序设计教程(李春葆第三版)第五章课后程序题答案exci5-1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace exci5_1
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person(2, 50);
Animal a1 = new Animal(4,120);
p1.show();
a1.show();
}
}
public class Person //定义⼈类
{
public int legs; //腿的只数
protected float weight; //重量
public Person() //默认构造函数
{ }
public Person(int legs1, float weight1)//⾃定义⽅法F
{
legs = legs1;
weight = weight1;
}
public void show()
{
Console.WriteLine(“某⼈有{0}只腿,重量为{1}kg”, legs, weight);
}
}
class Animal //定义动物类
{
public int num; //腿的条数
private float weight; //重量
public Animal() //Animal类的默认构造函数
{ }
public Animal(int n, float w) //Animal类带2个参数的构造函数
{
num = n;
weight = w;
}
public void show()
{
Console.WriteLine(“某动物有{0}只脚,重量为{1}kg”, num, weight);
}
}
}
exci5-2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace exci5_2
{
//定义了⼀个委托,委托在传递⽅法时,⽅法必须带两个int型的参数。public delegate int Call(int num1, int num2);
//在Delegates类的内部定义Math类和TestDelegates类。
class Math
{
public int fun1(int num1, int num2)
{
return num1 * num1 + num2 * num2;
}
public int fun2(int num1, int num2)
{
return num1 * num1 - num2 * num2;
}
}
class Program
{
static void Main(string[] args)
{
int result;
Call objCall; //委托的对象
Math objMath = new Math(); //Math类的对象
objCall = new Call(objMath.fun1);
result = objCall(5, 3); //将委托实例化
Console.WriteLine(“55+33={0}”, result);
objCall = new Call(objMath.fun2);
result = objCall(5, 3); //将委托实例化
Console.WriteLine(“55-33={0}”, result);
}
}
}
exci5-3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace exci5_3
{
class List
{
private int Max = 100; //存储最多元素
private int num = 0; //存储的实际元素个数
private object[] list; //存储元素数组
public List() //构造函数
{
list = new object[Max];
}
public void add(object obj) //添加⼀个元素
{
list[num] = obj;
num++;
}
public void delete(int pos) //删除⼀个元素
{
for (int i = pos + 1; i < num; i++)
list[i - 1] = list[i];
num–;
}
public object get(int pos) //获取指定位置的元素
{
if (pos < num)
return list[pos];
else
return null;
}
public int getnum() //获取实际元素个数
{
return num;
}
public string disp() //获取所有元素
{
string s = “”;
for (int i = 0; i < num; i++)
s += list[i] + " ";
return s;
}
}
class Program
{
static void Main(string[] args)
{
List list = new List();
list.add(“abc”);
list.add(1.23);
list.add(2);
list.add(‘a’);
Console.WriteLine(“元素序列:{0}”, list.disp()); Console.WriteLine(“元素个数:{0}”, um()); Console.WriteLine(“位置1的元素:{0}”, (1)); Console.WriteLine(“删除位置2的元素”);
list.delete(2);
Console.WriteLine(“元素序列:{0}”, list.disp());
}
}
}
exci5-4
using System;writeline教程
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace exci5_4
{
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("\t{0}\t{1}\t{2}\t{3}", 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 + 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:”);
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数学\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数学\t总分");
for (i = 0; i < n; i++)
{
Console.Write(“第{0}名:”, i + 1);
p[i].display();
}
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。