C#怎样编写Point类

 我来答
糖糖寳寳
推荐于2016-06-30 · TA获得超过6.4万个赞
知道大有可为答主
回答量:1.8万
采纳率:92%
帮助的人:3970万
展开全部
思路如下:
(1)生命含有10个point的数组(要用new,否则是栈对象而不是堆对象)
(2)顺序为10个对象赋值
(3)顺序显示10个对象
(4)这个是纯数学问题。
(5)delete掉十个对象
(6)可以为每个对象命名来进行演示,在析构函数中输出信息。
代码如下:
#include <string>
#include <conio.h>
using namespace std;
#define MAX_COUNT 2//如果是10个点,把这里改成10
class Point{
private:
public:
double _x, _y;
char _name[10];
Point() { _x = _y = 0; strcpy(_name, ""); }
Point(char *n, int x, int y){
_x = x; _y = y; strcpy(_name, n);
}
~Point(){
printf("Point '%s' with position {%lf, %lf} is being destroyed...\n", _name, _x, _y);
}
};

void Set(Point *pts){
for(int index = 0; index < MAX_COUNT; index++){
scanf("%lf%lf%s", &(pts[index]._x), &(pts[index]._y), pts[index]._name);
}
}

void Display(Point *pts){
for(int index = 0; index < MAX_COUNT; index++){
printf("0x%08X ==>> %s, {%lf, %lf}\n",
pts + index, pts[index]._name, pts[index]._x, pts[index]._y);
}
}

void Calculate(Point *pts){
double lineLength = .0;
for(int index = MAX_COUNT - 1; index >= 1; index--){
lineLength += sqrt(pow(pts[index - 1]._x - pts[index]._x, 2) + pow(pts[index - 1]._y - pts[index]._y, 2));
}
printf("The length for this line is %lf\n", lineLength);
}
void main(){
Point *pts = new Point[MAX_COUNT];
Set(pts);
Display(pts);
Calculate(pts);
delete []pts;
getch();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
lcg1986
2009-03-27 · TA获得超过3374个赞
知道大有可为答主
回答量:1858
采纳率:90%
帮助的人:1620万
展开全部
有现成的啊.........................................
或者:
Class Point{
public float x;
public float y;
public Point()
{

}

public Point(float x,float y)
{
this.x =x;
this.y=y;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
定复2d
推荐于2016-05-26 · TA获得超过3196个赞
知道大有可为答主
回答量:2052
采纳率:0%
帮助的人:1935万
展开全部
基本就是下面这样实现的
话说原来就有这个类你写他干什么···

[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true), TypeConverter(typeof(PointConverter))]
public struct Point
{
public static readonly Point Empty;
private int x;
private int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}

public Point(Size sz)
{
this.x = sz.Width;
this.y = sz.Height;
}

public Point(int dw)
{
this.x = (short) LOWORD(dw);
this.y = (short) HIWORD(dw);
}

[Browsable(false)]
public bool IsEmpty
{
get
{
return ((this.x == 0) && (this.y == 0));
}
}
public int X
{
get
{
return this.x;
}
set
{
this.x = value;
}
}
public int Y
{
get
{
return this.y;
}
set
{
this.y = value;
}
}
public static implicit operator PointF(Point p)
{
return new PointF((float) p.X, (float) p.Y);
}

public static explicit operator Size(Point p)
{
return new Size(p.X, p.Y);
}

public static Point operator +(Point pt, Size sz)
{
return Add(pt, sz);
}

public static Point operator -(Point pt, Size sz)
{
return Subtract(pt, sz);
}

public static bool operator ==(Point left, Point right)
{
return ((left.X == right.X) && (left.Y == right.Y));
}

public static bool operator !=(Point left, Point right)
{
return !(left == right);
}

public static Point Add(Point pt, Size sz)
{
return new Point(pt.X + sz.Width, pt.Y + sz.Height);
}

public static Point Subtract(Point pt, Size sz)
{
return new Point(pt.X - sz.Width, pt.Y - sz.Height);
}

public static Point Ceiling(PointF value)
{
return new Point((int) Math.Ceiling((double) value.X), (int) Math.Ceiling((double) value.Y));
}

public static Point Truncate(PointF value)
{
return new Point((int) value.X, (int) value.Y);
}

public static Point Round(PointF value)
{
return new Point((int) Math.Round((double) value.X), (int) Math.Round((double) value.Y));
}

public override bool Equals(object obj)
{
if (!(obj is Point))
{
return false;
}
Point point = (Point) obj;
return ((point.X == this.X) && (point.Y == this.Y));
}

public override int GetHashCode()
{
return (this.x ^ this.y);
}

public void Offset(int dx, int dy)
{
this.X += dx;
this.Y += dy;
}

public void Offset(Point p)
{
this.Offset(p.X, p.Y);
}

public override string ToString()
{
return ("{X=" + this.X.ToString(CultureInfo.CurrentCulture) + ",Y=" + this.Y.ToString(CultureInfo.CurrentCulture) + "}");
}

private static int HIWORD(int n)
{
return ((n >> 0x10) & 0xffff);
}

private static int LOWORD(int n)
{
return (n & 0xffff);
}

static Point()
{
Empty = new Point();
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式