C++ 简单的成绩管理

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include<iostream>
#include<iomanip>
using namespace std;
 
struct Student{
       int studentNunber;//学号
       string studentName;//姓名
       double Chinese;//语文成绩
       double English;//英语成绩
       double Computer;//计算机成绩
       double Math;//数学成绩
       double Total;//总分
       int rank;//排名
        
       };
      
    /*****************
    成绩排序
    m=1,按照名次排序
    m=2,按照学号排序
    m=3,按照语文成绩排序
    m=4,按照英语成绩排序
    m=5,按照计算机成绩排序
    m=6,按照数学成绩排序
    *****************/  
    void sort(int m,struct Student *stu,int n)
    {
        if(m==1)
           {
            for (int k =0; k<n-1;k++)
                 {
                     for(int h=0; h<n-1-k;h++)
                     
                         if(stu[h].Total<stu[h+1].Total)
                         
                             struct Student tempStu;
                             tempStu=stu[h];
                             stu[h]=stu[h+1];
                             stu[h+1]=tempStu;
                             
                         }
                              
                      }
                       stu[n-k-1].rank=n-k;
                        
                  }
 
                
           }
          
           else if(m==2)
             {
                 for (int k =0; k<n-1;k++)
                     {
                         for(int h=0; h<n-1-k;h++)
                         
                             if(stu[h].studentNunber>stu[h+1].studentNunber)
                             
                                 struct Student tempStu;
                                 tempStu=stu[h+1];
                                 stu[h+1]=stu[h];
                                 stu[h]=tempStu;
                                 
                             }
                                  
                          }
                            
                            
                      }   
                      
               }
           else if(m==3)
               {
                    for (int k =0; k<n-1;k++)
                     {
                         for(int h=0; h<n-1-k;h++)
                         
                             if(stu[h].Chinese<stu[h+1].Chinese)
                             
                                 struct Student tempStu;
                                 tempStu=stu[h];
                                 stu[h]=stu[h+1];
                                 stu[h+1]=tempStu;
                                 
                             }
                                  
                          }
                            
                            
                      }   
                     
                    }
                else if(m==4)
                   {
                        for (int k =0; k<n-1;k++)
                         {
                             for(int h=0; h<n-1-k;h++)
                             
                                 if(stu[h].English<stu[h+1].English)
                                 
                                     struct Student tempStu;
                                     tempStu=stu[h];
                                     stu[h]=stu[h+1];
                                     stu[h+1]=tempStu;
                                     
                                 }
                                      
                              }
                                
                                
                          }   
                         
                        }
                else if(m==5)
                   {
                        for (int k =0; k<n-1;k++)
                         {
                             for(int h=0; h<n-1-k;h++)
                             
                                 if(stu[h].Computer<stu[h+1].Computer)
                                 
                                     struct Student tempStu;
                                     tempStu=stu[h];
                                     stu[h]=stu[h+1];
                                     stu[h+1]=tempStu;
                                     
                                 }
                                      
                              }
                                
                                
                          }   
                         
                        }
                else if(m==6)
                   {
                        for (int k =0; k<n-1;k++)
                         {
                             for(int h=0; h<n-1-k;h++)
                             
                                 if(stu[h].Math<stu[h+1].Math)
                                 
                                     struct Student tempStu;
                                     tempStu=stu[h];
                                     stu[h]=stu[h+1];
                                     stu[h+1]=tempStu;
                                     
                                 }
                                      
                              }
                                
                                
                          }   
                         
                        }
          
         }
          
       
     /*****************
    成绩录入
    *****************/   
       void input(struct Student *stu,int n)
       {
          
          for(int i=0;i<n;i++)
          {
                  cout<<"请输入学号为"<<i+1<<"的学生的姓名以及语文,英语,计算机,数学无门课程的成绩,每一项以回车键结束"<<endl;
                  stu[i].studentNunber=i+1;
                  cin>>stu[i].studentName;
                  cin>>stu[i].Chinese;
                  cin>>stu[i].English;
                  cin>>stu[i].Computer;
                  cin>>stu[i].Math;
                  stu[i].Total= stu[i].Chinese+ stu[i].English+stu[i].Computer+stu[i].Math;
                  stu[i].rank=1;
          }
      //排序,计算名次          
       sort(1,stu,n); 
       }
          
  void update(struct Student *stu,int n)
  { int num;
    int sub;
   double score;
     cout<<"请输入要更改学生的学号:"<<endl;
     cin>>num;
     while(num>n||num<1)
     {cout<<"该学号不存在,请重新输入:"<<endl;
      cin>>num;
     }
       
     cout<<"请输入要更改的课程代号1:语文;2:英语;3:计算机;4:数学:"<<endl;
     cin>>sub;
     while(sub>4||sub<1)
     {cout<<"请输入正确的课程代号:"<<endl;
         cin>>sub;
     }
     cout<<"请输入新成绩(1 -100)"<<endl;
     cin>>score;
     while(score>100||sub<0)
     {cout<<"请输入正确范围的成绩:"<<endl;
         cin>>score;
     }
      
      //按照学号重新排序          
           sort(2,stu,n);
      
       switch(sub)
            {
            case 1:
              stu[num-1].Chinese=score;
                break;
            case 2:
              stu[num-1].English=score;
                break;
            case 3:
              stu[num-1].Computer=score;
                break;
            case 4:
              stu[num-1].Math=score;
                break;
                 
            default:
              
                break;
             
            }
             
       //重新排序,计算名次          
           sort(1,stu,n);
           cout<<"成绩更改成功!" ;
              
            }
             
   
         
 
    /*****************
    成绩输出
    *****************/
         void display(struct Student *stu,int n)
       {
            
                cout<<"学号:"<<setw(10)<<"姓名:"<<setw(10)<<"语文"<<setw(10)<<
                "英语"<<setw(10)<<"计算机"<<setw(10)<<"数学"<<setw(10)<<"总分"<<setw(10)<<"名次"<<setw(10)<<""<<endl;
                  
                  for(int j=0;j<n;j++)
                 {
                  cout<<setw(4)<<
                  stu[j].studentNunber<<setw(10)<<stu[j].studentName<<setw(10)<<
                  stu[j].Chinese<<setw(10)<<stu[j].English<<setw(10)<<stu[j].Computer<<
                  setw(10)<<stu[j].Math<<setw(10)<<
                  stu[j].Total<<setw(10)<<stu[j].rank<<endl;
                  }       
        
               
         }  
          
          
    /*****************
    格式化输出选项
    *****************/
         void printChoice()
         {
                 cout<<"请选择要操作的类型"<<endl;
                 cout<<"*************************************"<<endl;
                 cout<<"*查看按名次排序的名单,请输入1      *"<<endl;
                 cout<<"*查看按学号排序的名单,请输入2      *"<<endl;
                 cout<<"*查看按语文成绩排序的名单,请输入3  *"<<endl;
                 cout<<"*查看按数学成绩排序的名单,请输入4  *"<<endl;
                 cout<<"*查看按计算机成绩排序的名单,请输入5*"<<endl;
                 cout<<"*查看按数学成绩排序的名单,请输入6  *"<<endl;
                 cout<<"*修改学生成绩,请输入7              *"<<endl;
                 cout<<"*退出系统,请输入1 — 7以外的任意键 *"<<endl;
                 cout<<"*************************************"<<endl;
          }
          
          
int main()
{   int n=0;//班级人数
      
    cout<<"欢迎张老师进入成绩输入系统,请输入班级人数"<<endl;
    cout<<"-------(请保证班级人数小于等于50)-------"<<endl;
       
      while(!(cin>>n))
      {
        cout<<"请输入数字!"<<endl;
        cin.clear();
        cin.sync();
       }
      
      while(n<0||n>50)
      {
       cout<<"请输入于等于50的班级人数"<<endl;      
       cin>>n;  
      }
        struct Student *stu =new struct Student [n];
         
       //录入成绩
       input(stu,n);
       cout<<"成绩录入成功!"<<endl;
        
       printChoice();
              
          int m ; 
         cin>>m;
           while(m>0&&m<8)
           {
           if(m==7)
           {
             update(stu,n);
               
           }
            else
            {
               sort(m,stu,n);
               display(stu,n);
              
            }
            printChoice(); 
             cin>>m;      
           }
          
          cout<<"退出系统成功!";
 
        int a;
        cin>>a;
            
    return 0;
    }

  

C++ 简单的成绩管理,布布扣,bubuko.com

C++ 简单的成绩管理

上一篇:Python字符编码详解


下一篇:3个步骤轻松解决Java开发包的统一