C语言题目,求大佬解答

2024-12-04 06:40:53
推荐回答(2个)
回答1:

#include 
#include 
    int n;
 typedef struct student{
    int num;
    int mingci;
    char name[20];
    double a[4];
    double sum;
    double avreage;
 
    struct student *next;
} STU;
    STU *creat();
    void add_student(STU *);
    void show(STU *);
    STU *delete_student(STU *);
    void paiming(STU *);
    void show_one(STU *s);
    int panduan(double l);
 
    int main()  {
            int choose;
             STU *head=creat();
 
            while(1){
                        printf("1 录入信息\n");
                        printf("2 查询学生信息\n");
                        printf("3 删除学生信息\n");
 
 
                    scanf("%d",&choose);
                switch(choose){
                case 1:add_student(head);break;
                case 2:show(head);break;
                 case 3:head=delete_student(head);break;
                default: break;
 
                }
 
            }
 
 
 
    return 0;
}
//创建链表表头
STU *creat(){
    n=0;
    STU *head;
 
    head=(STU *)malloc(sizeof(STU));
     head->next=NULL;
    return head;
    }
//录入学生信息
void add_student(STU *s){
    STU *p=s;
    STU *p1;
    while(p->next!=NULL){
        p=p->next;
    }   while(1){
 
        printf("请输入学号:学号为0表示输入结束输入!");
        scanf("%d",&p->num);
        if(p->num!=0){
                 n++;
        printf("请输入姓名:");
        scanf("%s",p->name);
         printf("请输入成绩 格式C程序设计 高等数学 英语 线性代数:");
        scanf("%lf%lf%lf%lf",&p->a[0],&p->a[1],&p->a[2],&p->a[3]);
        p->sum=p->a[0]+p->a[1]+p->a[2]+p->a[3];
        p->avreage=p->sum/4;
        p->mingci=0;
 
        p1=(STU *)malloc(sizeof(STU));
        p->next=p1;
        p1->next=NULL;
        p=p1;
        }else
            {
            printf("录入结束!\n");
            paiming(s);
            break;
                        }
 
        }
}
//根据学号删除学生信息
STU *delete_student(STU *s){
    int num;
    STU *p=s,*p1;
    printf("请输入要删除学生的学号:");
    scanf("%d",&num);
    while(p->next!=NULL){
 
        if(p->num==num&&s==p){
                n--;
            return p->next;
            }else if(p->num==num){
                n--;
                p1->next=p->next;
                paiming(s);
                return s;
 
            }   p1=p;
                p=p->next;
 
 
            }
            if(n==0){printf("数据库目前为空,请录入信息!\n");}
            else{
            printf("没有发现你输入的学号!\n");}
            return s;
 
}
//显示学生数据库信息
void show(STU *s){
    STU *head=s;
        if(n==0){
            printf("数据库为空,请先录入信息!\n");
    }
        else{
            while(s->next!=NULL){
            printf("学号:%d 姓名:%s 成绩:%.2lf %.2lf %.2lf %.2lf 平均分:%.2lf 名次:%d \n",s->num,s->name,s->a[0],s->a[1],s->a[2],s->a[3],s->avreage,s->mingci);
            s=s->next;
    }
    show_one(head);
}
}
//对学生按平均分排名
 void paiming(STU *s){
     double score[n];
     int j,i=0;
     STU *head=s;
     while(s->next!=NULL){
 
        score[i]=s->avreage;
        s=s->next;
        i++;
     }
     double temp;
     for(i=0;i        for(j=i;j               if(score[i]                temp=score[i];
                score[i]=score[j];
                score[j]=temp;
               }
 
        }
     }
 
 
    for(i=0;i            if(score[i]!=0){
                s=head;
            while(s->next!=NULL){
               if(s->mingci==0){
 
                    if(score[i]==s->avreage){
                        score[i]=0;
                        s->mingci=i+1;
                        break;
                                            }
 
                               }
                s=s->next;
        }
    }
}
 
 
 
}
//单科平均分统计
void show_one(STU *s){
 
    double ar[4]={0};
    int b[4][4]={{0},{0},{0},{0}};
    int i;
    while(s->next!=NULL){
 
            for(i=0;i<4;i++){
                b[i][panduan(s->a[i])]++;
                ar[i]+=s->a[i];
            }
        s=s->next;
    }
    printf("各科平均分 %.2lf %.2lf %.2lf %.2lf\n",ar[0]/n,ar[1]/n,ar[2]/n,ar[3]/n);
    printf("90-100分 %d %d %d %d\n",b[0][0],b[1][0],b[2][0],b[3][0]);
    printf("80-89分 %d %d %d %d\n",b[0][1],b[1][1],b[2][1],b[3][1]);
    printf("60-79分 %d %d %d %d\n",b[0][2],b[1][2],b[2][2],b[3][2]);
    printf("60分以下 %d %d %d %d\n",b[0][3],b[1][3],b[2][3],b[3][3]);
 
}
 
int panduan(double l){
 
    if(l>=90&&l<=100) return 0;
   else if(l>=80&&l<=89) return 1;
    else  if(l>=60&&l<=79) return 2;
    else return 3;
 
}

回答2:

还要吗,电脑上有程序,要的话我给你找一下