编辑代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100;
struct book
{
    char name[20];
    int  type;
    int no;
    float price;
};
void book_sort(struct book bk[],int n)
{
    struct book t;
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-i;j++)
        {
            if(bk[j].type>bk[j+1].type)
            {
                t=bk[j];
                bk[j]=bk[j+1];
                bk[j+1]=t;
            }
            else if(bk[j].type==bk[j+1].type)
            {
                if(strcmp(bk[j],bk[j+1])<0)
                {
                    t=bk[j];
                    bk[j]=bk[j+1];
                    bk[j+1]=t;
                }
            }
        }
    }
}
int main()
{
    struct book bk[N];
    FILE *fp;
    int i,j,n,count;
    if((fp=fopen("book.rec","r"))==NULL)
    {
        printf("can not open the file");
        exit(0);
    }
    for(i=0;!feof(fp);i++)
        fscanf(fp,"%s %d %d %f",bk[i].name,&bk[i].type,&bk[i].no,&bk[i].price);
    fclose(fp);
    n=i;
    book_sort(bk,n);
    fp=fopen("book.rec","w");
    for(i=0;i<n;i++)
        fprintf(fp,"%s %d %d %f",bk[i].name,bk[i].type,bk[i].no,bk[i].price);
    fclose(fp);
    printf("类别\t数量\n");
    for(i=0;i<n;i++)
    {
        count=1;
        for(j=i+1;j<n;j++)
        {
            if(bk[j].type==bk[j+1].type)
                count++;
            else
                break;
        }
        printf("%d\t%d\n",bk[i].type,count);
    }
    printf("--------\n");
    printf("共%d本\n",n);
    return 0;
}