c语言问题求解答

#include<stdio.h>
#include<stdlib.h>
#define course 4

typedef struct student
{
    long int num;
    char name[20];
    char  sex[3];
    int age;
    float score[course];
    float aver;
    float  total;
    struct student *next;
}STU;
float sum(STU *stu)
{
    int j;
    float result=0;
    for(j=0;j<course;j++)
    {
        result+=stu->score[j];
    }
    return result;
}
STU *input(int n)
{
    STU *head=NULL,*p=NULL,*end=NULL;
    int i;
    for(i=0;i<n;i++)
    {
        p=(STU*)malloc(sizeof(STU));
        p->next=NULL;
        printf("请输入第%d的学生的学号,姓名,性别,年龄,成绩1,成绩2,成绩3,成绩4\n",i+1);
        scanf("%ld%s%s%d%f%f%f%f",&p->num,p->name,p->sex,&p->age,&p->score[0],&p->score[1],&p->score[2],&p->score[3]);
        p->total=sum(p);
        p->aver=p->total/course;
        if(i==0)
        {
            head=p;
        }
        else
        {
            end->next=p;
        }
        end=p;
    }
    return head;
}
STU *insert(STU *head)
{
    STU *p=NULL,*p1=NULL,*tmp=NULL;
    tmp=(STU*)malloc(sizeof(STU));
    tmp->next=NULL;
    printf("Input the num, name, sex, age and 4 scores of student you want to insert:\n");
    scanf("%ld%s%s%d%f%f%f%f", &tmp->num, tmp->name, tmp->sex, &tmp->age, &tmp->score[0],
           &tmp->score[1], &tmp->score[2], &tmp->score[3]);
    tmp->total=sum(tmp);
    tmp->aver=tmp->total/course;
    if(head=NULL)
    {
        return tmp;
    }
    if(tmp->num < head->num)
    {
        tmp->next=head;
        return tmp;
    }
    p1=p=head;
    while(p!=NULL&&p->num<=tmp->num)
    {
        p1=p;
        p=p->next;
    }
    p1->next=tmp;
    tmp->next=p;
    return head;
}
void main()
{
    int n;
    STU *p,*q;
    printf("请输入你想输入的学生的个数\n");
    scanf("%d",&n);
    p=input(n);
    q=insert(p);
    printf("学号   姓名  性别   年龄   语文    数学    英语     政治    平均分    总分\n");
    while(q)
    {
        printf("%-7ld%-7s%-4s%-5d%-9.2f%-9.2f%-9.2f%-9.2f%-9.2f%-9.2f",q->num,q->name,q->sex,q->age,q->score[0],q->score[1],
            q->score[2],q->score[3],q->aver,q->total);
        q=q->next;
    }
    

}
帮忙看下,这个最基本的学生管理系统只有输入,插入,显示功能,请问插入功能为什么不能实现,错误在哪里呢?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值