站点图标 陌路寒暄

点线面

点线面

时间: 1ms        内存:128M

描述:

定义一个点的类CPoint作为基类。派生出一条直线的类Cline,并定一个方法(Dis)可求出两点间的距离。请在此基础上再派生出一个矩形类CRect。要求成员函数能计算矩形的周长(Getlength)和面积(Getarea)等。构造完整的程序并设计一个测试程序进行测试。测试程序中调用类中相应方法,验证各方法的正确性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
//



//



    class Program
    {
        static void Main(string[] args)
        {
            CPoint p1 = new CPoint();
            CPoint p4 = new CPoint();
int i;
int num= Convert.ToInt32(Console.ReadLine());
int num2= Convert.ToInt32(Console.ReadLine());
// Console.WriteLine(num);
            p1.setPoint(num,num);
            p4.setPoint(num2,num2);
           CRect cr=new CRect();
           
            Cline cl1 = new Cline();
            
            Console.WriteLine("p1p2={0}",cl1.Dis(p1,p4));
            Console.WriteLine("Perimeter and area={0},{1}", cr.Getlength(p1, p4), cr.Getarea(p1, p4));
            Console.ReadKey();
 
        }
    }
}



输入:

两个int,num和num2.构成两个坐标p1(num,num)p2(num2,num2).

输出:

两行
第一行为p1 p2 的距离,
第二行为矩形的周长和面积。

示例输入:

1
2

示例输出:

p1p2=1.4142135623731
Perimeter and area=4,1

提示:

参考答案(内存最优[0]):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
     class CPoint
    {
        public double x;
        public double y;
        public double GetX() { return x; }
        public double GetY() { return y; }     
        public void setPoint(int m,int n){x=m;y=n;}
 
    }
    class Cline : CPoint
    {
        public double Dis(CPoint P1,CPoint P2)
        {
            double dis;
            dis = Math.Sqrt((P1.GetX() - P2.GetX()) * (P1.GetX() - P2.GetX()) + (P1.GetY() - P2.GetY()) * (P1.GetY() - P2.GetY()));
            return dis;
        }
    }
    class CRect : Cline
    {
      public  CRect() { }
        double length=0, area=0;
        public double Getlength(CPoint p1,CPoint p4)
        {
            return length = 2 * (Math.Abs((p1.GetY() - p4.GetY())) + Math.Abs((p4.GetX() - p1.GetY())));
        }
        public double Getarea(CPoint p1,CPoint p4)
        {
            return area = Math.Abs(p1.GetY() - p4.GetY()) * Math.Abs(p4.GetX() - p1.GetY());
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("p1=(1,1),p4=(2,2)");
            CPoint p1 = new CPoint();
            CPoint p4 = new CPoint();
			int i;
			int num= Convert.ToInt32(Console.ReadLine());
			// Console.WriteLine(num);
            p1.setPoint(1,1);
            p4.setPoint(2,2);
           CRect cr=new CRect();
           
            Cline cl1 = new Cline();
            
            Console.WriteLine("p1p2={0}",cl1.Dis(p1,p4));
            Console.WriteLine("矩形的周长为{0},面积为{1}", cr.Getlength(p1, p4), cr.Getarea(p1, p4));
            Console.ReadKey();
 
        }
    }
}

参考答案(时间最优[0]):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
     class CPoint
    {
        public double x;
        public double y;
        public double GetX() { return x; }
        public double GetY() { return y; }     
        public void setPoint(int m,int n){x=m;y=n;}
 
    }
    class Cline : CPoint
    {
        public double Dis(CPoint P1,CPoint P2)
        {
            double dis;
            dis = Math.Sqrt((P1.GetX() - P2.GetX()) * (P1.GetX() - P2.GetX()) + (P1.GetY() - P2.GetY()) * (P1.GetY() - P2.GetY()));
            return dis;
        }
    }
    class CRect : Cline
    {
      public  CRect() { }
        double length=0, area=0;
        public double Getlength(CPoint p1,CPoint p4)
        {
            return length = 2 * (Math.Abs((p1.GetY() - p4.GetY())) + Math.Abs((p4.GetX() - p1.GetY())));
        }
        public double Getarea(CPoint p1,CPoint p4)
        {
            return area = Math.Abs(p1.GetY() - p4.GetY()) * Math.Abs(p4.GetX() - p1.GetY());
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("p1=(1,1),p4=(2,2)");
            CPoint p1 = new CPoint();
            CPoint p4 = new CPoint();
			int i;
			int num= Convert.ToInt32(Console.ReadLine());
			// Console.WriteLine(num);
            p1.setPoint(1,1);
            p4.setPoint(2,2);
           CRect cr=new CRect();
           
            Cline cl1 = new Cline();
            
            Console.WriteLine("p1p2={0}",cl1.Dis(p1,p4));
            Console.WriteLine("矩形的周长为{0},面积为{1}", cr.Getlength(p1, p4), cr.Getarea(p1, p4));
            Console.ReadKey();
 
        }
    }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

退出移动版