본문 바로가기

C언어 콘서트

C언어 콘서트 10장 LAB 풀이

1. 427P 점을 구조체로 표현하자 (책이랑 똑같)

#define _CRT_SECURE_NO_WARNINGS
#include <conio.h>
#include <stdio.h>
#include <stdlib.h> //rand(), srand()
#include <time.h>  //time()
#include <string.h> //strcmp()문자열비교
#include <math.h> // sqrt

#define SIZE 6

struct point {
	int x;
	int y;
};

int main(void)
{
	struct point p1, p2;
	int xdiff, ydiff;
	double dist;

	printf("점의 좌표를 입력하시오(x y): ");
	scanf("%d %d", &p1.x, &p1.y);
	printf("점의 좌표를 입력하시오(x y): ");
	scanf("%d %d", &p2.x, &p2.y);

	xdiff = p1.x - p2.x;
	ydiff = p1.y - p2.y;

	dist = sqrt(xdiff * xdiff + ydiff * ydiff);

	printf("두 점 사이의 거리는 %f입니다.\n", dist);
	return 0;
}

 

2. 434P 4지 선다 퀴즈 시스템 만들기

도전문제(3) 아무리 붙잡아봐도 모르겠어서 고민하다가 2시간 흐른 듯..
낼 수강정정 어느정도 마친 후에 좀만 더 잡아보고 안되면 포기
--> 포기 결론
아랫쪽에 마지막까지 건드렸던 코드 복붙
#define _CRT_SECURE_NO_WARNINGS
#include <conio.h>
#include <stdio.h>
#include <stdlib.h> //rand(), srand()
#include <time.h>  //time()
#include <string.h> //strcmp()문자열비교
#include <math.h> // sqrt

#define SIZE1 100
#define SIZE2 3 // 문제갯수

struct QUESTION {
	char question[SIZE1];
	char item1[SIZE1];
	char item2[SIZE1];
	char item3[SIZE1];
	char item4[SIZE1];
	int solution;
};

struct QUESTION bank[SIZE1] = {
	{"임베디드 장치에 가장 적합한 프로그래밍 언어는?", "1. Python", "2. Java", "3. C", "4. Javascript", 3 },
	{"서로 다른 자료형을 모을 수 있는 구조는?", "1. 배열", "2, 변수", "3. 구조체", "4. 포인터", 3 },
	{"1+3은?", "1. 1", "2. 2", "3. 3", "4. 4", 4}
};

int main(void)
{
	int select, i, q_num, count = 0;
	int selected_num[SIZE2] = { 0 };
	int q1check = 0, q2check = 0, q3check = 0;

	srand((unsigned)time(NULL));

	for (i = 0; i < SIZE2; i++) {
		q_num = rand() % SIZE2;

		if (q1_check == 1 || q2_check == 1 || q3_check == 1)
			i--;

		if (q_num == 0) {
			q1_check = 1; // q1은 이미 사용됨.
		}
	}
	
	printf("%s\n", bank[q_num].question);
	printf("%s", bank[q_num].item1);
	printf("%s", bank[q_num].item2);
	printf("%s", bank[q_num].item3);
	printf("%s\n", bank[q_num].item4);

	scanf("%d", &select);
	if (select == bank[q_num].solution) {
		printf("정답입니다!\n\n");
		count++;
	}
	else
		printf("틀렸습니다!\n\n");

	printf("총 %d문제 맞추셨습니다. 축하합니다!\n", count);
	return 0;
}