본문 바로가기
솔루션모음/쉽게 풀어 쓴 C언어 Express 실습문제

[쉽게 풀어 쓴 C언어 Express] 16장 프로그래밍 programming 솔루션 답지

by 이얏호이야호 2020. 4. 12.

쉽게 풀어 쓴 C언어 express 솔루션입니다.

 

1.

#include <stdio.h>

#include <stdlib.h>

int main(void) {

	FILE *fp1, *fp2;

	char file1[100], file2[100];

	printf("첫번쨰 파일 이름: ");

	scanf("%s", file1);

	printf("두번째 파일 이름: ");

	scanf("%s", file2);

	// 첫번째 파일을 읽기 모드로 연다.

	if( (fp1 = fopen(file1, "r")) == NULL ) {

		fprintf(stderr,"원본 파일 %s을 열 수 없습니다.\n", file1);

		exit(1);

	}

	// 두번째 파일을 읽기 모드로 연다.

	if( (fp2 = fopen(file2, "r")) == NULL ) {

		fprintf(stderr,"복사 파일 %s을 열 수 없습니다.\n", file2);

		exit(1);

	}

	// 첫 번째 파일과 두 번째 파일을 비교

	while( 1 ) {

		int c1 = fgetc(fp1);

		int c2 = fgetc(fp2);

		if( c1 == EOF || c2 == EOF )

		break;

		if( c1 != c2 ) {

			printf("두 파일은 서로 다릅니다.\n");

			return 0;

		}

	}

	printf("두 파일은 서로 같습니다.\n");

	fclose(fp1);

	fclose(fp2);

	return 0;

}

 

 

 

 

 

2.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

int main(void) {

	FILE *fp1, *fp2;

	char file1[100], file2[100];

	char c;

	printf("첫번쨰 파일 이름: ");

	scanf("%s", file1);

	printf("두번째 파일 이름: ");

	scanf("%s", file2);

	// 첫번째 파일을 읽기 모드로 연다.

	if( (fp1 = fopen(file1, "r")) == NULL ) {

		fprintf(stderr,"파일 %s을 열 수 없습니다.\n", file1);

		exit(1);

	}

	// 두번째 파일을 읽기 모드로 연다.

	if( (fp2 = fopen(file2, "w")) == NULL ) {

		fprintf(stderr,"파일 %s을 열 수 없습니다.\n", file2);

		exit(1);

	}

	// 첫 번째 파일과 두 번째 파일을 비교

	while( 1 ) {

		c = fgetc(fp1);

		if( c== EOF)

		break;

		fputc(toupper(c), fp2);

	}

	fclose(fp1);

	fclose(fp2);

	return 0;

}

 

 

3.

// 이진 파일 버전

#include <stdio.h>

#define SIZE 100

int main(void) {

	int buffer[SIZE];

	FILE *fp = NULL;

	FILE *fp1 = NULL;

	int i;

	int count;

	char file1[100], file2[100];

	printf("원본 파일 이름: ");

	scanf("%s", file1);

	printf("복사 파일 이름: ");

	scanf("%s", file2);

	fp = fopen(file1, "rb");

	// ①

	if( fp == NULL ) {

		fprintf(stderr, "파일을 열 수 없습니다.");

		return 1;

	}

	fp1 = fopen(file2, "wb");

	// ①

	if( fp1 == NULL ) {

		fprintf(stderr, "파일을 열 수 없습니다.");

		return 1;

	}

	while( (count=fread(buffer, sizeof(char), SIZE, fp)) != 0 ) {

		fwrite(buffer, sizeof(char), count, fp1);

	}

	fclose(fp);

	fclose(fp1);

	return 0;

}

 

 

4.

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

int main(void) {

	FILE *fp1, *fp2;

	char file1[100], file2[100];

	char buffer1[1000], buffer2[1000];

	printf("첫번쨰 파일 이름: ");

	scanf("%s", file1);

	printf("두번째 파일 이름: ");

	scanf("%s", file2);

	// 첫번째 파일을 읽기 모드로 연다.

	if( (fp1 = fopen(file1, "r")) == NULL ) {

		fprintf(stderr,"원본 파일 %s을 열 수 없습니다.\n", file1);

		exit(1);

	}

	// 두번째 파일을 읽기 모드로 연다.

	if( (fp2 = fopen(file2, "r")) == NULL ) {

		fprintf(stderr,"복사 파일 %s을 열 수 없습니다.\n", file2);

		exit(1);

	}

	// 첫번째 파일을 두번째 파일로 복사한다. while( 1 ){

	char *p1 = fgets(buffer1, 1000, fp1);

	char *p2 = fgets(buffer2, 1000, fp2);

	if( p1 == NULL || p2 == NULL )

	break;

	if( strcmp(buffer1, buffer2)!= 0 ) {

		printf("<< %s", buffer1);

		printf(">> %s", buffer2);

	}

}

fclose(fp1);

fclose(fp2);

return 0;

}

 

 

 

 

 

5.

#include <stdio.h>

int main(void) {

FILE *fp = NULL;

FILE *fpout = NULL;

char buffer[1000]= {

	0

}

;

char name[1000]= {

	0

}

;

int korean, math, english;

fp = fopen("sample.txt", "r");

if( fp == NULL ) {

	printf("파일 열기 실패\n");

	return 0;

}

fpout = fopen("sample1.txt", "w");

if( fpout == NULL ) {

	printf("파일 열기 실패\n");

	return 0;

}

while(fgets(buffer, 1000, fp) != NULL ) {

	sscanf(buffer, "%s %d %d %d", name, &korean, &math, &english);

	fprintf(fpout, "%s", name);

	fprintf(fpout, "%10.2f\n", (korean+math+english)/3.0);

}

fclose(fp);

return 0;

}

 

 

6.

#include <stdio.h>

#include <ctype.h>

int main(void) {

FILE *fp = NULL;

int c, count=0;

char file1[100];

printf("파일 이름을 입력하시오: ");

scanf("%s", file1);

// 첫번째 파일을 읽기 모드로 연다.

if( (fp = fopen(file1, "r")) == NULL ) {

	fprintf(stderr,"원본 파일 %s을 열 수 없습니다.\n", file1);

	exit(1);

}

while((c = fgetc(fp)) != EOF ) {

	if( isprint(c) )

	count++;

}

fclose(fp);

printf("문자의 개수는 %d\n", count);

return 0;

}

 

 

7.

#include <stdio.h>

#include <stdlib.h>

int main(void) {

FILE *fp1;

char file1[100];

char buffer[1000];

printf("파일 이름을 입력하시오: ");

scanf("%s", file1);

if( (fp1 = fopen(file1, "w")) == NULL ) {

	fprintf(stderr,"파일 %s을 열 수 없습니다.\n", file1);

	exit(1);

}

while( 1 ) {

	char *p= gets(buffer);

	if( p == NULL )

	break;

	fprintf(fp1, "%s\n", buffer);

}

fclose(fp1);

return 0;

}

 

 

8.

#include <stdio.h>

int main(void) {

FILE *fp1 = NULL;

FILE *fp2 = NULL;

int value, i;

fp1 = fopen("sample1.txt", "w");

if( fp1 == NULL ) {

	printf("파일 열기 실패\n");

	return 0;

}

fp2 = fopen("sample2.txt", "wb");

if( fp2 == NULL ) {

	printf("파일 열기 실패\n");

	return 0;

}

for (i=0;i<100;i++) {

	value = rand();

	fprintf(fp1, "%d", value);

	fwrite(&value, sizeof(int), 1, fp2);

}

fclose(fp1);

fclose(fp2);

return 0;

}

 

 

 

 

 

9.

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[]) {

FILE *fp1, *fp2, *fp3;

char file1[100], file2[100], file3[100];

char buffer[100];

if( argc >= 4 ) {

	strcpy(file1, argv[1]);

	strcpy(file2, argv[2]);

	strcpy(file3, argv[3]);

} else {

	printf("인수가 부족합니다.\n");

	return 0;

}

printf("%s 파일+ %s파일을 %s파일로 복사합니다.\n", file1, file2, file3);

// 첫번째 파일을 읽기 모드로 연다.

if( (fp1 = fopen(file1, "r")) == NULL ) {

	fprintf(stderr,"원본 파일 %s을 열 수 없습니다.\n", file1);

	exit(1);

}

// 두번째 파일을 읽기 모드로 연다.

if( (fp2 = fopen(file2, "r")) == NULL ) {

	fprintf(stderr,"원본 파일 %s을 열 수 없습니다.\n", file2);

	exit(1);

}

// 두번째 파일을 읽기 모드로 연다.

if( (fp3 = fopen(file3, "w")) == NULL ) {

	fprintf(stderr,"결과 파일 %s을 열 수 없습니다.\n", file3);

	exit(1);

}

// 첫번째 파일을 두번째 파일로 복사한다. while( fgets(buffer, 100, fp1) != NULL )

fputs(buffer, fp3);

// 첫번째 파일을 두번째 파일로 복사한다. while( fgets(buffer, 100, fp2) != NULL )

fputs(buffer, fp3);

fclose(fp1);

fclose(fp2);

fclose(fp3);

return 0;

}

 

 

10.

#include <stdio.h>

#include <ctype.h>

int main(void) {

FILE *fp = NULL;

char buffer[500];

int line_number=1;

fp = fopen("sample.txt", "r");

if( fp == NULL ) {

	printf("파일 열기 실패\n");

	return 0;

}

while(fgets(buffer, 500, fp)!=NULL) {

	printf("%d: %s\n", line_number, buffer);

	line_number++;

}

fclose(fp);

return 0;

}

 

 

11.

#include <stdio.h>

#include <string.h>

#define TITLE_SIZE 50

#define NAME_SIZE 50

#define PUBLISHER_SIZE 50

typedef struct BOOK {

char title[TITLE_SIZE];

char author[NAME_SIZE];

char publisher[PUBLISHER_SIZE];

}

BOOK;

void add_record(BOOK library[], int count);

void menu();

int get_input();

void search_record(BOOK library[], int count);

void print_record(BOOK library[], int count);

int main(void) {

int num, count = 0;

BOOK library[30] = {

	'\0'

}

;

while(1) {

	menu();

	num = get_input();

	switch(num) {

		case 1:

		add_record(library, count);

		count++;

		continue;

		case 2:

		print_record(library, count);

		continue;

		case 3:

		search_record(library, count);

		continue;

		case 4:

		return -1;

	}

	return 0;

}

}

void add_record(BOOK library[], int count) {

int type;

fflush(stdin);

printf("도서의 이름:");

gets(library[count].title);

printf("저자:");

gets(library[count].author);

printf("출판사:");

gets(library[count].publisher);

}

void menu() {

printf("====================\n");

printf(" 1. 추가\n");

printf(" 2. 출력\n");

printf(" 3. 검색\n");

printf(" 4. 종료\n");

printf("====================\n");

}

int get_input() {

int num;

printf("정수값을 입력하시오 : ");

scanf("%d",&num);

return num;

}

void search_record(BOOK library[], int count) {

int i;

char title[TITLE_SIZE];

fflush(stdin);

printf("제목: ");

gets(title);

for (i = 0; i < count; i++) {

	if(strcmp(title,library[i].title) == 0) {

		printf("출판사는 %s\n",library[i].publisher);

		return;

	}

}

printf("찾는 책이 테이블에 없습니다.\n");

}

void print_record(BOOK library[], int count) {

int i;

fflush(stdin);

for (i = 0; i < count; i++) {

	printf("제목 : %s\n",library[i].title);

	printf("저자 : %s\n",library[i].author);

	printf("출판사 : %s\n",library[i].publisher);

}

}

 

12.

#include <stdio.h>

int main(void) {

FILE *fp = NULL;

char fname[100];

char word[100];

char buffer[1000];

int count=0;

printf("파일 이름: ");

scanf("%s", fname);

printf("탐색할 단어: ");

scanf("%s", word);

fp = fopen(fname, "r");

if( fp == NULL ) {

	printf("파일 열기 실패\n");

	return 0;

}

while(fgets(buffer, 1000, fp) != NULL ) {

	count++;

	if( strstr(buffer, word) != NULL ) {

		printf("%s:%d %s\n", fname, count, buffer);

	}

}

fclose(fp);

return 0;

}

 

 

13.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main(void) {

FILE *fp1, *fp2;

char file1[100], file2[100];

char buffer[100];

printf("파일 이름: ");

scanf("%s", file1);

printf("파일 이름: ");

scanf("%s", file2);

// 첫번째 파일을 읽기 모드로 연다.

if( (fp1 = fopen(file1, "r")) == NULL ) {

	fprintf(stderr,"파일 %s을 열 수 없습니다.\n", file1);

	exit(1);

}

// 두번째 파일을 쓰기 모드로 연다.

if( (fp2 = fopen(file2, "w")) == NULL ) {

	fprintf(stderr,"파일 %s을 열 수 없습니다.\n", file2);

	exit(1);

}

// 첫번째 파일을 두번째 파일로 복사한다. while( fgets(buffer, 100, fp1) != NULL ) {

char *pos = strtok(buffer, " ");

while(pos != NULL) {

	if( strcmp("Android" , pos)==0 )

	fprintf(fp2, "안드로이드 "); else

	fprintf(fp2, "%s ", pos);

	pos = strtok(NULL, " ");

}

}

fclose(fp1);

fclose(fp2);

return 0;

}

 

 

 

 

 

 

 

더 많은  쉽게 풀어 쓴 C언어EXPRESS 이론 솔루션

 https://chuinggun.tistory.com/category/%EC%86%94%EB%A3%A8%EC%85%98%EB%AA%A8%EC%9D%8C/%EC%89%BD%EA%B2%8C%20%ED%92%80%EC%96%B4%20%EC%93%B4%20C%EC%96%B8%EC%96%B4%20Express%20%EC%9D%B4%EB%A1%A0

더 많은 쉽게 풀어 쓴 C언어EXPRESS 프로그래밍 솔루션 : 

https://chuinggun.tistory.com/category/%EC%86%94%EB%A3%A8%EC%85%98%EB%AA%A8%EC%9D%8C/%EC%89%BD%EA%B2%8C%20%ED%92%80%EC%96%B4%20%EC%93%B4%20C%EC%96%B8%EC%96%B4%20Express%20%EC%8B%A4%EC%8A%B5%EB%AC%B8%EC%A0%9C

 

 

댓글