public void makesale(int num) {
this.count = this.count - num; //修改商品库存量
sum = sum + this.price * num; //修改所有商品的销售总额 }
public int Count
{ get { return count; } } public static double Sum { get { return sum; } } }
class Program {
static void Main(string[] args) {
Goods Candy = new Goods(\糖果\, 200, 2); Goods Chips = new Goods(\薯片\, 500, 5); Candy.makesale(50); Chips.makesale(10);
Console.WriteLine(\的库存={1}\, Candy.Name, Candy.Count); Console.WriteLine(\的库存={1}\, Chips.Name,Chips.Count); Console.WriteLine(\, Goods.Sum); Console.Read(); } }
4.声明一个名为MyCar的类,具体要求如下:
? MyCar类可以被任何类访问
? 该类包含有两个属性:string类型的CarType和double类型的CarPrice
? 该类具有两个重载构造函数:一个无参数,一个包含两个参数(对应CarType和CarPrice属
性)。使用无参数构造函数初始化MyCar类对象时,CarType属性值默认为“SUV”,CarPrice属性值默认为24.5。
5.编写一个名为CTimer1的类,该类能够存放时间信息(设定三个域分别用来存放时分秒信息),
并且具有设置时间和显示时间的功能(设置和显示时间可定义成类的方法)。 6.创建一个名为Person的类,具体要求如下:
? 含有静态字段total(存放学生总数),实例字段ID(学号)、name(姓名)、sex(性别) ? 创建构造函数,为新实例设置字段值,并记录实例个数。 ? 创建静态方法,显示当前学生总数。
? 创建实例方法,显示学生的学号、姓名和性别。 ? 在主函数中调用实例化Person类,并调用静态方法和实例方法,要求第一次实例化显示自己
的学号和姓名等。
class Person { public static int total=0; public int num; public string name; public string sex; public Person(int n, string a, string s) { num = n; name = a; sex = s; total++; } public static void displayNum()
- 30 -
{ Console .WriteLine (\} public void DisplayAll() { Console.WriteLine(\学号:{0}\\t姓名:{1}\\t性别:{2}\} } public class test { static void Main() { Person myPerson = new Person(1001, \张小为\女\Person.displayNum(); myPerson.DisplayAll(); } } 7.创建一个复数类Complex,可以实现复数的赋值(通过构造函数)、显示(通过实例方法)和加
法运算(通过“+”运算符重载)。 class Complex { int real, imag; public Complex(int a, int b) { real = a; imag = b; } public void Display() { Console.WriteLine(\ } public static Complex operator +(Complex p1, Complex p2) { Complex p=new Complex(0,0); p.real = p1.real + p2.real; p.imag = p1.imag + p2.imag; return p; } } 8.设计一个控制台应用程序,创建一个List类,可以存储整数、实数、字符数据等(最多存放100
个元素),并可以添加元素、删除元素、获取指定位置的元素、获取元素的实际个数、获取所有元素等。用相关数据进行测试。
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()
- 31 -
//获取实际元素个数 { 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(\ list.add(1.23); list.add(2); list.add('a'); Console.WriteLine(\元素序列:{0}\ Console.WriteLine(\元素个数:{0}\ Console.WriteLine(\位置1的元素:{0}\ Console.WriteLine(\删除位置2的元素\ list.delete(2); Console.WriteLine(\元素序列:{0}\ Console.Read(); } } 9.设计一个控制台应用程序,编程计算0~100中所有能被7整除的整数。要求:将输出结果的命令
置于事件处理程序中,每找到一个符合条件的数,就通过触发事件来执行输出程序。 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 //事件源类 //定义事件 //引发事件Event1的方法 //创建订阅者类 { public static void show(int i) { Console.WriteLine(\事件触发,能被7整除的数是:{0}\ } public static void Main() { MyEvent a = new MyEvent(); //订阅事件,把方法FireEvent1添加到事件Event1的列表中 a.Event1 += new mydelegate1(show); for(int i=0;i<=100;i++) if (i % 7 == 0) a.FireEvent1(i); //触发事件 Console.Read(); } } 10.请设计一个类,用来模拟栈及其操作。
- 32 -
说明:栈是一种重要的数据结构,在内存中占用连续的存储单元。栈有两个端点,固定的栈底和浮动的栈顶。栈有两种基本操作:push(压栈)和pop(出栈)。压栈是向栈顶位置写入一个元素,然后使栈顶指示器加1;出栈是先使栈顶指示器减1,再把该位置的元素读出来。
提示:可用一个数组成员(假设为buffer)表示栈的空间,用一个整型成员(假设为sp)表示栈顶指示器,并在构造函数中分配栈的空间和设置sp的值为0。出栈与压栈均用类的方法来实现,一般设其方法名分别为push和pop。
class Stack { private int[] arr; private int sp; private int count; public Stack(int Length) //栈空间 //栈顶指示器 //允许的栈元素的最多个数 //栈的构造函数形式一 { count = Length; sp = 0; arr = new int[Length]; } public Stack() //栈的构造函数的另一种形式 { count = 10; sp = 0; arr = new int[10]; } public bool isEmptyStack() { if (sp == 0) return true; else return false; } public bool isFullStack() { if (sp ==count ) return true; else return false; } public void push(int element) //压栈操作 { arr[sp] = element; public int pop() sp = sp + 1; } //出栈操作 { sp = sp - 1; return (arr[sp]); } public string putstack() { int i; string stack = \ for (i = 0; i < sp; i++) stack = stack + arr[i].ToString()+ \ \ return stack; } } 11.设计一个控制台应用程序项目a,输入若干个学生的姓名、英语和数学成绩,求出总分,并按
总分从高到低排序。要求设计一个学生类Student,所有学生对象存放在一个Student对象数组中,通过一个方法对其按照总分进行降序排序,最出输出排序后的结果,输出结果类似于下图所示。
- 33 -
控制台应用程序项目a的执行结果
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(\ } } class Program { const int Max = 100; static void sort(int n, params Student[] p) //采用冒泡排序法排序 { int i, j; Student tmp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n-i-1; j++) if (p[j].psum < p[j+1].psum) { tmp = p[j + 1]; p[j + 1] = p[j]; p[j] = tmp; } } } 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++)
- 34 -
//创建对象引用的实例