C#怎样编写Point类
3个回答
展开全部
思路如下:
(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();
}
(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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有现成的啊.........................................
或者:
Class Point{
public float x;
public float y;
public Point()
{
}
public Point(float x,float y)
{
this.x =x;
this.y=y;
}
}
或者:
Class Point{
public float x;
public float y;
public Point()
{
}
public Point(float x,float y)
{
this.x =x;
this.y=y;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
基本就是下面这样实现的
话说原来就有这个类你写他干什么···
[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();
}
}
话说原来就有这个类你写他干什么···
[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();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询