본문 바로가기
프로그램/C언어 1000제

C언어 120제] C언어 콘서트 CHAPTER 12 Programming 12 소규모의 데이터베이스 프로그램 작성. p491

by 건티 2022. 2. 8.
728x90

출처 : 반크_세계유산 고인돌

 

참고풀이]

//Dev-C++ 5.11로 작업함.
#include <stdio.h>
#include <stdlib.h> //exit(), toupper()
#include <string.h> //strlen(), strstr(), strcmp(), strcpy(), strcat()

FILE *fp1;
struct Book{
char bookname[50];
char author[20];
char publisher[30];
}Books;
char StrLine[255];
int chk;

void Menu();
void Append();
void Print();
void Print_title();
void Print_List(char *Str);
void Find(char *F);

int main(void)
{
   char Chk[255];//검색할 문자열 변수 
   int n;//작업할 번호 선택변수 
   int i;

   while(1)
   {
      //메뉴를 띄운다.
      while(1)
      {
         Menu();
         scanf("%d%*c",&n);//%*c:enter key
         if(n>=1 && n<=4) break;
         printf("1~4번만 선택하세요...\n");
         printf("다시 선택하세요...\n");
      }

      //선택된 작업을 수행한다.
      if(n==4) break;
      switch(n)
      {
         case 1:
            //자료를 추가한다.
            Append(); break;
         case 2:
            //자료를 출력한다. 
            Print(); break;
         case 3:
            //검색할 문자열을 입력한다.
            printf("검색할 자료를 입력하시오 : ");
            gets(Chk);

            //자료를 검색한다. 
            Find(Chk);

      }
      printf("\n");
 
   }
   printf("수고하셨습니다.\n");

   return 0;
}

void Menu()
{
   printf("====================\n");
   printf("1. 추가\n");
   printf("2. 출력\n");
   printf("3. 검색\n");
   printf("4. 종료\n");
   printf("====================\n");
   printf("정수값을 입력하시오 : ");
}

//자료 추가함수 
void Append()
{
   // 도서파일을 추가 모드로 연다.
   if ((fp1 = fopen("book220203.txt", "a")) == NULL)
   {
      fprintf(stderr, "book220203.txt을 열 수 없습니다.\n");
      exit(1);
   }

   printf("도서의 이름 : "); gets(Books.bookname);
   printf("도서의 저자 : "); gets(Books.author);
   printf("출  판  사  : "); gets(Books.publisher);

   fprintf(fp1,"%s, %s, %s,\n",Books.bookname,Books.author,Books.publisher);

   fclose(fp1);
}

//자료 검색함수 
void Find(char *F)
{
   int ns=0;//검색된 문자열이 있는 결과 개수 
   char Line[255];//검색할 문자열이 있는 지 체크할 문자열 
   char L_word[255];//검색된 문자열 
   char F_word[255];//검색할 문자열 
   int i;//반복변수 
 
   // 도서파일을 읽기 모드로 연다.
   if ((fp1 = fopen("book220203.txt", "r")) == NULL)
   {
      fprintf(stderr, "book220203.txt을 열 수 없습니다.\n");
      exit(1);
   }

   //검색할 문자열을 전부 대문자로 변환
   for(i=0;i<strlen(F);i++)
      F_word[i]=toupper(F[i]);

   //자료를 한줄씩 읽어와서 원하는 문자열이 있는 지 체크한다. 
   Print_title();
   while((chk=fgets(StrLine,sizeof(StrLine),fp1)) != NULL)
   {
      //검색 대상 문자열을 전부 대문자로 변환
      for(i=0;i<strlen(StrLine);i++) 
         L_word[i]=toupper(StrLine[i]);

      //검색 문자열이 있는지 체크한다. 
      if(strstr(L_word,F_word)) strcpy(Line, strstr(L_word, F_word));
      else strcpy(Line,"");

      //검색할 문자열이 있으면 찾은 문자열을 출력한다. 
      if(strlen(Line))
      {
         ns++;
         Print_List(StrLine);
      }
   }

   //검색할 문자열이 없으면 없다고 출력한다. 
   if(!ns) printf("%s는 등록된 자료에 없습니다.\n");
}

//자료 출력함수 
void Print()
{
   // 도서파일을 읽기 모드로 연다.
   if ((fp1 = fopen("book220203.txt", "r")) == NULL)
   {
      fprintf(stderr, "book220203.txt을 열 수 없습니다.\n");
      exit(1);
   }

   Print_title();
   while((chk=fgets(StrLine,sizeof(StrLine),fp1)) != NULL)
      Print_List(StrLine);

   fclose(fp1);
}

void Print_title()
{
   printf("     도서제목\t\t\t저    자\t    출 판 사\n");
   for(int i=1;i<=64;i++) printf("=");

   printf("\n");
}

void Print_List(char *Str)
{
   char *token=strtok(StrLine,",");
   while(token != NULL)
   {
      printf("%19s ",token);
      token=strtok(NULL,",");
   }
}

 

참고풀이 결과]

 

원본파일)

book220203.txt
0.00MB

 

 

 

 

대한민국의 아름다운 영토, 독도의 가을

 

반응형

댓글