xszp.edu.sh.cn:问个关于C++很菜鸟的小程序.同志们都来看看

来源:百度文库 编辑:神马品牌网 时间:2024/04/27 14:40:12
编一个3维求2点距离的小程序.(最好简洁明了)有空的同志帮忙写个,小弟先写过了.

呵,可能是我想复杂了吧,下面代码有40行,远远超过了10行。

#include <iostream>
using namespace std;

class Point3D
{
private:
int x, y, z;
public:
Point3D();
Point3D(int, int, int);

int getX(); int getY(); int getZ();
void setX(int); void setY(int); void setZ(int);
};
Point3D::Point3D()
{ this->x = 0; this->y = 0; this->z = 0;}
Point3D::Point3D(int x, int y, int z)
{ this->x = x; this->y = y; this->z = z;}
int Point3D::getX() { return this->x;}
int Point3D::getY() { return this->y;}
int Point3D::getZ() { return this->z;}
void Point3D::setX(int x) { this->x = x;}
void Point3D::setY(int y) { this->y = y;}
void Point3D::setZ(int z) { this->z = z;}

float getDistance(Point3D a, Point3D b)
{
float x, y, z;
x = a.getX() - b.getX();
y = a.getY() - b.getY();
z = a.getZ() - b.getZ();
return sqrt(x * x + y * y + z * z);
}

int main(int argc, char *argv[])
{
Point3D pa;
Point3D pb(0, 0, 3);

cout << "Distance = " << getDistance(pa, pb) << endl;
system("Pause");
return 0;
}

服了你了,把高中数学书拿出来看看,里面有算法的公式,然后把变量定义了,80%不就完了,用不了10行代码就可以搞定的