?? readerms.c
字號:
/**********************************************/
/* ReaderMS.c */
/**********************************************/
#include <stdio.h>
#include <string.h>
#define PAGESIZE 20
#define RDIDSZ 6
#define RDNAMESZ 16
#define GRADESZ 5
#define MAJORSZ 16
struct ReaderInfor
{
char rdID[RDIDSZ];
char Name[RDNAMESZ];
char Grade[GRADESZ];
char Major[MAJORSZ];
};
struct ReaderInfor *cur_reader_ptr;
static const int RDRECSIZE = sizeof(struct ReaderInfor);
static int reader_total = 0;
static int reader_newID_Position = 0; /*記錄下次插入記錄時的起始搜索未知 */
static int reader_delete_total = 0;
/******************************************/
/* funtions declarations */
void Reader_File_Init();
void Get_Reader_Infor();
void Save_Reader_Infor_Word();
int Load_Reader_Record(int recordNum);
void Save_Reader_Node();
int Add_New_Reader();
int reader_Alloc_NewID();
int reader_Search_Null_Record();
void Get_Grade(char *instr);
int Assure_Grade_True(char *str);
void Display_cur_Reader_Record();
void Display_Reader_List();
/**************************************************************/
/* Initailize the book datafile, if the file is not exsit, */
/* to create a new file. If it is exsit and is a new file, */
/* the InforWord is not exsit, to write the new InforWord to */
/* the head of the file */
void Reader_File_Init()
{
char ch;
char str[6];
FILE *fp;
printf("Reader File Initializing...\n");
delay(3000);
if((fp=fopen("ReaderData.txt","r"))==NULL)
{
printf("Open File Error!\n");
printf("Press any key to create a new file!");
getch();
printf("\n");
/*the file is not exist, create the file */
if((fp=fopen("ReaderData.txt","w+"))==NULL)
{
printf("Create File Error!\n");
return;
}
printf("Create file successful!\n");
printf("The file is BookData.txt!\n");
}
ch = fgetc(fp);
fclose(fp); /*打開文件讀出首字符后關閉文件 */
/*因為后面的Save_Reader_Infor_Word()將再次打開文件 */
if(ch==EOF) /*若文件為空時,調用函數Save_Reader_Infor_Word()在文件頭寫入信息字段*/
{
reader_total = 0;
Save_Reader_Infor_Word();
}
return;
}
/*****************************************************/
/* to get the book record total in the data file */
/* and to get the posotion the newID will start form */
void Get_Reader_Infor()
{
FILE *rfp;
int num;
char ch;
char str1[6], str2[6], str3[6];
if((rfp=fopen("ReaderData.txt","r")) != NULL)
{
fgets(str1, 6, rfp);
reader_total = atoi(str1);
fgets(str2, 6, rfp);
reader_newID_Position = atoi(str2);
fgets(str3, 6, rfp);
reader_delete_total = atoi(str3);
num = atoi(str1) - atoi(str3);
printf("There are %d reader records in the reader file.\n", num);
}
else
{
printf("Open File Error!\n");
return;
}
fclose(rfp);
return;
}
/*********************************************************/
/* if the infor_word has been modified, this function to */
/* save the new infor_word to the data file */
void Save_Reader_Infor_Word()
{
FILE *wfp;
char str1[6],str2[6],str3[6];
sprintf(str1, "%-5d", reader_total);
sprintf(str2, "%-5d", reader_newID_Position);
sprintf(str3, "%-5d", reader_delete_total);
if((wfp=fopen("ReaderData.txt","r+")) == NULL)
{
printf("Open File Error!\n");
return;
}
fputs(str1, wfp);
fputs(str2, wfp);
fputs(str3, wfp);
fclose(wfp);
wfp = NULL;
return;
}
/*****************************************************************/
/* to load a record from the file to the pointer(cur_reader_ptr) */
int Load_Reader_Record(int recordNum)
{
FILE *rfp;
char ch;
int step = 0, loop;
struct ReaderInfor *new_Node_ptr;
new_Node_ptr =
(struct ReaderInfor*)malloc(RDRECSIZE);
if(recordNum >= reader_total)
return -1;
if((rfp=fopen("ReaderData.txt","r")) == NULL)
{
printf("Open File Error!\n");
return 0;
}
step = recordNum * (RDRECSIZE+1);
step += 15;
fseek(rfp, step, 0);
ch = fgetc(rfp);
if(ch != '0')
{
fread(new_Node_ptr, RDRECSIZE, 1, rfp);
cur_reader_ptr = new_Node_ptr;
new_Node_ptr = NULL;
}
else
return -1;
fclose(rfp);
return 1;
}
/***********************************************************/
/* when add a new book, this function to save the new node */
/* to the data file */
void Save_cur_Reader_Node()
{
int num, step;
FILE *wfp;
num = atoi(cur_reader_ptr->rdID);
/*找到記錄插入位置 */
step = num * (RDRECSIZE+1);
step += 15;
if((wfp=fopen("ReaderData.txt","r+")) == NULL)
{
printf("Open File Error!\n");
return;
}
fseek(wfp, step, 0);
fputc('1', wfp); /*寫入記錄狀態標志,1表示該記錄有效*/
fwrite(cur_reader_ptr, RDRECSIZE, 1, wfp); /*用數據塊寫入方式 */
printf("Add successfully!\n");
fclose(wfp); /*關閉文件*/
return;
}
/*****************************/
/* Add a new reader */
int Add_New_Reader() /* 返回的新添加讀者號,為添加相應的借閱記錄提供位置 */
{
int num, i, rdID;
char c;
char newID[RDIDSZ], instr[RDNAMESZ], str[RDNAMESZ];
struct ReaderInfor *new_node_ptr;
new_node_ptr =
(struct ReaderInfor*)malloc(RDRECSIZE);
if(new_node_ptr==NULL)
{
printf("WARNING:Memory Error!\n");
return -1;
}
/*分配讀者證號*/
num = reader_Alloc_NewID();
rdID = num;
for(i=RDIDSZ-2; i>=0; i--)
{
newID[i] = num%10 + 48;
num = num/10;
}
newID[RDIDSZ-1] = '\0';
printf("new ReaderID: %s\n", newID);
strcpy(new_node_ptr->rdID, newID);
/*獲得讀者姓名*/
printf("Please Input Reader's name(1~15 chars):\n");
gets(instr);
while(strlen(instr) > RDNAMESZ)
{
printf("Wrong input, it is too long!\n");
printf("Please Input Reader's name(1~15 chars):\n");
gets(instr);
}
sprintf(str, "%-15s", instr);
strcpy(new_node_ptr->Name, str);
/*獲得讀者年級*/
Get_Grade(instr);
strcpy(new_node_ptr->Grade, instr);
/*獲得德讀者專業*/
printf("Please Input Reader's Major(1~15 chars):\n");
gets(instr);
while(strlen(instr) > MAJORSZ)
{
printf("Wrong input, it is too long!\n");
printf("Please Input Reader's Major(1~15 chars):\n");
gets(instr);
}
sprintf(str, "%-15s", instr);
strcpy(new_node_ptr->Major, str);
cur_reader_ptr = new_node_ptr;
new_node_ptr = NULL;
/*詢問是否添加該新書 */
printf("Add this reader? (y/n):");
c = getch();
printf("%c\n", c);
if(c=='y'||c=='Y')
{
Save_cur_Reader_Node(); /*保存讀者信息, 寫入文件*/
free(cur_reader_ptr); /*釋放指針 */
cur_reader_ptr = NULL;
if(rdID==reader_total) /*若記錄是在最后插入的,則記錄總數應該加1*/
{ /*新插入點為最后*/
reader_total++;
reader_newID_Position = reader_total;
}
else
{
reader_newID_Position = rdID+1;
reader_delete_total--; /* 在中間插入一個新紀錄,則中間的無效記錄數目減一 */
}
Save_Reader_Infor_Word();
}
else
{
printf("Adding cancled!\n");
}
free(cur_reader_ptr);
cur_reader_ptr = NULL;
return rdID;
}
/***********************************************************/
/* to allocate a new ID to the new reader automatically */
int reader_Alloc_NewID()
{
int num;
int result, i;
if(reader_newID_Position==reader_total) /*前面沒有空記錄,從后面依次分配*/
num = reader_newID_Position;
/*存在空記錄,調用函數Search_Null_Record()搜索book_newID_Position后的*/
/*第一條空記錄位置作為新數添加的位置 */
else
{
result = reader_Search_Null_Record();
if(result < 0)
{
printf("Allocate newID Failed!\n");
return -1;
}
else
num = result;
}
return(num);
}
/**********************************************************************/
/*start from the book_newID_Position to search the ineffective record */
int reader_Search_Null_Record()
{
char ch;
FILE *rfp;
int step;
if((rfp=fopen("ReaderData.txt","r")) == NULL)
{
printf("Open File Error!\n");
return -1;
}
step = reader_newID_Position * (RDRECSIZE+1);
step += 15;
fseek(rfp, step, 0);
ch = fgetc(rfp); /*讀有效標志 */
while((ch=='1')&&(reader_newID_Position<reader_total))
{
reader_newID_Position++;
fseek(rfp, RDRECSIZE, 1); /*跳過一條記錄*/
ch = fgetc(rfp); /*讀有效標志 */
}
fclose(rfp);
return(reader_newID_Position);
}
/***************************************************/
/* get the grade of the new reader from the user */
void Get_Grade(char *str)
{
int loop;
char instr[GRADESZ];
do /* 檢驗輸入的年級格式是否正確*/
{
printf("Please Input Reader's Grade(4 chars,like:2004):\n");
gets(instr);
loop = Assure_Grade_True(instr);
if(loop==-1)
printf("Wrong input!\n");
}while(loop==-1);
strcpy(str, instr);
return;
}
/****************************************/
/* to judge the grade is legal or not */
int Assure_Grade_True(char *str)
{
int i;
char ch;
if(strlen(str)!= GRADESZ-1)
return -1;
for(i=0; i<GRADESZ-1; i++)
{
ch = *(str+i);
if(ch<48 || ch>57)
return -1;
}
return 1;
}
/******************************************************/
/* display current reader record information */
void Display_cur_Reader_Record()
{
printf("\t %s |", cur_reader_ptr->rdID);
printf(" %s |", cur_reader_ptr->Name);
printf(" %s |", cur_reader_ptr->Grade);
printf(" %s", cur_reader_ptr->Major);
printf("\n");
return;
}
/***************************************/
/* display a list of all readers */
void Display_Reader_List()
{
int i, dsp_total=0;
int loop;
clrscr();
if(reader_total==0)
{
printf("There is no reader!\n");
return;
}
for(i=0; i<reader_total; i++)
{
loop = Load_Reader_Record(i);
if(loop==-1)
continue;
dsp_total++;
if(dsp_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(dsp_total % PAGESIZE==0) /* 分頁顯示 */
{
printf("\nPress any key to the next page!\n");
getch();
}
}
printf("\n There are %d readers total.\n", reader_total - reader_delete_total);
return;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -