.NET实验7-1

    把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类Cline,再派生出一个矩形类CRect。要求成员函数能求出两点间的距离、矩形的周长和面积等。设计一个测试程序,并构造完整的程序。

  提示:
           (1) 可能用到的函数:Math类中的函数

           (2) 注意数据类型的转换。

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

namespace ConsoleApplication1
{
    class Cpoint
    {
        protected double x;
        protected double y;
        public void set(double a, double b)
        { 
            x = a;
            y = b;
        }
        public double getx()
        {
            return x;
        }
        public double gety()
        {
            return y;
        }
    }
    class Cline : Cpoint//直线类
    {
        public double ShowLength(Cpoint p1,Cpoint 

p2)//求两点间距离
        {
            return Math.Sqrt((p1.getx() - p2.getx()) 

* (p1.getx() - p2.getx()) + (p1.gety() - p2.gety()) 

* (p1.gety() - p2.gety()));
        }
    }
    class CRect : Cpoint//矩形类
    {
        public double showdecline(Cpoint p1, Cpoint 

p2)//求矩形周长
        {
            return 2*(Math.Abs(p1.getx-p2.getx

())+Math.Abs(p1.gety()-p2.gety()));
        }
        public double showarea(Cpoint p1, Cpoint 

p2)//求矩形面积
        {
            return (Math.Abs(p1.getx() - p2.getx()) 

* Math.Abs(p1.gety() - p2.gety()));
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int[] point = new int[4];
            Console.WriteLine("输入两个点:");
            for (int i = 0; i < 4; i++)
            {
                point[i] = int.Parse

(Console.ReadLine());
            }
            Cpoint c1 = new Cpoint();
            c1.set(point[0], point[1]);
            Cpoint c2 = new Cpoint();
            c2.set(point[2], point[3]);
            Cline c1 = new Cline();
            double length = c1.ShowLength(c1,c2);
            Console.WriteLine("该两点之间的长度为:

{0}",length);
            CRect cr = new CRect();
            Console.WriteLine("以该两点为对角线所组

成的矩形周长:{0},面积:{1}", cr.showarea(c1, c2), 

cr.showlength(c1, c2));
            Console.ReadKey();
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值