随机数产生姓名,语数外三科 ,以及总分

#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;

                    }
                }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值