[数据结构实验]学生成绩管理

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <malloc.h>
  4 #include <cstring>
  5 #define NULL 0
  6 #define LEN sizeof(Node)
  7 using namespace std;
  8 const char lesson[][12] = {"Math", "English", "Chinese", "physics", "Chemistry"};
  9 struct Node {
 10         char ID[110];
 11         int arr[5];
 12         struct Node *next;
 13         Node() {
 14                 memset(ID, 0, sizeof(ID));
 15                 memset(arr, 0, sizeof(arr));
 16                 next = NULL;
 17         }
 18 };
 19 
 20 struct StudentGradeManageSystem {
 21         Node ListHead;
 22         Node *head = &ListHead;
 23         bool find(char str[]) {
 24                 Node *p = ListHead.next;
 25                 while(p) {
 26                         if(strcmp(p->ID, str) == 0) return 1;
 27                         p = p->next;
 28                 }
 29                 return 0;
 30         }
 31         // add students' information
 32         void addStu() {
 33                 Node *p = head;
 34                 while(p->next != NULL) p = p->next;
 35                 while(1) {
 36                         Node *tmp;
 37                         tmp = (Node*) malloc(LEN);
 38                         tmp->next = NULL;
 39                         cout<< endl;
 40                         puts("*************** add student information ****************");
 41                         cout<< "Input ID(input 0 to exit): ";
 42                         cin>> tmp->ID;
 43                         if(find(tmp->ID)) {
 44                                 printf("ID already exist!\n");
 45                                 continue;
 46                         }
 47                         if(strlen(tmp->ID) == 1 && tmp->ID[0] == '0') break;
 48                         for(int i = 0; i < 5; i++) {
 49                                 printf("Input %s grade: ", lesson[i]);
 50                                 cin>> tmp->arr[i];
 51                         }
 52                         p = p->next = tmp;
 53                         cout<< endl;
 54                 }
 55         }
 56         // delete students' information
 57         void deleteStu() {
 58                 while(1) {
 59                         char str[102];
 60                         cout<< endl;
 61                         puts("*************** delete student information ****************");
 62                         cout<< "Input ID(input 0 to exit): ";
 63                         cin>> str;
 64                         if(strlen(str) == 1 && str[0] == '0') break;
 65                         Node *p = head->next, *fa = head;
 66                         int F = 0;
 67                         while(p) {
 68                                 if(strcmp(p->ID, str) == 0) {
 69                                         fa->next = p->next;
 70                                         F = 1;
 71                                         break;
 72                                 }
 73                                 fa = p;
 74                                 p = p->next;
 75                         }
 76                         if(F) puts("successfully delete!");
 77                         else puts("ID don't exist!");
 78                         cout<< endl;
 79                 }
 80         }
 81         // view students' information
 82         void displayStu() {
 83                 cout<< endl;
 84                 cout<< "************************* view student information ****************************"<< endl;
 85                 printf("%12s", "ID");
 86                 for(int i = 0; i < 5; i++) printf("%12s", lesson[i]);
 87                 cout<< endl;
 88                 Node *p = head->next;
 89                 while(p) {
 90                         printf("%12s", p->ID);
 91                         for(int i = 0; i < 5; i++) printf("%12d", p->arr[i]);
 92                         cout<< endl;
 93                         p = p->next;
 94                 }
 95                 cout<< "*******************************************************************************"<< endl;
 96         }
 97 };
 98 int Menu()
 99 {
100         cout<< endl;
101         puts("---------------- Welcome to Student Grade Manage System --------------");
102         puts("0: exit");
103         puts("1: add students' information");
104         puts("2: delete students' information");
105         puts("3: view students' information");
106         printf("Input: ");
107         int x;
108         cin>> x;
109         return x;
110 }
111 int main()
112 {
113         StudentGradeManageSystem G;
114         while(1) {
115                 int key = Menu();
116                 if(key == 0) break;
117                 if(key == 1) G.addStu();
118                 if(key == 2) G.deleteStu();
119                 if(key == 3) G.displayStu();
120         }
121         return 0;
122 }
View Code

 

posted @ 2014-11-25 11:36  jklongint  阅读(355)  评论(0)    收藏  举报