c语言程序设计有10个学生

有10个学生,每个学生的数据包括学号,姓名,3门功课的成绩。从键盘上输入10个学生的数据,要求按平均成绩降序显示出所有学生的数据(包括学号,姓名,3门课成绩,平均成绩),并将其写入文本文件result.txt中

Tools: codeblocks

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TOTAL_NUMBER_OF_STUDENTS 10

struct StudentListStr{
    char StdID[50];
    char StdName[50];
    int SujectA,SujectB,SujectC;
    float AverageScore;
    struct StudentListStr *next;
};

int insertList(struct StudentListStr** head,struct StudentListStr* new_node);
int freeList(struct StudentListStr* head);

int main()
{
    FILE *fd;
    int cnt = 0;;
    struct StudentListStr* Std_head = NULL;
    struct StudentListStr* Std_new = NULL;

    /*设置终端的输出编码格式,要跟自己编辑器所设置的编码格式要保持一致,避免中文乱码*/
    system("chcp 936");

    /*若文件不存在,则创建;若文件存在,则覆盖*/
    fd = fopen("./result.txt","w");
    if(fd == NULL){
        printf("file create failed.");
        return 0;
    }

    printf("[第n个同学:5623 张三 60 70 90]\n\n");
    while((++cnt) <= TOTAL_NUMBER_OF_STUDENTS){
        printf("第%d个同学:",cnt);
        Std_new = NULL;
        Std_new = (struct StudentListStr*)malloc(sizeof(struct StudentListStr));

        memset(Std_new,0,sizeof(struct StudentListStr));
        scanf("%s %s %d %d %d",Std_new->StdID,Std_new->StdName,&Std_new->SujectA,&Std_new->SujectB,&Std_new->SujectC);

        /*判断输入数据的有效性,可以继续优化*/
        if((strlen(Std_new->StdID) == 0) || (strlen(Std_new->StdName) == 0) || Std_new->SujectA < 0 || Std_new->SujectB < 0 || Std_new->SujectC < 0){
            free(Std_new);
            cnt--;
            printf("The input data is wrong,please try again");
            continue;
        }
        Std_new->AverageScore = (Std_new->SujectA+Std_new->SujectB+Std_new->SujectC)/3.0;
        insertList(&Std_head,Std_new);
    }

    /*打印结果并保存到文件*/
    struct StudentListStr *n = Std_head;
    printf("\n");
    printf("%-10s %-10s %-10s %-10s %-10s %-10s\n","学号","姓名","功课A","功课B","功课C","平均分");
    fprintf(fd,"%-10s %-10s %-10s %-10s %-10s %-10s\n","学号","姓名","功课A","功课B","功课C","平均分");
    while(n != NULL){
        printf("%-10s %-10s %-10d %-10d %-10d %-10.2f\n",n->StdID,n->StdName,n->SujectA,n->SujectB,n->SujectC,n->AverageScore);
        fprintf(fd,"%-10s %-10s %-10d %-10d %-10d %-10.2f\n",n->StdID,n->StdName,n->SujectA,n->SujectB,n->SujectC,n->AverageScore);
        n = n->next;
    }
    /*关闭文件,释放内存*/
    freeList(Std_head);
    fclose(fd);
    return 0;
}

int insertList(struct StudentListStr** head,struct StudentListStr* new_node)
{
    struct StudentListStr* n = *head;
    struct StudentListStr* pre = *head;

    if(new_node == NULL) return 1;

    if(*head == NULL){
        *head = new_node;
        return 0;
    }

    while(n != NULL){
        if(new_node->AverageScore > n->AverageScore){
            if(n == *head){
                new_node->next = *head;
               *head = new_node;
            }else{
                new_node->next = n;
                pre->next = new_node;
            }
            break;
        }

        if(n->next == NULL){
            n->next = new_node;
        }else{
            pre = n;
            n = n->next;
        }
    }

    return 0;
}

int freeList(struct StudentListStr* head)
{
    struct StudentListStr *n = head,*free_node;

    while(n != NULL){
        free_node = n;
        n = n->next;
        free(free_node);
    }
    return 0;
}

Result:

img

img