user-0 提问: 习题纠错

回复:
user: user-0
content: 哦哦哦，是这样，终于解决了，谢谢哈

user: user-1
content: #include <stdio.h> #include <stdlib.h> #include <string.h> char * s_gets(char * st, int n); #define MAXTITL 40 #define MAXAUTL 40 #define MAXBKS 100  struct book {     char title[MAXTITL];     char author[MAXAUTL];     float value; };  /* 14.2 manybook.c int main() {     struct book library[MAXBKS];     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')     {         printf("Now enter the author.\n");         s_gets(library[count].author, MAXAUTL);         printf("Now enter the value.\n");         scanf("%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].value);     }     else         printf("No books? Too bad.\n");      return 0; } */  void show_books_info(struct book **list, int num);  int main() {     struct book library[MAXBKS];     struct book *alphaorder[MAXBKS];     struct book *priceorder[MAXBKS];     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')     {         printf("Now enter the author.\n");         s_gets(library[count].author, MAXAUTL);         printf("Now enter the value.\n");         scanf("%f", &library[count++].value);         while (getchar() != '\n')             continue;         if (count < MAXBKS)             printf("Enter the next title.\n");     }      struct book * temp;      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);          for (int n = 0; n < count; n++)         {             alphaorder[n] = &library[n];             priceorder[n] = &library[n];         }          printf("Here are the books by alpha order:\n");         for (int i = 0; i < count - 1; i++)         {             for (int j = i + 1;j < count; j++)             {                 if(strcmp(alphaorder[i]->title, alphaorder[j]->title) < 0) {   //需要括号                     temp = alphaorder[i];                     alphaorder[i] = alphaorder[j];                     alphaorder[j] = temp;                 }             }             //printf("%s by %s: $%.2f\n",alphaorder[i]->title,alphaorder[i]->author, alphaorder[i]->value);         }         show_books_info(alphaorder, count);         //printf("%s by %s: $%.2f\n",alphaorder[count - 1]->title,alphaorder[count - 1]->author, alphaorder[count - 1]->value);          printf("Here are the books by price order:\n");         for (int k = 0; k < count -1; k++)         {             for (int m = k + 1; m < count; m++)             {                 if(priceorder[k]->value > priceorder[m]->value) {    //需要括号                     temp = priceorder[k];                     priceorder[k] = priceorder[m];                     priceorder[m] = temp;                 }             }             //printf("%s by %s: $%.2f\n",priceorder[k]->title,priceorder[k]->author, priceorder[k]->value);         }         show_books_info(priceorder, count);         //printf("%s by %s: $%.2f\n",priceorder[count - 1]->title,priceorder[count - 1]->author, priceorder[count - 1]->value);     }     else         printf("No books? Too bad.\n");      return 0; }  void show_books_info(struct book **list, int num) {     int i;      printf("After order:\n");     for (i = 0; i < num; i++)             printf("%s by %s: $%.2f\n", list[i]->title, list[i]->author, list[i]->value); }   char * s_gets(char * st, int n) {     char * ret_val;     char * find;      ret_val = fgets(st, n, stdin);     if(ret_val)     {         find = strchr(st, '\n');         if (find)             *find = '\0';         else             while(getchar() != '\n');     }     return ret_val; }  供参考～  该加大括号的没加，输出排序后的，需要用循环，不能在排序的逻辑里输出排序后的内容～