69,369
社区成员
发帖
与我相关
我的任务
分享
typedef struct node2
{
char booknum[20]; //书号
char stunum[20]; //学号
char stuname[20]; //学生名字
char time[20]; //归还时间
}BO;
#include<time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include"stdafx.h"
#include "Book.h"
extern struct node *head;
extern struct node2 *head2;
extern struct node *index[10];
int larger(BK data1,BK data2) //通用的比较函数,通过condition的不同来进行不同的比较
{
if(strcmp(data1.No,data2.No)==1) //书号1>书号2
return 1;
else
return 0;
}
void InsertOrder(BK data)
{
struct node *p,*p1,*p2;
p2=head;
p=(struct node*)malloc(sizeof(struct node)); //利用指针P申请结点动态空间
p->data =data; //数据域赋值
p->next =NULL; //指针域直接赋值为空,以后根据插入位置在修改
if(head==NULL) //如果链表原来为空的插入
{
head=p; //新插入结点成为头结点
return ; //返回头指针
} //原链表不为空时的插入
while(p2)
{
if(strcmp(p2->data.No,p->data.No)==0)
{
p2->data.ku+=p->data.ku;
p2->data.xc+=p->data.ku;
break;
}
else if(strcmp(p2->data.No,p->data.No)==1)
{
if(head==p2)
head=p;
else
p1->next=p;
p->next=p2;
break;
}
else
{
p1=p2;
p2=p2->next;
}
}
if(!p2) {p1->next=p;p->next=p2;}
}
void printList(char *buff) //通过同pritfNode函数来打印链表信息
{
struct node *p;
char str[1000];
memset(str,0,sizeof(str));
if(head==NULL)
{
strcat(buff,"无记录\r\n");
return ;
}
wsprintf(buff,"书号\t书名\t作者\t库存\t现存\r\n");
for(p=head;p;p=p->next)
{
wsprintf(str,"%s\t%s\t%s\t%d\t%d\r\n",(p->data).No,p->data.Name,p->data.Author,p->data.ku,p->data.xc);
}
strcat(buff,str);
}
void readFile() //将文件中的内容读出置入单链表中
{
BK data;
FILE *fp;
head=NULL;
if((fp=fopen("book.dat","rb"))==NULL) //以读的方式打开指定文件
{
printf("\t\t\t#此次为首次运行,需要先创建新文件#\n");
return ;
}
fread(&data,sizeof(BK),1,fp); //读出第一条记录
while(!feof(fp)) //文件未结束时循环
{
InsertOrder(data); //从文件读出后按书号顺序插入链表
fread(&data,sizeof(BK),1,fp); //在读出下一条记录
}
fclose(fp); //关闭文件
}
void addborrow(BO data2)
{
struct node2 *p,*q;
p=(struct node2 *)malloc(sizeof(struct node2));
p->data2=data2;
q=head2;
head2=p;
p->next=q;
}
void readFile2()
{
BO data2;
FILE *fp;
head2=NULL;
if((fp=fopen("borrow.dat","rb"))==NULL) {fp=fopen("borrow.dat","wr"); return ;}
fread(&data2,sizeof(BO),1,fp);
while(!feof(fp))
{
addborrow(data2);
fread(&data2,sizeof(BO),1,fp);
}
fclose(fp);
}
void saveFile() //将链表中各结点的值一次写入文件
{
struct node *p=head;
FILE *fp;
if((fp=fopen("book.dat","wb"))==NULL)
{
printf("无法打开book.dat\n");
exit(0);
}
if(head==NULL)
{
printf("\n数据为空\n");
return ;
}
while(p)
{
fwrite(&p->data,sizeof(BK),1,fp);
p=p->next;
}
fclose(fp);
}
void saveFile2()
{
struct node2 *p=head2;
FILE *fp;
if((fp=fopen("borrow.dat","wb"))==NULL)
{
printf("无法打开borrow.dat\n");
exit(0);
}
if(head2==NULL)
{
return ;
}
while(p)
{
fwrite(&p->data2,sizeof(BO),1,fp);
p=p->next;
}
fclose(fp);
}
struct node *Index() //索引函数
{
struct node *p;
char sign,temp;
p=head;
index[p->data.No[0]-'0']=p;
sign=temp=p->data.No[0];
p=p->next;
while(p)
{
temp=p->data.No[0];
if(sign!=temp)
{
index[p->data.No[0]-'0']=p;
sign=temp;
p=p->next;
}
else p=p->next;
}
return head;
}
void SearchBook(CString name,char *buff)
{
int god=0;
struct node *p;
char str[1000];
memset(str,0,sizeof(str));
BK pdata;
strcpy(pdata.Name,name);
p=index[1]; //按索引搜索
while(p)
{
if(strstr((p->data).Name,pdata.Name))
{
wsprintf(str,"%s\t%s\t%s\t%d\t%d\r\n",(p->data).No,p->data.Name,p->data.Author,p->data.ku,p->data.xc);
god=1;
p=p->next;
}
else p=p->next;
}
if(god!=1) strcat(buff,"查找失败。");
else {
wsprintf(buff,"书号\t书名\t作者\t库存\t现存\r\n");
strcat(buff,str);
strcat(buff,"查找成功。");
}
void BorrowBook(CString no,CString stunum,CString stuname) //借书
{
char str[200];
memset(str,0,sizeof(str));
int have=0;
struct node *q;
BK data;
BO data2;
if(head==NULL) {MessageBox(NULL,"书库为空"," ",MB_OK);return ;}
strcpy(data.No,no);
q=head;
while(q)
{
if(strcmp(q->data.No,data.No)==0)
{
have=1;break;
}
else q=q->next;
}
if(have==0) {MessageBox(NULL,"没找到相应的图书"," ",MB_OK);return;}
else if(q->data.xc==0) {MessageBox(NULL,"图书现存量为零,请改天再借"," ",MB_OK);}
else
{
strcpy(data2.stunum,stunum);
strcpy(data2.stuname,stuname);
time_t ltime; //time
struct tm *today;
time(<ime);
ltime += 60 * 60 * 24 * 30;
today = localtime(<ime);
char ch[10];
char space[]="-";
itoa(today->tm_year+1900,ch,10);
strcpy(data2.time,ch);
strcat(data2.time,space);
itoa(today->tm_mon+1,ch,10);
strcat(data2.time,ch);
strcat(data2.time,space);
itoa(today->tm_mday,ch,10);
strcat(data2.time,ch);
strcpy(data2.booknum,data.No);
q->data.xc--;
addborrow(data2);
MessageBox(NULL,"借阅成功"," ",MB_OK);
}
}
void pBrorrow(char *buff)
{
struct node2 *p;
p=head2;
if(head2==NULL) {
strcat(buff,"记录为空\r\n");
return ;
}
strcat(buff,"书号\t借阅者\t学号\t归还时间\r\n")
while(p)
{
wsprintf(buff,"%s\t%s\t%s\t%s\r\n",p->data2.booknum,p->data2.stuname,p->data2.stunum,p->data2.time);
p=p->next;
}
}
void ReturnBook(CString stunum,CString booknum)
{
struct node *q;
struct node2 *p,*p2;
BO data2;
strcpy(data2.stunum,stunum);
strcpy(data2.booknum,booknum);
p2=p=head2;
q=head;
while(p)
{
if((strcmp(p->data2.stunum,data2.stunum)==0)&&(strcmp(p->data2.booknum,data2.booknum)==0)) break;
else
{
p2=p;
p=p->next;
}
}
if(p==NULL) {MessageBox(NULL,"无改借阅信息"," ",MB_OK);return ;}
while(q)
{
if(strcmp(q->data.No,data2.booknum)==0) break;
else q=q->next;
}
if(head2=p) head2=NULL;
else
{
p2->next=p->next;
p->next=NULL;
}
q->data.xc++;
MessageBox("还书成功\n");
}