📄 libout.c
字号:
#include <stdio.h>
#include "string.h"
#define LEN sizeof(struct book)
#define LEN1 sizeof(struct list)
struct book
{char name[10];
int k;}num;
struct list
{char lname[10];
char passw[6];
struct book1
{bname[10];
int n;}b[4];
int m;}vip;
FILE * fp,* fp1;
void regist()
{int i,qq;
char ch, str[10];
if((fp1=fopen("list","a+"))==NULL)
{puts("open fail!\n");
puts("print any key to coutinue......");
getch();exit(0);}
strcpy(vip.lname,"***");
for(strcpy(vip.lname,"***");strcmp(vip.lname,"/close")!=0;)
{vip.m=0;
for(i=0;i<4;i++)
{vip.b[i].n=0;
strcpy(vip.b[i].bname,"***");}
printf("\t\n input use name:");
loop0:scanf("%s",vip.lname);
qq=0;
rewind(fp1);
while(!eof(fp1))
{fread(str,LEN1,1,fp1);
if(strcmp(str,vip.lname)==0)
qq=1;}
if(qq)
{puts("the name have been used,\nuse other name\n");
goto loop0;}
for(i=0;(ch=str[i])!='\0';i++)
{if(ch!='_'&&ch>'9'&&ch<'A'&&(ch<'0'||ch>'Z'&&ch<'a'||ch>'z'))
{puts("Must as a result of the letter, the numeral,\n get down draws a line the composition");
goto loop0;}
else
{ch=str[0];
if(ch>='0'&&ch<='9')
{puts("the first_char must be The letter or under draws a line\n ");
goto loop0;}}}
puts("Congratulation to you! this name is allow to use.\n");
loop1:{puts("\n\t set password:\n");
scanf("%s",vip.passw);
puts("\n\t make sure this password \n");
scanf("%s",str);}
if(strcmp(str,vip.passw)!=0)
{puts("Password confirmation mistake,Please again tries\n");
goto loop1;}
fseek(fp1,0,2);
if(fwrite(&vip,LEN1,1,fp1)==1)
{strcpy(vip.lname,"/close");
puts("\n\n\tRegisters successfully\n");
}
else
puts("\n\n\t Registration fail\n");}
fclose(fp1);
}
void browse()
{
int a;char str[10];
if((fp=fopen("b_list","r"))==NULL)
{puts("open fail!\n");
puts("print any key to coutinue......");
getch();exit(0);}
do
{puts("select:\n0.EXIT\n1.browse all\n2.browse one book\n");
scanf("%d",&a);
if(a==0);
else if(a==1)
{printf("\n\nbook_name Surplus quantity\n");
while(!eof(fp))
{fread(&num,LEN,1,fp);
printf("\n%s %d",num.name,num.k);}
rewind(fp);}
else if(a==2)
do
{printf("input a book_name:\n");
scanf("%s",str);
if(strcmp(str,"/close")!=0)
{while(!eof(fp))
{fread(&num,LEN,1,fp);
if(strcmp(str,num.name)==0)
{printf("\nbook_name:%s\nSurplus quantity:%d\n",num.name,num.k);
break;}}
if(feof(fp))
puts("NO found\n");
rewind(fp);}
}
while(strcmp(str,"/close")!=0);
else
puts("input error\n");
}
while(a!=0);
fclose(fp);
}
void lend()
{char str1[10]={"***"};
int i,flag1;
if((fp=fopen("b_list","r+"))==NULL)
{puts("open fail!\n");
puts("print any key to coutinue......");
getch();exit(0);}
while(strcmp(str1,"/close")!=0)
{
printf("which book are you want to lend?\n");
i=0;flag1=0;
scanf("%s",str1);
if(strcmp(str1,"/close")==0)
continue;
if(vip.m>3)
{puts("cannot lend anymore!\n");
break;}
for(;i<vip.m;i++)
{if(strcmp(str1,vip.b[i].bname)==0)
{puts("Has already borrowed this book, again borrows\n");
b_a();}
continue;}
i=0;
while(!eof(fp))
{fseek(fp,i*LEN,0);
read(&num,LEN,1,fp);
if(strcmp(str1,num.name)==0)
{flag1=1;break;}
i++;}
if(flag1)
if(num.k>0)
{num.k--;i=vip.m;
strcpy(vip.b[i].bname,str1);
vip.b[i].n++;vip.m++;
printf("%s borrow success\nSurplus quantity:%d\n",str1,num.k);
fseek(fp,i*LEN,0);fwrite(&num,LEN,1,fp);}
else
puts("this book had lend out!\n");
else
puts("no this book!\n");}
fclose(fp);
}
void returns()
{char str2[10]={"***"};
int i,j,flag2,flag3,h;
while(strcmp(str2,"/close")!=0)
{printf("which book are you want to return?\n");
i=0;flag2=0;flag3=0;
scanf("%s",str2);
if(strcmp(str2,"/close")==0)continue;
if((fp=fopen("b_list","r+"))==NULL)
{puts("open fail!\n");
puts("print any key to coutinue......");
getch();exit(0);}
while(feof(fp)==0)
{fseek(fp,i*LEN,0);
fread(&num,LEN,1,fp);
if(strcmp(str2,num.name)==0)
{flag2=1;h=vip.m;break;}
i++;}
for(j=0;j<h;j++)
if(strcmp(str2,vip.b[j].bname)==0)
{flag3=1;break;}
if(flag2 && flag3)
{strcpy(vip.b[j].bname,"***");
printf("%s return successed!\n",str2);
strcpy(str2,vip.b[h-1].bname);strcpy(vip.b[h-1].bname,vip.b[j].bname);
strcpy(vip.b[j].bname,str2);vip.m--;num.k++;
fseek(fp,i*LEN,0);fwrite(&num,LEN,1,fp);}
else puts("input error\n");}
fclose(fp);}
b_a()
{int i,p;
char bb[10],str3[10]={"***"};
while(strcmp(str3,"/close")!=0)
{puts("input book's name you want borrow-again:\n");
gets(str3);
if(strcmp(str3,"/close")==0) break;
for(i=0;i<vip.m;i++)
{while(!feof(fp))
{fp=fread(bb,LEN,1,fp);
if(!strcmp(str3,bb))p=1;}
if(p)
if(vip.b[i].n<3)
{vip.b[i].n++;
puts("borrow-again successed\n");}
else
puts("cannot borrow-again\n");
else
puts("input error\n");}}
}
void land()
{int flag,j,b,l=0;char str4[10]={"***"};
if((fp1=fopen("list","r+"))==NULL)
{puts("open fail!\n");
puts("print any key to coutinue......");
getch();
exit(0);}
loop1:
while(strcmp(str4,"/close")!=0)
{j=0;flag=0;
printf("\t input user name:\n\t");
scanf("%s",str4);
if(strcmp(str4,"/close")==0) continue;
while(!feof(fp1))
{fseek(fp1,j*LEN1,0);
fread(&vip,LEN1,1,fp1);
if(strcmp(str4,vip.lname)==0)
{flag=1;b=j;break;}
j++;}
j=0;
if(flag==0)
{puts("input error\n");l++;
if(l<3)goto loop1;
else regist();}
else
{loop2:puts("\t input password:\n\t");
gets(str4);
if(strcmp(str4,vip.passw)!=0)
{puts("\npassword error!\n");j++;
if(j<3)
goto loop2;
else
goto loop1;}
else
{do
{puts("\nSELECT:\n0.EXIT\n1.borrow\n2.return\n3.borrow-again\n\t");
scanf("%d",&j);
switch(j)
{case 0 : break;
case 1 : lend();break;
case 2 : returns();break;
case 3 : b_a();break;
default: puts("input error!\n");break;
}
}
while(j!=0);
fseek(fp1,b*LEN1,0);
fwrite(&vip,b*LEN1,1,fp1);}}}
fclose(fp1);
}
main()
{int a;
if((fp=fopen("b_list","r"))==NULL)
fp=fopen("b_list","w");
else if((fp1=fopen("list","r+"))==NULL)
fp1=fopen("list","w");
do
{printf("\n\t select:\n\t 0.EXIT\n\t 1.browse\n\t 2.land\n\t");
scanf("%d",&a);
if(a==0);
else if(a==1)
browse();
else if(a==2)
land();
else
puts("\n\tinput error\n Please again tries\n");}
while(a!=0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -