用c语言编写的一个小程序,200行以上,急!!!
展开全部
C语言课程设计报告-------学生成绩简单管理程序一、系统菜单的主要功能(1)输入若干条记录(2)显示所有记录(3)按学号排序(4)插入一条记录(5)按姓名查找,删除一条记录(6)查找并显示一条记录(7)输出统计信息 (新增)(8)从正文中添加数据到结构体数组中(9)将所有数据写入文件中(0)退出程序二、题目分析该题主要考察学生对结构体,指针,文件的操作,以及C语言算法的掌握,所以完成此道题目要求较强的设计能力,尤其是要有一种大局观的意识。如何调程序也非常重要,通过这个程序可以学习到以前调试短程序没有的的经验。菜单中的每一个选项都对应一个子程序,子程序的算法几乎囊获了所有C语言学过的技巧,下面就各个子程序中的功能进行说明:功能1和4的算法相似,输入一条记录到结构体中去,其中有一部很关键,就是通过gets将所有的多余的字符,回车读去,否则就会出错。功能2是显示所有的记录,通过循环输出,格式也比较重要。功能3为按学号排序,因为学号定义成了字符数组的形式,因此在运用冒泡法进行排序的时候,要用到strcmp,strcpy等函数。功能5为按姓名删除记录,先输入姓名,再一一比较,如果没有则返回失败信息,如果找到就将此记录都向前移一位,返回n-1。功能6的算法在5中就已经体现了,输入姓名,一一比较。功能7为新增的功能,因为考虑到原来给出的函数中竟然没有对学生成绩的统计功能,因此新增此功能,可以得出所有的记录个数,最高、最低、平均分,并输出相关的学生信息等。功能8和9是对文件的操作,提前准备好数据。三、程序正文部分#include<stdio.h> /*引用库函数*/#include<stdlib.h>#include<ctype.h>#include<string.h>typedef struct /*定义结构体数组*/{char num[10]; /*学号*/char name[20]; /*姓名*/int score; /*成绩*/}Student;Student stu[80]; /*结构体数组变量*/int menu_select() /*菜单函数*/{char c;do{system("cls"); /*运行前清屏*/printf("\t\t****Students' Grade Management System****\n"); /*菜单选择*/printf("\t\t | 1. Input Records |\n");printf("\t\t | 2. Display All Records |\n");printf("\t\t | 3. Sort |\n");printf("\t\t | 4. Insert a Record |\n");printf("\t\t | 5. Delete a Record |\n");printf("\t\t | 6. Query |\n");printf("\t\t | 7. Statistic |\n");printf("\t\t | 8. Add Records from a Text File|\n");printf("\t\t | 9. Write to a Text file |\n");printf("\t\t | 0. Quit |\n");printf("\t\t*****************************************\n");printf("\t\t\tGive your Choice(0-9):");c=getchar(); /*读入选择*/}while(c<'0'||c>'9');return(c-'0'); /*返回选择*/}int Input(Student stud[],int n) /*输入若干条记录*/{int i=0;char sign,x[10]; /*x[10]为清除多余的数据所用*/while(sign!='n'&&sign!='N') /*判断*/{ printf("\t\t\tstudent's num:"); /*交互输入*/scanf("\t\t\t%s",stud[n+i].num);printf("\t\t\tstudent's name:");scanf("\t\t\t%s",stud[n+i].name);printf("\t\t\tstudent's score:");scanf("\t\t\t%d",&stud[n+i].score);gets(x); /*清除多余的输入*/printf("\t\t\tany more records?(Y/N)");scanf("\t\t\t%c",&sign); /*输入判断*/i++;}return(n+i);}void Display(Student stud[],int n) /*显示所有记录*/{int i;printf("\t\t\t-----------------------------------\n"); /*格式头*/printf("\t\t\tnumber name score\n");printf("\t\t\t-----------------------------------\n");for(i=1;i<n+1;i++) /*循环输入*/{printf("\t\t\t%-16s%-15s%d\n",stud[i-1].num,stud[i-1].name,stud[i-1].score);if(i>1&&i%10==0) /*每十个暂停*/{printf("\t\t\t-----------------------------------\n"); /*格式*/printf("\t\t\t");system("pause");printf("\t\t\t-----------------------------------\n");}}printf("\t\t\t");system("pause");}void Sort_by_num(Student stud[],int n) /*按学号排序*/{ int i,j,*p,*q,s;char t[10];for(i=0;i<n-1;i++) /*冒泡法排序*/for(j=0;j<n-1-i;j++)if(strcmp(stud[j].num,stud[j+1].num)>0){strcpy(t,stud[j+1].num);strcpy(stud[j+1].num,stud[j].num);strcpy(stud[j].num,t);strcpy(t,stud[j+1].name);strcpy(stud[j+1].name,stud[j].name);strcpy(stud[j].name,t);p=&stud[j+1].score;q=&stud[j].score;s=*p;*p=*q;*q=s;}}int Insert_a_record(Student stud[],int n) /*插入一条记录*/{char x[10]; /*清除多余输入所用*/printf("\t\t\tstudent's num:"); /*交互式输入*/scanf("\t\t\t%s",stud[n].num);printf("\t\t\tstudent's name:");scanf("\t\t\t%s",stud[n].name);printf("\t\t\tstudent's score:");scanf("\t\t\t%d",&stud[n].score);gets(x);n++;Sort_by_num(stud,n); /*调用排序函数*/printf("\t\t\tInsert Successed!\n"); /*返回成功信息*/return(n);}int Delete_a_record(Student stud[],int n) /*按姓名查找,删除一条记录*/{ char s[20];int i=0,j;printf("\t\t\ttell me his(her) name:"); /*交互式问寻*/scanf("%s",s);while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判断*/if(i==n){ printf("\t\t\tnot find!\n"); /*返回失败信息*/return(n);}for(j=i;j<n-1;j++) /*删除操作*/{strcpy(stud[j].num,stud[j+1].num);strcpy(stud[j].name,stud[j+1].name);stud[j].score=stud[j+1].score;}printf("\t\t\tDelete Successed!\n"); /*返回成功信息*/return(n-1);}void Query_a_record(Student stud[],int n) /*查找并显示一个记录*/{ char s[20];int i=0;printf("\t\t\tinput his(her) name:"); /*交互式输入*/scanf("\t\t\t%s",s);while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判断*/if(i==n){ printf("\t\t\tnot find!\n"); /*输入失败信息*/return;}printf("\t\t\this(her) number:%s\n",stud[i].num); /*输出该学生信息*/printf("\t\t\this(her) score:%d\n",stud[i].score);}void Statistic(Student stud[],int n) /*新增功能,输出统计信息*/{ int i,j=0,k=0,sum=0;float aver; /*成绩平均值*/for(i=0;i<n;i++) /*循环输入判断*/{sum+=stud[i].score;if(stud[j].score>stud[i].score) j=i;if(stud[k].score<stud[i].score) k=i;}aver=1.0*sum/n;printf("\t\t\tthere are %d records.\n",n); /*总共记录数*/printf("\t\t\tthe hignest score:\n"); /*最高分*/printf("\t\t\tnumber:%s name:%s score:%d\n",stud[j].num,stud[j].name,stud[j].score);printf("\t\t\tthe lowest score:\n"); /*最低分*/printf("\t\t\tnumber:%s name:%s score:%d\n",stud[k].num,stud[k].name,stud[k].score);printf("\t\t\tthe average score is %5.2f\n",aver); /*平均分*/}int AddfromText(Student stud[],int n) /*从文件中读入数据*/{ int i=0,num;FILE *fp; /*定义文件指针*/char filename[20]; /*定义文件名*/printf("\t\t\tInput the filename:");scanf("\t\t\t%s",filename); /*输入文件名*/if((fp=fopen(filename,"rb"))==NULL) /*打开文件*/{ printf("\t\t\tcann't open the file\n"); /*打开失败信息*/printf("\t\t\t");system("pause");return(n);}fscanf(fp,"%d",&num); /*读入总记录量*/while(i<num) /*循环读入数据*/{fscanf(fp,"%s%s%d",stud[n+i].num,stud[n+i].name,&stud[n+i].score);i++;}n+=num;fclose(fp); /*关闭文件*/printf("\t\t\tSuccessed!\n");printf("\t\t\t");system("pause");return(n);}void WritetoText(Student stud[],int n) /*将所有记录写入文件*/{int i=0;FILE *fp; /*定义文件指针*/char filename[20]; /*定义文件名*/printf("\t\t\tWrite Records to a Text File\n"); /*输入文件名*/printf("\t\t\tInput the filename:");scanf("\t\t\t%s",filename);if((fp=fopen(filename,"w"))==NULL) /*打开文件*/{printf("\t\t\tcann't open the file\n");system("pause");return;}fprintf(fp,"%d\n",n); /*循环写入数据*/while(i<n){fprintf(fp,"%-16s%-15s%d\n",stud[i].num,stud[i].name,stud[i].score);i++;}fclose(fp); /*关闭文件*/printf("Successed!\n"); /*返回成功信息*/}void main() /*主函数*/{int n=0;for(;;){switch(menu_select()) /*选择判断*/{case 1:printf("\t\t\tInput Records\n"); /*输入若干条记录*/n=Input(stu,n);break;case 2:printf("\t\t\tDisplay All Records\n"); /*显示所有记录*/Display(stu,n);break;case 3:printf("\t\t\tSort\n");Sort_by_num(stu,n); /*按学号排序*/printf("\t\t\tSort Suceessed!\n");printf("\t\t\t");system("pause");break;case 4:printf("\t\t\tInsert a Record\n");n=Insert_a_record(stu,n); /*插入一条记录*/printf("\t\t\t");system("pause");break;case 5:printf("\t\t\tDelete a Record\n");n=Delete_a_record(stu,n); /*按姓名查找,删除一条记录*/printf("\t\t\t");system("pause");break;case 6:printf("\t\t\tQuery\n");Query_a_record(stu,n); /*查找并显示一个记录*/printf("\t\t\t");system("pause");break;case 7:printf("\t\t\tStatistic\n");Statistic(stu,n); /*新增功能,输出统计信息*/printf("\t\t\t");system("pause");break;case 8:printf("\t\t\tAdd Records from a Text File\n");n=AddfromText(stu,n); /*新增功能,输出统计信息*/break;case 9:printf("\t\t\tWrite to a Text file\n");WritetoText(stu,n); /*循环写入数据*/printf("\t\t\t");system("pause");break;case 0:printf("\t\t\tHave a Good Luck,Bye-bye!\n"); /*结束程序*/printf("\t\t\t");system("pause");exit(0);}}}四、函数调用关系图注:“→”代表调用Input函数打印链表记录Display函数输入若干条记录menu_select()函数选择菜单Sort_by_num函数显示所有记录Delete_a_record函数按姓名查找,删除一条记录Query_a_record查找并显示一条记录Statistic函数输出统计信息 (新增)AddfromText函数从正文中添加数据到结构体数组中Main函数Insert_a_record插入一条记录WritetoText函数 将所有数据写入文件中退出程序Reverse(head)函数按学号排序五、设计测试流程1、进入界面2、输入选项1,回车;按提示输入数据:3、回到主菜单;输入选项7,回车;输入文件名:data.txt,回车;出现成功提示,则读入文件操作成功。4、回到主菜单,输入2,回车每10个暂停显示数据5、回到主菜单,输入3,回车出现排序成功信息。6、回到主菜单,输入4,回车按提示插入一组数据7、回到主菜单,输入5,回车按提示输入姓名,删除数据出现删除成功的信息8、回到主菜单,输入6,回车输入姓名进行查询9、回到主菜单,输入7,回车出现统计信息10、回到主菜单,输入9,回车输入result.txt,回车出现成功写入文件的信息11、回到主菜单,输入0,回车退出系统
展开全部
这是一款图书管理系统 但是我没有写排序 没有写归还 因为归还和借出都差不多 里面有图书删除操作 学生查找 图书查找 学生输出 图书输出 图书借出 插入图书 插入学生 最多借三本书 没有对把数据格式化写入文件 而是块读入 读出 插入学生和图书 约定学生 图书看、号不为0 输入图书label 和 stu_num 为0时就结束插入 插入全部插在文件末尾 程序很简单 也是随便写的 不过我觉得接近我们这种初学者 以下附上代码
#ifndef B_MANAGEMENT_H
#define B_MANAGEMENT_H
#include<stdio.h>
#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<stdio.h>
#include<stdlib.h>
#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<malloc.h>
#include<stdlib.h>
#include<stdio.h>
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<stdio.h>
#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<MAX_BO)
{
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<stdio.h>
#include<stdlib.h>
#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<stdio.h>
#include<stdlib.h>
#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<stdio.h>
#include<stdlib.h>
#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<stu[i].num_book;j++)
printf("%-30s, ",stu[i].book[j]);
printf("\n");
}
getch();
fclose(fp);
}
#include<stdio.h>
#include"bk_management.h"
#include<stdlib.h>
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++)
;
sum_student=i;
if(num_stu==stu[i].num_student)
return &stu[i];
else
printf("no student you search");
return NULL;
}
#include<stdio.h>
#include"bk_management.h"
#include<stdlib.h>
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;j<stu->num_book;j++)
printf("%-30s, ",stu->book[j]);
printf("\n");
}
fclose(fp);
getch();
}
#include<stdio.h>
#include<stdlib.h>
#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<malloc.h>
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<malloc.h>
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<stdio.h>
#include"bk_management.h"
#include<stdlib.h>
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++)
;
sum_book=i;
if(!strcmp(&book[i].book_name,ptr))
return &book[i];
else
printf("no book you search");
return NULL;
}
#include<stdio.h>
#include<stdlib.h>
#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();
}
#ifndef B_MANAGEMENT_H
#define B_MANAGEMENT_H
#include<stdio.h>
#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<stdio.h>
#include<stdlib.h>
#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<malloc.h>
#include<stdlib.h>
#include<stdio.h>
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<stdio.h>
#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<MAX_BO)
{
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<stdio.h>
#include<stdlib.h>
#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<stdio.h>
#include<stdlib.h>
#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<stdio.h>
#include<stdlib.h>
#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<stu[i].num_book;j++)
printf("%-30s, ",stu[i].book[j]);
printf("\n");
}
getch();
fclose(fp);
}
#include<stdio.h>
#include"bk_management.h"
#include<stdlib.h>
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++)
;
sum_student=i;
if(num_stu==stu[i].num_student)
return &stu[i];
else
printf("no student you search");
return NULL;
}
#include<stdio.h>
#include"bk_management.h"
#include<stdlib.h>
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;j<stu->num_book;j++)
printf("%-30s, ",stu->book[j]);
printf("\n");
}
fclose(fp);
getch();
}
#include<stdio.h>
#include<stdlib.h>
#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<malloc.h>
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<malloc.h>
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<stdio.h>
#include"bk_management.h"
#include<stdlib.h>
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++)
;
sum_book=i;
if(!strcmp(&book[i].book_name,ptr))
return &book[i];
else
printf("no book you search");
return NULL;
}
#include<stdio.h>
#include<stdlib.h>
#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();
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询