📄 bookms.c
字号:
}
step = num * (BKRECSIZE+1);
step += (INFORSZ-1)*3;
if((wfp=fopen("BookData.txt","r+")) == NULL)
{
printf("Open File Error!\n");
return;
}
fseek(wfp, step, 0);
fputc('0', wfp); /*修改有效标志,置0表示该记录无效 */
fclose(wfp);
/* 即删除的是最后一个记录,则将最后一条无效记录删除,*/
/* 擦除记录占用的空间,修改记录总数,即记录总数减1 */
if(num==book_record_Total-1)
book_record_Total--;
else
book_delete_total++; /* 无效记录增一 */
/*删除的记录在前面(包括删除的是最后一条),则修改book_newID_Position */
/*下次分配ID时优先分配 */
if(num < book_newID_Position)
book_newID_Position = num;
Save_Book_Infor_Word(); /*保存信息字*/
printf("Delete Successfully!\n");
printf("Press any key to return!\n");
getch();
return;
}
/*******************************************/
/* to be transfered by the Main-Menu */
void Add_New_Reader_Lend()
{
int loop;
loop = Add_New_Reader(); /*添加一个新读者 并 同时添加相应的借阅信息记录*/
if(loop != -1)
Add_Lend_Record(loop); /* 同时添加相应的借阅信息记录 */
}
/***********************************************/
void Search_Reader()
{
static char choice; /* 此处很重要,因为Switch_Search_Reader()中default中*/
/* 递归调用了该函数,设置为static则递归调用时使用同一个 */
/* choice 变量,即只做一次处理,而且可以正确退出*/
do
{
clrscr();
printf(" Searching Menu \n");
printf("1 - Search a reader by a Reader's ID\n");
printf("2 - Search a reader by a Reader's fullname\n");
printf("ESC - Return\n");
printf("Please input your choice:");
choice = getch();
printf("%c\n", choice);
if(choice!=27)
Switch_Search_Reader(choice);
}while(choice!=27);
return;
}
/*************************************************/
void Switch_Search_Reader(char c)
{
switch(c)
{
case('1'):
Search_By_ReaderID();
break;
case('2'):
Search_By_Reader_Fullname();
break;
default:
printf("Invalid choice\n");
printf("Press any key to continue!\n");
getch();
Search_Reader();
break;
}
return;
}
/******************************************/
/* search a reader by the reader's ID */
void Search_By_ReaderID()
{
int num, loop;
num = Get_Reader_ID(); /* 获得读者证号 */
if(num==-1) /* 操作取消 */
return;
loop = Load_Reader_Record(num); /* 将读者基本信息读到当前读者指针 */
if(loop != 1)
{
printf("The Reader is not exsit!\n");
printf("Please check your ID or register an new ID.\n");
printf("Press any key to return!\n");
getch();
return;
}
Load_Lend_Record(num); /* 将借阅信息读到当前借阅指针 */
Display_Reader_Detail();
free(cur_reader_ptr); /*使用完当前指针,一定要释放*/
cur_reader_ptr = NULL;
free(cur_lend_ptr); /*使用完当前指针,一定要释放*/
cur_lend_ptr = NULL;
printf("\n Press any key to continue!\n");
getch();
return;
}
/***********************************************************/
void Search_By_Reader_Fullname()
{
int i, loop, match_total=0;
char ch;
char instr[RDNAMESZ], name_str[RDNAMESZ], temp_str[RDNAMESZ];
printf("Please Input Reader'Fullname(1~15 chars):\n");
gets(instr);
while(strlen(instr) > RDNAMESZ-1)
{
printf("Wrong input, it is too long!\n");
printf("Please Input Reader'Fullname(1~15 chars):\n");
gets(instr);
}
sprintf(name_str, "%-15s", instr);
clrscr();
for(i=0; i<reader_total; i++)
{
loop = Load_Reader_Record(i);
if(loop != 1)
continue;
strcpy(temp_str, cur_reader_ptr->Name);
loop = strcmp(strlwr(temp_str), strlwr(name_str));
if(!loop)
{
match_total++;
if(match_total % PAGESIZE==1)
{
clrscr();
printf("\t Number| Name | Grade | Major \n");
printf("\t-----------------------------------------------------\n");
}
Display_cur_Reader_Record();
free(cur_reader_ptr);
cur_reader_ptr = NULL;
if(match_total % PAGESIZE==0)
{
printf("Press any key to the next page!\n");
getch();
}
}
}
printf("\nThere are %d matching records!\n", match_total);
if(match_total)
{
printf("Press P to continue, Press else key to return!\n");
ch = getch();
if(ch=='p'||ch=='P')
Search_By_ReaderID();
else
return;
}
else
{
printf("Press any key to continue!\n");
getch();
}
return;
}
/********************************************************/
/* Display the current Reader's Detailed information */
void Display_Reader_Detail()
{
int i, num, temp, loop;
num = cur_lend_ptr->bkCount - 48;
clrscr();
printf("\t\t Reader Information \n");
printf("\t\t -----------------------------------------\n");
printf("\t\t | Reader's ID: %s |\n", cur_reader_ptr->rdID);
printf("\t\t | Reader's Name: %s |\n", cur_reader_ptr->Name);
printf("\t\t | Reader's Grade: %s |\n", cur_reader_ptr->Grade);
printf("\t\t | Reader's Major: %s |\n", cur_reader_ptr->Major);
printf("\t\t -----------------------------------------\n");
printf("\t\t Borrowing Information \n");
printf(" -----------------------------------------------------------------------\n");
printf(" Books Total: %c \n", cur_lend_ptr->bkCount);
printf(" -----------------------------------------------------------------------\n");
if(num)
{
printf(" Book ID Book Name O-Time R-Time \n");
// printf("\t*--------------------------------------------------------------------*\n");
}
/*读书目文件,将所借书目信息显示出来*/
for(i=0; i<num; i++)
{
temp = atoi(cur_lend_ptr->bkID[i]);
Load_Book_Record(temp);
loop = Overdue_Judge(cur_lend_ptr->EndTime[i]); /* 判断是否过期 */
printf(" Book %d: %s %s %s %s ",
i+1,
cur_lend_ptr->bkID[i],
cur_book_ptr->bkName,
cur_lend_ptr->StartTime[i],
cur_lend_ptr->EndTime[i]
);
if(loop==1)
printf(" Time Out!");
printf("\n");
free(cur_book_ptr); /*使用完当前指针,一定要释放*/
cur_book_ptr = NULL;
}
printf(" -----------------------------------------------------------------------\n");
return;
}
/************************************/
/* Display the timeout lendings */
void Display_Timeout_Lending()
{
int i, j, loop, num, temp;
int dsp_total = 0;
clrscr();
for(i=0; i<reader_total; i++)
{
loop = Load_Reader_Record(i);
if(loop==-1) /* 若读者不存在则返回 */
continue;
Load_Lend_Record(i);
num = cur_lend_ptr->bkCount - 48;
for(j=0; j<num; j++)
{
loop = Overdue_Judge(cur_lend_ptr->EndTime[j]); /* 判断是否过期 */
if(loop!=1) continue; /* 未过期 */
temp = atoi(cur_lend_ptr->bkID[j]);
Load_Book_Record(temp);
dsp_total++;
if(dsp_total % PAGESIZE==1)
{
clrscr();
printf(" Reader ID | Reader Name | Book ID | Book Name | End-Time \n");
printf(" ----------------------------------------------------------------------------\n");
}
Display_cur_Timeout_Lending(j);
free(cur_book_ptr);
cur_book_ptr = NULL;
if(dsp_total % PAGESIZE==0) /* 分页显示 */
{
printf("\nPress any key to the next page!\n");
getch();
}
}
/*************释放指针(释放内存空间是很重要的)*************** */
free(cur_lend_ptr);
cur_lend_ptr = NULL;
free(cur_reader_ptr);
cur_reader_ptr = NULL;
}
printf("\n There are %d Timeout lendings total!\n", dsp_total);
return;
}
/*****************************************/
/* Display the current timeout lending */
void Display_cur_Timeout_Lending(int p)
{
printf(" %s ", cur_reader_ptr->rdID);
printf(" %s ", cur_reader_ptr->Name);
printf(" %s ", cur_lend_ptr->bkID[p]);
printf(" %s ", cur_book_ptr->bkName);
printf(" %s ", cur_lend_ptr->EndTime[p]);
printf("\n");
return;
}
/************************/
/* delete a reader */
void Delete_Reader()
{
int num, loop, step, result;
FILE *wfp;
num = Get_Reader_ID();
if(num==-1) /* 操作取消 */
return;
/* 看读者是否存在 */
loop = Load_Reader_Record(num);
if(loop != 1)
{
printf("The Reader is not exsit!\n");
printf("Press any key to return!\n");
getch();
return;
}
/* 载入借阅记录 */
Load_Lend_Record(num);
/* 当前借了书,则读者记录不允许删除 */
if(cur_lend_ptr->bkCount!='0')
{
printf("Sorry! The Reader has borrowed some books, You can not delete the record!\n");
printf("Press any key to return!\n");
getch();
return;
}
result = Delet_Verify();
if(result != 1)
{
printf("Delete Cancled!\n");
printf("Press any key to return!\n");
getch();
return;
}
step = num * (RDRECSIZE+1);
step += (INFORSZ-1)*3;
if((wfp=fopen("ReaderData.txt","r+")) == NULL)
{
printf("Open File Error!\n");
return;
}
fseek(wfp, step, 0);
fputc('0', wfp); /*修改有效标志,置0表示该记录无效 */
fclose(wfp);
/* 即删除的是最后一个记录,则将最后一条无效记录删除,*/
/* 擦除记录占用的空间,修改记录总数,即记录总数减1 */
if(num==reader_total-1)
reader_total--;
else
reader_delete_total++; /* 无效记录增一 */
/*删除的记录在前面(包括删除的是最后一条),则修改book_newID_Position */
/*下次分配ID时优先分配 */
if(num < reader_newID_Position)
reader_newID_Position = num;
Save_Reader_Infor_Word(); /*保存信息字*/
printf("Delete Successfully!\n");
printf("Press any key to return!\n");
getch();
return;
}
/*************************************************/
/* get the Reader ID of a reader from the user */
int Get_Reader_ID()
{
int loop, i;
char ch;
char rdID_str[RDIDSZ];
do
{
printf("Please input the reader's ID(5 Numbers,like(00012)):\n");
for(i=0; i<RDIDSZ-1; i++)
{
ch = getch();
printf("%c", ch);
if(ch==27)
return -1;
rdID_str[i] = ch;
}
rdID_str[RDIDSZ-1] = '\0';
loop = Assure_ReaderID_True(rdID_str);
if(loop==-1)
printf("\nWrong input!");
getch();
printf("\n");
}while(loop!=1);
loop = atoi(rdID_str);
return loop;
}
/*******************************************/
/* to judge the input ID is legal or not */
int Assure_ReaderID_True(char *str)
{
int i;
char ch;
for(i=0; i<RDIDSZ-1; i++)
{
ch = *(str+i);
if(ch<48 || ch>57)
return -1;
}
return 1;
}
/********************************************/
/* get the BookID of a book from the user */
int Get_Book_ID()
{
int i, loop;
char ch;
char bkID_str[BKIDSZ];
do
{
printf("Please input the Book ID(5 Numbers,like(00012)):\n");
for(i=0; i<BKIDSZ-1; i++)
{
ch = getch();
printf("%c", ch);
if(ch==27)
return -1;
bkID_str[i] = ch;
}
bkID_str[BKIDSZ-1] = '\0';
loop = Assure_BookID_True(bkID_str);
if(loop==-1)
printf("\nWrong input!\n");
getch();
printf("\n");
}while(loop!=1);
loop = atoi(bkID_str);
return loop;
}
/*******************************************/
/* to judge the input ID is legal or not */
int Assure_BookID_True(char *str)
{
int i;
char ch;
for(i=0; i<BKIDSZ-1; i++)
{
ch = *(str+i);
if(ch<48 || ch>57)
return -1;
}
return 1;
}
/****************************************************/
/* warm the user is sure to delete a record or not */
int Delet_Verify()
{
char ch;
printf("Are you sure to delete this record! (y/n)");
ch = getch();
printf("%c\n", ch);
if(ch=='Y' || ch=='y')
return 1;
else
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -