- 무작위 쿠폰 코드 생성기
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CODE_LENGTH 6
#define NUM_CODES 5
int main(){
int count;
int random_prefix, random_suffix;
srand(time(NULL));
count = 0;
while(count < NUM_CODES){
random_prefix = rand() % 900 + 100; // 100-999
random_suffix = rand() % 9000 + 1000; // 1000-9999
printf("COUPON-%04d-%04d\n", random_prefix, random_suffix);
count++;
}
return 0;
}
answer1: srand(time(NULL)) 함수는 프로그램 실행 시마다 다른 시드 값을 생성하여 매번 다른 난수 시퀀스를 생성합니다. 이 함수를 제거하면 프로그램이 실행될 때마다 동일한 난수 패턴이 생성됩니다. answer2: 무작위 쿠폰 코드 생성은 할인 이벤트나 추첨 시스템에 활용될 수 있습니다.
- 할인 계산 자동화 시스템
#include <stdio.h>
int main(){
int item, quantity;
float base_price = 0, discount = 0, final_price = 0;
while(1){
printf("\n자동 할인 계산 시스템:\n");
printf("1, 노트북 — 1,000,000원\n");
printf("2, 마우스 — 30,000원\n");
printf("3, 키보드 — 80,000원\n");
printf("4, 모니터 — 500,000원\n");
printf("0, 종료\n");
printf("제품 번호를 입력하세요: ");
scanf("%d", &item);
if(item == 0)
break;
if(item < 1 || item > 4){
printf("유효하지 않은 제품 번호입니다. 다시 입력하세요.\n");
continue;
}
printf("수량을 입력하세요: ");
scanf("%d", &quantity);
if(quantity < 0){
printf("수량은 음수일 수 없습니다. 다시 입력하세요.\n");
continue;
}
if(item == 1)
base_price = 1000000 * quantity;
else if(item == 2)
base_price = 30000 * quantity;
else if(item == 3)
base_price = 80000 * quantity;
else
base_price = 500000 * quantity;
if(quantity >= 5)
discount = base_price * 0.2; // 20% 할인
else if(quantity >= 3)
discount = base_price * 0.1; // 10% 할인
else
discount = 0;
final_price = base_price - discount;
printf("총 금액: %.0f원\n", base_price);
printf("할인 금액: %.0f원\n", discount);
printf("최종 결제 금액: %.0f원\n", final_price);
base_price = 0;
}
printf("감사합니다. 다시 방문해주세요!\n");
return 0;
}
answer1: base_price 변수를 초기화하지 않으면 이전 구매 내역의 금액이 누적되어 잘못된 계산이 발생합니다. answer2: continue 문은 현재 반복을 즉시 종료하고 다음 반복으로 넘어가도록 합니다. 이를 통해 사용자가 잘못된 입력을 했을 때 재입력을 요청할 수 있습니다.
- 보행 신호 시뮬레이터
#include <stdio.h>
int main(){
char signal;
while(1){
scanf(" %c", &signal);
if(signal == 'R' || signal == 'r'){
printf("정지!\n");
}
else if(signal == 'G' || signal == 'g'){
printf("건너가세요\n");
}
else if(signal == 'Y' || signal == 'y'){
printf("잠시 기다려주세요\n");
}
else
printf("신호 오류입니다...\n");
}
return 0;
}
- 일일 온도 기록 시스템
#include <stdio.h>
int main(){
float temp;
float sum = 0.0;
float max = -50.0; // 매우 낮은 초기값
float min = 50.0; // 매우 높은 초기값
printf("일일 온도를 입력하세요. -1을 입력하면 종료됩니다:\n");
while(1){
scanf("%f", &temp);
if(temp == -1){
break;
}
sum += temp;
if(temp > max)
max = temp;
if(temp < min)
min = temp;
}
printf("일평균 기온: %.1f\n", sum / 30); // 30일간의 평균
printf("최고 기온: %.1f\n", max);
printf("최저 기온: %.1f\n", min);
return 0;
}
- 사각형 분류 프로그램
#include <stdio.h>
int main(){
int a, b, c, d;
while(scanf("%d %d %d %d", &a, &b, &c, &d) != EOF)
{if((a <= 0) || (b <= 0) || (c <= 0) || (d <= 0)){
printf("유효하지 않은 길이입니다\n");
}
else{
while(1){
if(a == b && b == c && c == d){
if((a*a + b*b) == (c*c + d*d)){ // 정사각형이면서 대각선이 같은지 확인
printf("정사각형\n");
break;
}
}
else if((a == b && c == d) || (a == c && b == d) || (a == d && b == c)){
printf("직사각형\n");
break;
}
else if((a == b && b == c) || (a == b && b == d) || (a == c && c == d) || (b == c && c == d)){
printf("마름모\n");
break;
}
else if((a == b && c != d) || (a == c && b != d) || (a == d && b != c) || (b == c && a != d) || (b == d && a != c) || (c == d && a != b)){
printf("평행사변형\n");
break;
}
else{
printf("일반 사각형\n");
break;
}
}
}
}
return 0;
}
- 숫자 추측 게임
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int target_num, guess, attempts;
srand(time(NULL));
target_num = rand() % 100 + 1; // 1-100 사이의 숫자
printf("1부터 100 사이의 숫자를 맞춰보세요\n");
printf("게임 시작! 7번의 기회가 있습니다. 숫자를 입력하세요: ");
for(attempts = 0; attempts < 7; ++attempts){
scanf("%d", &guess);
if(guess < 1 || guess > 100){
printf("1부터 100 사이의 숫자를 입력하세요\n");
continue;
}
if(attempts == 6){
printf("기회를 모두 사용했습니다. 정답은 %d입니다\n", target_num);
break;
}
if(guess == target_num){
printf("정답입니다! %d번 만에 맞췄습니다\n", attempts + 1);
break;
}
else if(guess < target_num){
printf("너무 작은 숫자입니다. 더 큰 숫자를 시도해보세요\n");
printf("남은 기회: %d번, 다시 시도: ", 6 - attempts);
}
else{
printf("너무 큰 숫자입니다. 더 작은 숫자를 시도해보세요\n");
printf("남은 기회: %d번, 다시 시도: ", 6 - attempts);
}
}
return 0;
}
실험 요약 이번 실험을 통해 if 문, for 문, while 문 등 C 언어의 제어 구문 사용에 대한 이해를 더욱 깊게 할 수 있었습니다. 또한 변수 초기화의 중요성과 사용자 입력 검증의 필요성을 체득했습니다. 이러한 기본 개념을 숙달함으로써 더 복잡한 C 프로그램을 효율적으로 개발할 수 있는 기반이 마련되었습니다.