C#之类的设计:点和三角形类的设计

描述点的类Point

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

namespace ClassStudy2
{
    class Point
    {
        private int x;
        private int y;
        private int z;

        public int X 
        {
            get { return this.x; }
            set { this.x = value; }
        }
        public int Y 
        {
            get { return this.y; }
            set { this.y = value; }
        }

        public Point()
        {
        }

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public Point(int z)
            : this(z, z)
        {
        }

        public double DistanceFromOrgin()
        {
            return Math.Sqrt(x * x + y * y);
        }

        public double DistanceFromPoint(Point p)
        {
            return Math.Sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
        }

        public static double DistanceFromPoint(Point p1, Point p2)
        {
            return p1.DistanceFromPoint(p2);
        }

        public override string ToString()
        {
            return "[" + x + "," + y + "]";
        }
    }
}

描述三角形的类Triangle

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

namespace ClassStudy2
{
    class Triangle
    {
        private Point p1;
        private Point p2;
        private Point p3;

        public Point P1 
        {
            get { return this.p1; }
            set { this.p1 = value; }
        }
        public Point P2
        {
            get { return this.p2; }
            set { this.p2 = value; }
        }
        public Point P3
        {
            get { return this.p3; }
            set { this.p3 = value; }
        }

        public Triangle()
        {
        }

        public Triangle(Point p1, Point p2, Point p3)
        {
            double len12 = p1.DistanceFromPoint(p2);
            double len13 = p1.DistanceFromPoint(p3);
            double len23 = p2.DistanceFromPoint(p3);

            if (len12 + len13 <= len23 || len12 + len23 <= len13 || len13 + len23 <= len12)
            {
                Console.WriteLine(p1 + "," + p2 + "," + p3 + "这三点无法构成三角形,构造三角形失败!");
            }
            else
            {
                this.p1 = p1;
                this.p2 = p2;
                this.p3 = p3;
            }
        }

        public double GetGirth()
        {
            double len12 = p1.DistanceFromPoint(p2);
            double len13 = p1.DistanceFromPoint(p3);
            double len23 = p2.DistanceFromPoint(p3);

            return (len13 + len23 + len12); 
        }
        public double GetArea()
        {
            //海伦公式
            double len12 = p1.DistanceFromPoint(p2);
            double len13 = p1.DistanceFromPoint(p3);
            double len23 = p2.DistanceFromPoint(p3);

            double p = (len13 + len23 + len12)/2;
            return Math.Sqrt(p * (p - len12) * (p - len13) * (p - len23));
        }

        public override string ToString()
        {
            return ("此三角形的三个点为"+ p1 + ","+ p2 + "," + p3);
        }
    }
}

调用的类Program

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

namespace ClassStudy2
{
    class Program
    {
        static void Main(string[] args)
        {
            Point p1 = new Point();
            Console.WriteLine(p1);

            Point p2 = new Point(-3, 15);
            Console.WriteLine(p2);

            Point p3 = new Point();
            p3.X = 4;
            p3.Y = 7;
            //4.0后的新语法:构造函数调用时初始化,Point2D p3 = new Point2D{X=4, Y=7};注意要和函数的命名调用区分开来,如果这里是命名调用应该是Point3D p3 = new Point2D(x:4,y:7);

            Console.WriteLine(p3.X + "," + p3.Y);

            Point p4 = new Point(6);
            Console.WriteLine(p4);

            double dis1 = Point.DistanceFromPoint(p2, p3);
            double dis2 = p2.DistanceFromPoint(p3);
            double dis3 = p2.DistanceFromOrgin();

            Console.WriteLine(p2 + "到" + p3 + "的距离为" + dis1);
            Console.WriteLine(p2 + "到" + p3 + "的距离为" + dis2);
            Console.WriteLine(p2 + "到原点的距离为" + dis3);

            //下面是三角形的验证代码,作为作业
            Triangle t = new Triangle(p1, p2, p3);
            Console.WriteLine(t + ",面积为" + t.GetArea() + ",周长为" + t.GetGirth());
            Console.ReadLine();

        }
    }
}





  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值