家用空气检测标准:这个C语言程序50分,有没有高手来!!

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/03 04:46:23
1、建立一个存放图书的二进制文件"bookf.dat",在该文件中使每个记录包括图书编号、图书名、出版社、作者和价格。

2、浏览文件"bookf.dat"。

3、用键盘输入某本图书的编号,在文件"bookf.dat"中查找该图书的图书名、出版社、作者和价格。

4、在文件"bookf.dat"末尾添加新记录。

5、按给定编号删除相应的记录。
要求:分别编写函数完成上述各项工作;并在main函数中使用菜单选择某项功能。

/*****************************************
版权所有 陈冠钢
*****************************************/

#include<stdio.h>
#include<string.h>

FILE *fp;

/*书的结构体*/
struct book
{
int number; /*书号*/
char name[100]; /*书名*/
char publisher[200];/*出版社*/
char author[20]; /*作者*/
float price; /*价钱*/
};
struct book book1;

/*
openfile(): 功能,打开bookf.dat文件用于读写,若文件不存在则创建一个。
*/
void openfile()
{
if((fp=fopen("bookf.dat","a+"))==NULL)
{
printf("can not open file bookf.dat!");
}
}

/*
查找与number号相同的记录并显示出来。
*/
int findrecord(int number)
{
if(fp!=NULL)
{
rewind(fp);
while(!feof(fp))
{
fscanf(fp,"%d %s %s %s %f\n",&book1.number,book1.name,book1.publisher,book1.author,&book1.price);
if(book1.number==number)
{
printf("%d %s %s %s %f\n",book1.number,book1.name,book1.publisher,book1.author,book1.price);
}
}
}
return 0;
}

/*
显示整个文件
*/
void viewfile()
{
if(fp!=NULL)
{
rewind(fp);
printf("number \t bookname \t publisher\t Author \t price\n");
while(!feof(fp)&& fp!=NULL)
{
fscanf(fp,"%d %s %s %s %f\n",&book1.number,book1.name,book1.publisher,book1.author,&book1.price);
printf("%d\t %s\t %s\t %s\t %f\n",book1.number,book1.name,book1.publisher,book1.author,book1.price);
}
}
}

/*
添加记录。
*/
void addrecord()
{
if(fp!=NULL)
{
fseek(fp,0,2);
printf("enter number bookname publisher author price:\n");
scanf("%d %s %s %s %f",&book1.number,book1.name,book1.publisher,book1.author,&book1.price);
fprintf(fp,"%d %s %s %s %f\n",book1.number,book1.name,book1.publisher,book1.author,book1.price);
}
else
{
printf("file not open can not add record!\n");
}

}

int main()
{
int p=1,number=0,n=0,i=0;
openfile();
while(p==1 || p==2 || p==3 || p==4)
{
printf("\nenter 1,2,3,4 to choise:");
printf("\n1. find the book from number");
printf("\n2. view the file bookf.dat:");
printf("\n3. add record contining");
printf("\n4. add one record");
printf("\n5. exit\n");
scanf("%d",&p);
switch(p)
{
case 1:
printf("enter number of the book:"); /*根据书号查找书的记录*/
scanf("%d",&number);
findrecord(number);
break;
case 2: /*显示文件所有记录*/
printf("the file bookf.dat: \n");
viewfile();
break;
case 3: /*添加多个记录*/
printf("how many record do you want to add?\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
addrecord();
}
break;
case 4:
addrecord(); /*添加一个记录*/
break;
default:break;
}
}
if(fp!=NULL)
fclose(fp);
return 0;
}

这个其实很简单的,你参照一下通讯录等程序就可以搞定的!