hyman_panda
2022-01-11 22:04
采纳率: 83.3%
浏览 40

为什么不能运行?该如何修改?

void sort()
{
FILE* fp;
int a=0;
char na[10],i[10],j[10];
char clas[10],stu_code[15],id_code[20];
char sex[5], enyear[10], birthday[20], major[20], name[10];
struct stu_node* tail = NULL, * head = NULL, * pMove = NULL, * n = NULL,* pNext = NULL,temp= NULL;
if ((fp = fopen("test.dat", "r")) == NULL)
{
fp = fopen("test.dat", "w+");
fclose(fp);
return;
}
while (1)
{
if (fscanf(fp,"%s%s%s%s%s%s%s%s\n", name, sex, major, clas,
birthday, stu_code, id_code, enyear) != 8) break;
n = (struct stu_node
)malloc(sizeof(struct stu_node));
n->next = NULL;
strcpy(n->clas,clas);strcpy(n->stu_code,stu_code);
strcpy(n->id_code,id_code);
strcpy(n->name, name); strcpy(n->sex, sex);
strcpy(n->major, major); strcpy(n->enyear, enyear);
strcpy(n->birthday, birthday);
if (head == NULL)
head = n;
else
tail->next = n;
tail = n;
}
fclose(fp);
while(a!=3)
{
printf("----------1.按班级排序-------------\n");
printf("----------2.按学籍号码排序----------\n");
printf("----------3.退出------------------\n");
scanf("%d",&a);
switch(a)
{
case 1: pMove=head;
while(pMove)
{
pNext = pMove->next;
while (pNext != NULL)
{
if (strcmp(pMove->clas, pNext->clas) > 0)
{
temp->clas = pMove->clas;
pMove->clas = pNext->clas;
pNext->clas = temp->clas;
}
pNext = pNext->next;
}
pMove = pMove->next;
}
pMove = head->next;break;
case 2:
pMove=head;
while(pMove!=NULL)
{
pMove=pMove->next;
}break;
case 3:printf("退出排序!\n");return;
default :printf("输入错误,请重新输入!\n");
}
pMove=head;
while (pMove != NULL)
{
printf("%5s%5s%20s%5s%20s%20s%20s%10s\n", pMove->name, pMove->sex, pMove->major,
pMove->clas, pMove->birthday, pMove->stu_code, pMove->id_code, pMove->enyear);
pMove = pMove->next;
}
}
}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • qzjhjxj 2022-01-12 11:20
    最佳回答

    修改如下,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct dat {
        char name[5];
        char sex[5];
        char major[20];
        char clas[5];
        char birthday[20];
        char stu_code[20];
        char id_code[20];
        char enyear[10];
    }Data;
    typedef struct lnode {
        Data   data;
        struct lnode* next;
    }LNode;
    void sort()
    {
        FILE* fp;
        int  a = 0;
        char na[10], i[10], j[10];
        char clas[10], stu_code[15], id_code[20];
        char sex[5], enyear[10], birthday[20], major[20], name[10];
        LNode* tail = NULL, * head = NULL, * pMove = NULL, * n = NULL, * pNext = NULL;
        Data  temp;
        if ((fp = fopen("test.dat", "r")) == NULL)
        {
            printf("open file fail!\n");
            return;
        }
        while (1)
        {
            if (fscanf(fp, "%s %s %s %s %s %s %s %s\n", name, sex, major, clas,
                                       birthday, stu_code, id_code, enyear) != 8) break;
            n = (LNode*)malloc(sizeof(LNode));
            n->next = NULL;
            strcpy(n->data.clas, clas); strcpy(n->data.stu_code, stu_code);
            strcpy(n->data.id_code, id_code);
            strcpy(n->data.name, name); strcpy(n->data.sex, sex);
            strcpy(n->data.major, major); strcpy(n->data.enyear, enyear);
            strcpy(n->data.birthday, birthday);
            if (head == NULL)
                head = n;
            else
                tail->next = n;
            tail = n;
        }
        fclose(fp);
        while (a != 3)
        {
            printf("----------1.按班级排序-------------\n");
            printf("----------2.按学籍号码排序----------\n");
            printf("----------3.退出------------------\n");
            scanf("%d", &a);
            switch (a)
            {
            case 1: 
                pMove = head;
                while (pMove)
                {
                    pNext = pMove->next;
                    while (pNext != NULL)
                    {
                        if (strcmp(pMove->data.clas, pNext->data.clas) > 0)
                        {
                            temp= pMove->data;
                            pMove->data = pNext->data;
                            pNext->data = temp;
                        }
                        pNext = pNext->next;
                    }
                    pMove = pMove->next;
                }
                break;
            case 2:
                pMove = head;
                while (pMove)
                {
                    pNext = pMove->next;
                    while (pNext != NULL)
                    {
                        if (strcmp(pMove->data.id_code, pNext->data.id_code) > 0)
                        {
                            temp = pMove->data;
                            pMove->data = pNext->data;
                            pNext->data = temp;
                        }
                        pNext = pNext->next;
                    }
                    pMove = pMove->next;
                }
                break;
            case 3:printf("退出排序!\n"); return;
            default:printf("输入错误,请重新输入!\n"); break;
            }
            pMove = head;
            while (pMove != NULL)
            {
                printf("%5s%5s%20s%5s%20s%20s%20s%10s\n", pMove->data.name,
                         pMove->data.sex, pMove->data.major, pMove->data.clas,
                        pMove->data.birthday, pMove->data.stu_code,
                        pMove->data.id_code, pMove->data.enyear);
                pMove = pMove->next;
            }
        }
    }
    
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题