C#NET程序设计教程实验指导(清华大学江红,余青松)实验源码第五章


5.1

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

namespace c5._1
{
    class MyMath{ 
    
        public const double PI = 3.1415926;
        public static double Perimeter(double r)
        {
            return 2 * PI * r;
        }
        public static double Area(double r)
        {
            return PI * r * r;
        }
        public static double Volume(double r)
        {
            double v = 4 * PI * Math.Pow(r, 3) / 3;
            return v;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入半径:");
            double r = double.Parse(Console.ReadLine());
            Console.WriteLine("圆的周长={0}",MyMath.Perimeter(r));
            Console.WriteLine("圆的面积={0}",MyMath.Area(r));
            Console.WriteLine("圆的体积={0}",MyMath.Volume(r));
            Console.ReadKey();

        }
    }
}

5.2

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

namespace c5._2
{
    public class TemperatureCelsius
    {
        protected double degree;
        public TemperatureCelsius(double d)
        {
            this.degree = d;
        }
        public double ToFahrenheit()
        {
            return (degree * 9 / 5)+32;
        }
        
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入摄氏温度:");
            double d = Double.Parse(Console.ReadLine());
            TemperatureCelsius t = new TemperatureCelsius(d);
            Console.WriteLine("摄氏温度={0},华氏温度={1}",d,t.ToFahrenheit());
            Console.ReadKey();
        }
    }
}

5.3

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

namespace c5._3
{
    public class Person
    {
        public string name;
        public uint age;
        public Person(string name, uint age)
        {
            this.name = name;
            this.age = age;
        }
        public virtual void GetInfo()
        {
            Console.WriteLine("Name:{0}", name);
            Console.WriteLine("Age:{0}", age);
        }
    }
    public class Teacher:Person
    {
        public uint TeacherID;
        public Teacher(string name, uint age, uint id)
            : base(name, age)
        {
            this.TeacherID = id;
        
        }
        public override void GetInfo()
        {
            base.GetInfo();
            Console.WriteLine("TeacherID:{0}", TeacherID);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Teacher t = new Teacher("陈世光", 15, 100);
            t.GetInfo();
            Console.ReadKey();


        }
    }
}

5.4

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

namespace c5._4
{
    public abstract class Shape {
        protected string name;
        public Shape(string name)
        {
            this.name = name;
        }
        public abstract void Show();
        public abstract double Area();
    
    }
    public class Rectangle : Shape
    {
        protected double weight;
        protected double height;
        public Rectangle(string name, double w, double h)
            : base(name)
        {
            this.weight = w;
            this.height = h;
        }
        public override void Show()
        {
            Console.WriteLine("Rectangle:{0},   Area:{1}", name, Area());
        }
        public override double Area()
        {
            return weight * height;
        }
    }
    public class Circle : Shape
    {
        protected double r;
        const double PI = 3.1415929;
        public Circle(string n, double r)
            : base(n)
        {
            this.r = r;
        }
        public override void Show()
        {
            Console.WriteLine("Circle:{0},   Area:{1}", name, Area());

        }
        public override double Area()
        {
            return PI * r * r;
           //return base.Area();
        }
    
    
    }


    class Program
    {
        static void Main(string[] args)
        {
            Shape[] s={new Rectangle("矩形",10,2.5),new Circle("圆",4)};
            foreach(Shape e in s)
            {
                e.Show();
            }
            Console.ReadKey();

        }
    }
}

5.5

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

namespace c5._5
{
    public class Complex
    {
        protected int real;//shibu
        protected int imaginary;//虚部
        public Complex(int r, int i)
        {
            this.real = r;
            this.imaginary = i;
        }
        public static Complex  operator +(Complex c1, Complex c2)//必须定义为static
        {
            return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
        }
        public static Complex operator -(Complex c1, Complex c2)
        {
            return new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary);
        }
        public static Complex operator *(Complex c1, Complex c2)
        {
            return new Complex(c1.real * c2.real - c1.imaginary * c2.imaginary,
                c1.real * c2.imaginary + c1.imaginary * c2.real);
        }
        public override string ToString()
        {
            return (String.Format("{0}+{1}i", real, imaginary));
            //return base.ToString();
        }

    
    
    }
    class Program
    {
        static void Main(string[] args)
        {
            Complex c1 = new Complex(4, 5);
            Complex c2 = new Complex(3, 2);
            Console.WriteLine("第一个复数:{0}", c1.ToString());
            Console.WriteLine("第二个复数:{0}", c2.ToString());
            Console.WriteLine("两个复数之和:{0}", (c1+c2).ToString());
            Console.WriteLine("两个复数之差:{0}", (c1 - c2).ToString());
            Console.WriteLine("两个复数之积:{0}", (c1 * c2).ToString());
            Console.ReadKey();
        }
    }
}

 

5.6

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

namespace c5._6
{
    public interface ICDPlayer
    {
        void Play();
        void Stop();
        void PreviousTrack();
        void NextTrack();
        void Off();
        int CurrentTrack
        {
            get;
        }
    
    }
    public class CDPlayer : ICDPlayer
    {
        private int currentTrack = 0;
        public void Play()
        {
            Console.WriteLine("启动Rhythmbox...");
        }
        public void Stop()
        {
            Console.WriteLine("暂停Rhythmbox...");
        }
        public void PreviousTrack()
        {
            Console.WriteLine("上一首");
            if (currentTrack >= 1)
                currentTrack--;
        }
        public void NextTrack()
        {
            Console.WriteLine("下一首...");
            //if(currentTrack)
            currentTrack++;
        }
      
        public void Off()
        {
            Console.WriteLine("OFF!");
        }
        public int CurrentTrack {//好奇葩的方法,没有参数列表的??
            get {
                return currentTrack;
            }
        
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            CDPlayer cd = new CDPlayer();
            cd.Play();
            Console.WriteLine("CD.currentTrack={0}", cd.CurrentTrack);
            cd.NextTrack();
            cd.NextTrack();
            Console.WriteLine("CD.currentTrack={0}", cd.CurrentTrack);
            cd.PreviousTrack();
            Console.WriteLine("CD.currentTrack={0}", cd.CurrentTrack);
            cd.Stop();
            cd.Off();
            Console.ReadKey();
        }
    }
}

5.7

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;//for ArrayList

namespace c5._7
{
    //①:声明提供事件数据的类,作为事件方法的参数
    //即传递给事件的参数
    public class NameListEventArgs : EventArgs
    {
        public string Name { get; set; }
        public int Count{ get; set; }
        public NameListEventArgs(string n, int c)//:Name(namespace),Count(c)
        //实践证明不能用C++的默认初始化方法
        {
            Name = n;
            Count = c;
        }
    }
    //②:声明事件处理委托
    public delegate void NameListEventHadndler(object source,NameListEventArgs args);
    //③:声明引发事件的类,事件产生类
    public class NameList
    {
        ArrayList list;
        //④:在事件产生类中声明事件
        public event NameListEventHadndler nameListEvent;//声明事件
        public NameList()
        {
            list = new ArrayList();
        }
        public void Add(string name)
        {
            list.Add(name);
            //⑤:产生事件
            /*
            if (nameListEvent != null)
            {
                nameListEvent(this, new NameListEventArgs(name, list.Count));
            }*/
            nameListEvent(this, new NameListEventArgs(name, list.Count));
        }
    }
    //⑥:声明处理事件的类
    public  class EventDemo
    {
        //声明事件处理方法,跟委托声明中参数要一样
        public static void Method1(object source, NameListEventArgs args)
        {
            Console.WriteLine("列表中增加了项目:{0}" , args.Name);
        }
        public static void Method2(object source, NameListEventArgs args)
        {
            Console.WriteLine("列表中的数目:{0}" , args.Count);
        }

        static void Main(string[] args)
        {
            NameList n1 = new NameList();

            NameListEventHadndler p = new NameListEventHadndler(EventDemo.Method1);

            //n1.nameListEvent+=new NameListEventHadndler(EventDemo.Method1);
            //⑦:订阅事件
            n1.nameListEvent += p;
            n1.nameListEvent+=new NameListEventHadndler(EventDemo.Method2);
            n1.Add("张三");
            n1.Add("311006121");
            n1.nameListEvent -= p;
            n1.Add("陈某某");


            Console.ReadKey();

        }
    }
}

 

 

 


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注