星危 2020-12-08 18:20 采纳率: 33.3%
浏览 104

用链表保存学生的各科成绩,编写函数求学生成绩总和,凭借总分求函数的降序排列

//用链表保存学生的各科成绩,编写函数求学生成绩总和,凭借总分求函数的降序排列
#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";
}
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-20 21:14
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示