您的位置:首页 > 编程语言 > C语言/C++

c语言问题求解答

2014-04-10 12:34 295 查看
#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;
}

}
帮忙看下,这个最基本的学生管理系统只有输入,插入,显示功能,请问插入功能为什么不能实现,错误在哪里呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: