如何在结构体内部实现指针

How to implement pointers inside a struct?

本文关键字:实现 指针 内部 结构体      更新时间:2023-10-16

我是全新的c++(和编程),我试图实现一个"点"结构与以下访问器函数:

void setData(struct Point*, int u, int v);
void getRadius(struct Point*);

程序应该:

  1. 创建点P1在X=3, Y=4
  2. 输出P1的半径

使用这些指南:

struct Point A;   // instantiates a point at A based on the struct Point
A_ptr = &A;
setData(A_ptr, 3, 4);   // initializes the point A(3,4)
int Radius = getRadius(A_ptr);   // calculates R and returns the value to Radius
下面是我的代码(显然是错误的):
#include "stdafx.h"
#include <iostream>
#include <cmath>;
using namespace std;
// the equation of a circle is:
// (x-a)^2 + (y-b)^2 = r^2, where (a,b) are the coordinates of the center
struct Point
{
    int X;    // x position
    int Y;    // y position
    float R;  // radius of circle
    void setData(struct Point*, int u, int v);
    float getRadius(struct Point*);
    void inputCoordinates(int X, int Y);
};

int main()
{
    struct Point A;   // instantiates a point at A based on the struct Point
    A_ptr = &A;
    setData(A_ptr, 3, 4);   // initializes the point A(3,4)
    int Radius = getRadius(A_ptr);   // calculates R and returns the value to Radius
    cout << "The radius of a circle with points x = " << X << " and Y = " << Y << " is: " << Radius;

}

    return 0;
}
void Point::setData(struct Point*, int u, int v)
{
    X = u;
    Y = v;
}
float Point::getRadius(struct Point*)
{
    return sqrt(pow(X,2) + pow(Y,2));
}

问题本身很简单。我似乎无法将问题(和问题参数)"映射"到指针和结构的概念(我已经读到过,但对我来说仍然是模糊的)。

任何建议和指导都是非常感谢的。

提前谢谢你看一看。——瑞安

这是我在c++风格中的做法

#include <iostream>
#include <cmath>
using namespace std;
// the equation of a circle is:
// (x-a)^2 + (y-b)^2 = r^2, where (a,b) are the coordinates of the center
struct Point
{
    int X;    // x position
    int Y;    // y position 
    void setData(int u, int v);
    float getRadius();
    void inputCoordinates(int X, int Y);
};

int main()
{
    Point A;   // instantiates a point at A based on the struct Point
    A.setData(3, 4);   // initializes the point A(3,4)
    float Radius = A.getRadius();   // calculates R and returns the value to Radius
    cout << "The radius of a circle with points x = " << A.X << " and Y = " << A.Y << " is: " << Radius;
    return 0;

}
void Point::setData(int u, int v)
{
    X = u;
    Y = v;
}
float Point::getRadius()
{
    float value;
    value = (float)sqrt(pow(X, 2) + pow(Y, 2));
    return value;
}

在最后两个函数setDatagetRadius中,您从不使用Point参数。

改为:

void setData(Point* p, int u, int v)
{
    p->X = u;
    p->Y = v;
}  

float getRadius(Point* p)
{
    return sqrt(pow(p->X,2) + pow(p->Y,2));
}  

也就是说,不要让它们成为Point的成员函数。

你的观点::setData需要读取:

void Point::setData(struct Point*, int u, int v)
{
    point->X = u;
    point->Y = v;
}

,因为->是指针的成员访问器(相当于。对于非指针变量)

或者如果你在用C语言编程,让它成为一个函数,而不是一个方法:

void setData(struct Point*, int u, int v)
{
    point->X = u;
    point->Y = v;
}

我强烈建议您使用float或double作为X和Y坐标的数据类型。

你的半径不应该是结构体的成员。它在概念上不是点的一部分,而是描述两点之间的距离。

为getRadius()参数指定一个名称,例如"p"。那么getRadius()方法的主体可以简单地为

{
  float dx = X - p->x;
  float dy = Y - p->y;
  return sqrt(dx * dx + dy * dy); // Pythagorean formula
}

我希望这能给你正确的方向。