做一个编程题

老大给我们新人出了个题,记录一下自己写的结构混乱的代码= = 


题目说明:


文本文件中有N条书本的记录,每条记录的格式为:“类型编号TABLE类内序列号TABLE书名TABLE条形码”。
其中“类型编号”的范围是1~255;“类内序列号”的范围1~999;“书名”长度小于64字节;“条形码”长度18字节,其中前4字节固定为"ISBN",后14字节为数字。

请用C语言或JAVA编写程序处理文件中的书本记录:
1、交付C语言或JAVA源代码和编译后的demo。
2、执行方式: "./demo books.txt books_sort.txt"。其中books.txt文本文件存有书本记录,作为程序的输入;books_sort.txt作为输出文件,保存排序后的结果。
3、算法要求:
  3.1 按“类型编号”由小到大的顺序排序;
  3.2 “类型编号”相同的记录,按“类内序列号”由大到小排序。
  3.3 如果文件中有不符合范围要求的书本记录,请将其提取出来排放在已经排好序的正确记录的最后面。
4、示例和开发测试数据:在Sample文件夹中有10条,50条,150条和错误数据四种测试数据及其对应的结果。

我的代码如下:

#include <stdio.h>
#include <stdlib.h>
typedef struct book_list
{
int type_number;
int serial_number;
char name[100];
char bar_code[50];
}Book;
Book book[200];

int main()
{
    FILE *fs,*fd;
    if((fs=fopen("D:\books.txt","r"))==NULL)       //读取书本记录
    {
        printf("open file error
");
        exit(0);
    }
    int i=0;
    char str[100];
    for(i = 0; i >= 0; i++)
    {
        fscanf(fs, "%d", &book[i].type_number);      //读类型编号
        fscanf(fs, "%d", &book[i].serial_number);   //读类内序列号
        fscanf(fs, "%[^
]%*c", str);          //读剩下的字符串
        if(feof(fs))
        {
            break;
        }
        char* p1 = strtok(str, "	
");              //用Tab分割字符串
        char* p2 = strtok(NULL, "	
");
        strcpy(book[i].name, p1);
        strcpy(book[i].bar_code, p2);
        printf("%d %d %s %s 
",book[i].type_number, book[i].serial_number, book[i].name, book[i].bar_code);
    }
    /*这里不明白为什么用下面的方法读取150条记录出错
    while(!feof(fs))
    {
        fscanf(fs,"%d %d %s %s
",&book[i].type_number, &book[i].serial_number, book[i].name, book[i].bar_code);
        printf("%d %d %s %s 
",book[i].type_number, book[i].serial_number, book[i].name, book[i].bar_code );
        i++;
    }*/
    int n=i,s=i;
    Book src;
    Book t[200];
    int j;
printf("
");
    for(i=0;i<n;i++)
    {
        char str1[4];
        char str2[14];
        strncpy(str1, book[i].bar_code, 4);
        strncpy(str2, book[n-1].bar_code , 4);
        if (book[i].type_number < 1 || book[i].type_number >255 || book[i].serial_number < 1 || book[i].serial_number > 999 || strlen(book[i].name) > 32 ||strlen(book[i].bar_code) >18 ||strcmp(str1, "ISBN"))  //不符合范围要求的书本记录
        {
            if (book[n-1].type_number < 1 || book[n-1].type_number >255 || book[n-1].serial_number < 1 || book[n-1].serial_number > 999 || strlen(book[n-1].name) > 32 ||strlen(book[n-1].bar_code) >18||strcmp(str2, "ISBN") )
            {
                n--;
                i--;
            }
            else
            {
                src = book[n-1];
                book[n-1] = book[i];
                book[i] = src;
                n--;
            }
        }

    }

    for(i = 0; i < n-1 ; i++)
    {
        for (j = 0;j < n-i-1;j++)
        {
            if(book[j].type_number == book[j+1].type_number)//类型编号相同,按“类内序列号”由大到小排序。
            {
                int a,b;
                for( a = 0; a < n ; a++)
                {
                    for ( b = 0;b < n-b;b++)
                    {
                        if(book[b].serial_number < book[b+1].serial_number)
                        {
                            t[b] = book[b];
                            book[b] = book[b+1];
                            book[b+1] = t[b];
                        }
                    }
                }
            }
        }
    }

    for(i = 0; i < n-1 ; i++)
    {
        for (j = 0;j < n-i-1;j++)
        {
            if(book[j].type_number > book[j+1].type_number) //按“类型编号”由小到大的顺序排序;
            {
                t[j] = book[j];
                book[j] = book[j+1];
                book[j+1] = t[j];
            }
        }
    }


    printf("
");
    if((fd=fopen("D:\books_sort.txt","w"))==NULL)
    {
        printf("cannot open des file
");
        exit(0);
    }

    for(i = 0; i <s;i++)                         //输出排序后的书本记录到txt
    {
        printf("%d %d %s %s
",book[i].type_number, book[i].serial_number, book[i].name, book[i].bar_code);
        fprintf(fd,"%d %d %s %s
",book[i].type_number, book[i].serial_number, book[i].name, book[i].bar_code);
    }
    fclose(fs);
    fclose(fd);
    }
原文地址:https://www.cnblogs.com/zy791976083/p/9928324.html