<C#任务导引教程>练习九
阅读原文时间:2023年07月09日阅读:2

//75,异常情况
using System;
class Program
{
    public static void Main()
    {
        Console.Write("请输入编号:");
        string number = Console.ReadLine();
        Console.Write("请输入工资:");
        double earnings = Convert.ToDouble(Console.ReadLine());
        Console.Write("请输入年龄:");
        int age = Convert.ToInt32(Console.ReadLine());
        int f = test(number);
        int g = test(earnings);
        int h = test(age);
        if (f == 1 && g == 1 && h == 1)
            Console.WriteLine("\n编号:{0}\n工资:{1:C2}\n年龄:{2}", number, earnings, age);
    }
    public static int test(string p)
    {
        int f = 1;
        try
        {
            if (p[0] >= '0' && p[0] <= '9')                 throw new Exception();         }         catch (Exception)         {             f = 0;             Console.WriteLine("编号错误:{0}", p[0]);         }         return f;     }     public static int test(double d)     {         int g = 1;         try         {             if (d < 0 || d > 20000)
                throw new Exception();
        }
        catch (Exception)
        {
            g = 0;
            Console.WriteLine("工资错误:{0}", d);
        }
        return g;
    }
    public static int test(int a)
    {
        int w = 1;
        try
        {
            if (a < 18 || a > 60)
                throw new Exception( );
        }
        catch (Exception)
        {
            w = 0;
            Console.WriteLine("年龄错误:{0}", a);
        }
        return w;
    }
}
//76,多重异常类型捕捉示例
using System;
class Program
{
    public static void Main()
    {
        short sh = 0;
        string s = null;
        for (int i = 1; i <= 4; i++)
        {
            try
            {
                Console.Write(" 请输入一个整数 :");
                s=Console.ReadLine();
                sh=short.Parse(s);
                double d=100/sh;
            }
            catch(FormatException e)
            {
                Console.WriteLine("格式错误!{0}",s);
            }
            catch (OverflowException e)
            {
                Console.WriteLine("数据溢出!{0}", s);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.WriteLine("主函数结束:");
        }
    }
}
//77,编写程序,Stud,用于输入学生的数据,当成绩score大于100或小于0时,抛出一个异常,显示出错信息后,继续输入学生的成绩
using System;
class Program
{
    class Stud
    {
        protected int no;
        protected string name;
        protected int score;

        public void getdata()
        {
            Console.Write("  学号:");
            no = Convert.ToInt32(Console.ReadLine());
            Console.Write("  姓名:");
            name = Console.ReadLine();
            Console.Write("  成绩:");
            score = Convert.ToInt32(Console.ReadLine());
            if (score > 100 || score < 0)
                throw new Exception();
        }
        public void getscore()
        {
            score = Convert.ToInt32(Console.ReadLine());
        }
        public void disp()
        {
            Console.WriteLine("学号:{0,4}姓名:{1,8}成绩:{2,3}", no, name, score);
        }
    }
    public static void Main()
    {
        Stud[ ] st = new Stud[3];
        Console.WriteLine("输入数据:");
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("请输入第{0}位学生的学号、姓名、成绩:",i+1);
            st[i]=new Stud( );
                try
                {
                    st[i].getdata();
                }
                catch(Exception)
                {
                    Console.Write("   成绩不正确,重新输入:");
                    st[i].getscore();
                }
        }
        Console.WriteLine("\n输出数据:");
        for(int j=0;j<3;j++)
            st[j].disp();
    }
}
//78,公有派生方式之下对继承来的成员访问属性的变化示例
using System;
class A
{
    private int x;
    protected int y;
    public int z;
    public void setx(int i)
    {
        x = i;
    }
    public int getx()
    {
        return x;
    }
}
class B : A
 {
    private int m;
    protected int n;
    public int p;
    public void setvalue(int a, int b, int c, int d, int e, int f)
    {
        setx(a);
        y = b;
        z = c;
        m = d;
        n = e;
        p = f;
    }
    public void display()
    {
        Console.WriteLine("x={0}", getx());
        //Console.WriteLine("x={0}", x);
        Console.WriteLine("y={0}", y);
        Console.WriteLine("m={0}", m);
        Console.WriteLine("n={0}", n);
    }
}
class MainClass
{
    static void Main()
    {
        B obj = new B();
        obj.setvalue(1, 2, 3, 4, 5, 6);
        obj.display();
        Console.WriteLine("z={0}", obj.z);
        //Console.WriteLine("m={0}", obj.m);
        //Console.WriteLine("n={0}", obj.n);
        Console.WriteLine("p={0}", obj.p);
    }
}
//79,类的派生层次
using System;
class Person
{
    protected static int no;
    protected static string name;
    public static void getdata()
    {
        Console.Write("  编号:");
        no = Convert.ToInt32(Console.ReadLine());
        Console.Write("  姓名:");
        name = Console.ReadLine();
    }
    public static void dispdata()
    {
        Console.WriteLine("  编号:{0}", no);
        Console.WriteLine("  姓名:{0}", name);
    }
}
class Teacher : Person
{
    protected string title;
    protected string section;
    public new void getdata()
    {
        Console.WriteLine("\n输入一个教师数据:");
        Person.getdata();
        Console.Write("  职称:");
        title = Console.ReadLine();
        Console.Write("  教研室:");
        section = Console.ReadLine();
    }
    public new void dispdata()
    {
        Console.WriteLine("\n\n输出一个教师数据:");
        Person.dispdata();
        Console.WriteLine("  职称:{0}", title);
        title = Console.ReadLine();
        Console.WriteLine("  教研室:{0}", section);
        section = Console.ReadLine();
    }
}
class Student : Person
{
    protected static string sex;
    protected static string cname;
    public static new void getdata()
    {
        Person.getdata();
        Console.Write("  性别:");
        sex = Console.ReadLine();
        Console.Write("  班号:");
        cname = Console.ReadLine();
    }
    public static new void dispdata()
    {
        Person.dispdata();
        Console.WriteLine("  性别:{0}", sex);
        Console.WriteLine("  班号:{0}", cname);
    }
}
class Unstudent : Student
{
    private int score1;
    private int score2;
    private int score3;
    public new void getdata()
    {
        Console.WriteLine("\n\n输入一个大学生数据:");
        Student.getdata();
        Console.Write("  大学英语:");
        score1 = Convert.ToInt32(Console.ReadLine());
        Console.Write("  高等数学:");
        score2 = Convert.ToInt32(Console.ReadLine());
        Console.Write("  程序设计:");
        score3 = Convert.ToInt32(Console.ReadLine());
    }
    public new void dispdata()
    {
        Console.WriteLine("\n\n输出一个大学生数据:");
        Student.dispdata();
        Console.WriteLine("  大学英语:{0}", score1);
        Console.WriteLine("  高等数学:{0}", score2);
        Console.WriteLine("  程序设计:{0}", score3);
        Console.WriteLine("  平均分:{0:F1}", (score1+score2+score3)/3.0f);
    }
}
class Mistudent : Student
{
    private int score1;
    private int score2;
    private int score3;
    public new void getdata()
    {
        Console.WriteLine("\n\n输入一个中学生数据:");
        Student.getdata();
        Console.Write("  英语:");
        score1 = Convert.ToInt32(Console.ReadLine());
        Console.Write("  数学:");
        score2 = Convert.ToInt32(Console.ReadLine());
        Console.Write("  语文:");
        score3 = Convert.ToInt32(Console.ReadLine());
    }
    public new void dispdata()
    {
        Console.WriteLine("\n\n输出一个大学生数据:");
        Student.dispdata();
        Console.WriteLine("  英语:{0}", score1);
        Console.WriteLine("  数学:{0}", score2);
        Console.WriteLine("  语文:{0}", score3);
        Console.WriteLine("  平均分:{0:F1}", (score1 + score2 + score3) / 3.0f);
    }
}
class MainClass
{
    static void Main()
    {
        Teacher t = new Teacher();
        t.getdata();
        t.dispdata();
        Unstudent s1 = new Unstudent();
        s1.getdata();
        s1.dispdata();
        Mistudent s2 = new Mistudent();
        s1.getdata();
        s2.dispdata();
    }
}
//80,声明一个接口ISharp,用于计算矩形面积
using System;
public interface ISharp
{
    double Width { get; set; }
    double GetArea();
}
public class Rectangle : ISharp
{
    public double dwidth;
    public double dheight;
    public Rectangle(double w, double h)
    {
        this.dwidth = w;
        this.dheight = h;
    }
    public double Width
    {
        get { return dwidth; }
        set { dwidth = value; }
    }
    public double Height
    {
        get { return dheight; }
        set { dheight = value; }
    }
    public double GetArea()
    {
        return this.dheight * this.dwidth;
    }
}
class Program
{
    static void Main()
    {
        Rectangle rt = new Rectangle(4, 5);
        Console.WriteLine("矩形面积为:{0:f}", rt.GetArea());
        rt.Width = 8;
        Console.WriteLine("矩形面积为:{0:f}", rt.GetArea());

    }
    
}
/*81,创建一个控制台程序,其中声明了两个接口,ImyInterface1和ImyInterface2,在这两个接口中声明了一个同名方法Add,然后定义一个类MyClass,
 该类继承自己已经声明的两个接口。在MyClass类中实现接口中的方法时,由于ImyInterface1和ImyInterface2接口中声明的方法名相同,这里使用了显式接口成员实现,
 最后在主程序类Program的Main方法中使用接口对象调用接口中定义的方法*/
using System;
interface ImyInterface1
{
    int Add();
}
interface ImyInterface2
{
    int Add();
}
class myClass : ImyInterface1, ImyInterface2
{
    int ImyInterface1.Add()
    {
        int x = 98;
        int y = 368;
        return x + y;
    }
    int ImyInterface2.Add()
    {
        int x = 98;
        int y = 368;
        int z = 698;
        return x + y + z;
    }
}
class Program
{
    static void Main()
    {
        myClass myclass = new myClass();
        ImyInterface1 imyinterface1 = myclass;
        Console.WriteLine("x+y={0}", imyinterface1.Add());
        ImyInterface2 imyinterface2 = myclass;
        Console.WriteLine("x+y+z={0}", imyinterface2.Add());
    }
}
/*82,使用接口完成多继承问题*/
using System;
interface ITeacher
{
    string Name
    {
        get;
        set;
    }
    int Age
    {
        get;
        set;
    }
    string Title
    {
        get;
        set;
    }
}
interface IStudent
{
    string Sex
    {
        get;
        set;
    }
    double Score
    {
        get;
        set;
    }
}
class Graduate : ITeacher, IStudent
{
    string name = " ";
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    int age = 0;
    public int Age
    {
        get { return age; }
        set { age = value; }
    }
    string title = " ";
    public string Title
    {
        get { return title; }
        set { title = value; }
    }
    string sex = " ";
    public string Sex
    {
        get { return sex; }
        set { sex = value; }
    }
    double score =0.0;
    public double Score
    {
        get { return score; }
        set { score = value; }
    }
    public void Set(double w)
    {
        wage = w;
    }
    public void show()
    {
        Console.WriteLine(" 基本信息: ");
        Console.WriteLine("  姓名:{0}",name);
        Console.WriteLine("  性别:{0}",sex);
        Console.WriteLine("  年龄:{0}",age);
        Console.WriteLine("  职称:{0}",title);
        Console.WriteLine("  工资:{0}",wage);
        Console.WriteLine("  成绩:{0}",score);
    }
    private double wage;
}
class Program
{
    static void Main()
    {
        Graduate grad = new Graduate();
        Console.Write("请输入姓名:");
        grad.Name = Console.ReadLine();
        Console.Write("请输入性别:");
        grad.Sex =Console.ReadLine();
        Console.Write("请输入年龄:");
        grad.Age = Convert.ToInt32(Console.ReadLine());
        Console.Write("请输入职称:");
        grad.Sex = Console.ReadLine();
        Console.Write("请输入工资:");
        grad.Set(Convert.ToDouble(Console.ReadLine()));
        Console.Write("请输入成绩:");
        grad.Score = Convert.ToDouble(Console.ReadLine());
        grad.show();
    }
}