#include<stdio.h>
#include<string.h>
char* s_gets(char*, int);
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100
struct book {
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
int main(void)
{
struct book library[MAXBKS];
struct book* pstc[MAXBKS];
struct book* temp;
int count = 0;
int index;
printf("Please enter the book title.\n");
printf("Press [enter] at the start of a line to stop.\n");
while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL
&& library[count].title[0] != '\0')
{
pstc[count] = &library[count];
printf("Now enter the author.\n");
s_gets(library[count].author, MAXAUTL);
printf("Now enter the value.\n");
scanf_s("%f", &library[count++].value);
while (getchar() != '\n')
continue;
if (count < MAXBKS)
printf("Enter the next title.\n");
}
if (count > 0)
{
printf("Here is the list of your books:\n");
for (index = 0; index < count; index++)
printf("%s by %s:&%.2f\n", library[index].title,
library[index].author, library[index].value);
printf("\nHere is another list of your books:\n");
while(--index>=0)
printf("%s by %s:&%.2f\n", library[index].title,
library[index].author, library[index].value);
for(int i=0;i<count-1;i++)
for (index = i + 1; index < count; index++)
{
if (strncmp(library[i].title, library[index].title, MAXTITL) > 0)
{
temp = pstc[i];
pstc[i] = pstc[index];
pstc[index] = temp;
}
}
printf("\nHere is another list of your books:\n");
for (index = 0; index < count; index++)
printf("%s by %s:&%.2f\n", pstc[index]->title, pstc[index]->author, pstc[index]->value);
for (int i = 0; i < count - 1; i++)
for (index = i + 1; index < count; index++)
{
if (pstc[i]->value>pstc[index]->value)
{
temp = pstc[i];
pstc[i] = pstc[index];
pstc[index] = temp;
}
}
printf("\nHere is another list of your books:\n");
for (index = 0; index < count; index++)
printf("%s by %s:&%.2f\n", pstc[index]->title, pstc[index]->author, pstc[index]->value);
}
else printf("No books?Too bad.\n");
return 0;
}
char* s_gets(char*st, int n)
{
char* ret_val,*find;
if (ret_val = fgets(st, n, stdin))
if (find=strchr(st, '\n'))
*find = '\0';
else
while (getchar() != '\n')
continue;
return ret_val;
}
C PRIMER PLUS 第14章 第3题
最新推荐文章于 2024-05-20 22:25:23 发布