C pirmer Plus(第五版) 第十四章 课后习题 3
来源:互联网 发布:波尔代热斯现象知乎 编辑:程序博客网 时间:2024/06/08 05:37
#include <stdio.h>
#include <stdlib.h>
#define MAXITL 40
#define MAXAUTL 40
#define MAXBKS 10
struct book{
char title[MAXITL];
char author[MAXAUTL];
float value;
};
int cmp1(const void * a,const void *b)
{
struct book *c=(struct book*)a;
struct book *d=(struct book*)b;
return strcmp(c->title,d->title);
}
int cmp2(const void *a,const void *b)
{
return (*(struct book *)a).value>(*(struct book *)b).value;
}
int main(int argc, char **argv)
{
//printf("hello world\n");
//
struct book library[MAXBKS];
int count=0;
int index,filecount;
FILE * pbooks;
int size=sizeof(struct book);
if((pbooks=fopen("book.dat","a+b"))==NULL)
{
fputs("Can't open book.dat file\n",stderr);
exit(1);
}
rewind(pbooks);
while(count<MAXBKS&&fread(&library[count],size,1,pbooks)==1)
{
if(count==0)
puts("Current contents of book.dat:");
printf("%s by %s:$%.2f\n",library[count].title,library[count].author,library[count].value);
count++;
}
printf("\n");
filecount=count;
count=0;
while(count<MAXBKS&&count<filecount)
{
qsort(library,filecount,sizeof(struct book),cmp1);
if(count==0)
puts("Current contents of book.dat:");
printf("%s by %s:$%.2f\n",library[count].title,library[count].author,library[count].value);
count++;
}
printf("\n");
filecount=count;
count=0;
while(count<MAXBKS&&count<filecount)
{
qsort(library,filecount,sizeof(struct book),cmp2);
if(count==0)
puts("Current contents of book.dat:");
printf("%s by %s:$%.2f\n",library[count].title,library[count].author,library[count].value);
count++;
}
if(count==MAXBKS)
{
fputs("The book.dat file is full.",stderr);
exit(2);
}
puts("Please add new book titles.");
puts("Press [enter] at the start of a line to stop.");
while(count<MAXBKS&&gets(library[count].title)!=NULL&&library[count].title[0]!='\0')
{
puts("Now enter the author.");
gets(library[count].author);
puts("Now enter the value.");
scanf("%f",&library[count++].value);
while(getchar()!='\n')
continue;
if(count<MAXBKS)
puts("Enter the next title.");
}
if(count>0)
{
puts("Here is the list of your books:");
for(index=0;index<count;index++)
printf("%s by %s: $%.2f\n",library[index].title,library[index].author,library[index].value);
fwrite(&library[filecount],size,count-filecount,pbooks);
}
else
puts("No books?Too bad.\n");
puts("Bye.\n");
fclose(pbooks);
return 0;
//printf("Please enter the book title.\n");
/*gets(library.title);
printf("Now enter the author.\n");
gets(library.author);
printf("Now enter the value.\n");
scanf("%f",&library.value);
printf("%s by %s:$%.2f\n",library.title,library.author,library.value);
printf("%s:\"%s\"($%.2f)\n",library.author,library.title,library.value);*/
}
- C pirmer Plus(第五版) 第十四章 课后习题 3
- C Primer Plus(第五版) 第十四章 课后习题 4
- C Primer Plus(第五版) 第十四章 课后习题 6
- C Primer Plus(第五版) 第十四章 课后习题5
- C++Pirmer(第五章课后习题)(程序题源代码)
- C++Primer 中文版 第五版 第五章课后习题答案
- c++primer 第五版课后习题 第五章
- C primer plus(第五版)编程练习第十四章
- C Primer Plus(第五版) 第十四章 第八题
- C++Primer Plus(第六版) 第十四章 第五题
- c primer plus第十一章课后习题
- C Primer Plus 第五版 课后练习
- C++Primer 中文版 第五版 第二章课后习题答案
- C++Primer 中文版 第五版 第三章课后习题答案
- C++Primer 中文版 第五版 第四章课后习题答案
- C++Primer 中文版 第五版 第六章课后习题答案
- C++Primer 中文版 第五版 第七章课后习题答案
- C++Primer 中文版 第五版 第九章课后习题答案
- MapReduce
- ubuntu环境hadoop全分布式模式配置过程
- 数字转换成字符串,int 转换成const char*
- 谈谈eclipse使用技巧二
- 解决oracle数据库中空表的导出
- C pirmer Plus(第五版) 第十四章 课后习题 3
- java集合类Stack类
- Swift Runtime分析
- iOS开发中UITextField限制输入文字个数
- JAVA的StringBuffer类
- C语言 计算a-|b|,aj减b的绝对值
- OJ
- centos 启动脚本
- 《Linux内核设计与实现》读书笔记(十四)- 块I/O层