728x90
반응형
#include <iostream>
#include <Windows.h>
#include <time.h>
#include <list>
#include <algorithm>


using namespace std;

// 구조체
struct STAR {
	int id;
	int age; // 나이
};

void main() 
{
	srand((unsigned)time(NULL)); //랜덤
	list<STAR*> star;
	list<STAR*>::iterator it;

	// 프로그램이 언제 끝날지 모르기 때문에 while 구문 사용
	while (true) {
		// 갱신
		for (it = star.begin(); it != star.end(); ++it) 
		{
			if (rand() % 2 == 0) // 죽었다
			{
				delete (*it);
				it = star.erase(it);
			}
			else // 살았다
			{
				(*it)->age++;
				++it;
			}
		}
				
		// data 생성 (10보다 작으면 데이터 생성)
		while (star.size() < 10) {
			STAR* t = new STAR;
			static int id = 0;
			t->id = id++;
			t->age = 0;
			star.push_back(t);
		}
		
		// 출력 코드
		for (it = star.begin(); it != star.end(); ++it) {
			printf("%02d ", (*it)->id);
		}
		printf("\n");
		
		for (it = star.begin(); it != star.end(); ++it) {
			printf("%02d ", (*it)->age);
		}
		printf("\n");
		
		int num;
		scanf_s("%d", &num);
		if (num == 99) {
			printf("Exit\n");
			break;
		}
	}
	printf("1:%02d\n", star.size());
	for (it = star.begin(); it != star.end(); ++it)
	{
		delete(*it);
	}
	printf("2:%02d\n", star.size());
	star.clear();
	printf("3:%02d\n", star.size());
}

728x90
반응형

+ Recent posts