C语言一道题目,求教教

3.统计一个班的学生成绩。要求程序具有如下功能:
(1) 每个学生的学号和四门功课的成绩从键盘读入。
(2) 计算每个学生的总分和平均分。
(3) 按平均成绩进行排序,输出排序后的成绩单(包括学号、四门功课的成绩和平均分),给出名次。如果分数相同,则名次并列,其他学生名次不变。
实验步骤与要求:
(1) 每个功能为一个独立的函数。
(2) 调试程序时,可先输入少量学生的成绩作为实验数据。如可输入3名学生4门课成绩:
学号成绩
9701 67,72,65,80
9702 75,82,94,95
9703 70,74,80,76
希望能给代码来,在下感激不尽

#include <stdio.h>
#include <malloc.h>
#define NULL0
#define LEN sizeof(struct student)
struct student
{
long num;
char name[20];
float score1;
float score2;
float score3;
float score4;
float score5;
float c;
float z;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f,%f,%f,%f,%f,%s",&p1->num,&p1->score1,&p1->score2,&p1->score3,&p1->score4,&p1->score5,p1->name);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
scanf("%ld,%f,%f,%f,%f,%f,%s",&p1->num,&p1->score1,&p1->score2,&p1->score3,&p1->score4,&p1->score5,p1->name);
}
p2->next=NULL;
return(head);

}

void print(struct student *head)
{
struct student *p;
printf("\nNow,these %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
p->z=(p->score1)+(p->score2)+(p->score3)+(p->score4)+(p->score5);
p->c=(p->z)/5;
printf("%ld %5.1f%5.1f%5.1f%5.1f%5.1f %s ",p->num,p->score1,p->score2,p->score3,p->score4,p->score5,p->name);
printf("总分=%f 平均分=%f\n",p->z,p->c);
p=p->next;
}
while(p!=NULL);

}

struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{printf("\nlist null\n");
return head;}
p1=head;
while(num!=p1->num && p1->next!=NULL)
{p2=p1;
p1=p1->next;
}
if(num==p1->num)
{if(p1==head)head=p1->next;
else p2->next=p1->next;
printf("delete:%ld\n",num);
n=n-1;
free(p1);
}
else printf("%ld not been found!\n",num);
return(head);

}

struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{if(head==p1)head=p0;
else p2->next=p0;
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;}
}
n=n+1;
return(head);
}

struct student * ordination(struct student * head)
{
struct student * first;
struct student * tail;
struct student * p_min;
struct student * min;
struct student * p;
first=NULL;
while(head!=NULL)
{
for(p=head,min=head;p->next!=NULL;p=p->next)
{
if(p->next->num<min->num)
{
p_min=p;
min=p->next;
}
}
if(first==NULL)
{
first=min;
tail=min;
}
else
{
tail->next=min;
tail=min;
}
if(min==head)
{
head=head->next;}
else{p_min->next=min->next;}
}
if(first!=NULL)
{
tail->next=NULL;
}
head=first;
return(head);
}

struct student * ordinationc(struct student * head)
{
struct student * first;
struct student * tail;
struct student * p_min;
struct student * min;
struct student * p;
first=NULL;
while(head!=NULL)
{
for(p=head,min=head;p->next!=NULL;p=p->next)
{
if(p->next->z>min->z)
{
p_min=p;
min=p->next;
}
}
if(first==NULL)
{
first=min;
tail=min;
}
else
{
tail->next=min;
tail=min;
}
if(min==head)
{
head=head->next;}
else{p_min->next=min->next;}
}
if(first!=NULL)
{
tail->next=NULL;
}
head=first;
return(head);
}

struct student *cha(struct student *head,long cha)
{
struct student *p1,*p2;
if(head==NULL)
{printf("\nlist null\n");
return head;}
p1=head;
while(cha!=p1->num && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(cha==p1->num)
{
printf("%ld,%5.1f%5.1f%5.1f%5.1f%5.1f,%s\n",p1->num,p1->score1,p1->score2,p1->score3,p1->score4,p1->score5,p1->name);
}

else printf("%ld not been found!\n",cha);
return(head);

}
struct student *chap(struct student *head)
{
struct student *p1,*p2;
int x;
x=1;
if(head==NULL)
{printf("\nlist null\n");
return head;}
p1=head;
while( p1->next!=NULL)
{
if(p1->c>85)
{
printf("%ld,%5.1f%5.1f%5.1f%5.1f%5.1f,%s\n",p1->num,p1->score1,p1->score2,p1->score3,p1->score4,p1->score5,p1->name);
x=0;
}

p1=p1->next;
}
if(x) printf("没有平均分在85分以上的学生\n");

return(head);

}

struct student *chapb(struct student *head)
{
struct student *p1,*p2;
int x;
x=1;
if(head==NULL)
{printf("\nlist null\n");
return head;}
p1=head;
while( p1->next!=NULL)
{
if(p1->c<60)
{
printf("%ld,%5.1f%5.1f%5.1f%5.1f%5.1f,%s\n",p1->num,p1->score1,p1->score2,p1->score3,p1->score4,p1->score5,p1->name);
x=0;

}

p1=p1->next;
}
if(x) printf("没有不及格的学生\n");

return(head);

}

void main()
{
struct student * head, * stu;
long del_num;
long cha_num;
printf("input record:(学号、5科成绩、姓名)\n");
head=creat();
print(head);
printf("\ninput the deleted number:");
scanf("%ld",&del_num);
while(del_num!=0)
{
head=del(head,del_num);
print(head);
printf("input the deleted number:");
scanf("%ld",&del_num);
}
printf("\ninput the inserted record:");
stu=(struct student * )malloc(LEN);
scanf("%ld,%f,%f,%f,%f,%f,%s",&stu->num,&stu->score1,&stu->score2,&stu->score3,&stu->score4,&stu->score5,stu->name);
while(stu->num!=0)
{
head=insert(head,stu);
print(head);
printf("input the inserted record:");
stu=(struct student * )malloc(LEN);
scanf("%ld,%f,%f,%f,%f,%f,%s",&stu->num,&stu->score1,&stu->score2,&stu->score3,&stu->score4,&stu->score5,stu->name);
}
printf("\ninput the 查找 number:");
scanf("%ld",&cha_num);
while(cha_num!=0)
{
head=cha(head,cha_num);
printf("input the 查找 number:");
scanf("%ld",&cha_num);
}

printf("下面是85分以上的:\n\n");
head=chap(head);
printf("\n");
printf("下面是不及格的:\n\n");
head=chapb(head);
printf("\n");
printf("下面是学号排序");
head=ordination(head);
print(head);
printf("\n\n");
printf("总分排序");
head=ordinationc(head);
print(head);
}
这是我上学期的作业,跟你的差不多
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-29
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

const int maxn = 10000 + 10;

struct node{
int id,num[5],sum,rank;
double ave;
}a[maxn];

int n, b[maxn];

void init(){
scanf("%d",&n);
for (int i = 1; i <= n; i++){
scanf("%d",&a[i].id);
for (int j = 1; j <= 4; j++)
scanf("%d",&a[i].num[j]);
}
}

void count(){
for (int i = 1; i <= n; i++){
a[i].sum = 0;
for (int j = 1; j <= 4; j++)
a[i].sum += a[i].num[j];
a[i].ave = (double)a[i].sum / 4;
b[i] = i;
}
}

bool cmp(int x, int y){
return a[x].sum > a[y]. sum ? 1 : 0;
}

void work(){
a[b[1]].rank = 1;
for (int i = 2; i <= n; i++)
if (a[b[i]].sum == a[b[i-1]].sum) a[b[i]].rank = a[b[i-1]].rank;
else a[b[i]].rank = i;
}

void print(){
for (int i = 1; i <= n; i++){
printf("%d ",a[i].id);
for (int j = 1; j <= 4; j++)
printf("%d ",a[i].num[i]);
printf("%.2lf %d\n",a[i].ave,a[i].rank);
}
}

int main(){
init();
count();
sort(b+1,b+n+1,cmp);
work();
print();
}
第2个回答  2013-11-29
号和四门功课的成绩从键盘读入。
(2) 计算每个学生的总分和平均分。
(3) 按平均成绩进行排序,输出
第3个回答  2013-11-29
第4个回答  2013-11-29
这个你得自己做 ,给你提醒一下,可以数组做

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网