用c语言编写的一个小程序,200行以上,急!!!

2020-07-09 教育 51阅读
这是一款图书管理系统 但是我没有写排序 没有写归还 因为归还和借出都差不多 里面有图书删除操作 学生查找 图书查找 学生输出 图书输出 图书借出 插入图书 插入学生 最多借三本书 没有对把数据格式化写入文件 而是块读入 读出 插入学生和图书 约定学生 图书看、号不为0 输入图书label 和 stu_num 为0时就结束插入 插入全部插在文件末尾 程序很简单 也是随便写的 不过我觉得接近我们这种初学者 以下附上代码
#ifndef B_MANAGEMENT_H
#define B_MANAGEMENT_H
#include
#define MAX_BO 3
#define LENTH_SB sizeof(sb_book)
#define LENTH_ST sizeof(st_book)
#define sb_book struct b
#define st_book struct s
//声明学生的数据结构
struct s
{
char name[20];
int num_student;
char book[MAX_BO][30];
int num_book;
};
//声明图书的数据结构
struct b
{
long book_label;
char book_name[30];
int book_num;
int book_remain;
sb_book* next;
};
//声明图书的操作
sb_book* search_book(const char*,FILE*,sb_book*);//用线性存储结构存储文件数据
void delete_book();
void insert_book(sb_book*);
//声明学生的操作
st_book* search_student(int,FILE *p,st_book*);
void insert_student(st_book*);
void borrow_book(int,const char*,sb_book*,st_book*,FILE*,FILE*);
#endif
void main()
{
delete_book();
}
#include
#include
#include"bk_management.h"
void insert_book(sb_book* pb_book)
{
FILE* fp=NULL;
fp=fopen("D:\\book.txt","a+");
if(fp==NULL)
{
printf("can not open file");
exit(0);
}
if(fwrite(pb_book,LENTH_SB,1,fp)==0)
{
printf("failed to write string to book.txt");
exit(0);
}

fclose(fp);
}
#include"bk_management.h"
#include
#include
#include
void delete_book()
{
long delete_label;
FILE* fp=NULL;
sb_book *p1,*p2,*head=NULL;
printf("please input book_label:");
scanf("%ld",&delete_label);
fp=fopen("D:\\book.txt","r");
if(fp==NULL)
{
printf("can not open file");
exit(0);
}
head=p1=p2=(sb_book*)malloc(LENTH_SB);
fread(p1,LENTH_SB,1,fp);
if(feof(fp)) exit(0);
//将文件中的数据读入内存形成链表,head为链表首指针
while(!feof(fp))
{
p2=p1;
p1=(sb_book*)malloc(LENTH_SB);
fread(p1,LENTH_SB,1,fp);
p2->next=p1;
}
p1->next=NULL;
fclose(fp);
//删除指定的书籍的数据块

p1=p2=head;
while(p1->book_label!=delete_label&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->book_label==delete_label)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
}
else
{
printf("no book you want to delete");

}
//将数据块写会文件
fp=fopen("D:\\book.txt","w");
if(fp==NULL)
{
printf("can not open file");
exit(0);
}
while(head->next!=NULL)
{
fwrite(head,LENTH_SB,1,fp);
head=head->next;
}
fclose(fp);

}
#include
#include"bk_management.h"
void borrow_book(int num_student,const char* book_name,sb_book book[1000],st_book stu[1000],FILE* fp_sb,FILE* fp_st)
{
extern sum_book;
extern sum_student;
sb_book* book_sb;
st_book* stu_st;
stu_st=search_student(num_student,fp_st,stu);
if(stu_st->num_book {
book_sb=search_book(book_name,fp_sb,book);
(book_sb->book_remain)--;
strcpy(stu_st->book[stu_st->num_book],book_name);
(stu_st->num_book)++;
fseek(fp_sb,sum_book*LENTH_SB,0);
fwrite(book_sb,LENTH_SB,1,fp_sb);
fseek(fp_st,sum_student*LENTH_ST,0);
fwrite(stu_st,LENTH_ST,1,fp_st);
}
else printf("you can not borrow book!!!");
}
#include
#include
#include"bk_management.h"
void main()
{
FILE *fp_sb=NULL,*fp_st=NULL;
int stu_num;
char _book[30];
sb_book book[1000];
st_book stu[1000];
fp_sb=fopen("D:\\book.txt","r+");
fp_st=fopen("D:\\stu.txt","r+");
if(fp_sb==NULL || fp_st==NULL)
{
printf("fail to open file");
exit(0);
}
printf("Please input student number:");
scanf("%d",&stu_num);
getchar();
printf("please input book:");
gets(_book);
borrow_book(stu_num,_book,book,stu,fp_sb,fp_st);
getch();
fclose(fp_sb);
fclose(fp_st);
}
#include
#include
#include"bk_management.h"
void insert_student(st_book* pt_book)
{
FILE* fp=NULL;
fp=fopen("D:\\stu.txt","a+");
if(fp==NULL)
{
printf("can not open file");
exit(0);
}
if(fwrite(pt_book,LENTH_ST,1,fp)==0)
{
printf("failed to write string to book.txt");
exit(0);
}

fclose(fp);
}
#include
#include
#include"bk_management.h"
void main()
{
st_book stu[1000];
int i,j;
FILE* fp=NULL;
fp=fopen("D:\\stu.txt","r");
if(fp==NULL)
{
printf("can not open file");
exit(0);
}
for(i=0;i<1000;i++)
{
fread(&stu[i],LENTH_ST,1,fp);
if(feof(fp)) break;
printf("%8d %-20s %8d\n",stu[i].num_student,stu[i].name,stu[i].num_book);
for(j=0;j printf("%-30s, ",stu[i].book[j]);
printf("\n");
}
getch();
fclose(fp);
}
#include
#include"bk_management.h"
#include
int sum_student;
st_book* search_student(int num_stu,FILE* fp,st_book stu[1000])
{
int i;
for(i=0;(!feof(fp))&&i<1000;i++)
{
fread(&stu[i],LENTH_ST,1,fp);
sum_student++;
}
for(i=0;num_stu!=stu[i].num_student&&i ;
sum_student=i;
if(num_stu==stu[i].num_student)
return &stu[i];
else
printf("no student you search");
return NULL;
}
#include
#include"bk_management.h"
#include
void main()
{
FILE* fp=NULL;
st_book _stu[1000];
int num_stu;
st_book* stu;
int j;
fp=fopen("D:\\stu.txt","r");
if(fp==NULL)
{
printf("can not open file");
exit(0);
}
printf("please input student number:");
scanf("%d",&num_stu);
stu=search_student(num_stu,fp,_stu);
if(stu!=NULL)
{
printf("%8d %-20s %8d\n",stu->num_student,stu->name,stu->num_book);
for(j=0;jnum_book;j++)
printf("%-30s, ",stu->book[j]);
printf("\n");
}
fclose(fp);
getch();
}
#include
#include
#include"bk_management.h"
void main()
{
sb_book* p;
FILE* fp=NULL;
fp=fopen("D:\\book.txt","r");
if(fp==NULL)
{
printf("can not open file");
exit(0);
}
while(1)
{
p=(sb_book*)malloc(LENTH_SB);
fread(p,LENTH_SB,1,fp);
if(feof(fp)) break;
printf("%8d %-30s %-3d %-3d\n",p->book_label,p->book_name,p->book_num,p->book_remain);
}
fclose(fp);
getch();

}
#include "bk_management.h"
#include
void main()
{
sb_book *pb;
while(1)
{
pb=(sb_book*)malloc(LENTH_SB);
printf("please input book_label:");
scanf("%ld",&pb->book_label);
getchar();
printf("please input book_name:");
gets(pb->book_name);
printf("please input book_num:");
scanf("%d",&pb->book_num);
pb->book_remain=pb->book_num;
if(pb->book_label==0) break;
insert_book(pb);
}

}
#include "bk_management.h"
#include
void main()
{
st_book* pt;
while(1)
{
pt=(st_book*)malloc(LENTH_ST);
printf("please input num_student:");
scanf("%d",&pt->num_student);
getchar();
printf("please input name:");
gets(pt->name);
strcpy(pt->book[0],"0");
strcpy(pt->book[1],"0");
strcpy(pt->book[2],"0");
pt->num_book=0;
if(pt->num_student==0) break;
insert_student(pt);
}

}
#include
#include"bk_management.h"
#include
int sum_book;
sb_book* search_book(const char* ptr,FILE* fp,sb_book book[1000])
{
int i;
for(i=0;(!feof(fp))&&i<1000;i++)
{
fread(&book[i],LENTH_SB,1,fp);
sum_book++;
}
for(i=0;strcmp(&book[i].book_name,ptr)&&i ;
sum_book=i;
if(!strcmp(&book[i].book_name,ptr))
return &book[i];
else
printf("no book you search");
return NULL;
}
#include
#include
#include"bk_management.h"
void main()
{
FILE* fp=NULL;
char book_name[30];
sb_book* book;
sb_book _book[1000];
fp=fopen("D:\\book.txt","r");
if(fp==NULL)
{
printf("can not open file");
exit(0);
}
printf("please input book_name:");
gets(book_name);
book=search_book(book_name,fp,_book);
if(book!=NULL)
printf("%8d %-30s %-3d %-3d\n",book->book_label,book->book_name,book->book_num,book->book_remain);
fclose(fp);
getch();
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com