//用链表保存学生的各科成绩,编写函数求学生成绩总和,凭借总分求函数的降序排列
#include <iostream>
#include <cstdlib>
#include <string>
const int number = 5;
using namespace std;
typedef struct Date
{
string name;
int English;
int Math;
int Chineselanguage;
char PE;
}Date;
typedef struct Student
{
Date *date;
Student *next;
}Student;
//链表的创建同时初始化
Student * Create()
{
Student *head,*node;
head=(Student *)malloc(sizeof(Student));//头结点,date结构体为空,不需要申请空间
node=(Student *)malloc(sizeof(Student));
node->date=(Date *)malloc(sizeof(Date));
head->next=node;
node->next=NULL;
//输入最后一个节点的信息
cout<<"请输入第"<<number<<"位学生的个人信息";
cout<<"\ninput the name of the last student\n";
cin>>node->date->name;
cout<<"\ninput the English score\n";
cin>>node->date->English;
cout<<"\ninput the Math score\n";
cin>>node->date->Math;
cout<<"\ninput the Chineselanguage score\n";
cin>>node->date->Chineselanguage;
cout<<"\ninput the PE score\n";
cin>>node->date->PE;
//开始头插法
for(int i=0;i<number-1;i++)
{
node = (Student *)malloc(sizeof(Student));
node->date =(Date *)malloc(sizeof(Date));
//按给定的表格依次从后往前存储(输入)学生的信息
cout<<"请输入第"<<number-i-1<<"位学生的信息";
cout<<"\ninput the name of the student\n";
cin>>node->date->name;
cout<<"input the English score\n";
cin>>node->date->English;
cout<<"input the Math score\n";
cin>>node->date->Math;
cout<<"input the Chineselanguage score\n";
cin>>node->date->Chineselanguage;
cout<<"input the PE score\n";
cin>>node->date->PE;
//关键代码
node->next = head->next;
head->next = node;
}
cout<<"----------------------------------"<<endl;//往新节点存入数据,注意我们只给后面的节点存
return head;
}
/*
//创建一个计算学生总分的函数
int * Total()
{
Student *s = Create();
int * r = new int;
for(int i=0;i<number;i++)
r[i] = s->date->English + s->date->Chineselanguage + s->date->Math + (int)(s->date->PE);
return r;
}
//凭借学生的总成绩对学生进行升序排列
void Arrange()
{
int smallIndex, i, j;
int *p = Total();//数组p为之前的乱序数组
cout<<"未排序是统计的学生总分;"<<endl;
for(int k=0;k<number;k++)//循环遍历一遍数组看是否已经求得每个学生的各科成绩之和
{
cout<<"第"<<k+1<<"个学生的总成绩: "<<endl;
cin>>p[k];
}
for(i=0; i<number-1; i++)
{
smallIndex = i;
for(j=i+1; j<number; j++)
if(p[j] < p[smallIndex])
smallIndex = j;
int temp;
temp = p[smallIndex];
p[smallIndex] = p[i];
p[i] = temp;
}
cout <<"降序排序为:"<< endl;
for(i=number-1; i>=0; i--)
cout<<p[i]<<"\t";
cout<<endl;
}
*/
//使用总函数main调用所有的函数
void main()
{
Student *s = Create()->next;
int r[number] ;
for(int l=0;l<number;l++)
{
r[l] = s->date->English + s->date->Chineselanguage + s->date->Math + (int)(s->date->PE);
s= s->next;
}
for(int k=0;k<number;k++)//循环遍历一遍数组看是否已经求得每个学生的各科成绩之和
{
cout<<"第"<<k+1<<"个学生的总成绩: \n";
cin>>r[k];
}
int smallIndex, i, j;
for(i=0; i<number-1; i++)
{
smallIndex = i;
for(j=i+1; j<number; j++)
if(r[j] < r[smallIndex])
smallIndex = j;
int temp;
temp = r[smallIndex];
r[smallIndex] = r[i];
r[i] = temp;
}
cout <<"降序排序为:"<< endl;
for(i=number-1; i>=0; i--)
cout<<r[i]<<"\t";
cout<<"\n";
}