Student management system (pure C implementation)

学生管理系统(纯C语言实现)

Student management system (pure C implementation)

  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
#include "stdio.h"
#include"string.h"
#include"stdlib.h" 

struct Student
{
     char ID[20];
     char Name[20];
     float Mark1;
     float Mark2;
     float Mark3;
     float Average;
};
 
struct Student students[1000];
int num=0;
 
float Avg(struct Student stu)
{
     return (stu.Mark1+stu.Mark2+stu.Mark3)/3;
}

int Student_SearchByIndex(char id[])
 
{
 
     int i;
     for (i=0;i<num;i++)
     {
         if (strcmp(students[i].ID,id)==0)
         {
              return i;
         }
     }
     return -1;
}

/*Returns the array subscript by Name*/
int Student_SearchByName(char Name[])
{
     int i;
     for (i=0;i<num;i++)
     {
         if (strcmp(students[i].Name,Name)==0)
         {
              return i;
         }
     }
     return -1;
}

/*Display a single student record*/ Student number
void Student_DisplaySingle(int index)
{
     printf("%10s%10s%8s%8s%8s%10s\n","Student number","Name","Higher Mathematics score","C Language score","Computers score","Average score");
     printf("-------------------------------------------------------------\n");
     printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[index].ID,students[index].Name,students[index].Mark1,students[index].Mark2,students[index].Mark3,students[index].Average);
}

/*Insert student information*/ 
void Student_Insert()
{
     while(1)
     {
         printf("Please enter Student number:");
         scanf("%s",&students[num].ID);
         getchar();
         printf("Please enter Name:");
         scanf("%s",&students[num].Name);
         getchar();
         printf("Please enter Higher Mathematics score:");
         scanf("%f",&students[num].Mark1);
         getchar();
         printf("Please enter C Language score:");
         scanf("%f",&students[num].Mark2);
         getchar();
         printf("Please enter Computers score:");
         scanf("%f",&students[num].Mark3);
         getchar();
         students[num].Average=Avg(students[num]);
         num++;
         printf("Whether to continue?(y/n)");
         if (getchar()=='n')
         {
              break;
         }
     }
}
 
/*Modify student information*/
void Student_Modify()
{
     //float mark1,mark2,mark3;
     while(1)
     {
         char id[20];
         int index;
         printf("Please enter the student to be modified's Student number:");
         scanf("%s",&id);
         getchar();
         index=Student_SearchByIndex(id);
         if (index==-1)
         {
              printf("Students do not exist!\n");
         }
         else
         {
              printf("The student information you want to modify is:\n");
              Student_DisplaySingle(index);
              printf("-- Please enter a new value--\n");
              printf("Please enter Student number:");
              scanf("%s",&students[index].ID);
              getchar();
              printf("Please enter Name:");
              scanf("%s",&students[index].Name);
              getchar();
              printf("Please enter Higher Mathematics score:");
              scanf("%f",&students[index].Mark1);
              getchar();
              printf("Please enter C Language score:");
              scanf("%f",&students[index].Mark2);
              getchar();
              printf("Please enter Computers score:");
              scanf("%f",&students[index].Mark3);
              getchar();
              students[index].Average=Avg(students[index]);
         }
         printf("Whether to continue?(y/n)");
         if (getchar()=='n')
         {
              break;
         }
     }
}
 
/*Delete Student Information*/
void Student_Delete()
{
     int i;
     while(1)
     {
         char id[20];
         int index;
         printf("Please enter the student to be deleted's Student number:");
         scanf("%s",&id);
         getchar();
         index=Student_SearchByIndex(id);
         if (index==-1)
         {
              printf("Students do not exist!\n");
         }
         else
         {
              printf("The student information you want to delete is:\n");
              Student_DisplaySingle(index);
              printf("Is it really necessary to delete?(y/n)");
              if (getchar()=='y')
              {
                   for (i=index;i<num-1;i++)
                   {
                       students[i]=students[i+1];//Move all the objects behind you forward
                   }
                   num--;
              }
              getchar();
         }
         printf("Whether to continue?(y/n)");
         if (getchar()=='n')
         {
              break;
         }
     }
}
 
/*Search by Name*/
void Student_Select()
{
     while(1)
     {
         char Name[20];
         int index;
         printf("Please enter the student's Name:");
         scanf("%s",&Name);
         getchar();
         index=Student_SearchByName(Name);
         if (index==-1)
         {
              printf("Students do not exist!\n");
         }
         else
         {
              printf("The student information you are looking for is:\n");
              Student_DisplaySingle(index);
         }
         printf("Whether to continue?(y/n)");
         if (getchar()=='n')
         {
              break;
         }
     }
}
 
/*Sort by average*/
void Student_SortByAverage()
{
     int i,j;
     struct Student tmp;
     for (i=0;i<num;i++)
     {
         for (j=1;j<num-i;j++)
         {
              if (students[j-1].Average<students[j].Average)
              {
                   tmp=students[j-1];
                   students[j-1]=students[j];
                   students[j]=tmp;
              }
         }
     }
}
 
/*Show student information*/
void Student_Display()
{
     int i;
     printf("%10s%10s%8s%8s%8s%10s\n","Student number","Name","Higher Mathematics score","C Language score","Computers score","Average score");
     printf("-------------------------------------------------------------\n");
     for (i=0;i<num;i++)
     {
         printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[i].ID,students[i].Name,students[i].Mark1,students[i].Mark2,students[i].Mark3,students[i].Average);
     }
}
 
/*Read student information out of the file*/
void IO_ReadInfo()
{
     FILE *fp;
     int i;
     if ((fp=fopen("Database.txt","rb"))==NULL)
     {
         printf("Cannot open file!\n");
         return;
     }
     if (fread(&num,sizeof(int),1,fp)!=1)
     {
         num=-1;
     }
     else
     {
         for(i=0;i<num;i++)
         {
              fread(&students[i],sizeof(struct Student),1,fp);
         }
     }
     fclose(fp);
}
 
/*Writing student information to a file*/
void IO_WriteInfo()
{
     FILE *fp;
     int i;
     if ((fp=fopen("Database.txt","wb"))==NULL)
     {
         printf("Cannot open file!\n");
         return;
     }
     if (fwrite(&num,sizeof(int),1,fp)!=1)
     {
         printf("Write to file error!\n");
     }
     for (i=0;i<num;i++)
     {
         if (fwrite(&students[i],sizeof(struct Student),1,fp)!=1)
         {
              printf("Write to file error!\n");
         }
     }    
     fclose(fp);
}
 
/*Main Program*/
void main()
{
     int choice;
     IO_ReadInfo();
     while(1)
     {
         /*Main Menu*/
         printf("\n------ Student Management System------\n");
         printf("1. Adding student records\n");
         printf("2. Modify student records\n");
         printf("3. Delete student records\n");
         printf("4. Search Student Records by Name\n");
         printf("5. Sorted by average score\n");
         printf("6. Exit\n");
         printf("Please select(1-6):");
         scanf("%d",&choice);
         getchar();
         switch(choice)
         {
         case 1:
              Student_Insert();
              break;
         case 2:
              Student_Modify();
              break;
         case 3:
              Student_Delete();
              break;
         case 4:
              Student_Select();
              break;
         case 5:
              Student_SortByAverage();
              Student_Display();
              break;
         case 6:
              exit(0);
              break;
         }
         IO_WriteInfo();
     }
}
Built with Hugo
Theme Stack designed by Jimmy