AR삽질러

C로 시작하는 컴퓨터 프로그래밍4판 - 11장 구조체와 열거형 본문

C

C로 시작하는 컴퓨터 프로그래밍4판 - 11장 구조체와 열거형

아랑팡팡 2023. 6. 22. 06:38
728x90

 

혼자해보기 11-1

 다음 프로그램에서 틀린 부분을 찾아 수정하시오.

#include<stdio.h>

struct list {
	char name;
	char gender;
	int age;
};

int main(void) {

	list str1 = { 'T', 'M', 25 };

	printf("st1 객체는 이름이 %c, 성별이 %c, 나이가 %d이다.\n",
		name, gender, age);

	return 0;
}

수정후

#include<stdio.h>

struct list {
	char name;
	char gender;
	int age;
};

int main(void) {
	struct list st1 = { 'T', 'M', 25 };

	printf("st1 객체는 이름이 %c, 성별이 %c, 나이가 %d이다.\n", st1.name, st1.gender, st1.age);


	return 0;
}

 

혼재해보기 11-2

 다음 프로그램에서 틀린 부분을 찾아 수정하시오.

#include<stdio.h>

struct list {
	char name;
	char gender;
	int age;
};

typedef struct list list;

typedef struct {
	char name[20];
	char address[30];
	int age;
} student;

int main(void) {
	
	list st1 = { 'T', 'M', 25 };
	struct student st2 = { "Moon", "Seoul", 30 };

	printf("st1 객체는 이름이 %c, 성별이 %c, 나이가 %d이다.\n", lst1.name, lst1.gender, lst1.age);
	printf("st1 객체는 이름이 %s, 주소가 %s이며, 나이가 %d이다.\n", st1.name, st1.address, st1.age);

	return 0;
}

수정후

#include<stdio.h>

struct list {
	char name;
	char gender;
	int age;
};

typedef struct list lists;

typedef struct student{
	char name[20];
	char address[30];
	int age;
} student;

int main(void) {
	
	lists st1 = { 'T', 'M', 25 };
	struct student st2 = { "Moon", "Seoul", 30 };

	printf("st1 객체는 이름이 %c, 성별이 %c, 나이가 %d이다.\n", st1.name, st1.gender, st1.age);
	printf("st2 객체는 이름이 %s, 주소가 %s이며, 나이가 %d이다.\n", st2.name, st2.address, st2.age);

	return 0;
}

 

1. 다음 프로그램의 오류를 찾아 수정하시오.

 

1) 

#include<stdio.h>

struct cad {
	int x;
	int y;
}

int main(void) {

	cad num = { 3.0, 4.3 };;
	printf("num의 x좌표 : y좌표 : ", num.x, num.y);

	return 0;
}

수정후

#include <stdio.h>

struct cad {
    double x;
    double y;
};

int main(void) {
    struct cad num = { 3.0, 4.3 };
    printf("num의 x좌표: %.2lf, y좌표: %.2lf", num.x, num.y);

    return 0;
}

 

2)

#include<stdio.h>

enum subject {math, korean, english};

int main(void) {

	enum math = a;
	a = 2;
	printf("영어는 %d번쨰 과목이다.\n", a + 1);

	return 0;
}

수정후

#include<stdio.h>

enum subject { math, korean, english };

int main(void) {

	enum subject a = english;
	printf("영어는 %d번쨰 과목이다.\n", a + 1);

	return 0;
}

 

 

2. 복소수를 나타내는 구조체 complex를 만드시오. 멤버 변수는 2개로 모두 double형이고 이름은 실수를 나타내는 real과 허수를 나타내는 image로 작성한다.

#include<stdio.h>

struct complex {
	double real;
	double image;
};

int main(void) {

	struct complex num;

	num.real = 3.14;
	num.image = 2.71;

	printf("실수 : %.2f\n", num.real);
	printf("허수 : %.2f\n", num.image);

	return 0;
}

 

3. 02번 문제에서 만든 구조체 complex를 포인터 인자로 받아 켤레복소수를 반환하는 paircomplex()함수를 만드시오.

#include<stdio.h>

struct complex {
	double real;
	double image;
};

struct complex paircomplex(const struct complex* num) {
	struct complex result;
	result.real = num->real;
	result.image = -num->image;
	return result;
}

int main(void) {

	struct complex num = { 3.14, 2.71 };
	struct complex conjugate = paircomplex(&num);

	printf("실수 : %.2f, %.2f\n", num.real, num.image);
	printf("허수 : %.2f, %.2f\n", conjugate.real, conjugate.image);

	return 0;
}

 

 

6. 멤버 변수로 제목, 지은이, 출판사라는 문자형 변수 3개, 쪽수와 가격이라는 정수형 변수 2개를 가진 구조체 Books를 정의하고 다음의 책 정보를 담을 수 있는 구조체 배열을 프로그램하시오.

#include<stdio.h>

struct book {
	char title[20];
	char author[20];
	char publisher[20];
	int page;
	int price;
};

int main(void) {

	struct book books[3] = {
		{ "TRUTH", "JOHN", "CENTURY", 300, 20000 },
		{ "PAUL", "PAUL", "GOODS", 200, 15000 },
		{ "JOY", "JAMES", "COOKIE", 250, 18000 }
	};

	for (int i = 0; i < 3; i++) {
		printf("Title : %s\t Authors : %s\t Press : %s\t Page : %d\t Price : %d \n", books[i].title, books[i].author, books[i].publisher, books[i].page, books[i].price);
	}

	return 0;
}

 

7. 06번 문제의 Books구조체를 이용해서 사용자가 책 제목을 입력하면 책 정보를 제공해주는 프로그램을 작성하시오. 이때 사용자는 책 제목을 대문자 또는 소문자로 입력해도 검색이 되며, 보유하고 있지 않은 책 제목을 입력하면 보유하고 있지 않다고 알려주도록 한다.

#include<stdio.h>
#include<string.h>
#pragma warning(disable:4996)

struct book {
	char title[20];
	char author[20];
	char publisher[20];
	int page;
	int price;
};

int main(void) {
	struct book books[3] = {
		{ "TRUTH", "JOHN", "CENTURY", 300, 20000 },
		{ "PAUL", "PAUL", "GOODS", 200, 15000 },
		{ "JOY", "JAMES", "COOKIE", 250, 18000 }
	};

	char title[20];
	printf("책 제목을 입력하세요: ");
	scanf("%s", &title);

	int found = 0;

	for (int i = 0; i < 3; i++) {
		if (strcmp(books[i].title, title) == 0) {
			printf("===입력한 책의 정보===\n");
			printf("Title: %s\n", books[i].title);
			printf("Authors: %s\n", books[i].author);
			printf("Press: %s\n", books[i].publisher);
			printf("Page: %d\n", books[i].page);
			printf("Price: %d\n", books[i].price);
			found = 1;
		}
	}

	if (!found) {
		printf("존재하지 않는 책입니다.\n");
	}

	return 0;
}

728x90
반응형
LIST