(Lab 7-1) 현재시간
*tm_sec 범위 ( 0 - 59)

tm_year 의 경우 출력 시: +1900 해야함
tm_mon 의 경우 출력 시: +1 해야함
tm_wday 는 일요일 부터 시작함 (일 ~ 토)
ctime()는 문자열 내에 '\n'을 포함시켜 자동 줄바꿈이 됨 -> 추가로 printf()에 '\n'을 적을 필요 없음
time_t 자료형(정수형)- 시간을 나타내는 변수를 사용할때 사용
clock_t 자료형(정수형)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
int main() {
char* wday[] = { "일","월","화","수","목","금","토" };
time_t now;
time(&now); // 1970년 이후 1초 단위로 증가
printf("now = %llu\n", now);
//char* ctime(&now); // 시간을 문자열로 반환
printf("ctime() : %s", ctime(&now));
//struct tm* localtime(const time_t*);
struct tm* tp = localtime(&now);
printf("%d년 %d월 %d일 %s요일 %d시 %d분 %d초\n", tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday,wday[tp->tm_wday], tp->tm_hour, tp->tm_min, tp->tm_sec);
}
#if 0
struct tm { /* a broken-down time */
int tm_sec; /* seconds after the minute: [0 - 60] */
int tm_min; /* minutes after the hour: [0 - 59] */
int tm_hour; /* hours after midnight: [0 - 23] */
int tm_mday; /* day of the month: [1 - 31] */
int tm_mon; /* months since January: [0 - 11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday: [0 - 6] */
int tm_yday; /* days since January 1: [0 - 365] */
int tm_isdst; /* daylight saving time flag: <0, 0, >0 */
};
#endif
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
int main() {
char* wday[] = { "일","월","화","수","목","금","토" };
time_t now;
time(&now);
printf("now = %llu\n", now);
printf("ctime() : %s", ctime(&now));
struct tm* tp = localtime(&now);
printf("%d년 %d월 %d일 %s요일 %d시 %d분 %d초\n", tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday, wday[tp->tm_wday], tp->tm_hour, tp->tm_min, tp->tm_sec);
}
(Lab 7-2) 움직이는 시계 - '/r' 활용

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
int main() {
char* wday[] = { "일","월","화","수","목","금","토" };
time_t now;
struct tm* tp;
while (1) {
time(&now);
//printf("now = %llu\r", now);
//printf("ctime() : %s\r", ctime(&now)); // ctime() 문자열은 문자열 내에 '\n'이 포함되어있어서 '\r'을 사용해도 줄바꿈이 일어남
tp = localtime(&now);
printf("%d년 %d월 %d일 %s요일 %d시 %d분 %d초\r", tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday, wday[tp->tm_wday], tp->tm_hour, tp->tm_min, tp->tm_sec);
}
}
(Lab 7-3) 실행 시간 측정
clock_t 자료형 - clock() 함수 사용할때 사용

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main(void)
{
clock_t start, finish;
double duration;
int i, j, k = 0;
start = clock();
// 수행시간을 측정하고자 하는 코드....
for (i = 1; i <= 10000; i++)
for (j = 1; j < 10000; j++)
k++;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("%f 초입니다.\n", duration);
}
(Lab 7-4) 시작/종료시간 표시 및 시간측정

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main(void)
{
clock_t start, finish;
time_t now;
double duration;
int i, j, k = 0;
struct tm* p;;
time(&now); //now = time(NULL);
start = clock();
printf("시작 : %s", ctime(&now));
// 수행시간을 측정하고자 하는 코드
for (i = 1; i <= 100000; i++)
for (j = 1; j < 100000; j++)
k++;
time(&now); //now = time(NULL);
finish = clock();
printf("종료 : %s", ctime(&now));
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("실행시간 = %lf초입니다. \n\n", duration);
time(&now);
p = localtime(&now);
printf("시작 : %d:%d:%d \n", p->tm_hour, p->tm_min, p->tm_sec);
start = clock();
// 수행시간을 측정하고자 하는 코드
for (i = 1; i <= 100000; i++)
for (j = 1; j < 100000; j++)
k++;
time(&now);
p = localtime(&now);
printf("종료 : %d:%d:%d \n", p->tm_hour, p->tm_min, p->tm_sec);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("실행시간 = %lf초입니다. \n\n", duration);
}
( Lab 7-5) 스톱워치

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int main() {
clock_t start, now;
clock_t duration, sec, min, hour, milsec;
int ch1, ch2;
printf("Enter 로 시작 CTRL-C누르면 종료 \n");
while (getchar() != '\n');
start = clock();
while (1) {
now = clock();
duration = now - start;
sec = duration / CLOCKS_PER_SEC;
milsec = duration % CLOCKS_PER_SEC;
hour = sec / 3600;
min = (sec / 60) % 60;
sec = sec % 60;
printf("%d:%02d:%02d.%03d\r", hour, min, sec, milsec);
if (_kbhit()) {
ch1 = _getch();
if (ch1 == 0xe0 || ch1 == 0x0) {
ch2 = _getch();
if (ch2 == 75)
break;
}
else if (ch1 <= 26)
break;
}
}
printf("\n");
}
(Lab 7-6) 재귀함수 Sum, Factorial, Fibonacci, Binary
facotrial(0) = 1 이라는 것에 유의하라

#include <stdio.h>
int sum(int n) {
if (n <= 1)
return n; // 1도 가능
else
return n + sum(n - 1);
}
int factorial(int n) {
if (n <= 1)
return 1;
else
return n * factorial(n - 1);
}
int fibo(int n) {
if (n <= 1)
return n;
else
return fibo(n - 1) + fibo(n - 2);
}
//방법1
void binary(int n) {
if (n > 0) {
binary(n / 2);
printf("%d", n % 2);
}
}
////방법2
//void binary(int n) {
// if (n < 2)
// printf("%d", n);
//
// else {
// binary(n / 2);
// printf("%d", n % 2);
// }
//}
int main() {
int n = 10;
int bi = 12345;
printf("sum(%d) = %d\n",n, sum(n));
printf("factorial(%d) = %d\n", n, factorial(n));
printf("fibonacci(%d) = ", n);
for (int i = 0; i <= 10; i++) {
printf("%d ", fibo(i));
}
printf("\n");
printf("binary(%d) = ", bi);
binary(bi);
}
+8진수 출력 재귀 함수
//방법 1
void hexa(int n) {
if (n > 0)
{
hexa(n / 8);
printf("%d", n % 8);
}
}
//방법2
void hexa(int n) {
if (n < 8)
printf("%d", n);
else {
hexa(n / 8);
printf("%d", n % 8);
}
}
+16진수 출력 재귀 함수
//방법1
void octal(int n) {
int k;
if (n > 0) {
octal(n / 16);
k = n % 16;
if (k < 10)
printf("%d", k);
else
printf("%c", 'a' + (k - 10));
}
}
//방법2
void octal(int n) {
int k;
if (n < 16) {
if (n < 10)
printf("%d", n);
else
printf("%c", 'a' + (n- 10));
}
else {
octal(n / 16);
k = n % 16;
if (k < 10)
printf("%d", k);
else
printf("%c", 'a' + (k - 10));
}
}
'프로그래밍랩' 카테고리의 다른 글
[프로그래밍랩] 14주차, 난수 (1) | 2024.11.26 |
---|---|
프로그래밍랩 13주차 (0) | 2024.11.25 |
프로그래밍랩 10-11 주차 (0) | 2024.11.08 |
프로그래밍랩 9주차 (0) | 2024.11.07 |
[프로그래밍 랩] C언어 복습 문제 4 (0) | 2024.10.25 |