과제 1: 랜덤 번호 생성기
이 프로그램은 두 개의 범위 내에서 임의의 숫자를 생성하여 학번 형식의 문자열을 출력합니다. 첫 번째 범위는 397부터 476까지이며, 두 번째 범위는 1부터 21까지입니다.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TOTAL_COUNT 5
#define RANGE_MIN1 397
#define RANGE_MAX1 476
#define RANGE_MIN2 1
#define RANGE_MAX2 21
int main() {
int attempt = 0;
int selector, random_value;
srand(time(NULL));
while (attempt < TOTAL_COUNT) {
selector = rand() % 2;
if (selector) {
random_value = rand() % (RANGE_MAX1 - RANGE_MIN1 + 1) + RANGE_MIN1;
printf("20248329%04d\n", random_value);
} else {
random_value = rand() % (RANGE_MAX2 - RANGE_MIN2 + 1) + RANGE_MIN2;
printf("20248395%04d\n", random_value);
}
attempt++;
}
return 0;
}
과제 2: 이차방정식 해 계산기
입력된 계수에 따라 이차방정식의 해를 구하고, 실근, 중근, 허수근에 맞게 출력합니다.
#include <stdio.h>
#include <math.h>
int main() {
double coef_a, coef_b, coef_c;
double discriminant, root_mid, root_diff;
while (scanf("%lf %lf %lf", &coef_a, &coef_b, &coef_c) != EOF) {
if (coef_a == 0) {
printf("a = 0, invalid input\n");
continue;
}
discriminant = coef_b * coef_b - 4 * coef_a * coef_c;
root_mid = -coef_b / (2 * coef_a);
root_diff = sqrt(fabs(discriminant)) / (2 * coef_a);
if (discriminant == 0)
printf("x1 = x2 = %.2g\n", root_mid);
else if (discriminant > 0)
printf("x1 = %.2g, x2 = %.2g\n", root_mid + root_diff, root_mid - root_diff);
else
printf("x1 = %.2g + %.2gi, x2 = %.2g - %.2gi\n", root_mid, root_diff, root_mid, root_diff);
}
return 0;
}
과제 3: 신호등 상태 처리기
사용자 입력에 따라 'r', 'y', 'g'에 해당하는 메시지를 출력하며, 잘못된 입력은 오류 메시지로 처리합니다.
#include <stdio.h>
int main() {
char signal;
while (scanf("%c", &signal) != EOF) {
getchar(); // 버퍼 비우기
switch (signal) {
case 'r':
printf("stop!\n");
break;
case 'y':
printf("wait a minute\n");
break;
case 'g':
printf("go go go\n");
break;
default:
printf("something must be wrong\n");
break;
}
}
return 0;
}
과제 4: 일일 지출 통계 분석기
사용자가 입력한 지출 금액을 누적하고, 최고/최저 지출액 및 총합을 계산해 출력합니다.
#include <stdio.h>
int main() {
double total_expense = 0.0;
double highest = 0.0;
double lowest = 20000.0;
double amount;
printf("입력 오늘 지출금액 (-1로 종료): ");
while (1) {
scanf("%lf", &amount);
if (amount == -1.0) break;
total_expense += amount;
if (amount > highest) highest = amount;
if (amount < lowest) lowest = amount;
}
if (total_expense == 0) lowest = 0;
printf("총 지출액: %.1lf\n", total_expense);
printf("최고 지출액: %.1lf\n", highest);
printf("최저 지출액: %.1lf\n", lowest);
return 0;
}
과제 5: 삼각형 유형 판별기
세 변의 길이를 입력받아 삼각형 구성 여부와 종류(등변, 직각, 일반)를 판단합니다.
#include <stdio.h>
#include <math.h>
int main() {
int side_a, side_b, side_c;
int a_sq, b_sq, c_sq;
while (scanf("%d %d %d", &side_a, &side_b, &side_c) != EOF) {
a_sq = side_a * side_a;
b_sq = side_b * side_b;
c_sq = side_c * side_c;
if (side_a + side_b <= side_c || side_b + side_c <= side_a || side_a + side_c <= side_b) {
printf("삼각형이 될 수 없습니다.\n");
} else if (side_a == side_b && side_b == side_c) {
printf("정삼각형\n");
} else if (a_sq + b_sq == c_sq || b_sq + c_sq == a_sq || a_sq + c_sq == b_sq) {
printf("직각삼각형\n");
} else if (side_a == side_b || side_b == side_c || side_a == side_c) {
printf("이등변삼각형\n");
} else {
printf("일반삼각형\n");
}
}
return 0;
}
과제 6: 행운의 날 추측 게임
1~30 사이의 난수를 기반으로 사용자가 세 번의 기회 안에 정답을 맞추도록 하는 간단한 게임입니다.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int guess, target_day, attempts = 0;
srand(time(NULL));
target_day = (rand() % 30) + 1;
printf("2024년 11월의 행운의 날을 맞춰보세요! (1~30)\n");
printf("기회는 3번입니다. 시작하세요: ");
while (attempts < 3) {
scanf("%d", &guess);
if (guess == target_day) {
printf("정답입니다! 축하합니다 :)\n");
break;
} else if (guess < target_day) {
attempts++;
if (attempts == 3) break;
printf("너무 이르네요. 다시 시도해보세요 (1~30): ");
} else {
attempts++;
if (attempts == 3) break;
printf("너무 늦네요. 다시 시도해보세요 (1~30): ");
}
}
if (attempts == 3 && guess != target_day) {
printf("기회가 모두 소진되었습니다. 정답은 %d일입니다.\n", target_day);
}
return 0;
}