IT/Back-end

[C++] 무선 링크 및 개수 구하기

#바로 2022. 3. 28. 00:25
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<math.h>

using namespace std;

class Node{
	int x[100];
	int y[100];
public:
	void node_numset(int num)
	{
		for (int i = 0; i < num; i++)
		{
			this->x[i] = rand() % 501;
			this->y[i] = rand() % 501;
		}
	}
	void node_print(int num)//노드 좌표 출력
	{
		cout << "\n노드 좌표출력\n";
		for (int i = 0; i < num; i++)
		{
			cout << "노드" << i << ": (" << this->x[i] << ", " << this->y[i] << ")\n";
		}
	}
	int nodelinked_num(int num)//노드의 링크들과 총 링크 개수 출력
	{
		double range;
		double dist[5050];//점과점사이의 거리
		int i = 0;
		int j = 1;
		int count = 0;
		int a = 0;

		cout << "\n전송범위를 입력하세요. (0<=전송범위<=708) : ";
		cin >> range;

		if (range == 0)
			return 1;
		else
		{
			cout << "\n노드들의 링크들과 총 링크 개수 출력\n";

			for (a = 0, i = 0; i < num - 1; i++, a++)
			{
				j = i + 1;
				for (; j < num; j++, a++)
				{
					dist[a] = sqrt(((this->x[i] - this->x[j])*(this->x[i] - this->x[j]))
						+ ((this->y[i] - this->y[j])*(this->y[i] - this->y[j])));
					if (dist[a] <= range)
					{
						cout << "노드" << i << "-" << "노드" << j << " : " << dist[a] << " < " << range << " => O" << endl;;
						count++;
					}
					else
					{
						cout << "노드" << i << "-" << "노드" << j << " : " << dist[a] << " > " << range << " => X" << endl;
					}
				}
			}
			cout << "\n총 링크의 개수는 " << count << "개 이다." << endl;

			return 0;
		}
	}
};

int main()
{
	Node p;
	int number;
	int end;

	srand((unsigned)time(NULL));

	cout << "노드의 수(n)를 입력하세요. (0<n<=100) : ";
	cin >> number;

	p.node_numset(number);
	p.node_print(number);

	do
	{
		end=p.nodelinked_num(number);
	} while (end == 0);


	return 0;
}

■ 구현한 클레스 설명
 ● Node 클래스는 노드의 좌표 x,y를 맴버변수로 한다. 
    - 좌표는 100개를 저장하기 위해서 배열로 100를 만든다.
    1. void node_numset(int num)
      - 노드의 좌표를 출력하는 함수이다.
    2. void node_print(int num)
      - 노드를 프린터하는 함수이다.
    3. int nodelinked_num(int num)
      - 전송범위를 설정하고 노드의 링크들의 거리 계산, 
        총 링크의 개수를 출력한다.

■ 구현한 메인한수 설명
   - srand((unsigned)time(NULL));// 시드값을 바꿔준다.
   - 노드의 개수를 입력받아서 함수로 넘겨준다.

■ 실행화면