#include<iostream>
#include<ctime>
#include<cstring>
#include<stdlib.h>
using namespace std;
struct student
{
int number;
char name[20];
float scores[4];
student *next;
};
int counts(student *head);
student *creat(student *head, int n);
student *shuchu(student *head);
student *paixu(student *head);
int main()
{
cout<<"有多少学生"<<endl;
int n;
cin>>n;
student *head=new student;
head=creat(head,n);
shuchu(head);
cout<<endl;
cout<<"排序后的成绩是"<<endl;
shuchu(paixu(head));
return 0;
}
student *creat(student *head, int n)
{
int i=1,j;
char t;
student *p=head;
char na[6];
while(i<=n)
{
j=i;
p->number=j;
for(j=0;j<=4;j++)
{
na[j]=rand()%26+65;
}
na[5]='\0';
strcpy(p->name,na);
for(j=0;j<=2;j++)
{
p->scores[j]=rand()%151;
}
for(j=0;j<=2;j++)
{
p->scores[3]+=p->scores[j];
}
i++;
if(i==n+1)
{
p->next=0;
}
else
{
p->next=new student;
p=p->next;
}
}
return head;
}
student *paixu(student *head)
{
student *p1=head;
student *p2=head;
int t;
int coun=counts(head);
char tname[20];
int i=1,j;
for(p1=head;p1->next!=0;p1=p1->next)
{
i=1;
for(p2=p1->next;i<=coun-1;p2=p2->next)
{
if(p1->scores[3]<p2->scores[3])
{
t=p1->number;
p1->number=p2->number;
p1->number=t;
strcpy(tname,p1->name);
strcpy(p1->name,p2->name);
strcpy(p2->name,tname);
for(j=0;j<=3;j++)
{
t=p1->scores[j];
p1->scores[j]=p2->scores[j];
p2->scores[j]=t;
}
}
i++;
}
coun--;
}
return head;
}
int counts(student *head)
{
student *p=head;
int i=0;
while(p!=0)
{
p=p->next;
i++;
}
return i;
}
student *shuchu(student *head)
{
student *p=head;
int i;
char a[4][20]={{"语文"},{"数学"},{"英语"},{"总分"}};
while(p!=0)
{
cout<<" 学号:"<<p->number<<"\t"<<"姓名"<<p->name<<"\t";
for(i=0;i<=3;i++)
{
cout<<a[i]<<":"<<p->scores[i]<<" ";
}
cout<<endl;
p=p->next;
}
}
随机数产生姓名,语数外三科 ,以及总分
最新推荐文章于 2023-11-13 12:57:22 发布