?? 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); /*修改有效標(biāo)志,置0表示該記錄無效 */
fclose(wfp);
/* 即刪除的是最后一個(gè)記錄,則將最后一條無效記錄刪除,*/
/* 擦除記錄占用的空間,修改記錄總數(shù),即記錄總數(shù)減1 */
if(num==book_record_Total-1)
book_record_Total--;
else
book_delete_total++; /* 無效記錄增一 */
/*刪除的記錄在前面(包括刪除的是最后一條),則修改book_newID_Position */
/*下次分配ID時(shí)優(yōu)先分配 */
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(); /*添加一個(gè)新讀者 并 同時(shí)添加相應(yīng)的借閱信息記錄*/
if(loop != -1)
Add_Lend_Record(loop); /* 同時(shí)添加相應(yīng)的借閱信息記錄 */
}
/***********************************************/
void Search_Reader()
{
static char choice; /* 此處很重要,因?yàn)镾witch_Search_Reader()中default中*/
/* 遞歸調(diào)用了該函數(shù),設(shè)置為static則遞歸調(diào)用時(shí)使用同一個(gè) */
/* 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); /* 將讀者基本信息讀到當(dāng)前讀者指針 */
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); /* 將借閱信息讀到當(dāng)前借閱指針 */
Display_Reader_Detail();
free(cur_reader_ptr); /*使用完當(dāng)前指針,一定要釋放*/
cur_reader_ptr = NULL;
free(cur_lend_ptr); /*使用完當(dāng)前指針,一定要釋放*/
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); /*使用完當(dāng)前指針,一定要釋放*/
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();
}
}
/*************釋放指針(釋放內(nèi)存空間是很重要的)*************** */
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);
/* 當(dāng)前借了書,則讀者記錄不允許刪除 */
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); /*修改有效標(biāo)志,置0表示該記錄無效 */
fclose(wfp);
/* 即刪除的是最后一個(gè)記錄,則將最后一條無效記錄刪除,*/
/* 擦除記錄占用的空間,修改記錄總數(shù),即記錄總數(shù)減1 */
if(num==reader_total-1)
reader_total--;
else
reader_delete_total++; /* 無效記錄增一 */
/*刪除的記錄在前面(包括刪除的是最后一條),則修改book_newID_Position */
/*下次分配ID時(shí)優(yōu)先分配 */
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;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -