C#习题集(6)

2018-12-10 22:47

五.程序阅读题

1.阅读下列程序,写出程序的输出结果。 考察类对象 public class TPoint { int x, y; public void setpoint(int x1,int y1) { x = x1; y = y1; } public void dispoint()

{ Console.WriteLine(\点:({0},{1})\ } }

class Program

{ static void Main(string[] args)

{ TPoint p1, p2; p1= new TPoint(); p1.setpoint(2, 6); p2 = p1; p2.setpoint(8, 3);

p1.dispoint(); p2.dispoint(); } }

点:(8,3) 点:(8,3) 2.阅读下列程序,写出程序的输出结果。考察方法参数传递 class Program

{ static void f1(ref int a, ref int b, out int sum, out int sub) { int t; t=a; a=b; b=t;

sum = a + b; sub = a - b; }

public static void Main(string[] args) { int x=10, y=20,xx=30,yy=40; f1(ref x, ref y, out xx, out yy); Console.WriteLine(\ } }

x=20,y=10,xx=30,yy=10 3.阅读下列程序,写出程序的输出结果。考察索引器 public class Weeks

{ string[] week= { \ public string this[int i]

{ get

{ if (i >= 0 && i< 7) return week[i]; else return week[0]; } set

{ if (i >= 0 && i< 7) week[i] = value;

- 25 -

} } }

class Program

{ static void Main()

{ Weeks w = new Weeks();

w[0] = \星期天\ w[1] = \星期一\

Console.WriteLine(w[2]); Console.WriteLine(w[7]); } } Tues 星期天 4.阅读下列程序,写出程序的输出结果。考察委托使用 delegate void mydelegate(double x, double y); class MyDeClass

{ public void add(double x, double y)

{ Console.WriteLine(\ public void sub(double x, double y)

{ Console.WriteLine(\ }

class Program

{ static void Main(string[] args)

{ MyDeClass obj = new MyDeClass(); mydelegate p;

p = obj.add; p += obj.sub; p(10, 20); } } 10+20=30 10-20=-10 5.阅读下列程序,写出程序的输出结果。考察委托使用 public delegate int mydelegate(int x, int y); class Math

{ public int fun1(int x, int y) { return x * x + y * y; } public int fun2(int x, int y) { return x * x - y * y; } }

class Program

{ static void Main(string[] args) { int result;

mydelegate objCall;

- 26 -

} } Math objMath = new Math(); objCall = objMath.fun1; result = objCall(5, 3); Console.WriteLine(\结果为{0}\ objCall = objMath.fun2; result = objCall(5, 3); Console.WriteLine(\结果为{0}\ Console.Read(); } }

结果为34 结果为16 5. 阅读下列程序,写出程序的输出结果。考察事件 public delegate void mydelegate1(int i);

class MyEvent { public event mydelegate1 Event1; public void FireEvent1(int i)

{ if (Event1 != null) Event1(i); } }

public class Program { public static void show(int i)

{ Console.Write(\ } public static void Main()

{ MyEvent a = new MyEvent();

a.Event1 += new mydelegate1(show); for (int i = 0; i <= 30; i++)

if (i % 7 == 0) a.FireEvent1(i); } }

0 7 14 21 28 6. 阅读下列程序,写出程序的输出结果。考察运算符重载 class Complex { double a, b;

public Complex(double m, double n) { a=m; b=n; }

public static Complex operator +(Complex x,Complex y) { Complex t=new Complex(0,0); t.a=x.a+y.a; t.b=x.b+y.b; return(t); }

public void DispCom() { if (b>0 ) Console.WriteLine(\ else Console.WriteLine(\ }

- 27 -

}

class Program

{ public static void Main(string[] args)

{ Complex x=new Complex(1,2); Complex y=new Complex(3,4); Complex z=new Complex(0,0);

z=x+y;

x.DispCom(); y.DispCom();

z.DispCom(); } } 1+2i 3+4i 4+6i 六.编程题

1.创建一个Student类,要求:

? 该类封装学生的姓名、性别和成绩等信息。 ? 通过构造函数给姓名和性别信息赋值。

? 姓名和性别信息只能读不能写,成绩信息通过属性进行读写,对成绩属性进行赋值时,若成

绩大于100分赋100分,若成绩低于0分赋0分。 ? 具有一个判断成绩等级的方法 class Student { private string name; private string sex; private int cj; public Student(string name, string sex) { this.name = name; this.sex = sex; } public string Name { get { return name; } } public string Sex { get { return sex; } } public int Score { get { return cj; } set { if (value > 100) cj = 100; else if (value < 0) cj = 0; else cj = value; } } public string grade(int a) { string dj; if (a >= 90) dj = \优秀\ else if (a >= 80) dj = \良好\ else if (a >= 70) dj = \中等\ else if (a >= 60) dj = \及格\

- 28 -

else dj = \不及格\ return dj; } } 2.声明一个用于检查用户合法性及用户级别的类CheckUser,具体要求如下:

? 该类包含UserName(用户名)和UsePwd(密码)两个string类型的属性。 ? 包含一个带有两个string类型参数的构造函数。

? 包含一个返回值为int类型的UserLevel()方法,返回值为0表示高级用户,为1表示普通用

户,为2表示用户不存在。若用户名为“zhangsan”,密码为”123456”,则为高级用户。若用户名为“lisi”,密码为”654321”,则为普通用户。所有其他用户均为不合法用户。 class CheckUser { private string name; private string pwd; public string UserName { get { return name; } set { name = value; } } private string UserPwd { get { return pwd; } set { pwd = value; } } public CheckUser(string name, string pwd) { this.name = name; this.pwd = pwd; } public int UserLevel() { int level = 2; if (UserName == \ level = 0; if (UserName == \ level = 1; return level; } } 3.设计实现一个商品销售的简单管理程序。具体要求如下:

? 每一种商品对象要存储的是商品名称、库存量及商品的单价。 ? 每销售一件商品要计算销售总额和商品库存量。

class Goods {

private string name; public string Name {

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

private int count; //商品库存量 private double price; //商品价格

static private double sum = 0; //所有商品的销售额 public Goods(string name, int count, double price)

{ this.name = name; this.count = count; this.price = price; }

- 29 -


C#习题集(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:《职业生涯规划》教案第三单元第3课:制定措施落实职业生涯规划

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: