Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<math.h>
- struct library{
- int number;
- char name[60];
- int lent_amount;
- int remain_amount;
- };
- struct borrower{
- int id;
- char name[20];
- int borrow;
- char bookname[50][50];
- };
- void register_books(void);
- void lend_out(void);
- void return_books(void);
- void list(void);
- void add_borrower(void);
- void inquire_borrower(void);
- void search(void);
- int function,i,j,k,l;
- char * divide;
- char pre_books_divide[300][20][20],pre_books[300][100];
- struct library books[300];
- int m,n,o,p;
- char pre_name_divide[100][20][20],pre_name[100][20];
- char * dive;
- struct borrower borrow[100];
- int main(void)
- {
- //================================================================================
- FILE* ofptr;
- ofptr=fopen("books.dat","r");
- i=0;
- char use[100];
- while(!feof(ofptr)){
- fgets(use,100,ofptr);
- if(*use!='\n'){
- strcpy(pre_books[i],use);
- i++;
- }
- }
- if(isdigit(pre_books[i][0])!=1){
- i--;
- }
- for(j=0;j<i;j++){
- divide=strtok(pre_books[j]," ");
- strcpy(pre_books_divide[j][0],divide);
- books[j].number=atoi(pre_books_divide[j][0]);
- //printf("%d\t",books[j].number);
- k=1;
- while(1){
- divide=strtok(NULL," ");
- if(divide==NULL){
- break;
- }
- strcpy(pre_books_divide[j][k],divide);
- k++;
- }
- for(l=1;l<k-2;l++){
- strcat(books[j].name,pre_books_divide[j][l]);
- if(l+1!=k-2){
- strcat(books[j].name," ");
- }
- }
- //printf("%s\t",books[j].name);
- books[j].lent_amount=atoi(pre_books_divide[j][k-2]);
- books[j].remain_amount=atoi(pre_books_divide[j][k-1]);
- //printf("%d\t%d\n",books[j].lent_amount,books[j].remain_amount);
- }
- fclose(ofptr);
- //==================================================================================
- FILE* pfptr;
- pfptr=fopen("borrower.dat","r");
- m=0;
- char pre[100],hello[100];
- while(!feof(pfptr)){
- fgets(pre,100,ofptr);
- if(*pre!='\n'){
- strcpy(pre_name[m],pre);
- }
- dive=strtok(pre_name[m]," ");
- strcpy(pre_name_divide[m][0],dive);
- borrow[m].id=atoi(pre_name_divide[m][0]);
- //printf("%d\t",books[j].number);
- o=1;
- while(1){
- dive=strtok(NULL," ");
- if(dive==NULL){
- break;
- }
- strcpy(pre_name_divide[m][o],dive);
- o++;
- }
- for(p=1;p<o-1;p++){
- strcat(borrow[m].name,pre_name_divide[m][p]);
- if(p+1!=o-1){
- strcat(borrow[m].name," ");
- }
- }
- //printf("%s\t",books[j].name);
- //books[j].lent_amount=atoi(pre_books_divide[j][k-2]);
- borrow[m].borrow=atoi(pre_name_divide[m][o-1]);
- //printf("%d\t%d\n",books[j].lent_amount,books[j].remain_amount);
- for(n=0;n<borrow[m].borrow;n++){
- char * ch;
- fgets(hello,100,pfptr);
- ch=strtok(hello,"\n");
- strcpy(borrow[m].bookname[n],ch);
- }
- fgets(use,100,ofptr);
- m++;
- }
- fclose(pfptr);
- //=========================================================================
- while(1){
- printf("Welcome to uduck 3000 Library System\n"
- "1. Register new books\n"
- "2. Lend out books\n"
- "3. Return back books\n"
- "4. List all the books in the library\n"
- "5. Add new borrower\n"
- "6. Inquire borrower state\n"
- "7. Search\n"
- "8. Quit\n");
- scanf("%d",&function);
- while(getchar() != '\n')
- ;
- if(function==8){
- break;
- }
- switch(function){
- case 1:
- register_books();
- break;
- case 2:
- lend_out();
- break;
- case 3:
- return_books();
- break;
- case 4:
- list();
- break;
- case 5:
- add_borrower();
- break;
- case 6:
- inquire_borrower();
- break;
- case 7:
- search();
- break;
- }
- }
- }
- void register_books(void){
- char n[100],new_book[100];
- char * newbook;
- char amount_s[10];
- int amount,a,b;
- int det1=0,det2=0,det3=0;
- printf("Please enter the new book's name: ");
- fgets(n,100,stdin);
- newbook=strtok(n,"\n");
- strcpy(new_book,newbook);
- while(1){
- printf("Please enter book amount:");
- scanf("%s",&amount_s);
- if(isdigit(amount_s[0])==1){
- amount=atoi(amount_s);
- while(getchar() != '\n')
- ;
- a=0;
- while(a<i){
- if(strcmp(new_book,books[a].name)==0){
- books[a].remain_amount+=amount;
- det1+=1;
- det2=a;
- }
- a++;
- }
- if(det1!=0){
- FILE* rfptr;
- rfptr=fopen("books.dat","w");
- for(a=0;a<i;a++){
- fprintf(rfptr,"%d %s %d %d\n",books[a].number,books[a].name,books[a].lent_amount,books[a].remain_amount);
- }
- fclose(rfptr);
- }else{
- FILE* rfptr;
- rfptr=fopen("books.dat","a");
- books[i].number=i+1;
- det2=i+1;
- strcpy(books[i].name,new_book);
- books[i].lent_amount=0;
- books[i].remain_amount=amount;
- fprintf(rfptr,"%d %s %d %d\n",books[a].number,books[a].name,books[a].lent_amount,books[a].remain_amount);
- det2=i;
- i++;
- fclose(rfptr);
- }
- break;
- }else{
- printf("Book amount should be a digit!!\n");
- det3+=1;
- }
- }
- if(det3==0){
- printf("Register complete!\n");
- }
- printf("There are %d \"%s\" in the library now!!\n\n",books[det2].remain_amount,new_book);
- printf("Press \"Enter\" for return to main menu.");
- while(getchar() != '\n')
- ;
- }
- void lend_out(void){
- int id,book_id,a,b,c;
- char bookk[10];
- printf("Borrower id: ");
- while(1){
- scanf("%d",&id);
- if(id>m){
- printf("Sorry, no borrower belongs to this id.\n");
- }else{
- break;
- }
- printf("Please enter borrower id again: ");
- }
- printf("Please enter the book id you want to borrow (0 for back): ");
- while(1){
- scanf("%s",&bookk);
- getchar();
- book_id=atoi(bookk);
- if(isdigit(bookk[0])==1&&book_id>0&&book_id<i){
- break;
- }else{
- printf("There is no book with this id!!\n\n");
- }
- printf("Please enter again (0 for back): ");
- }
- if(books[book_id-1].remain_amount==0){
- printf("Sorry, the book you want to borrow is insufficient.\n\n");
- }else{
- printf("%s borrows \"%s\" successfully!!\n\n",borrow[id-1].name,books[book_id-1].name);
- books[book_id-1].remain_amount-=1;
- books[book_id-1].lent_amount+=1;
- strcpy(borrow[id-1].bookname[borrow[id-1].borrow],books[book_id-1].name);
- borrow[id-1].borrow+=1;
- FILE* rfptr;
- rfptr=fopen("books.dat","w");
- for(a=0;a<i;a++){
- fprintf(rfptr,"%d %s %d %d\n",books[a].number,books[a].name,books[a].lent_amount,books[a].remain_amount);
- }
- fclose(rfptr);
- FILE* qfptr;
- qfptr=fopen("borrower.dat","w");
- for(b=0;b<m;b++){
- fprintf(qfptr,"%d %s %d\n",borrow[b].id,borrow[b].name,borrow[b].borrow);
- for(c=0;c<borrow[b].borrow;c++){
- fprintf(qfptr,"%s\n",borrow[b].bookname[c]);
- }
- fprintf(qfptr,"\n");
- }
- fclose(qfptr);
- }
- printf("Press \"Enter\" for return to main menu.\n");
- getchar();
- }
- void return_books(void){
- }
- void list(void){
- int rows,a,pages=0,det2=0;
- char det;
- printf("Rows per page: ");
- scanf("%d",&rows);
- while(getchar() != '\n')
- ;
- while(1){
- printf(" Id Name \t\t\t\t\t Lend Out Remaining\n");
- for(a=0;a<rows;a++){
- if(books[a+pages*rows].number!=0)
- printf("%3d %-50s%3d\t %3d\n",books[a+pages*rows].number,books[a+pages*rows].name,books[a+pages*rows].lent_amount,books[a+pages*rows].remain_amount);
- }
- printf("\n\"1\" to view previous page, \"2\" to view next page\n"
- "Press \"Enter\" for return\n");
- det=getchar();
- if(det=='\n')
- break;
- int zz;
- zz=getchar();
- det2=0;
- switch(det){
- case '1':
- if(pages!=0)
- pages-=1;
- break;
- case '2':
- if(i%rows==0){
- if(pages!=((i/rows)-1))
- pages+=1;
- }else{
- if(pages!=((i/rows)))
- pages+=1;
- }
- break;
- // case 13:
- // case '\n':
- // det2+=1;
- // break;
- }
- }
- }
- void add_borrower(void){
- int a,dett;
- char * ll;
- char n[25],new_user[25];
- printf("Please enter the new borrowers' name: ");
- while(1){
- dett=0;
- fgets(n,25,stdin);
- ll=strtok(n,"\n");
- strcpy(new_user,ll);
- for(a=0;a<m;a++){
- if(strcmp(new_user,borrow[a].name)==0){
- printf("Sorry, %s has been registered.\n"
- "Please type again: ",new_user);
- dett+=1;
- }
- }
- if(dett==0)
- break;
- }
- FILE* lptr;
- lptr=fopen("borrower.dat","a");
- borrow[m].id=m+1;
- strcpy(borrow[m].name,new_user);
- borrow[m].borrow=0;
- fprintf(lptr,"\n%d %s %d\n",borrow[m].id,borrow[m].name,borrow[m].borrow);
- printf("Welcome %s!!\n"
- "Your id is %d\n\n"
- "Press \"Enter\" for return to main menu.\n",borrow[m].name,borrow[m].id);
- m++;
- fclose(lptr);
- getchar();
- }
- void inquire_borrower(void){
- char i[20],inquire[20];
- char * in;
- int inq,x,y,z,get=-1;
- printf("Please enter the user borrower name or id:");
- while(1){
- fgets(i,20,stdin);
- in=strtok(i,"\n");
- strcpy(inquire,in);
- if(isdigit(inquire[0])==1){
- inq=atoi(inquire);
- if(inq>m)
- get=-2;
- else
- get=inq-1;
- }else{
- for(x=0;x<m;x++){
- if(strcmp(inquire,borrow[x].name)==0){
- get=x;
- }
- }
- }
- if(get==-1){
- printf("Sorry, no one names %s in this system.\n"
- "Please type again:",inquire);
- continue;
- }else if(get==-2){
- printf("Sorry, no one's id is %d in this system.\n"
- "Please type again:",inq);
- continue;
- }else{
- printf("%d %s borrows %d books\n",borrow[get].id,borrow[get].name,borrow[get].borrow);
- for(z=0;z<borrow[get].borrow;z++){
- printf("%s\n",borrow[get].bookname[z]);
- }
- printf("\n");
- break;
- }
- }
- printf("Press \"Enter\" for return to main menu.\n");
- getchar();
- }
- void search(void){
- int a,b,count=0,num[200]={0},lent[200],remain[200],pages=0,det2;
- char name[200][60],search[25],det;
- printf("Please enter the search keyword:");
- scanf("%s",&search);
- getchar();
- for(a=0;a<i;a++){
- if(strstr(books[a].name,search)!=NULL){
- num[count]=books[a].number;
- strcpy(name[count],books[a].name);
- lent[count]=books[a].lent_amount;
- remain[count]=books[a].remain_amount;
- count++;
- }
- }
- if(count==0)
- printf("No book matches the pattern \"%s\"\n\n"
- "Press \"Enter\" for return.\n",search);
- else
- printf("There is %d book(s) match \"%s\"\n",count,search);
- while(1){
- det2=0;
- if(count!=0){
- printf(" Id Name \t\t\t\t\t Lend Out Remaining\n");
- for(b=0;b<10;b++){
- if(num[b+pages*10]!=0)
- printf("%3d %-50s%3d\t %3d\n",num[b+pages*10],name[b+pages*10],lent[b+pages*10],remain[b+pages*10]);
- }
- printf("\n\"1\" to view previous page, \"2\" to view next page\n"
- "Press \"Enter\" for return.\n");
- }
- det=getchar();
- if(det=='\n')
- break;
- int zz;
- zz=getchar();
- switch(det){
- case '1':
- if(pages!=0)
- pages-=1;
- break;
- case '2':
- if(count%10==0){
- if(pages!=((count/10)-1))
- pages+=1;
- }else{
- if(pages!=((count/10)))
- pages+=1;
- }
- break;
- // case 13:
- // det2+=1;
- // break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement