• 全部
• 问答

# 难题再发，请强人指导！！大家多多讨论，谢谢了！！！

jjduan185 2005-04-19 08:58:36

typedef struct coordinate
{
double x_cd;
double y_cd;
}* Point;/*Using a struct with two members 'x_cd' and 'y_cd' to descripe a point */
struct Line
{
coordinate StartPoint;
coordinate EndPoint;
};
#define MAXSIZE 10000/* Maximum number of points*/
#define MAXDISTANCE 100000/*maximum distance between points */
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
Point Read_data_from(char* filename);/*read coordinate of points from finame and store it in
array PointsArray ,then return the address of the array*/
double Min_dis(Point PointsArray,coordinate KeyPoint);/*Calculate the distance between point 'Keypoint' and the other points,
then return the index of the point who is the nearest point to KeyPoint,meanwhile store the minimum diatance
in the first position of the array as PointsArray[0].y_cd */
int main(int argc,char*argv[])
{
coordinate KeyPoint;
coordinate* Points=Read_data_from("data.txt");/*read data from file and store it in the array */
int size=(Points[0].x_cd--);/*Get the size of Point array */
cout<<Min_dis(Points,KeyPoint)<<endl;
cout<<Points[0].y_cd<<endl;
return 0;
}
/***********************************************************************************************************/
{
{
cout<<"Error,file not exist\n";
exit(1);
}/*If the file can not be opened ,end the programe*/
int counter(0);/*To count the number of points read in*/
Point PointsArray=new coordinate[MAXSIZE];
counter++;
PointsArray[0].x_cd=counter;/*We use the first position to store the number of points */
return PointsArray;
}
/*************************************************************************************************************/
double Min_dis(Point PointsArray,coordinate KeyPoint)
{
const length=PointsArray[0].x_cd;/*Get the size of array*/
double x=KeyPoint.x_cd,y=KeyPoint.y_cd;
int index(0);/* the number of the nearest point */
double MinDistance(MAXDISTANCE);/*initailize the minmun distance with a big number */
for(int i=1;i<=length;i++)/*Loop to calculate every distance*/
{
double temX=PointsArray[i].x_cd;
double temY=PointsArray[i].y_cd;
double dis=sqrt((x-temX)*(x-temX)+(y-temY)*(y-temY));
MinDistance= MinDistance > dis ? dis : MinDistance;/*if new distance is small than MinDistance,change
MinDistance with new distance 'dis',and then change the index of the nearest point */
index= MinDistance >= dis ? i:index;
}
PointsArray[0].y_cd=index;
return MinDistance;/*store the index of the nearest point as PointsArray[0].y_cd*/
}

...全文
173 点赞 收藏 6

6 条回复

jjduan185 2005-04-27

duanjigang1983@126.com

jjduan185 2005-04-27
liqiuhua123(程序人生) :

daidodo 2005-04-23

liqiuhua123 2005-04-23

xjp6688 2005-04-23

CgPanda_ 2005-04-23
up

C++ 语言

5.9w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等

2005-04-19 08:58