1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//class.h 头文件
#include <string>
#include <iostream>
#include <iomanip>            //实现setw()宽字节输出
using  namespace  std;
 
 
class  stuDate
{
public :
         struct  student
         {
             string name;        //姓名   
             string sex;             //性别
             int  id;
             float  eng,math,chinese,c,sum;
             struct  student *next;
         };
};
 
 
class  stuList : public  stuDate
{
private :
     int  num;        //人数
     struct  student *head;
public :
     stuList();
     void  _creat();
     void  _print();
     void  _sort();
};
 
stuList::stuList()
{
     this ->head=NULL;
     this ->num=0;
}
 
void  stuList::_creat()
{
     char  res =  'Y' ;
     struct  student *p1,*p2;
     p1=p2= new  struct  student;
     while (res== 'Y'  || res ==  'y' )
     {
         if ( this ->head == NULL)
         {
             this ->head=p1;
             p1->id=0;
         }
         else
         {
             p2->next=p1;
         }
         cout<< "Name:" ;
         cin>>p1->name;
         cout<< "Sex:" ;
         cin>>p1->sex;
         cout<< "Chinese:" ;
         cin>>p1->chinese;
         cout<< "Math:" ;
         cin>>p1->math;
         cout<< "English:" ;
         cin>>p1->eng;
         cout<< "C语言:" ;
         cin>>p1->c;
         p1->id= this ->num+1;
         p1->sum=p1->chinese+p1->math+p1->eng+p1->c;
         this ->num++;
         p2=p1;
         p1= new  struct  student;
         cout<< "\n是否继续输入(Y/N):" ;
         cin>>res;
         if (res == 'N' || res ==  'n' )
             break ;
     }
     p2->next=NULL;
}
 
void  stuList::_print()
{
     struct  student *temp;
     temp= this ->head;
     cout<< "id" <<setw(5)<< "name" <<setw(5)<< "sex" <<setw(10)<< "Chinese" <<setw(5)
         << "Math" <<setw(8)<< "English" <<setw(5)<< "C" <<setw(5)<< "Sum" <<endl;
     for ( int  i=0;i< this ->num;i++)
     {
         cout<<temp->id<<setw(5)<<temp->name<<setw(5)<<temp->sex<<setw(5)<<temp->chinese<<setw(5)
             <<temp->math<<setw(5)<<temp->eng<<setw(5)<<temp->c<<setw(5)<<temp->sum<<endl;
         temp=temp->next;
     }
     cout<<endl;
     cout<< "------------------------------------------------------------总人数:" << this ->num;
     cout<<endl;
}
 
void  stuList::_sort()
{
     struct  student *first;             /*排列后有序链的表头指针*/
     struct  student *tail;              /*排列后有序链的表尾指针*/
     struct  student *p_max;               
     struct  student *max;
     struct  student *p;
     p= this ->head;
     if ( this ->head!=NULL)
     {
         first=NULL;
             while  (head != NULL)
             {
                 for  (p= this ->head,max= this ->head; p->next!=NULL; p=p->next)
                 {
                     if  (p->next->sum > max->sum)          //以总分排序
                     {
                         p_max=p;
                         max=p->next;
                     }
                 }
                 if (first==NULL)
                 {
                     first=max;
                     tail=max;
                 }
                 else
                 {
                     tail->next=max;
                     tail=max;
                 }
                 if (max== this ->head)
                 {
                     this ->head= this ->head->next;
                 }
                 else
                 {
                     p_max->next=max->next;
                 }
             }
             if (first!=NULL)
             {
                 tail->next=NULL;
             }
             cout<< "id" <<setw(5)<< "name" <<setw(5)<< "sex" <<setw(10)<< "Chinese" <<setw(5)
                 << "Math" <<setw(8)<< "English" <<setw(5)<< "C" <<setw(5)<< "Sum" <<endl;
             for ( int  i=0;i< this ->num;i++)
             {
                 cout<<first->id<<setw(5)<<first->name<<setw(5)<<first->sex<<setw(5)<<first->chinese<<setw(5)
                     <<first->math<<setw(5)<<first->eng<<setw(5)<<first->c<<setw(5)<<first->sum<<endl;
                 first=first->next;
             }
     }
}
 
 
 
//stu.cpp    调用class。h
#include "class.h"
int  main()
{
     stuList A;
     A._creat();
     A._print();
     A._sort();
     return  0;
}