본문 바로가기

프로그래밍/c언어

도서관리프로그램

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
void menu();
void input();
void output();
void reference();
void dele();
void bye();
void gotoxy(int x,int y);
void init();

 

FILE *fp;
typedef struct Book_info  //책
{
 int number;
 char name[20]; //도서 제목
 char author[20]; //저자
 char publisher[20]; //출판사
 int price; //가격

 struct Book_info *next;
}BOOK;
BOOK *head,*tail;
int main()
{
 fp=fopen("data.txt","a");
 init();
 menu(); //메뉴
 return 0;
}


void menu() 
{
 char key;

 while(1){
  printf("도서관리프로그램\n");
  puts("--- 메뉴 ---");
  printf("1. 입력\t2. 출력\t3. 검색\t4. 삭제\t5. 종료\t\n");
  printf(">>");
  key=getch();
  fflush(stdin);
  if(key=='1')
   input();
  else if(key=='2')
   output();
  else if(key=='3')
   reference();
  else if(key=='4')
   dele();
  else if(key=='5')
   bye();
 }


}
void init()
{
 head = (BOOK *)malloc(sizeof(BOOK));
 tail = (BOOK *)malloc(sizeof(BOOK));

 head->next = tail;
 tail->next = tail;


}

 


void input()
{

 int p;
 BOOK *book =(BOOK*)malloc(sizeof(BOOK));

 fp=fopen("data.txt","a");
 do{
  fflush(stdin);
  printf("\n도서번호==>\n");
  scanf("%d",&book->number);
  if(book->number<0) //넘버가 0보다 작으면 오류
  {
   printf("번호는 음수/문자 불가능.다시입력하세요\n\n");
  }
 }while(book->number<0); //번호가 0보다 작으면 클때까지 계속 실행


 fflush(stdin);   
 printf("도서명을입력해주세요.\n");   
 scanf("%s",book->name);

 fflush(stdin);
 printf("저자를 입력해주세요.\n");
 scanf("%s",book->author);

 fflush(stdin);
 printf("출판사를 입력해주세요.\n");
 scanf("%s",book->publisher);


 do{
  fflush(stdin);
  printf("가격을 입력해주세요.\n");
  scanf("%d",&book->price);
  if(book->price<0)
  {printf("가격은 음수/문자 불가능.다시입력하세요\n\n");}
 }while(book->price<0);

 book->next=head->next;
 head->next=book;
 printf("입력이 완료되었습니다.\n");


 fprintf(fp,"%d",book->number);
 fprintf(fp,";");
 fprintf(fp,"%s",book->name);
 fprintf(fp,";");
 fprintf(fp,"%s",book->author);
 fprintf(fp,";");
 fprintf(fp,"%s",book->publisher);
 fprintf(fp,";");
 fprintf(fp,"%d\n",book->price);
 fclose(fp);
}

void output()
{
 BOOK *b;


 printf("\n  도서번호\t\t도서명\t\t저자\t\t출판사\t\t가격\n"); 
 printf("----------------------------------------------------------------------------\n");

 for (b=head->next; b != tail; b=b->next)
 {
  printf("%6d\t",b->number);
  printf( "%22s\t",b->name);
  printf( "%14s",b->author);
  printf( "%16s\t",b->publisher);
  printf( "%12d\n",b->price);

 }

 


}


void reference()
{
 int a;
 BOOK *s; 
 s= head;
 printf("찾을 도서 번호를 입력해주세요 : ");
 scanf("%d", &a);
 while (s->number != a)
 {
  s = s->next;
 }

 printf("\n  도서번호\t\t도서명\t\t저자\t\t출판사\t\t가격\n"); 
 printf("----------------------------------------------------------------------------\n");
 printf("%6d\t",s->number);
 printf( "%22s\t",s->name);
 printf( "%14s",s->author);
 printf( "%16s\t",s->publisher);
 printf( "%12d\n",s->price);

}
void dele()
{
 BOOK *pre, *del;
 int n;
 pre = head;
 del = pre->next;

 printf("삭제할 도서 번호를 입력해주세요 : ");
 scanf("%d", &n);
 while (del->number!= n && del != tail)
 {
  pre = pre->next;
  del = pre->next;
 }

 if(del != tail)
 {
  pre->next = del->next;
  free(del);
  printf("삭제되었습니다.\n");
  return 1;
 }
 else
  return 0;

}
void bye()
{
 system("cls");
 printf("\n\n\n\n\n                                 ---------- Good Bye ---------\n");
 gotoxy(10,23);
 printf("Copyright ⓒ 2011 by. 대현\n");
 gotoxy(10,24);
 printf("All pictures cannot be copied without permissions\n");

 exit(0);

}


void gotoxy(int x,int y)
{

 COORD Pos={x-1,y-1};
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Pos);
}

 

 


 

'프로그래밍 > c언어' 카테고리의 다른 글

fflush(stdin);  (0) 2011.01.08
기초중기초(파일입출력)  (0) 2011.01.08
BMI비만도측정 프로그램  (0) 2011.01.07
c언어 학교선배 시험  (0) 2011.01.07
Call by value, Call by reference  (0) 2011.01.07