溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

計(jì)算平面中點(diǎn)間距離

發(fā)布時(shí)間:2020-06-23 17:37:35 來源:網(wǎng)絡(luò) 閱讀:454 作者:wzdouban 欄目:編程語言
/*
理論是可以直接數(shù)組做的
然后呢現(xiàn)在復(fù)習(xí)呀……………………   用小demo復(fù)習(xí)c++
下面的代碼達(dá)到以下幾點(diǎn)
1.N=9好理解 好展示
2.N為一個(gè)大數(shù)時(shí),可以用于測
3.注意沒有寫入文件  可以直接用cmd  重定向exe 到txt

*/
//http://blog.csdn.net/yanxiaolx/article/details/51986428 

  //http://blog.csdn.net/yanxiaolx/article/details/51986428 


#include<iostream>
#include<list>
#include<cmath>
#include <iomanip>
#include<stdio.h>
#include<stdlib.h>
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<<"("<<point.startx<<","<<point.starty<<")"<<" "
		<<"("<<point.endx<<","<<point.endy<<")";
	return out;
}


template<class _Ty>
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<<endl;
	}
private:
	list<_Ty> List_;
	typename list<_Ty>::iterator it;
};

template<typename _Ty=Point>
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<<"距離:"<<d<<endl;
		for(;it != slist.end();++it)
		{
			cout<<*it<<endl;
		}
	}
	
private:
	double d;
	list<_Ty> 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<<setw(6)<<ds[i][j]<<" ";
		}
		cout<<endl;
	}
}

int main()
{
	List<SList<Point> > 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<Point>(ds[i][j]));
				}
				List.Insert(ds[i][j],Point(x[i],y[i],x[j],y[j]));
			}
		}
	}
	List.disp();
	return 0;
}
/*


LINUX  下的測試 隨機(jī)數(shù)需要添加


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI