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
반응형
'Education > Edu | .net' 카테고리의 다른 글
# 17.2) [Windows Network Programming] 시작하기1 (0) | 2021.01.27 |
---|---|
# 17.1) [MFC] 시작하기7 (Tab Control) (0) | 2021.01.27 |
# 15) [C/C++] 퀵 정렬(quick sort) 알고리즘 (0) | 2021.01.25 |
# 14.2) [MFC] 모달리스 다이얼로그(Modeless dialog) 공유 (3) | 2021.01.22 |
# 14.1) [MFC] 시작하기6 (0) | 2021.01.22 |