time 함수
time(), localtime()을 사용하기 위해선 <time.h>이라는 헤더파일을 추가해야한다.
time_t자료형은 unsigned int, unsigned long형으로 재정의 되어 있다
- time()함수로 얻어 온 값을 time_t형 변수에 저장한다
- 경과 시간을 지정할 영역을 지정하지만 보통은 NULL사용
- 그리니치 표준시로 1970년 1월 1일 자정부터 현재까지의 경과 시간을 초 단위로 계산한다
#include <stdio.h>
#include <time.h>
int main()
{
time_t t;
t = time(NULL);
printf("1970년 1월 1일 자정부터 흘러온 시간 : %u초 \n", t);
return;
}
1970년 1월 1일 자정부터 현재까지 흘러온 시간을 알 수 있다
local time 함수
localtime 함수로 얻어온 현재 시간 정보는 time.h에 미리 준비되어 있는 tm_구조체 변수에 저장된다
localtime함수에선 그 포인터만 반환함
#include <stdio.h>
#include <time.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
time_t t;
struct tm* today;
t = time(NULL);
today = localtime(&t);
int year, month, day;
year = today->tm_year + 1900;
month = today->tm_mon + 1;
day = today->tm_mday;
printf("오늘은 %d년 %d월 %d일 \n", year, month, day);
printf("오늘은 %d일째 되는 날\n ", today->tm_yday);
printf("현재시간 %d시 %d분 %d초 ", today->tm_hour, today->tm_min, today->tm_sec);
return;
}
난수 불러오기
srand()랑 rand()함수를 쓰기위해 stdlib.h 헤더파일 추가
- 정해진 난수표에서 순타적으로 수치를 가져온다
- 난수표는 os에 따라 수치의 순서가 다르게 짜여져 있다
- RAND_MAX 수피는 0~32767이다
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
printf("난수의 범위: 0부터 %d까지 \n", RAND_MAX);
int num1 = rand();
printf("숫자: %d\n", num1);
//1에서부터 100사이의 난수
//컴퓨터가 제시하는 임의의수 나머지 연산 % 으로 처리
//임의의 값 %6 => 나머지 계산후 +1
int num2 = rand() % 100 + 1;
printf("number:%d\n", num2);
return;
}
이렇게 하면 RAND_MAX가 32767이기 때문에 0에서 부터 32767까지 숫자중 아무 숫자나 내보낸다.
(컴터마다 고유의 난수가 있어서 바뀌진 않음)
num2는 %를 사용해서 1에서부터 100까지중 아무 수나 뽑아내라고 만들었는데 68이 나온걸 볼 수 있음.
그럼 아무 숫자나 5번 뽑아보자
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int i;
for (i = 0; i < 5; i++)
{
printf("random number: %d\n", rand());
}
}
이렇게 for문을 써서 5개를 뽑으면 이렇게 랜덤한 수가 5개 나오긴 하는데 이 또한 컴터에 저장된 고유값이라 아무리 실행해도 똑같은 값이 계속 출력되는거임...
그래서 뭔가 유용하게 쓰려면 실행할때마다 값이 맨날 랜덤으로 바껴야 좋겠지?
그래서 srand 를 쓰는거임
srand((int)time(NULL)); 이렇게 시간을 이용해서 렌덤으로 호출하게 만들거임.
이때 시간을 seed(시드)값이라 하는데 이걸 심어서 난수를 만드는거임
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int rolldie();
int main()
{
srand((int)time(NULL));
int die1 = rolldie();
int die2 = rolldie();
printf("die : %d, %d", die1, die2);
return;
}
int rolldie()
{
return rand() % 6 + 1;
}
이렇게 하면 매번 실행할때마다 1에서 6사이의 다른 값 두개가 출력됨
이거로 게임을 만들어볼거임
Up down game
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
int random();
int main()
{
srand((int)time(NULL));
int num = random();//1에서 100까지의 렌덤한 숫자를 num에 저장
int get = 0;
int chance = 5;
printf("what will be the number?:");
while (chance > 0) {
scanf_s("%d", &get);
printf("chance left: %d\n", chance--);
if (get > num) {
printf("down\n");
}
else if (get < num) {
printf("up\n");
}
else if (get = num) {
printf("U got it!\n");
break;
}
else
{
printf("error\n");
}
if (chance == 0)
{
printf("u use up all ur chances\n");
break;
}
printf("try again:");
}
}
int random()
{
return rand() % 50 + 1;
}
1부터 50까지의 숫자 사이에 하나의 렌덤한 수를 num에 저장시킴.
num에 들어있는 숫자가 뭘까 맞추면 됨
이때 chance가 5번 주어지는데 이 chance안에 수를 맞춰야 함
어떤 값을 넣으면 그 값이 num보다 작은지 큰지 if문을 써서 알려주는거임
'언어는 과-학인가요? > c언어' 카테고리의 다른 글
(c언어) typedef (0) | 2020.09.19 |
---|---|
(c언어) 구조체(structure) (0) | 2020.09.19 |
(c언어) 함수 호출 - 변수 값 위치 바꾸기 swap (0) | 2020.09.12 |
(c언어) 배열과 포인터(array and pointer) (0) | 2020.09.12 |
(c언어) 배열과 포인터 (0) | 2020.09.12 |