C# 实验二

C# 实验

一、实验目的:

熟悉C#的面向对象程序设计方法,掌握类、方法、属性和对象的定义与实现方法

二、实验内容:

创建控制台应用程序,根据要求使用面向对象程序设计方法,编译和运行程序。

一、 定义一个“学生”类,继承“人”类,并增加班级、总分数据域,以及判断是否合格的函数,合格的标准是:年龄在16-20岁之间,总分大于480分,或者年龄在21-30之间,总分大于450分。

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

namespace ClassLibrary1
{
    public class Class1
    {
        static void Main(string[] args)
        {
            //测试学生小明
            student xiaoming = new student();
            xiaoming.Name = "小明";
            xiaoming.Age = 18;
            xiaoming.Classroom = 1;
            xiaoming.Sum = 430;
            xiaoming.message();
            Console.WriteLine(",在{0}班,总分为{1}",xiaoming.Classroom,xiaoming.Sum 
                );
            xiaoming.quiz();
            Console.ReadLine();
        }
    }
    class people
    {
        string name;
        int age;
        public void message()
        {

            Console.Write(Name + "的年龄为" + Age + "岁");

        }

        public string Name { get => name; set => name = value; }
        public int Age { get => age; set => age = value; }
    }
    class student:people
    {
        int classroom;
        float sum;
        public void quiz()
        {
            if ((Age >= 16 && Age <= 20 && Sum > 480) || (Age >= 21 && Age <= 30 && Sum > 450))
                Console.WriteLine("合格");
            else
                Console.WriteLine("不合格");
        }

        public int Classroom { get => classroom; set => classroom = value; }
        public float Sum { get => sum; set => sum = value; }
    }
 }

在这里插入图片描述

二、

(1)定义一个“四边形”类,包含4个顶点坐标,以及定义判断这4个顶点是否构成四边形的函数,并计算其面积。
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp13
{
    class Program
    {
        static void Main(string[] args)
        {
            SBX sbx = new SBX();
            Console.Write("请输入第1个点x:");
            sbx.x1 = Convert.ToInt16(Console.ReadLine());
            Console.Write("请输入第1个点y:"); sbx.y1 = Convert.ToInt16(Console.ReadLine());
            Console.Write("请输入第2个点x:"); sbx.x2 = Convert.ToInt16(Console.ReadLine());
            Console.Write("请输入第2个点y:"); sbx.y2 = Convert.ToInt16(Console.ReadLine());
            Console.Write("请输入第3个点x:"); sbx.x3 = Convert.ToInt16(Console.ReadLine());
            Console.Write("请输入第3个点y:"); sbx.y3 = Convert.ToInt16(Console.ReadLine());
            Console.Write("请输入第4个点x:"); sbx.x4 = Convert.ToInt16(Console.ReadLine());
            Console.Write("请输入第4个点y:"); sbx.y4 = Convert.ToInt16(Console.ReadLine());
            if (sbx.IsSBX())
            {
                Console.WriteLine("该四边形的面积是{0}", sbx.MJ());
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("不能构成四边形!");
                Console.ReadLine();
            }
        }
    }
    class SBX
    {
        public double x1, x2, x3, x4, y1, y2, y3, y4;
        public bool IsSBX()
        {

            if (((y3 - y4) * (x3 - x1) == (x3 - x4) * (y3 - y1)) ||
                ((y3 - y2) * (x3 - x4) == (x3 - x2) * (y3 - y4)) ||
                ((y4 - y2) * (x4 - x1) == (x4 - x2) * (y4 - y1)) ||
                ((y3 - y2) * (x3 - x1) == (x3 - x2) * (y3 - y1)))
                return false;//任意三个顶点成直线,非四边形
            else
                return true;

        }
        public double MJ()
        {
            return Math.Abs(x1 * y2 + x2 * y3 + x3 * y1 - y1 * x2 - y2 * x3 - y3 * x1) / 2.0 +
                 Math.Abs(x4 * y2 + x2 * y3 + x3 * y4 - y4 * x2 - y4 * x3 - y3 * x4) / 2.0;
            
        }
    }
}
(2)继承“四边形”类,定义“平行四边形”类,增加判断是否为平行四边形的函数。
class PXSBX:SBX
    {
        public bool ISPXSBX()
        {
            double a, b, c, d;
            a = Math.Pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5);
            b = Math.Pow((x4 - x3) * (x4 - x3) + (y4 - y3) * (y4 - y3), 0.5);
            c = Math.Pow((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2), 0.5);
            a = Math.Pow((x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1), 0.5);
            if ((a == b) || (c == d))
                return true;
            else
                return false;
        }
    }
(3) “平行四边形”类,定义“矩形”类,增加判断是否为矩形的函数。
class JX : PXSBX
    {
        public bool ISJX()
        {
            double A, B, C, D, E;
            A = Math.Pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5);
            B = Math.Pow((x4 - x3) * (x4 - x3) + (y4 - y3) * (y4 - y3), 0.5);
            C = Math.Pow((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2), 0.5);
            D = Math.Pow((x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1), 0.5);
            E = (x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1);
            if ((E == C*C+ A * A) || (E == D*D + B*B))
                return true;
            else
                return false;
        }
    }

结果图:
在这里插入图片描述

以上。

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验 C#程序设计练习 一、实验目的 1.掌握C#语言的基本语法、控制语句及异常处理。 2.掌握C#的基本使用方法以及C#语言面向对象的基本特性。 实验内容 1.编写一个函数,用于计算1!+2!+3!+4!+5!,在控制台或页面输出运行结果。 2.在控制台或页面输出九九乘法表。 3.输入10个以内的整数,输出该组整数的降序排列,要求采用数组实现。 4.计算两个数的商,在控制台或页面输出结果,要求包含异常处理。 5.定义一个汽车,该具有重量和速度属性;再定义一个跑车,该继承汽车的属性,并拥有自己的颜色属性;然后声明一个汽车的对象和一个跑车的对象,并把它们的属性输出到控制台上。 6.假设某动物园管理员每天需要给他所负责饲养的狮子、猴子和鸽子喂食。请用一个程序来模拟他喂食的过程。 要求: (1)饲养员喂食时,不同动物执行不同的吃的功能,例如狮子吃肉、猴子吃香蕉、鸽子吃大米等。 (2)饲养员喂动物时,不能使用判断语句判断动物型。 (3)使用虚方法或抽象方法实现喂养不同动物的多态,不能使用方法重载。 提示:需要建一个动物,动物有一个虚的或抽象的吃方法,动物下面有几个子,不同的子重写父的吃方法。饲养员提供喂食方法。然后,在Main方法中一一调用吃的方法。 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Add2._1 { class Program { static void Main(string[] args) { int sum = 0; for (int i = 1; i < 6; i++) { int tmp = 1; for (int j = 1; j <= i; j++) { tmp = tmp * j; } sum += tmp; } Console.WriteLine("1!+2!+3!+4!+5!={0}\r\n", sum.ToString()); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值