Skip to content

Instantly share code, notes, and snippets.

@zzzz465
Created June 10, 2019 10:41
Show Gist options
  • Save zzzz465/2d42f9c88f29c777986fe755d5e93212 to your computer and use it in GitHub Desktop.
Save zzzz465/2d42f9c88f29c777986fe755d5e93212 to your computer and use it in GitHub Desktop.
과제3-2(ISBN)
#include <stdio.h>
#include <string.h>
int ReturnIndexByBookName(char []);
int SearchBookByAuthor(char []);
int _RentalBook(char []);
int _ReturnBook(char []);
int _PrintAllData(char);
typedef struct
{
int ISBN;
char BookName[60];
char Author[20];
int ReleasedYear;
int isAvailable;
} BookData;
BookData Library[1000];
int dataLength;
int AddBook()
{
printf("데이터를 입력해주세요\n");
int ISBN, ReleasedYear; char BookName[60], Author[20];
scanf("%d %s %s %d", &ISBN, BookName, Author, &ReleasedYear);
for(int count = 0; count < dataLength; count++)
{
if(Library[count].ISBN == ISBN)
{
printf("오류 : ISBN 중복\n");
return -1;
}
}
Library[dataLength].ISBN = ISBN;
Library[dataLength].ReleasedYear = ReleasedYear;
strcpy(Library[dataLength].BookName, BookName);
strcpy(Library[dataLength].Author, Author);
Library[dataLength].isAvailable = 1;
dataLength++;
return 0;
}
//책 대여~반납 함수들
int RentalBook()
{
char BookName[60];
printf("책 이름을 입력해주세요 : ");
fflush(stdin);
scanf("%s", BookName);
_RentalBook(BookName);
return 0;
}
int _RentalBook(char BookName[])
{
int index = ReturnIndexByBookName(BookName);
if(index != -1)
{
BookData *Book = &Library[index];
if(Book->isAvailable == 0)
{
printf("오류 : 도서 대출 중\n");
return -1;
}
Book->isAvailable = 0;
printf("대출이 완료되었습니다. 도서명 : <%s> | ISBN : <%d>\n", Book->BookName, Book->ISBN);
return 0;
}
printf("오류 : 존재하지 않는 도서\n");
return -2;
}
int ReturnBook()
{
char BookName[60];
printf("책 이름을 입력해주세요 : ");
fflush(stdin);
scanf("%s", BookName);
_ReturnBook(BookName);
return 0;
}
int _ReturnBook(char BookName[])
{
int index = ReturnIndexByBookName(BookName);
if(index != -1)
{
BookData *book = &Library[index];
if(book->isAvailable == 1)
{
printf("오류 : 대출 가능한 도서\n");
return -1;
}
printf("반납이 완료되었습니다. 도서명 : <%s> | ISBN : <%d>\n", book->BookName, book->ISBN);
book->isAvailable = 1;
return 0;
}
printf("오류 : 존재하지 않는 도서명\n");
return -2;
}
// Index를 반환하는 함수들
int ReturnIndexByISBN(int ISBN)
{
for(int count = 0; count < dataLength; count++)
{
if(Library[count].ISBN == ISBN)
return count;
}
return -1;
}
int ReturnIndexByBookName(char BookName[])
{
for(int count = 0; count < dataLength; count++)
{
if(strcmp(Library[count].BookName, BookName) == 0)
return count;
}
return -1;
}
int PrintDataForMenu()
{
char input;
printf("메뉴를 입력하세요\nA : 대출 가능한 모든 도서 출력\nB : 모든 도서 출력\n 입력 : ");
fflush(stdin);
scanf("%s", &input);
_PrintAllData(input);
return 0;
}
//데이터 프린트 공간
int _PrintData(int index)
{
BookData D = Library[index];
printf("%d %s %s %d %d\n", D.ISBN, D.BookName, D.Author, D.ReleasedYear, D.isAvailable);
return 0;
}
int _PrintAllData(char Menu)
{
if(Menu == 'A' || Menu == 'a')
{
for(int count = 0; count < dataLength; count++)
{
if(Library[count].isAvailable)
_PrintData(count);
}
return 0;
}
else if(Menu == 'B' || Menu == 'b')
{
for(int count = 0; count < dataLength; count++)
{
_PrintData(count);
}
return 0;
}
printf("잘못된 입력입니다.\n");
return 0;
}
//책 검색 함수들 위치
int SearchBookByISBN(int ISBN)
{
int value = ReturnIndexByISBN(ISBN);
if(value == -1)
printf("존재하지 않는 ISBN 코드\n");
else
_PrintData(value);
return 0;
}
int SearchBook()
{
char input;
printf("검색 방식을 입력해주세요 : ");
fflush(stdin);
scanf("%c", &input);
printf("\n");
if(input == 'A' || input == 'a')
{
int ISBN;
printf("ISBN 코드를 입력해주세요 : ");
fflush(stdin);
scanf("%d", &ISBN);
SearchBookByISBN(ISBN);
printf("\n");
return 0;
}
else if(input == 'B' || input == 'b')
{
char Bookname[60];
printf("책 이름을 입력해주세요 : ");
fflush(stdin);
scanf("%s", Bookname);
SearchBookByAuthor(Bookname);
printf("\n");
return 0;
}
return 0;
}
int SearchBookByAuthor(char Author[])
{
int flag = 1;
for(int count = 0; count < dataLength; count++)
{
if(strcmp(Author, Library[count].Author) == 0)
{
_PrintData(count);
flag = 0;
}
}
if(flag)
{
printf("오류 : 존재하지 않는 저자명\n");
}
return 0;
}
int MainLoop()
{
int input;
while(1)
{
printf("1 : 도서 정보 입력\n2 : 도서 검색\n3 : 도서 대출\n4 : 도서 반납\n5 : 도서 정보 출력\n6 : 프로그램 종료\n");
printf("메뉴를 입력해주세요 : ");
fflush(stdin);
scanf( "%d", &input);
printf("\n");
switch (input)
{
case 1:
AddBook();
break;
case 2:
SearchBook();
break;
case 3:
RentalBook();
break;
case 4:
ReturnBook();
break;
case 5:
PrintDataForMenu();
break;
case 6:
printf("프로그램을 종료합니다...");
return 0;
default:
break;
}
}
}
int main()
{
MainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment