본문 바로가기
IT/프로그래밍

C언어 본색 파트3 Chapter 1 연습문제 솔루션

by nutrient 2021. 5. 31.
728x90
728x170

 

 

1번

#include <stdio.h>

struct point
{
	unsigned int num;
	char bloodtype[2];
	double optical1, optical2;
	int weight;
};

int main ()
{
	struct point stu;

	return 0;
}


/*

구조체의 기본적인 정의를 해보는 문제입니다.
혈액형의 경우 AB형이라는 2글자를 필요로 하는 정보가 있기 때문에 2개의 배열로 저장합니다.

*/

 

2번

#include <stdio.h>

struct point
{
	unsigned int num;
	char bloodtype[2];
	double optical1, optical2;
	int weight;
};

int main ()
{
	struct point array[5];
	int i;

	for (i=0;i<5;i++)
	{
		printf("학번: ");
		scanf("%u", &array[i].num);
		printf("혈액형: ");
		scanf("%s", &array[i].bloodtype);
		printf("시력: ");
		scanf("%lf %lf", &array[i].optical1, &array[i].optical2);
		printf("몸무게: ");
		scanf("%d", &array[i].weight);
	}

	printf("\n\n");

	for (i=0;i<5;i++)
	{
		printf("학번: %u\n혈액형: %s\n시력: %.1lf, %.1lf\n몸무게: %d\n\n", array[i].num, &array[i].bloodtype, array[i].optical1, array[i].optical2, array[i].weight);
	}

	return 0;


}


/*

1번 문제의 구조체를 이용하여 실제로 해당 구조체에 데이터를 넣고
그것을 출력하는 문제입니다.
5명의 정보를 입력받기 위해 for문을 이용하고
마찬가지로 정보의 입력이 끝난뒤에 for문을 다시 사용하여 입력받은 정보를
출력해줍니다.

*/

 

3번

4번

#include <stdio.h>

struct prototype
{
	int x;
	int y;
};

int main ()
{
	struct prototype s1;
	struct prototype* sp;

	s1.x=10;
	s1.y=10;

	sp=&s1;

	printf("%x, %d, %d\n", sp, s1.x, s1.y);
	printf("%d, %d\n", sp->x, sp->y);

	return 0;
}


/*

그림에서 s1이라는 데이터 구조를 정의하기 위해
prototype라는 구조체를 사용하였습니다.
이 구조체의 시작점을 가리키는 포인터를 만들기 위에 sp를
이 구조체 형식을 가지는 포인터로서 선언하였고,
sp = &s1; 구문을 통해 sp가 s1을 가리키도록 한뒤
이를 이용하여 s1으로 구조체 값에 직접 접근하여 출력해보고,
sp를 이용하여 구조체 값에 간접적으로 접근하여 출력하여봅니다.

*/

 

5번

#include <stdio.h>
struct point
{
	int x;
	int y;
};
int main(void)
{
	struct point array[2]={1, 1, 2, 2};
	struct point* p=NULL;

	p=array;
	printf("%d %d \n", array[0].x, array[0].y);
	printf("%d %d \n", array[1].x, array[1].y);
	printf("%d %d \n", p[0].x, p[0].y);
	printf("%d %d \n", p[1].x, p[1].y);

	printf("%d %d \n", p->x, p->y);
	printf("%d %d \n", (p+1)->x, (p+1)->y);

	return 0;
}

/* 해설
	구조체 포인터 p는 생성된 구조체배열 array를 포인팅합니다.
	이때 구조체배열에 값이 순차적으로 1, 1, 2, 2가 할당되면서 각각을 struct point형으로 접근할 수 있습니다.
	따라서 메모리를 선언된 struct point형에 마추어 값에 접근할 수가 있습니다.
	즉, array[0], array[1]은 모두 int형 2개의 크기와 같으며, 각각 x, y로 구조체 내의 변수에 접근 할 수가 있습니다.

	또한  구조체 포인터 p는 array변수를 포인팅 하고 있으며, p->x, p->y는 각각 array[0]->x, array[0]->y의 값과 같고, 	(p+1)->x, (p+1)->y는 각각 array[1]->x, array[1]->y와 같은 값을 출력합니다.
*/

 

6번

#include <stdio.h>

struct student
{
	int id;
	char *lastName;
	double dData;
};

int main()
{
	struct student **pp;
	struct student *p;
	struct student stu;

	p=&stu;
	pp=&p;

	stu.id=20101323;
	stu.lastName="Park";
	stu.dData=160;

	printf("%d\n", stu.id);
	printf("%s\n", stu.lastName);
	printf("%lf\n", stu.dData);
	
	return 0;
}

/* 해설
	구조체, 구조체 포인터, 포인터와 다중 포인터에 관한 문제입니다.
	student라는 구조체를 정의하여 stu라는 변수명으로 student 구조체를 선언하고, pp와 p로 구조체 포인터를 생성하여 각각을 포인팅합니다.
	이때 pp는 이중 구조체 포인터로 구조체 포인터를 포인트 할 수 있습니다.
*/

 

7번

#include <stdio.h>
struct node
{
	int data;
	struct node* link;
};
int main(void)
{
	struct node n1, n2, n3;

	n1.data=10;
	n1.link=&n2;
	n2.data=20;
	n2.link=&n1;
	n3.data=30;
	n3.link=&n3;

	printf("%d %d %d \n", n1.data, n2.data, n3.data);
	printf("%d %d %d \n", n2.link->data, n1.link->data, n3.link->data);

	return 0;
}

/* 해설
	포인터를 사용하여 같은 부류의 데이터를 어떻게 묶어 관리하는지 간략하게 보여주는 예제입니다.
	node 구조체는 int형 변수와, 자기 자신의 구조체 포인터 변수를 갖습니다.
	이때 포인터변수를 이용하여 같은 타입의 구조체를 포인트 할 수 있기 때문에, 같은 종류의 데이터를 일련의 메모리 리스트 형태로 관리할 수가 있습니다.

	구조체 n1에는 데이터로 10을 대입하고, n1의 링크, link에 n2노드를 포인트 합니다.
	같은 방법으로 n2에 데이터로 20을 대입하고, n2의 링크, link에 n1노드를 포인트 합니다.
	마지막으로 n3에는 데이터로 30을 대입하고, n3의 링크, 즉 자기자신을 포인트 합니다.
*/

 

8번

#include <stdio.h>

struct student {
	char familyName[8];
	long point;
	struct student* left_link;
	struct student* right_link;
};

int main () {
	struct student stu1 = {"Kim", 90, NULL, NULL};
	struct student stu2 = {"Lee", 80, NULL, NULL};
	struct student stu3 = {"Goo", 60, NULL, NULL};
		
	stu1.left_link = &stu2;
	stu1.right_link = &stu3;
	
	printf("%s %s", stu1.left_link->familyName, stu1.right_link->familyName);
	return 0;
}

 

9번

#include <stdio.h>

struct node {
	int data;
	struct node* left_link;
	struct node* right_link;
};

int main () {
	struct node n7 = {70, NULL, NULL};
	struct node n6 = {60, NULL, NULL};
	struct node n5 = {50, NULL, NULL};
	struct node n4 = {40, NULL, NULL};
	struct node n3 = {30, &n6, &n7};
	struct node n2 = {20, &n4, &n5};
	struct node n1 = {10, &n2, &n3};
	
	printf("%d %d", n1.left_link->left_link->data, n1.right_link->right_link->data);
	return 0;
}

 

10번

#include <stdio.h>

struct point {
	int x;
	int y;
};

void func1(struct point p) {
	printf("%d %d\n", p.x, p.y);
}

void func2(struct point* p) {
	printf("%d %d\n", p->x, p->y);
}

int main () {
	struct point p1 = {10, 10};
	struct point p2 = {20, 20};

	func1(p1);
	func2(&p2);

	return 0;
}
728x90
그리드형

댓글