#include
#include
#include
#define M 100 //数组最大个数
struct student
{
int num;
char name[10];
char writer[10];
char press[10];
int data;
float price;
int kucun;
int jieyue;
};
typedef struct student BOOK;
extern BOOK boo[M];
void AppendScore(BOOK *head);
void PrintScore(BOOK *head,int i);
//....删除、修改等函数省略
#include"book.h"
int n=0;
//记录存入信息的个数
/*
函数功能: 提供用户进行菜单选择
参数:void
返回值:char 返回用户选择的菜单项
*/
char Menu(void)
{
char ch;
printf("\n图书管理查询 \n");
printf(" 1.添加图书信息 \n");
printf(" 2.现存图书信息的显示 \n");
printf(" 3.查询 \n");
printf(" 5.删除 \n");
printf(" 6.修改 \n");
printf(" 0.退出\n");
printf("Please Input your choice:");
scanf(" %c", &ch); /*在%c前面加一个空格,将存于缓冲区中的回车符读入*/
return ch;
}
void main()
{
char ch;
BOOK boo[M];
while (1)
{
ch = Menu(); /* 显示菜单,并读取用户输入 */
switch (ch)
{
case'1':AppendScore(boo ); /* 调用成绩添加模块 */
break;
case'2':PrintScore(boo,n); /* 调用成绩显示模块 */
break;
case'0':exit(0); /* 退出程序 */
printf("End of program!");
break;
default:printf("Input error!");
break;
}
}
}
#include"book.h"
extern int n;
void AppendScore(BOOK *head)
{
FILE *fp;
int i;
BOOK *p;
char next='Y';
printf("请输入数据:");
p=head+n;
while(next=='Y')
{
printf("\n请输入编号:");
scanf("%d", &p->num);
printf("请输入书名:");
scanf("%s", p->name);
printf("请输入作者名字");
scanf("%s",p->writer);
printf("请输入出版社名字");
scanf("%s",p->press);
printf("请输入出版日期如2002年1月10号则输入20010110");
scanf("%d",&p->data);
printf("请输入价格(精确到小数点后一位)");
scanf("%f",&p->price);
printf("请输入馆藏书");
scanf("%d",&p->kucun);
printf("请输入借阅数");
scanf("%d",&p->jieyue);
n++;
p++;
getchar();
printf("继续输入吗(Y/N):");
scanf("%c",&next);
}
if((fp=fopen("boo.dat","wb"))==NULL)
{
printf("cannot open file\n");
return;
}
for(i=0;i<n;i++)
if(fwrite(&boo[i],sizeof(BOOK),1,fp)!=1)
printf("file writer errror\n");
fclose(fp);
}
void ShowTable(void)
{
printf("编号\t书名\t作者\t出版社\t出版日期\t价格\t馆藏数\t借阅数\n");
}
void PrintScore(BOOK *head,int n)
{
FILE *fp;
int i;
ShowTable();
if((fp=fopen("boo.dat","rb"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
for(i=0;i<n;i++)
{
fread(&boo[i],sizeof(BOOK),1,fp);
printf("%10d%10s%10s%11s%12s%10f%11d%11d\n",boo[i].num,boo[i].name,boo[i].writer,boo[i].press,boo[i].data,boo[i].price,boo[i].kucun,boo[i].jieyue);
}
fclose(fp);
}