學生成績管理系統(C++指標、連結串列、檔案及物件導向的運用)

泰斗賢若如發表於2019-03-29

                                                                                   學生成績管理系統

 

功能頁面顯示:

 

  

 

 

 

 

實現原始碼:

 1 #include<iostream>
 2 #include<fstream>
 3 #include<cstring>  
 4 #include<iomanip>
 5 #include<stdlib.h>
 6 #include<windows.h>
 7 using namespace std;
 8 //學生類 
 9 class Student
10 {
11     public:
12         void inputStudent(Student *head);      //輸入學生的資訊 
13         void outputStudent(Student *head);    //輸出學生的資訊 
14         void deleteStudent(Student *head);   //刪除學生的資訊
15         void addStudent(Student *head);     //新增學生的資訊
16         void changeStudent(Student *head); //修改學生的資訊 
17         void findStudent(Student *head);  //查詢學生的資訊 
18         void paixuStudent(Student *head);//學生成績排序 
19         void saveStudent(Student *head); //儲存學生的資訊
20         void loadStudent(Student *head);//載入學生的資訊 
21         void getcountStudent(Student *head);
22         
23         int       num;
24         char    name[20];
25         char      sex[4];
26         float     Chinese;
27         float     English;
28         float     Math;
29         float     sum;
30         float     average;
31         Student *next;
32         
33         private:
34             
35 };

 

 1 void Student ::inputStudent(Student *head)//輸入學生的資訊 
 2 {
 3     system("cls");
 4     Student *p1,*p2;
 5     
 6     p1=head;
 7     int N;
 8     cout<<"哈嘍,小可愛,請輸入學生的總人數"<<endl; 
 9     cin>>N;
10     for(int i=0;i<N;i++)
11     {
12         p2= new Student ;
13         p1->next=p2;
14         p1=p1->next;
15         cout<<"哈嘍,小可愛,請輸入學生的資訊: "<<"學號 "<<"姓名 "<<"性別 " <<"語文 "<<"數學 "<<"英語 "<<endl; 
16         cin>>p1->num>>p1->name>>p1->sex>>p1->Chinese>>p1->Math>>p1->English;
17         p1->sum=p1->Chinese+p1->Math+p1->English;
18         p1->average=(p1->sum)/3;
19     }
20     p1->next=NULL;
21     cout<<"YES!輸入成功"<<endl;
22 }
 1 void Student ::outputStudent(Student *head)//瀏覽學生的資訊 
 2 {
 3     system("cls");
 4     Student *p1,*p2;
 5 
 6     p1=head->next;
 7     int r=0;//名次 
 8     cout<<"學號\t"<<"姓名\t"<<"性別\t"<<"語文"
 9     <<"\t"<<"數學"<<"\t"<<"英語"<<"\t"<<"\t"<<"平均分"<<"\t"<<"\t"<<"總分"
10     <<"\t"<<"\t"<<"排名"<<endl; 
11     while(p1!=NULL)
12     {
13         cout<<p1->num<<"\t"<<p1->name<<"\t"<<p1->sex<<"\t"<<p1->Chinese<<"\t"<<p1->Math<<"\t"<<p1->English<<"\t"<<"\t";
14         cout<<p1->average<<"\t"<<"\t";    
15         cout<<p1->sum<<"\t"<<"\t";
16         cout<<r+1<<endl;
17         r++;
18         p1=p1->next;
19     }
20     cout<<"YES!瀏覽成功"<<endl;
21     
22  } 

 

 1 void Student ::deleteStudent(Student *head)//刪除學生的資訊 
 2  {
 3      system("cls");
 4      
 5      Student *p1,*p2;
 6      char n;
 7      cout<<"哈嘍,小可愛,請輸入要刪除的位置:"<<endl;
 8      cin>>n;
 9      int a;
10      a=n-'0';
11      if(a>=0)
12      {
13          if(a>=0 && a<=count)
14          {
15              p1=head;
16              p2=p1->next;
17              while(--a)
18              {
19                  p1=p1->next;
20                  p2=p2->next;
21              }
22              p1->next=p2->next;
23       
24               cout<<"YES!刪除成功"<<endl; 
25          }
26          else
27           {
28             cout<<"輸入錯誤,請輸入0-"<<count<<"之間的數字位置:" ;
29            } 
30      }
31      else
32      {
33          cout<<"輸入錯誤,請輸入0-"<<count<<"之間的數字位置:" ;
34      } 
35  }

 

 1  void Student ::addStudent(Student *head)//新增學生的資訊 
 2  {
 3      system("cls");
 4      Student *p1,*p2;
 5      Student *p3;
 6      char n;
 7      cout<<"哈嘍,小可愛,請輸入要新增的位置 "<<endl;
 8     cin>>n;
 9     int a;
10     a=n-'0';
11     if(a>0)
12     {
13         if(a>=0 && a<=count)
14         {
15             p1=head;
16             p2=p1->next;
17         while(--a)
18         {
19             p2=p2->next;
20             p1=p1->next;
21          
22         }
23          p3=new Student;
24          cout<<"哈嘍,小可愛,請輸入你的資訊 :"<<endl<<"學號 "<<"姓名 "<<"性別 "<<"語文 "<<"數學 "<<"英語 "<<endl;
25      
26          cin>>p3->num>>p3->name>>p3->sex>>p3->Chinese>>p3->Math>>p3->English; 
27          p3->sum=p3->Chinese+p3->Math+p3->English;
28         p3->average=(p3->sum)/3;
29          p3->next=p2;
30          p1->next=p3;
31          cout<<"YES!新增成功 "<<endl;
32         }
33         else
34         {
35             cout<<"輸入錯誤,請輸入0-"<<count<<"之間的數字位置:" ;
36         }
37   }
38   else
39   {
40     cout<<"輸入錯誤,請輸入0-"<<count<<"之間的數字位置:" ;
41   }
42 }

 

 1 void Student::changeStudent(Student *head)//修改學生的資訊 
 2   {
 3       system("cls");
 4       
 5       Student *p1,*p2;
 6       Student *p3;
 7   
 8       char n;
 9       cout<<"哈嘍,小可愛,請選擇你要修改的位置 :"<<endl;
10       cin>>n;
11       int a;
12       a=n-'0';
13       if(a>0)
14       {
15           if(a>=0 && a<=count)
16           {
17               p1=head;
18               p2=p1->next;
19               while(--a)
20               {
21                   p1=p1->next;
22                   p2=p2->next ;
23               }
24               cout<<"哈嘍,小可愛,請輸入你的資訊 :"<<endl<<"學號 "<<"姓名 "<<"性別 "<<"語文 "<<"數學 "<<"英語 "<<endl;
25      
26              cin>>p2->num>>p2->name>>p2->sex>>p2->Chinese>>p2->Math>>p2->English; 
27              p2->sum=p2->Chinese+p2->Math+p2->English;
28              p2->average=(p2->sum)/3;
29              cout<<"YES!修改成功"<<endl;
30         
31         }
32         else
33         {
34             cout<<"輸入錯誤,請輸入0-"<<count<<"之間的數字位置:" ;
35         }
36       }
37       else
38       {
39           cout<<"輸入錯誤,請輸入0-"<<count<<"之間的數字位置:" ;
40       }
41       
42       
43   }

 

 1 void Student::findStudent(Student *head)//查詢學生的資訊 
 2   {
 3       system("cls");
 4       Student *p1;
 5       int p;
 6       int i=0;
 7       int stunum;
 8       char stuname[5];
 9       p1=head->next;
10       cout<<"請選擇 1.按學號查詢 2. 按姓名查詢"<<endl; 
11       cin>>p;
12       if(p==1)
13       {
14           system("cls");
15         cout<<"請輸入要查詢的學號 "<<endl;
16         cin>>stunum;
17         while(p1!=NULL)
18         {
19             if(stunum==p1->num)
20             {
21                 cout<<"學號\t"<<"姓名\t"<<"性別\t"<<"語文"
22                <<"\t"<<"數學"<<"\t"<<"英語"<<"\t"<<"\t"<<"平均分"<<"\t"<<"\t"<<"總分"
23                 <<"\t"<<"\t"<<endl; 
24                 cout<<p1->num<<"\t"<<p1->name<<"\t"<<p1->sex<<"\t"<<p1->Chinese<<"\t"<<p1->Math<<"\t"<<p1->English<<"\t"<<"\t";
25                 cout<<p1->average<<"\t"<<"\t";    
26                  cout<<p1->sum<<"\t"<<"\t";               
27             }
28             p1=p1->next;
29            }
30         
31          }else if(p==2)
32        {
33             system("cls");
34         cout<<"請輸入要查詢的姓名 "<<endl;
35         cin>>stuname;
36         while(p1!=NULL)
37         {
38             if(strcmp(p1->name,stuname)==0)
39             {
40                     cout<<"學號\t"<<"姓名\t"<<"性別\t"<<"語文"
41                <<"\t"<<"數學"<<"\t"<<"英語"<<"\t"<<"\t"<<"平均分"<<"\t"<<"\t"<<"總分"
42                 <<"\t"<<"\t"<<endl; 
43                 cout<<p1->num<<"\t"<<p1->name<<"\t"<<p1->sex<<"\t"<<p1->Chinese<<"\t"<<p1->Math<<"\t"<<p1->English<<"\t"<<"\t";
44                 cout<<p1->average<<"\t"<<"\t";    
45                  cout<<p1->sum<<"\t"<<"\t";
46             }
47             p1=p1->next;
48         }
49     }else
50     {
51         cout<<"輸入選項錯誤 "<<endl;
52     }
53 
54   } 

 

 

 1 void Student::paixuStudent(Student *head)//排序
 2 {
 3     Student *p1;
 4     Student *p2;
 5     p1=head;
 6     int n=0;
 7     int a=0;
 8     char b[20];
 9     int i,j;
10     while(p1->next)
11     {
12         p1=p1->next ;
13         n++;
14     }
15     for(i=0;i<n-1;i++)
16     {
17         p2=head->next;
18         p1=p2->next;
19         for(j=0;j<n-i-1;j++)
20         {
21            if(p1->average >p2->average )
22             {
23                 strcpy(b,p2->name);
24                 strcpy(p2->name ,p1->name );
25                 strcpy(p1->name,b);
26                 
27                 strcpy(b,p2->sex);
28                 strcpy(p2->sex ,p1->sex);
29                 strcpy(p1->sex,b);
30         
31                 a=p2->num ;
32                 p2->num =p1->num ;
33                 p1->num =a;
34                 
35                 a=p2->Chinese;
36                 p2->Chinese=p1->Chinese;
37                 p1->Chinese=a;
38                 
39                 a=p2->Math ;
40                 p2->Math =p1->Math ;
41                 p1->Math =a;
42                 
43                 a=p2->English ;
44                 p2->English =p1->English ;
45                 p1->English =a;
46                 
47                 a=p2->average;    
48                 p2->average =p1->average ;
49                 p1->average =a;
50                 
51                 a=p2->sum ;
52                 p2->sum =p1->sum ;
53                 p1->sum =a;
54             }
55             p1=p1->next;
56             p2=p2->next;
57         }
58     }
59     cout<<"YES!排序成功 "<<endl;
60 }

 

 

 1 void Student::saveStudent(Student *head) //儲存學生的資訊 
 2 {
 3     Student *p;
 4     ofstream os;
 5     os.open("sco.txt");
 6 
 7     p=head->next ;
 8     while(p)
 9     {
10        os<<p->num<<"\t"<<p->name<<"\t"<<p->sex<<"\t"<<p->Chinese<<"\t" <<p->Math<<"\t" <<p->English <<"\t"<<p->sum<<"\t"<<p->average; 
11         
12         p=p->next;
13         os<<endl;
14     }
15 
16     cout<<"YES!儲存成功 "<<endl; 
17     os.close();
18 }

 

 

 1 void Student:: loadStudent(Student *head)//載入學生的資訊 
 2 {
 3     Student *p,*p2;
 4     ifstream is;
 5     is.open("sco.txt");
 6     
 7     
 8     p2=head;
 9     
10     while(1)
11     {
12         p=new Student;
13            is>>p->num>>p->name>>p->sex>>p->Chinese 
14            >>p->Math>>p->English >>p->sum>>p->average;
15            if(is.fail() )//判斷是否到檔案結尾 
16            {
17                delete p; 
18                p2->next =NULL;
19                break;
20            }
21            
22         p2->next =p;
23         
24         p2=p2->next;    
25     }
26     
27     cout<<"YES!載入成功 "<<endl;
28 
29     is.close();
30     
31     
32 }

 

 

 1 int count;
 2 void Student::getcountStudent(Student *head)
 3 {
 4     Student *p;
 5     count=0;
 6     p=head;
 7     while(p->next!=NULL)
 8     {
 9         count++;
10         p=p->next;
11     }
12 }
13  
14 void welcome()
15 {
16  cout<<"       ~~~    歡迎進入**學生成績管理系統!   ~~~"<<endl;
17  system("color 1");
18 }
19 void menu()
20 {
21  cout<<"      ******************************************"<<endl;
22  cout<<"      ~~~~~~~   學生成績管理系統  ~~~~~~~"<<endl;
23  cout<<"      ******************************************"<<endl;
24  cout<<"      ******************************************"<<endl;
25  cout<<"      ******************************************"<<endl;
26  cout<<"              ~~哈嘍,小可愛,請選擇你要進行的操作**" <<endl;        
27  cout<<"              ~~1.    輸入學生的資訊          ~~"<<endl;
28  cout<<"              ~~2.    瀏覽學生的資訊          ~~"<<endl;
29  cout<<"              ~~3.    刪除學生的資訊         ~~"<<endl;
30  cout<<"              ~~4.    新增學生的資訊          ~~"<<endl;
31  cout<<"              ~~5.    修改學生的資訊          ~~"<<endl;
32  cout<<"              ~~6.    查詢學生的資訊          ~~"<<endl;
33  cout<<"              ~~7.    排序學生的成績          ~~"<<endl;
34  cout<<"              ~~8.    儲存學生的資訊          ~~"<<endl;
35  cout<<"              ~~9.    載入學生的資訊          ~~"<<endl;
36  cout<<"              ~~0.    退出學生資訊系統        ~~"<<endl; 
37  cout<<"       *****************************************"<<endl;
38  cout<<"       *****************************************"<<endl;
39  cout<<"       *****************************************"<<endl;
40  system("color b");
41 }

 

 1 int main()
 2 {
 3     Student h;
 4     system("cls"); 
 5     welcome();
 6     Sleep(1000);
 7     int i;
 8     Student *head=new Student;
 9     
10     while(1)
11     {
12         system("cls");
13         menu();
14         cout<<"哈嘍,小可愛,請輸入你要進行的操作 :"<<endl;
15         cin>>i; 
16         switch(i)
17         {
18             case 1:  cout<<"輸入學生的資訊 "<<endl;h.inputStudent(head);system("pause");h.getcountStudent(head);break;
19             case 2:  cout<<"瀏覽學生的資訊 "<<endl;h.outputStudent(head);system("pause");h.getcountStudent(head);break;
20             case 3:  cout<<"刪除學生的資訊 "<<endl;h.deleteStudent(head);system("pause");h.getcountStudent(head);break;
21             case 4:  cout<<"新增學生的資訊 "<<endl;h.addStudent(head);system("pause");h.getcountStudent(head);break;
22             case 5:  cout<<"修改學生的資訊 "<<endl;h.changeStudent(head);system("pause");h.getcountStudent(head);break;
23             case 6:  cout<<"查詢學生的資訊 "<<endl;h.findStudent(head);system("pause");h.getcountStudent(head);break;
24             case 7:  cout<<"學生成績的排序 "<<endl;h.paixuStudent(head);system("pause");h.getcountStudent(head);break; 
25             case 8:  cout<<"儲存學生的資訊 "<<endl;h.saveStudent(head);system("pause");h.getcountStudent(head);break;
26             case 9:  cout<<"載入學生的資訊 "<<endl;h.loadStudent(head);system("pause");h.getcountStudent(head);break;
27             case 0:  cout<<"謝謝使用,歡迎下次光臨 "<<endl;system("pause");exit(0);
28              
29             default: cout<<"輸入錯誤"<<endl; 
30         }
31     }
32     return 0;
33 }

 

相關文章