?? lendinf.c
字號:
/**********************************************/
/* LendInf.c */
/**********************************************/
#include <stdio.h>
#include <string.h>
#include <dos.h>
#include "ReaderMS.c"
#define ONCEMAX 3 /* 每人借閱的最大數目 */
#define BKIDSZ 6
#define TIMESZ 9
struct LendInfor
{
char rdID[RDIDSZ];
char bkCount;
char bkID[ONCEMAX][BKIDSZ];
char StartTime[ONCEMAX][TIMESZ];
char EndTime[ONCEMAX][TIMESZ];
};
static const int LDRECSIZE = sizeof(struct LendInfor);
struct LendInfor *cur_lend_ptr;
/******************************************/
/* funtions declarations */
void Lend_File_Init();
void Add_Lend_Record(int);
void Load_Lend_Record(int recordNum);
void Save_cur_Lend_Node();
int Overdue_Judge(char *p);
/**************************************************************/
/* 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 Lend_File_Init()
{
char ch;
printf("Lend File Initializing...\n");
delay(3000);
if(fopen("LendData.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(fopen("LendData.txt","w+")==NULL)
{
printf("Create File Error!\n");
return;
}
printf("Create file successful!\n");
printf("The file is LendData.txt!\n");
}
return;
}
/******************************/
/* add a new lend record */
void Add_Lend_Record(int p)
{
int i, num;
char newID[RDIDSZ];
struct LendInfor *new_lend_node;
new_lend_node =
(struct LendInfor *)malloc(LDRECSIZE);
if(new_lend_node==NULL)
{
printf("Memory Error!\n");
return;
}
num = p;
for(i=RDIDSZ-2; i>=0; i--)
{
newID[i] = num%10 + 48;
num = num/10;
}
newID[RDIDSZ-1] = '\0';
strcpy(new_lend_node->rdID, newID);
new_lend_node->bkCount = '0';
for(i=0; i<ONCEMAX; i++)
{
strcpy(new_lend_node->bkID[i], "*****");
strcpy(new_lend_node->StartTime[i], "********");
strcpy(new_lend_node->EndTime[i], "********");
}
cur_lend_ptr = new_lend_node;
new_lend_node = NULL;
Save_cur_Lend_Node();
free(cur_lend_ptr);
cur_lend_ptr = NULL;
return;
}
/********************************************************************/
/* to load a record from the file to the pointer(cur_reader_ptr) */
void Load_Lend_Record(int recordNum)
{
int i;
int step = 0, loop;
char ch;
FILE *rfp;
struct LendInfor *new_Node_ptr;
new_Node_ptr =
(struct LendInfor *)malloc(LDRECSIZE);
if((rfp=fopen("LendData.txt","r")) == NULL)
{
printf("Open File Error!\n");
return;
}
step = recordNum * LDRECSIZE;
fseek(rfp, step, 0);
fread(new_Node_ptr, LDRECSIZE, 1, rfp);
cur_lend_ptr = new_Node_ptr;
new_Node_ptr = NULL;
fclose(rfp);
return;
}
/************************************/
/* to save the current lend record */
void Save_cur_Lend_Node()
{
int num, step;
FILE *wfp;
num = atoi(cur_lend_ptr->rdID);
/*找到記錄插入位置 */
step = num * LDRECSIZE;
if((wfp=fopen("LendData.txt","r+")) == NULL)
{
printf("Open File Error!\n");
return;
}
fseek(wfp, step, 0);
fwrite(cur_lend_ptr, LDRECSIZE, 1, wfp); /*用數據塊寫入方式 */
fclose(wfp); /*關閉文件*/
return;
}
/*********************************************/
/* to judge the lending is overdue or not */
int Overdue_Judge(char *p)
{
long cur_time, p_time;
char instr[TIMESZ], mon_str[3], day_str[3];
struct date *cur_date;
cur_date = (struct date *)malloc(sizeof(struct date));
getdate(cur_date);
sprintf(instr, "%d", cur_date->da_year);
sprintf(mon_str, "%d", cur_date->da_mon);
sprintf(day_str, "%d", cur_date->da_day);
if(cur_date->da_mon < 10)
strcat(instr, "0");
strcat(instr, mon_str);
if(cur_date->da_day < 10)
strcat(instr, "0");
strcat(instr, day_str);
instr[TIMESZ-1] = '\0';
cur_time = atol(instr);
p_time = atol(p);
if(cur_time > p_time)
return 1; /* 過期 */
else
return -1; /* 未過期 */
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -