e心e意

导航

C#练习10至20题

第十题

namespace 第十题
{
    class Program
    {
        /*建立三个类:居民、成人、官员。居民包含身份证
         * 号、姓名、出生日期,而成人继承自居民,多包

含学历、职业两项数据;官员则继承自成人,
         * 多包含党派、职务两项数据。要求每个类
         * 中都提供数据输入输出的功能。
*/
        /// 居民类
        /// </summary>
        public class Inhabitant
        {
            public string ID { get; set; }//身份证号码
            public string Name { get; set; }//姓名
            public DateTime Birthday { get; set; }//生日
            /// <summary>
            /// 输入
            /// </summary>
            /// <param name="args">参数数组</param>
            public virtual void SetValues(params object[] args)
            {
                this.ID = args[0].ToString();
                this.Name = args[1].ToString();
                this.Birthday = Convert.ToDateTime(args[2]);
            }
            /// <summary>
            /// 输出
            /// </summary>
            /// <returns>按一定的格式返回所有的属性</returns>
            public virtual string GetValues()
            {
                return string.Format("Inhabitant| ID:{0}| Name:{1}| Birthday: {2}",
                    this.ID, this.Name, this.Birthday);
            }
        }
        /// <summary>
        /// 成人类(继承自居民类)
        /// </summary>
        public class Adult : Inhabitant
        {
            public string Degree { get; set; }//学历
            /// <summary>
            /// 输入
            /// </summary>
            /// <param name="args">参数数组</param>
            public override void SetValues(params object[] args)
            {
                this.ID = args[0].ToString();
                this.Name = args[1].ToString();
                this.Birthday = Convert.ToDateTime(args[2]);
                this.Degree = args[3].ToString();
            }
            /// <summary>
            /// 输出
            /// </summary>
            /// <returns>按一定的格式返回所有的属性</returns>
            public override string GetValues()
            {
                return string.Format("Inhabitant| ID:{0}| Name:{1}| Birthday: {2}| Degree:{3}",
                    this.ID, this.Name, this.Birthday, this.Degree);
            }
        }
        /// <summary>
        /// 官员类(继承自成人类)
        /// </summary>
        public class Official : Adult
        {
            public string Party { get; set; }//党派
            public string Position { get; set; }//职位
            /// <summary>
            /// 输入
            /// </summary>
            /// <param name="args">参数数组</param>
            public override void SetValues(params object[] args)
            {
                this.ID = args[0].ToString();
                this.Name = args[1].ToString();
                this.Birthday = Convert.ToDateTime(args[2]);
                this.Degree = args[3].ToString();
                this.Party = args[4].ToString();
                this.Position = args[5].ToString();
            }
            /// <summary>
            /// 输出
            /// </summary>
            /// <returns>按一定的格式返回所有的属性</returns>
            public override string GetValues()
            {
                return string.Format("Inhabitant| ID:{0}| Name:{1}| Birthday: {2}| Degree:{3}| Party:{4}| Position:{5}",
                   this.ID, this.Name, this.Birthday, this.Degree, this.Party, this.Position);
            }
        }
        static void Main(string[] args)
        {
            Official oc = new Official();
            oc.ID = "12222121";
            oc.Name ="xiao zhang";
            oc.Party = "gong chan dang";
            oc.Position = "jiangjun";
            oc.Degree = "da xue";
            Console.WriteLine(
                "职位{0}姓名{1}身份证:{2}党派:{3}学历:{4}",
                oc.Position ,oc.Name ,oc.ID ,oc.Party ,oc.Degree );
            Console.ReadKey();
        }
    }
}

十一题

namespace 十一题
{
    /*编写一个类,其中包含一个排序的方法Sort(),
     * 当传入的是一串整数,就按照从小到大的顺序输出,
     * 如果传入的是一个字符串,就将字符串反序输出。

     * 兄弟 你这能对么?!!!
'\0'是什么啊?在ASCII里 就是0
你一上来就ar[0]=0了
你这相当于让ar = ""; 也就是空字符串。后面都没意义了。
我不调试 都估计出你这个str是""空字串了。
感觉兄弟有个概念你没搞清楚。
ar[1] = 1 和 ar[1] = '1'是完全两码事
去搞清楚ASCII表
其实延伸问题就是你1楼的问题跟有没有'\0'没关系
char a1[3] = {1, 2, 3};
char a2[3] = {'1', '2', '3'};
char a3[4] = "123";
这3个意义都不一样。完全不同。要注意最后一个是a3[4]。
a1里存的三个字节是0x01, 0x02, 0x03
a2里存的三个字节是0x31, 0x32, 0x33
a3里存的四个字节是0x31, 0x32, 0x33, 0x00(这就是'\0')
     * 这个才是真正的字符串
a1,a2当字符串处理的话越界了,并且字符串长度不确定。
     * 并且a1是乱码。*/
    class A
    {
        public static int[] Sort(int[] a)
        {
            for (int i = 0; i < a.Length; i++)
            {
                for (int j = i + 1; j < a.Length; j++)
                {
                    if (a[i] > a[j])
                    {
                        int temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
            }
            return a;
        }
        public static String Sort(String a)
        {
            char[] c = a.ToCharArray();
            for (int i = 0; i < c.Length / 2; i++)
            {
                char temp = c[i];
                c[i] = c[c.Length - 1 - i];
                c[c.Length - 1 - i] = temp;
            }
            StringBuilder sb = new StringBuilder("");
            sb.Append(c);
            return sb.ToString();
        }
    };
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[] { 22, 33, 44, 55, 66, 21, 15, 9, 85 };
           A . Sort(a );
            int i = 0;
            // 排序后输出
            Console.WriteLine("排序后的数字为:");
            for (i = 0; i < a.Length; i++)
            {
                Console.WriteLine("{0}", a[i]);
            }
            Console.ReadLine();
        }
    }
}

十二题

namespace 十二题
{
    /*设计一个类,要求用事件每10秒报告机器的当前时间*/
    class Program
    {
        static void time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Console.Clear();
            Console.Write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
        } 
        static void Main(string[] args)
        {
            Console.Write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            System.Timers.Timer time = new System.Timers.Timer();
            time.Interval = 10000;
            time.Elapsed += new System.Timers.ElapsedEventHandler(time_Elapsed);
            time.Start();
            Console.Read(); 
        }
    }
}

十三题

namespace 十三题
{
    class Program
    {
        /*编写一个程序,从键盘上输入3个数,
         * 输出这3个数的积及它们的和。要求
         * 编写成控制台应用程序*/
        static void Main(string[] args)
        {
            Console.Write("输入你的第一个数:");
            string d1 = Console.ReadLine();
            Console.Write("输入你的第二个数:");
            string d2 = Console.ReadLine();
            Console.Write("输入你的第三个数:");
            string d3 = Console.ReadLine();
            Console.WriteLine("三个数的乘积是:{0}", Convert.ToInt32(d1) * Convert.ToInt32(d2) * Convert.ToInt32(d3));
            Console.WriteLine("三个数的和是:{0}", Convert.ToInt32(d1) + Convert.ToInt32(d2) + Convert.ToInt32(d3));
        }
    }
}

十四题

namespace 十四题
{
    /*兔子繁殖问题。设有一对新生的兔子,
     * 从第三个月开始他们每个月都生一对兔子,
     * 新生的兔子从第
    三个月开始又每个月生一对兔子。按此规律,
     * 并假定兔子没有死亡,20个月后共有多少个
     * 兔子?要求编写为控制台程序。
*/
    class Program
    {
        static void Main(string[] args)
        {
                        while (true) 
            { 
                Console.WriteLine("please enter the month ."); 
                int month = int.Parse(Console.ReadLine()); 
                long sum = calculator(month); 
                Console.WriteLine(string.Format("the result is  {0} .",sum.ToString())); 
            }             
 
        } 
        public static long calculator(int month) 
        { 
            if (month == 1 || month == 2) 
            { 
                return 1; 
            } 
            else 
            { 
                return calculator(month - 1) + calculator(month - 2); 
            } 
        } 
        }
    }

十五题

namespace 十五题
{
    /*编写程序,把由10个元素组成的一维数组
     * 逆序存放再输出*/
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            int i, j, temp;
            Console.WriteLine("排序前:");
            for (i = 0; i < 10; i++) Console.Write("{0} ", a[i]);
            Console.WriteLine();
            i = 0;
            j = 9;
            while (i < j)
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
                i++;
                j--;
            }
            Console.WriteLine("排序后:");
            for (i = 0; i < 10; i++) Console.Write("{0} ", a[i]);
            Console.ReadKey();
        }
    }
}

十六题

namespace 十六题
{
    /*编写一个应用程序用来输入的字符串
     * 进行加密,对于字母字符串加密规则如下:

‘a’→’d’      ‘b’→’e’     ‘w’→’z’   ……    ‘x’→’a’    ‘y’→’b’     

‘z’→’c’

‘A’→’B’      ‘B’→’E’     ‘W’→’Z’    ……    ‘X’→’A’    ‘Y’→’B’     

‘Z’→’C’

对于其他字符,不进行加密*/
    class Program
    {
        static string ch
        {
            get;
            set;
        }
        static void Main(string[] args)
        {
            ch= Console.ReadLine();
            int i;
            char t;
            for (i = 0; i < ch.Length; i++)
            {
                if (ch[i] >= 'a' && ch[i] <= 'z')
                {
                    t = (char)(ch[i] + 3);
                    if ((int)t > 122)
                    {
                        t = (char)(t - 26);
                    }
                    Console.Write(t);
                }
                else if (ch[i] >= 'A' && ch[i] <= 'Z')
                {
                    t = (char)(ch[i] + 3);
                    if ((int)t > 90)
                    {
                        t = (char)(t - 26);
                    }
                    Console.Write(t);
                }
                else
                    Console.Write(ch[i]);
            }
        }
    }
}

十七题

namespace 十七题
{
    /*定义一个车辆(Vehicle)基类,具有Run、Stop等方法,
     * 具有Speed(速度)、MaxSpeed(最大速度

)、Weight(重量)等域。然后以该类为基类,
     * 派生出bicycle、car等类。并编程对该派生类的功能进

行验证。*/
    class vehicle
    {
        public int speed
        {
            get;
            set;
        }
        public int maxspeed
        {
            get;
            set;
        }
        public int weight
        {
            get;
            set;
        }
        public void run()
        {
            speed = 60;
            maxspeed = 80;
            Console.WriteLine("vehicle run {0} {1}", speed, maxspeed);
        }
        public void stop()
        {
            speed = 0;
            maxspeed = 80;
            Console.WriteLine("vehicle stop {0} {1}", speed, maxspeed);
        }
    }
    class bacycle : vehicle
    {
        new public void run()
        {
            speed = 30;
            maxspeed = 40;
            Console.WriteLine("bacycle run {0} {1}", speed, maxspeed);
        }
        new public void stop()
        {
            speed = 0;
            maxspeed = 40;
            Console.WriteLine("bacycle stop {0} {1}", speed, maxspeed);
        }
    }
    class car : vehicle
    {
        new public void run()
        {
            speed = 70;
            maxspeed = 90;
            Console.WriteLine("car run {0} {1}", speed, maxspeed);
        }
        new public void stop()
        {
            speed = 0;
            maxspeed = 90;
            Console.WriteLine("car stop {0} {1}", speed, maxspeed);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            car c = new car();
            c.run();
            c.stop();
            vehicle v = c;
            v.run();
            v.stop();
            bacycle b = new bacycle();
            b.run();
            b.stop();
            Console.ReadKey();
        }
    }
}

十八题

namespace 十八题
{
    class person
    {
        public string name
        {
            set;
            get;
        }
        public int age
        {
            get;
            set;
        }
        public string sex
        {
            get;
            set;
        }
    }
    class student : person
    {
        public double[] mystr = new double[5];
        public double this[int nindex]
        {
            get
            {
                return mystr[nindex];
            }
            set
            {
                mystr[nindex] = value;
            }
        }
        public student()
        {
            name = "nobody";
            age = 20;
            sex = "未知";
            for (int i = 0; i < 5; i++)
                mystr[i] = 2 * i + 3;
        }
        public student(string Name)
        {
            name = Name;
            age = 20;
            sex = "未知";
            for (int i = 0; i < 5; i++)
                mystr[i] = 2 * i + 3;
        }
        public student(string Name, int Age)
        {
            name = Name;
            age = Age;
            sex = "未知";
            for (int i = 0; i < 5; i++)
                mystr[i] = 2 * i + 3;
        }
        public student(string Name, int Age, string Sex)
        {
            name = Name;
            age = Age;
            sex = Sex;
            for (int i = 0; i < 5; i++)
                mystr[i] = (2 * i + 3);
        }
        public void calcu()
        {
            Console.WriteLine("此人的信息如下:\n 姓名:{0} 年龄:{1} 性别:{2}", name, age, sex);
            double avg = 0;
            Console.Write("5门课成绩分别是:");
            for (int i = 0; i < 5; i++)
            {
                avg += mystr[i];
                Console.Write("{0} ", mystr[i]);
            }
            avg = avg / 5;
            Console.WriteLine("\n平均成绩是:{0}\n", avg);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            student s1 = new student();
            s1.calcu();
            student s2 = new student("robot");
            s2.calcu();
            student s3 = new student("pkm", 21, "man");
            s3.calcu();
            Console.ReadKey();
        }
    }
}

十九题

namespace 十九题
{
    /* 编写一个冒泡法排序程序,要求在程序
     * 中能够捕获到数组下标越界的异常。*/
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int[] inta = new int[10];
                int i;
                for (i = 0; i <= 9; i++)
                {
                    Console.Write("请输入第{0}个数:", i + 1);
                    inta[i] = int.Parse(Console.ReadLine());

                }
                int temp, j;
                for (i = 0; i < 9; i++)
                {
                    for (j = i + 1; j <= 10; j++)
                        if (inta[j] < inta[i])
                        {
                            temp = inta[i];
                            inta[i] = inta[j];
                            inta[j] = temp;
                        }
                }
                for (i = 0; i <= 9; i++)
                    Console.WriteLine("第{0}个元素:{1}", i, inta[i]);
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}\n", e.Message);
            }
        }
    }
}

二十题

namespace 第二十题
{
    /*编写一个计算器程序,要求在程序
     * 中能够捕获到被0除的异常与算术运算溢出的异常。*/
    class Program
    {
        static void Main(string[] args)
        {
            double a, b, c = 0;
            int t;
            while (true)
            {
                Console.Clear();
                Console.WriteLine("请选择");
                Console.WriteLine("1-加 2-减 3-乘 4-除 5-取模");
                t = int.Parse(Console.ReadLine());
                // Console.WriteLine(t);
                while (t > 5 || t < 1)
                {
                    Console.WriteLine("请输入1~5选择功能!");
                    t = int.Parse(Console.ReadLine());
                }
                Console.Write("请输入a的值:");
                a = Convert.ToDouble(Console.ReadLine());
                Console.Write("请输入b的值:");
                b = Convert.ToDouble(Console.ReadLine());
                switch (t)
                {
                    case 1:
                        c = a + b;
                        Console.WriteLine("a+b的结果为:{0}\n", c);
                        break;
                    case 2:
                        c = a - b;
                        Console.WriteLine("a-b的结果为:{0}\n", c);
                        break;
                    case 3:
                        c = a * b;
                        Console.WriteLine("a*b的结果为:{0}\n", c);
                        break;
                    case 4:
                        try
                        {
                            if (b == 0) throw new DivideByZeroException();
                            c = a / b;
                            Console.WriteLine("a/b的结果为:{0}\n", c);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("{0}\n", e.Message);
                        }
                        break;
                    case 5:
                        try
                        {

                            if (a != (int)a || b != (int)b) throw new ArithmeticException();
                            c = a % b;
                            Console.WriteLine("a%b结果为:{0}\n", c);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("{0}\n", e.Message);
                        }
                        break;
                }
                Console.Write("按任意键继续!");
                Console.ReadKey();
            }
        }
    }
}

 

posted on 2014-06-14 23:30  e心e意  阅读(598)  评论(0)    收藏  举报