본문 바로가기

C

(19)
쉽게 풀어쓴 C언어 Express 13장 Programming 01 책을 나타내는 구조체 book을 선언해서 사용해보자. 책은 제목(title), 저자(author), 분야(subject)를 가지고 있다고 가정한다. {1, "바람과 함께 사라지다", "마가렛 미첼"}의 값을 가지는 구조체 변수를 생성했다가 다시 화면에 출력해보자.12345struct book {    int id;    char title[100];    char author[20];};cs 12345678910111213#include stdio.h> struct book {    int id;    char title[100];    char author[20];}; int main() {    book b = { 1,"바람과 함께 사라지다","마가렛 미첼" };    printf("{ %d, ..
쉽게 풀어쓴 C언어 Express 12장 Programming 01 사용자로부터 문자를 입력받아서 그 문자의 아스키 코드값을 출력하는 프로그램을 작성하라. 1 2 3 4 5 6 7 8 9 10 #define _CRT_SECURE_NO_WARNINGS #include int main() { char a; printf("문자를 입력하시오: "); scanf("%c", &a); printf("아스키 코드값=%d", a); return 0; } cs 02 문자열을 입력으로 받아서 문자열에 포함된 모든 공백 문자를 삭제하는 함수를 작성하고 테스트하라. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #define _CRT_SECURE_NO_WARNINGS #include #include void delete_spac..
마지막 두 원소 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include #include int* solution(int num_list[], size_t num_list_len) { int n = num_list_len; int* answer = (int*)malloc((n+1)*sizeof(int)); int a = num_list[n-1]; int b = num_list[n-2]; for (int i=0;ib) answer[n]=a-b; else answer[n]=a*2; return answer; } Colored by Color Scripter cs answer = num_list 으로 코드를 작성했을 때 테스트를 통과하지 못하였다. 그 이유는 answer..
이어 붙인 수 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include #include int solution(int num_list[], size_t num_list_len) { int answer = 0; int odd=0; int even=0; for(int i=0;i
코드 처리하기 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include #include #include char* solution(const char* code) { char* answer = (char*)malloc(strlen(code)+1); answer[0]='\0'; bool mode=0; int j=0; for(int i=0;i
더 크게 합치기 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include #include int solution(int a, int b) { int answer = 0; char ab[15]; char ba[15]; sprintf(ab,"%d%d",a,b); sprintf(ba,"%d%d",b,a); if(atoi(ab)>atoi(ba)) answer=atoi(ab); else answer=atoi(ba); return answer; } cs 두 정수를 합치고자 sprintf 함수를 사용하여 문자열로 변환하였다. 문자열을 다시 정수로 변환해 값을 비교하기 위해 atoi 함수를 사용하였다.
쉽게 풀어쓴 C언어 Express 11장 Programming 01 포인터를 이용하여 자기가 사용하는 CPU의 바이트 순서를 살펴보는 프로그램을 작성해보자. 바이트 순서(byte ordering, endian)은 컴퓨터의 메모리에 바이트를 배열하는 방법이다. 바이트 순서는 큰 단위가 앞에 나오는 빅 엔디언(Big-endian)과 작은 단위가 앞에 나오는 리틀 엔디언(Little-endian)으로 나눌 수 있다. 아래의 프로그램에 주석을 추가하라. 종류 0x12345678의 표현 빅 엔디언 12 34 56 78 리틀 엔디언 78 56 34 12 1 2 3 4 5 6 7 8 #include int main() { int x = 0x12345678; unsigned char* xp = (char*)&x; printf("바이트순서: %x %x %x %x\n", xp[0],..
쉽게 풀어쓴 C언어 Express 10장 Programming 01 배열 days[]를 아래와 같이 초기화하고 배열 요소의 값을 다음과 같이 출력하는 프로그램을 작성하라. 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 1 2 3 4 5 6 7 8 #include int main() { int days[] = { 31,29,31,30,31,30,31,31,30,31,30,31 }; for (int i = 0; i