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

6.1

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

namespace c6._1
{
    public struct myDate
    {
        public int year;
        public int month;
        public int day;
        public myDate(int y, int m, int d)
        {
            //必须全部赋值
            year = y;
            month = m;
            day = d;
        }
        public void DisplayWeek()
        {
            Console.WriteLine("星期为:{0}", new DateTime(year, month, day).DayOfWeek);
        }
        public void DisplayData()
        {
            Console.WriteLine("日期为:{0}/{1}/{2}",year,month,day);
        }

    
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入年:");
            int y = int.Parse(Console.ReadLine());
            Console.Write("请输入月:");
            int m= int.Parse(Console.ReadLine());
            Console.Write("请输入日:");
            int d = int.Parse(Console.ReadLine());

            myDate d1 = new myDate(y, m, d);
            d1.DisplayData();
            d1.DisplayWeek();

            Console.ReadKey();
        }
    }
}
6.2

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

namespace c6._2
{
    public struct StudentGrade {
        public string name;
        public double score;
        public StudentGrade(string n, double s)
        {
            name = n;
            score = s;
        }
        public void Display() {
            Console.WriteLine("名字:{0} ,成绩:{1}", name, score);
        }
    
    }

    class Program
    {
        static void Main(string[] args)
        {
            //结构体初始化列表
            StudentGrade[] stu =
            {
                new StudentGrade("张三",100),
                new StudentGrade("李四",50),
                new StudentGrade("王五",40),
                new StudentGrade("赵六",60)
            };
            double sum = 0;
            foreach(StudentGrade s in stu)
            {
                s.Display();
                sum += s.score;
            }
            Console.WriteLine("平均分:{0}", sum / stu.Length);
            Console.ReadKey();


        }
    }
}

6.3

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

namespace c6._3
{
    public struct Point { 
        public int x;
        public int y;
        public Point(int a,int b)
        {
            x=a;
            y=b;
        }
    }
    /*
    public struct Triangle
    {
        Point a;
        Point b;
        Point c;
        public Triangle(Point x,Point y,Point z){
            a = x;
            b = y;
            c = z;
            
        }
    
    }
    */

    class Program
    {
        static void Main(string[] args)
        {
            Point a = new Point();
            Console.WriteLine("平面坐标点1:x={0},y={1}", a.x, a.y);
            Point b = new Point(10, 12);
            Console.WriteLine("平面坐标点2:x={0},y={1}", b.x, b.y);
            Point c;
            c.x = 22;
            c.y=3;
            Console.WriteLine("平面坐标点3:x={0},y={1}", c.x, c.y);

            double side1 = Math.Sqrt(Math.Pow(a.x - b.x, 2) + Math.Pow(a.y - b.y, 2));
            double side2 = Math.Sqrt(Math.Pow(a.x - c.x, 2) + Math.Pow(a.y - c.y, 2));
            double side3 = Math.Sqrt(Math.Pow(b.x - c.x, 2) + Math.Pow(b.y - c.y, 2));
            if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1)
            {
                double h = (side1 + side2 + side3) / 2;
                double area = Math.Sqrt(h * (h - side1) * (h - side2) * (h - side3));
                Console.WriteLine("三角形的面积:{0}", area);

            }
            else//两点重叠才不能构成三角形
                Console.WriteLine("不能构成三角形");

            Console.ReadKey();
        }
    }
}

6.4

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

namespace c6._4
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("前景色为Cyan");
            Console.ResetColor();
            Console.WriteLine("重置前景色");
            Console.BackgroundColor = ConsoleColor.Red;
            Console.WriteLine("背景色");
            Console.ReadKey();

        }
    }
}

6.5

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

namespace c6._5
{
    class Program
    {
        //指明从1开始
        enum Days { Monday = 1, Tuesday, Wednesday, Thursday, Firday = 5, Saturday, Sunday };

        static void Main(string[] args)
        {
            Console.WriteLine("输入一个数字:1--7");
            while (true)
            {
                try
                {
                    int d = int.Parse(Console.ReadLine());
                    if (d > 7 || d < 1)
                        Console.WriteLine("输入的数字不对");
                    else
                        Console.WriteLine("{0}对应于{1}", d, (Days)d);
                }
                catch (FormatException e)
                {
                    Console.WriteLine(e.Message);
                    break;
                }
            }
            Console.ReadKey();
        }
    }
}

6.6

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

namespace c6._6
{
    class Program
    {
        enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Firday };
        [FlagsAttribute]//指示枚举作为一组位域处理
            //枚举值是有限制的
        enum Colors { Red = 1, Green = 2, Blue = 8, Yello = 16 };
        static void Main(string[] args)
        {
            //typeof用于获取类型
            Type weekdays = typeof(Days);
            Console.WriteLine("一周七天----对应枚举类型Days中的值:");
            //Enum.GetNames检索指定枚举类型中常数名称的数组
            foreach (string s in Enum.GetNames(weekdays))//真是神奇的c#啊
                Console.WriteLine("{0,-11}={1}", s, Enum.Format(weekdays, Enum.Parse(weekdays, s), "d"));
            Colors mcolor = Colors.Red | Colors.Blue | Colors.Yello;
            Console.WriteLine("\n枚举变量mcolors存放如下颜色组合:{0}", mcolor);
            Console.ReadKey();
        }

    }
}

6.7

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

namespace c6._7
{
    class Program
    {
        enum Colors {Red,Blue,White,Black};
        static void Main(string[] args)
        {
            Console.WriteLine("4种颜色的球中取出3个不同色的球所有取法:");
            int count = 0;
            for(int i=0;i<4;i++)
                for(int j=0;j<4;j++)
                    for (int k = 0; k < 4; k++)
                    {
                        if (i != j && i != k && k != j)
                        {
                            Console.WriteLine("{0}\t{1}\t{2}\t", (Colors)i, (Colors)j, (Colors)k);
                            count++;
                        }
                    }
            Console.WriteLine("一共有{0}取法",count);
            Console.ReadKey();
            
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值