본문 바로가기
C

220802(CH23)

by 덤더리덤떰 2022. 8. 2.

1.공용체
공용체 변수를 이루는 멤버의 시작 주소 값 모두 동일
가장 큰 자료형을 기준으로 할당된다.
EX)int mem1, int mem2, double mem3;
1)공용체 변수
-------------double------------
--mem1--
--mem2--
-------------mem3-------------

2)일반 구조체 변수
--int mem1--
--int mem2--
---------double mem3--------

ex 23-4)
1. 프로그램 사용자로부터 int형 정수 하나를 입력 받을 것
2. 입력 받은 정수의 상위 2바이트와 하위 2바이트 값을 양의 정수로 출력할 것
3. 그 다음 상위 1바이트와 하위 1바이트에 저장된 값의 아스키 문자 출력할 것

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


typedef struct dbshort {
	unsigned short upper;
	unsigned short lower;
}DBshort;

typedef union rdbuf {
	int iBuf;
	DBshort sBuf;
	char bBuf[4];
}RDbuf;
int main() {
	RDbuf buf;
	printf("정수 입력:");
	scanf("%d", &(buf.iBuf));
	printf("상위 2바이트:%u\n", buf.sBuf.upper);

	printf("하위 2바이트:%u\n", buf.sBuf.lower);

	printf("상위 1바이트 아스키 코드:%c\n", buf.bBuf[0]);

	printf("하위 1바이트 아스키 코드:%c\n", buf.bBuf[3]);

	return 0;
}

##실행결과
정수 입력: 1145258561
상위 2바이트: 16961
하위 2바이트: 17475
상위 1바이트 아스키 코드:A
하위 1바이트 아스키 코드:D

2.열거형
->둘 이상의 연관이 있는 이름을 상수로 선언함으로써 프로그램의 가독성을 높인다.

구조체 공용체 열거형
struct union enum
'묶는다' '공유한다' '지정하겠다'

(1)열거형 syllable의 의미
"syllable형 변수에 저장이 가능한 정수 값들을 결정하겠다"


ex) enum syllable{
Do=1,Re=2,Mi=3,Fa=4,So=5,La=6,Ti=7 //Do,Re,...:열거형 상수
};

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef enum syllable {
	Do = 1, Re = 2, Mi = 3, Fa = 4, So = 5, La = 6, Ti = 7
}Syllable;

void Sound(Syllable sy)
{
	switch (sy) {
	case Do:
		puts("도는 하얀 도라지"); return;
	case Re:
		puts("레는 둥근 레코드"); return;
	case Mi:
		puts("미는 파란 미나리"); return;
	case Fa:
		puts("파는 예쁜 파랑새"); return;
	case So:
		puts("솔은 작은 솔방울"); return;
	case La:
		puts("라는 라디오고요"); return;
	case Ti:
		puts("시는 졸졸 시냇물"); return;
	}
	puts("다 함께 부르세~ 도레미파 솔라시도 솔 도~ 짠~");
}


int main() {
	Syllable tone;
	for (tone = Do; tone <= Ti; tone++) {
		Sound(tone);
	}
	return 0;
}

원래 switch구문에서 case마다 break;를 붙여서 swtich문을 끝냈었다.
return;과 break;의 차이점은
1)return;은 함수 자체를 종료(main함수로 돌아감)
2)break;은 식(switch문) 자체를 종료

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef enum syllable {
	Do = 1, Re = 2, Mi = 3, Fa = 4, So = 5, La = 6, Ti = 7
}Syllable;

void Sound(Syllable sy)
{
	switch (sy) {
	case Do:
		puts("도는 하얀 도라지"); break;
	case Re:
		puts("레는 둥근 레코드"); break;
	case Mi:
		puts("미는 파란 미나리"); break;
	case Fa:
		puts("파는 예쁜 파랑새"); break;
	case So:
		puts("솔은 작은 솔방울"); break;
	case La:
		puts("라는 라디오고요"); break;
	case Ti:
		puts("시는 졸졸 시냇물"); break;
	}
	puts("다 함께 부르세~ 도레미파 솔라시도 솔 도~ 짠~");
}


int main() {
	Syllable tone;
	for (tone = Do; tone <= Ti; tone++) {
		Sound(tone);
	}
	return 0;
}

문제 23-1 [구조체 변수의 연산] (p484)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct point {
	int xpos;
	int ypos;
}Point;

void SwapPoint(Point* ptr1, Point* ptr2) {
	int temp1;
	int temp2;
	temp1= ptr1->xpos;
	ptr1->xpos = ptr2->xpos;
	ptr2->xpos = temp1;

	temp2 = ptr1->ypos;
	ptr1->ypos = ptr2->ypos;
	ptr2->ypos = temp2;

}


int main() {
	Point pos1 = { 2,4 };
	Point pos2 = { 5,7 };
	SwapPoint(&pos1, &pos2);
	printf("변경된 pos1:[%d, %d]\n", pos1.xpos, pos1.ypos);
	printf("변경된 pos2:[%d, %d]\n", pos2.xpos, pos2.ypos);

	return 0;
}

문제 23-2[다양한 형태의 구조체 정의](p488)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct point {
	int xpos;
	int ypos;
}Point;

typedef struct Rectangle {
	Point rb;
	Point lt;
}Rec;

void RectangleArea(Rec left, Rec right) {
	int result;
	result = (right.rb.xpos - left.lt.xpos) * (left.lt.ypos-right.rb.ypos);
	printf("직사각형의 넓이는 %d\n", result);
}

void Coordinate(Rec left, Rec right) {
	printf("좌 상단의 좌표: [%d,%d]\n", left.lt.xpos, left.lt.ypos);
	printf("좌 하단의 좌표: [%d,%d]\n", left.lt.xpos, right.rb.ypos);
	printf("우 상단의 좌표: [%d,%d]\n", right.rb.xpos, left.lt.ypos);
	printf("우 하단의 좌표: [%d,%d]\n", right.rb.xpos, right.rb.ypos);
}
int main() {
	Rec right;
	Rec left;
	printf("좌 상단의 좌표 입력:");
	scanf("%d %d", &left.lt.xpos, &left.lt.ypos);
	printf("우 하단의 좌표 입력:");
	scanf("%d %d", &right.rb.xpos, &right.rb.ypos);
	RectangleArea(left,right);
	Coordinate(left, right);
	return 0;
}

'C' 카테고리의 다른 글

220810(CH26~27)  (0) 2022.08.10
220809(CH25)  (0) 2022.08.09
220728(CH21)  (0) 2022.07.28
220726(CH13,18)  (0) 2022.07.26
220721(CH16)  (0) 2022.07.26