计算平面中点间距离
/* 理论是可以直接数组做的 然后呢现在复习呀…………………… 用小demo复习c++ 下面的代码达到以下几点 1.N=9好理解 好展示 2.N为一个大数时,可以用于测 3.注意没有写入文件 可以直接用cmd 重定向exe 到txt */ //http://blog.csdn.net/yanxiaolx/article/details/51986428 //http://blog.csdn.net/yanxiaolx/article/details/51986428 #include#include #include
#include #include #include using namespace std; const int N = 9; double x[N] = {0,0,0,1,1,1,2,2,2}; double y[N] = {0,1,2,0,1,2,0,1,2}; double ds[N][N]= {0}; void Rand() { srand(time(0)); int n = 0; while(n < N) { x[n] = rand()%100; y[n] = rand()%100; ++n; } } class Point { public: Point(double x,double y,double ox,double oy) :startx(x),starty(y),endx(ox),endy(oy) {} Point(const Point& point) { startx = point.startx; starty = point.starty; endx = point.endx; endy = point.endy; } private: double startx; double starty; double endx; double endy; friend ostream& operator<<(ostream &out,Point &point); }; ostream& operator<<(ostream &out,Point &point) { out<<"("< class List { public: List(){} ~List(){} void add(const _Ty&point) { List_.push_back(point); } bool IsExist(double s) { it = List_.begin(); for(;it != List_.end();++it) { if(s == (*it).getdistence()) { return true; } } return false; } bool Insert(double s,Point point) { it = List_.begin(); for(;it != List_.end();++it) { if((*it).getdistence() == s) { (*it).add(point); return true; } } return false; } void disp() { it = List_.begin(); for(;it != List_.end();++it) { (*it).disp(); } cout< List_; typename list<_Ty>::iterator it; }; template class SList { public: SList(double s=0):d(s){} ~SList(){} void add(const _Ty&point) { slist.push_back(point); } double getdistence() { return d; } void disp() { it = slist.begin(); cout<<"距离:"< slist; typename list<_Ty>::iterator it; }; void calculate() { double d = 0; for(int i=0;i < N;++i) { for(int j=i+1;j < N;++j) { d = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j])); ds[i][j] = d; } } } void show() { for(int i=0;i < N;++i) { for(int j=0;j < N;++j) { cout< > List; //Rand(); calculate(); show(); for(int i=0;i < N;++i) { for(int j=0;j < N;++j) { if(ds[i][j] != 0 ) { if(!List.IsExist(ds[i][j])) { List.add(SList (ds[i][j])); } List.Insert(ds[i][j],Point(x[i],y[i],x[j],y[j])); } } } List.disp(); return 0; } /* LINUX 下的测试 随机数需要添加
新闻名称:计算平面中点间距离
URL地址:http://myzitong.com/article/jdhogj.html