본문 바로가기
IT창고/C

카드 관리..(??)

by 창구창고 2008. 2. 18.
반응형
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX 100

int menu(void);
void author_search(void);
void title_search(void);
void enter(void);
void save(void);
void load(void);
void display(int i);

struct book_type{
 unsigned date;
 unsigned char ed;
 unsigned pages;
};


struct catalog{
 char name[80];
 char title[80];
 char pub[80];
 struct book_type book;
} cat[MAX];

int top = 0;

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

 do{
  choice = menu();
  switch(choice){
   case 1: enter();
    break;
   case 2: author_search();
    break;
   case 3: title_search();
    break;
  }
 }while(choice != 4);

 return 0;
}

int menu(void)
{
 int i;
 char str[80];

 printf("Card catalog: \n");
 printf("1. Enter \n");
 printf("2. Search by Author \n");
 printf("3. Search by Title \n");
 printf("4. Quit \n");

 do{
  printf("Choose your selection: ");
  gets(str);
  i = atoi(str);
  printf("\n");
 } while(i<1 || i>5);

 return i;
}

void enter(void)
{
 int i;
 char temp[80];

 for(i=top; i<MAX; i++){
  printf("give me a Author name(enter is quit): ");
  gets(cat[i].name);
  if(!*cat[i].name) break;
  printf("give me a title: ");
  gets(cat[i].title);
  printf("give me a publisher: ");
  gets(cat[i].pub);
  printf("give me a date publication: ");
  gets(temp);
  cat[i].book.date = (unsigned) atoi(temp);
  printf("give me a edition: ");
  gets(temp);
  cat[i].book.pages = (unsigned) atoi(temp);
 }
 top = i;
}

void author_search(void)
{
 char name[80];
 int i, found;

 printf("Name: ");
 gets(name);
 found = 0;
 for(i=0; i<top; i++)
  if(!strcmp(name, cat[i].name)){
   display(i);
   found = 1;
   printf("\n");
  }

 if(!found) printf("no data\n");
}

void title_search(void)
{
 char title[80];
 int i, found;

 printf("Title: ");
 gets(title);
 found = 0;
 for(i=0; i<top; i++)
  if(!strcmp(title, cat[i].title)){
   display(i);
   found = 1;
   printf("\n");
  }
 if(!found) printf("not found\n");
}

void display(int i)
{
 printf("%s\n", cat[i].title);
 printf("by %s\n", cat[i].name);
 printf("Published by %s\n", cat[i].pub);
 printf("Printed: %u, %u edition\n", cat[i].book.date, cat[i].book.ed);
}
반응형

"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."