Student Dormitory management system written in C language and Student Dormitory management system
The struct and file pointer are used. In fact, writing is not difficult, mainly because the file pointer is rarely used, so it took a long time to continue debugging. There is still a BUG in the final program. For example, the two-person dormitory swap must be used before the single-person dormitory swap, otherwise the subsequent data reading will fail, in addition, the record of student A must be in front of student B During dormitory swap; otherwise, an error may occur. These are all file pointers and need to be improved. Although it took a long time, the teacher did not recognize such a job, because there is no visual interface, he said, a programmer must consider the user experience, so as long as there is no visual interface, no matter how good you write, you will not get a good score.
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Typedef struct {
Char ID [20]; // student ID
Char name [20]; // name
Char gender [10]; // gender
Char dormitory [4]; // dormitory No.
} Student;
Typedef struct {
Char ID [4]; // Dormitory number
Int max_num; // Number of people allowed
Int current_num; // number of people who have checked in
Char gender [10]; // men's or female's
} Dormitory;
Char filename [20];
FILE * fp; // student information FILE pointer
FILE * _ fp; // dormitory information FILE pointer
Void create (){
Printf ("\ n enter the name of the new building: \ n ");
Scanf ("% s", filename );
_ Fp = fopen (filename, "rb + ");
If (_ fp = NULL) {// if the file does not exist, create a file. Otherwise, the system prompts that the file already exists.
_ Fp = fopen (filename, "wb + ");
Fp = fopen (strcat (filename, "student"), "wb + ");
If (fp = NULL | _ fp = NULL ){
Printf ("File not open! \ N ");
Exit (-1 );
}
} Else
Printf ("the file already exists! \ N ");
Fclose (fp );
Fclose (_ fp );
}
Void open (){
Printf ("enter the building where the student dormitory is located: \ n ");
Scanf ("% s", filename );
_ Fp = fopen (filename, "rb + ");
Fp = fopen (strcat (filename, "student"), "rb + ");
If (fp = NULL | _ fp = NULL ){
Printf ("File not open! \ N ");
Exit (-1 );
}
}
Void insert_dorm (){
Dormitory * d = (Dormitory *) malloc (sizeof (Dormitory ));
Open ();
Printf ("\ n enter the Dormitory number, which can accommodate the number of students. The number of students has been accommodated. Men's/female's homes (separated by spaces, '#'): \ n ");
Scanf ("% s", d-> ID );
While (strcmp (d-> ID ,"#")){
Scanf ("% d % s", & d-> max_num, & d-> current_num, d-> gender );
Fwrite (d, sizeof (Dormitory), 1, _ fp );
Scanf ("% s", d-> ID );
}
Fclose (_ fp );
Fclose (fp );
}
Void insert_stu (){
Long locate, pre_locate;
Int flag = 0;
Student * s = (Student *) malloc (sizeof (Student ));
Dormitory * d = (Dormitory *) malloc (sizeof (Dormitory ));
Open ();
Printf ("\ n enter the student ID, name, gender, and Dormitory number (separated by spaces, '#'): \ n ");
Scanf ("% s", s-> ID );
While (strcmp (s-> ID, "#") {// not equal to continue reading, equal to 0 Then exit the loop and no longer read data
Scanf ("% s", s-> name, s-> gender, s-> dormitory );
Locate = ftell (_ fp );
While (fread (d, sizeof (Dormitory), 1, _ fp )){
If (! Strcmp (d-> ID, s-> dormitory) {// the actual number of students in the dormitory should be increased
Fseek (_ fp, locate, SEEK_SET );
D-> current_num ++;
Fwrite (d, sizeof (Dormitory), 1, _ fp );
Flag = 1;
Break;
}
Locate = ftell (_ fp );
}
If (flag = 0 ){
Printf ("this dormitory does not exist! \ N ");
Exit (-1 );
}
Fwrite (s, sizeof (Student), 1, fp );
Rewind (_ fp );
Scanf ("% s", s-> ID );
}
Fclose (_ fp );
Fclose (fp );
}
Void dormnumsearch (){
Int flag = 0, tag = 0;
Char input_dormnum [4];
Student * s = (Student *) malloc (sizeof (Student ));
Dormitory * d = (Dormitory *) malloc (sizeof (Dormitory ));
Open ();
Printf ("Enter the Dormitory number to be searched: \ n ");
Scanf ("% s", input_dormnum );
While (fread (d, sizeof (Dormitory), 1, _ fp )){
If (! Strcmp (input_dormnum, d-> ID )){
Flag = 1;
Printf ("\ n dormitory information: \ n %-20 s %-20 s %-20 s %-20s \ n", "dormitory No ", "Number of students allowed", "Number of students allowed", "Men's/female ");
Printf ("%-20 s %-20d %-20d %-20s \ n", d-> ID, d-> max_num, d-> current_num, d-> gender );
Break;
}
}
If (flag ){
Printf ("the dormitory student information is as follows: \ n %-20 s %-20 s %-20 s %-20s \ n", "student ID", "name ", "gender", "Dormitory number ");
While (fread (s, sizeof (Student), 1, fp )){
If (! Strcmp (input_dormnum, s-> dormitory )){
Tag = 1;
Printf ("%-20 s %-20 s %-20 s %-20s \ n", s-> ID, s-> name, s-> gender, s-> dormitory );
}
}
If (tag = 0)
Printf ("\ n no student stays in this dormitory! \ N ");
} Else
Printf ("this dormitory record does not exist !! \ N ");
Fclose (_ fp );
Fclose (fp );
}
Void change (){
Int flag = 0, tag = 0;
Long locate, locate_ B, locate_c;
Char input_stunum [20];
Char input_dormnum [4];
Char previus_dormnum [4];
Student * s = (Student *) malloc (sizeof (Student ));
Dormitory * d = (Dormitory *) malloc (sizeof (Dormitory ));
Open ();
Printf ("\ n please enter the student ID you want to change to: \ n ");
Scanf ("% s", input_stunum );
Locate = ftell (fp );
While (fread (s, sizeof (Student), 1, fp )){
If (! Strcmp (input_stunum, s-> ID )){
Flag = 1;
Printf ("the student information is as follows: \ n %-20 s %-20 s %-20 s %-20s \ n", "student ID", "name ", "gender", "Dormitory number ");
Printf ("%-20 s %-20 s %-20 s %-20s \ n", s-> ID, s-> name, s-> gender, s-> dormitory );
Strcmp (previous_dormnum, s-> dormitory );
Printf ("\ n enter the Dormitory number of the new dormitory: \ n ");
Scanf ("% s", input_dormnum );
Locate_ B = ftell (_ fp );
While (fread (d, sizeof (Dormitory), 1, _ fp )){
If (! Strcmp (input_dormnum, d-> ID )){
Tag = 1;
If (d-> current_num = d-> max_num)
Printf ("this dormitory is full! ");
Else {
// Change the Student Dormitory Information
Fseek (fp, locate, SEEK_SET );
Strcpy (s-> dormitory, input_dormnum );
Fwrite (s, sizeof (Student), 1, fp );
// Change the dormitory Information
Fseek (_ fp, locate_ B, SEEK_SET );
D-> current_num ++;
Fwrite (d, sizeof (Dormitory), 1, _ fp );
Rewind (_ fp );
// Change old dormitory Information
Locate_c = ftell (_ fp );
While (fread (d, sizeof (Dormitory), 1, _ fp )){
If (! Strcmp (previous_dormnum, d-> ID )){
Fseek (_ fp, locate_c, SEEK_SET );
D-> current_num --;
Fwrite (d, sizeof (Dormitory), 1, _ fp );
Break;
}
Locate_c = ftell (_ fp );
}
}
Break;
}
Locate_ B = ftell (_ fp );
}
If (tag = 0 ){
Printf ("this dormitory does not exist! ");
Exit (-1 );
}
Break;
}
Locate = ftell (fp );
}
If (flag = 0)
Printf ("\ n does not exist for this student! \ N ");
Fclose (_ fp );
Fclose (fp );
}
Void namesearch (){
Int flag = 0, tag = 0;
Long locate;
Char input_name [20];
Char dor_num [4];
Char ID [20];
Student * s = (Student *) malloc (sizeof (Student ));
Dormitory * d = (Dormitory *) malloc (sizeof (Dormitory ));
Open ();
Printf ("\ n enter the name to be searched: \ n ");
Scanf ("% s", input_name );
While (fread (s, sizeof (Student), 1, fp )){
Locate = ftell (fp); // records the location searched by name
If (! Strcmp (input_name, s-> name )){
Flag = 1;
Printf ("\ n student information: \ n %-20 s %-20 s %-20 s %-20s \ n", "student ID", "name ", "gender", "Dormitory number ");
Printf ("%-20 s %-20 s %-20 s %-20s \ n", s-> ID, s-> name, s-> gender, s-> dormitory );
Strcpy (dor_num, s-> dormitory );
Strcpy (ID, s-> ID );
Rewind (fp); // the pointer is reset to the file header to find the roommate
Printf ("\ n the roommate information is as follows: \ n %-20 s %-20 s %-20 s %-20s \ n", "student ID", "name ", "gender", "Dormitory number ");
While (fread (s, sizeof (Student), 1, fp) {// same roommate Dormitory number, different Student IDs
If ((! Strcmp (dor_num, s-> dormitory) & (strcmp (s-> ID, ID ))){
Printf ("%-20 s %-20 s %-20 s %-20s \ n", s-> ID, s-> name, s-> gender, s-> dormitory );
Tag = 1;
}
}
If (tag = 0)
Printf ("\ n no roommate! \ N ");
Tag = 0;
While (fread (d, sizeof (Dormitory), 1, _ fp )){
If (! Strcmp (dor_num, d-> ID )){
Printf ("\ n the student's dormitory information is as follows: \ n %-20 s %-20 s %-20 s %-20s \ n", "dormitory No ", "Number of students allowed", "Number of students allowed", "Men's/female ");
Printf ("%-20 s %-20d %-20d %-20s \ n", d-> ID, d-> max_num, d-> current_num, d-> gender );
Break;
}
}
// Break;
}
// The same name
If (flag)
Fseek (fp, locate, SEEK_SET );
}
If (flag = 0)
Printf ("\ n does not have a qualified student record! \ N ");
Fclose (_ fp );
Fclose (fp );
}
Void check_out (){
Char stu_num [20];
Char dorm_num [20];
Long locate, locate_ B;
Student * s = (Student *) malloc (sizeof (Student ));
Dormitory * d = (Dormitory *) malloc (sizeof (Dormitory ));
Open ();
Printf ("\ n enter the student ID: \ n ");
Scanf ("% s", stu_num );
Locate = ftell (fp );
While (fread (s, sizeof (Student), 1, fp )){
// Locate = ftell (fp );
If (! Strcmp (s-> ID, stu_num )){
Strcpy (dorm_num, s-> dormitory );
Fseek (fp, locate, SEEK_SET );
Memset (s, 0, sizeof (Student ));
Fwrite (s, sizeof (Student), 1, fp );
Break;
}
Locate = ftell (fp );
}
Locate_ B = ftell (_ fp );
While (fread (d, sizeof (Dormitory), 1, _ fp )){
If (! Strcmp (d-> ID, dorm_num )){
Fseek (_ fp, locate_ B, SEEK_SET );
D-> current_num --;
// Printf ("111 \ n ");
// Printf ("% d \ n", d-> current_num );
Fwrite (d, sizeof (Dormitory), 1, _ fp );
Printf ("deleted successfully! \ N ");
Break;
}
Locate_ B = ftell (_ fp );
}
Fclose (_ fp );
Fclose (fp );
}
Void numsearch (){
Int flag = 0, tag = 0;
Char input_num [20];
Char dor_num [4];
Student * s = (Student *) malloc (sizeof (Student ));
Dormitory * d = (Dormitory *) malloc (sizeof (Dormitory ));
Open ();
Printf ("\ n enter the student ID to be searched: \ n ");
Scanf ("% s", input_num );
While (fread (s, sizeof (Student), 1, fp )){
If (! Strcmp (input_num, s-> ID )){
Flag = 1;
Printf ("\ n the student information is as follows: \ n %-20 s %-20 s %-20 s %-20s \ n", "student ID", "name ", "gender", "Dormitory number ");
Printf ("%-20 s %-20 s %-20 s %-20s \ n", s-> ID, s-> name, s-> gender, s-> dormitory );
Break;
}
}
Strcpy (dor_num, s-> dormitory );
If (flag ){
Rewind (fp );
Printf ("\ n roommate information: \ n %-20 s %-20 s %-20 s %-20s \ n", "student ID", "name ", "gender", "Dormitory number ");
While (fread (s, sizeof (Student), 1, fp )){
If ((! Strcmp (dor_num, s-> dormitory) & strcmp (input_num, s-> ID )){
Printf ("%-20 s %-20 s %-20 s %-20s \ n", s-> ID, s-> name, s-> gender, s-> dormitory );
Tag = 1;
}
}
If (tag = 0)
Printf ("\ n no roommate! \ N ");
While (fread (d, sizeof (Dormitory), 1, _ fp )){
If (! Strcmp (dor_num, d-> ID )){
Printf ("\ n the student's dormitory information is as follows: \ n %-20 s %-20 s %-20 s %-20s \ n", "dormitory No ", "Number of students allowed", "Number of students allowed", "Men's/female ");
Printf ("%-20 s %-20d %-20d %-20s \ n", d-> ID, d-> max_num, d-> current_num, d-> gender );
Break;
}
}
} Else
Printf ("\ n does not have the student's record! \ N ");
Fclose (_ fp );
Fclose (fp );
}
Void exchange (){
Int flag = 0, tag = 0;
Long locate_a, locate_ B;
Char input_ID_a [20];
Char input_ID_ B [20];
Char dor_num [4];
Student * s = (Student *) malloc (sizeof (Student ));
Student * _ s = (Student *) malloc (sizeof (Student ));
Open ();
Printf ("\ n enter the student ID of dormitory student A to be exchanged: \ n ");
Scanf ("% s", input_ID_a );
Printf ("\ n enter the student ID of dormitory student B to be exchanged: \ n ");
Scanf ("% s", input_ID_ B );
Locate_a = ftell (fp );
While (fread (s, sizeof (Student), 1, fp )){
If (! Strcmp (input_ID_a, s-> ID )){
Printf ("\ n student A information: \ n %-20 s %-20 s %-20 s %-20s \ n", "student ID", "name ", "gender", "Dormitory number ");
Printf ("%-20 s %-20 s %-20 s %-20s \ n", s-> ID, s-> name, s-> gender, s-> dormitory );
Flag = 1;
Break;
}
Locate_a = ftell (fp );
}
Rewind (fp );
Locate_ B = ftell (fp );
While (fread (_ s, sizeof (Student), 1, fp )){
If (! Strcmp (input_ID_ B, _ s-> ID )){
Printf ("\ n student B information: \ n %-20 s %-20 s %-20 s %-20s \ n", "student ID", "name ", "gender", "Dormitory number ");
Printf ("%-20 s %-20 s %-20 s %-20s \ n", _ s-> ID, _ s-> name, _ s-> gender, _ s-> dormitory );
Tag = 1;
Break;
}
Locate_ B = ftell (fp );
}
If (flag = 0 | tag = 0 ){
Printf ("no records from A or B! ");
Exit (-1 );
} Else {
Strcpy (dor_num, s-> dormitory );
Strcpy (s-> dormitory, _ s-> dormitory );
Strcpy (_ s-> dormitory, dor_num );
// Locate_a = locate_a-sizeof (Student );
Fseek (fp, locate_a, 0 );
Fwrite (s, sizeof (Student), 1, fp );
// Locate_ B = locate_b-sizeof (Student );
Fseek (fp, locate_ B, 0 );
Fwrite (_ s, sizeof (Student), 1, fp );
}
Printf ("Change dormitory: \ n ");
Rewind (fp );
Locate_a = ftell (fp );
While (fread (s, sizeof (Student), 1, fp )){
If (! Strcmp (input_ID_a, s-> ID )){
Printf ("\ n student A information: \ n %-20 s %-20 s %-20 s %-20s \ n", "student ID", "name ", "gender", "Dormitory number ");
Printf ("%-20 s %-20 s %-20 s %-20s \ n", s-> ID, s-> name, s-> gender, s-> dormitory );
Break;
}
Locate_a = ftell (fp );
}
Locate_ B = ftell (fp );
While (fread (_ s, sizeof (Student), 1, fp )){
If (! Strcmp (input_ID_ B, _ s-> ID )){
Printf ("\ n student B information: \ n %-20 s %-20 s %-20 s %-20s \ n", "student ID", "name ", "gender", "Dormitory number ");
Printf ("%-20 s %-20 s %-20 s %-20s \ n", _ s-> ID, _ s-> name, _ s-> gender, _ s-> dormitory );
Break;
}
Locate_ B = ftell (fp );
}
Fclose (_ fp );
Fclose (fp );
}
Void display (){
Long locate;
Student * s = (Student *) malloc (sizeof (Student ));
Dormitory * d = (Dormitory *) malloc (sizeof (Dormitory ));
Open ();
Printf ("\ n enter the student ID, name, and Gender: \ n ");
Scanf ("% s", s-> ID, s-> name, s-> gender );
Locate = ftell (_ fp );
While (fread (d, sizeof (Dormitory), 1, _ fp )){
If (d-> max_num = d-> current_num ){
Strcpy (s-> dormitory, d-> ID );
Fwrite (s, sizeof (Student), 1, fp );
Fseek (_ fp, locate, 0 );
D-> current_num ++;
Fwrite (d, sizeof (Dormitory), 1, _ fp );
Printf ("the assigned dormitory is :");
Printf ("% s \ n", s-> dormitory );
Break;
}
Locate = ftell (fp );
}
Fclose (_ fp );
Fclose (fp );
}
Int main (){
Int select, flag = 1; // exit if the flag is 0
While (flag ){
Printf ("\ t ------------------------------------------------------ \ n ");
Printf ("\ t Student Dormitory management system \ n ");
Printf ("\ t 0 ---------------- add building \ n ");
Printf ("\ t 1 ---------------- add dormitory information \ n ");
Printf ("\ t 2 ---------------- new student accommodation information \ n ");
Printf ("\ t 3 ---------------- interchange dormitory \ n ");
Printf ("\ t 4 ---------------- change dormitory \ n ");
Printf ("\ t 5 ---------------- students leave school \ n ");
Printf ("\ t 6 ---------------- smart dormitory \ n ");
Printf ("\ t 7 ---------------- student id query \ n ");
Printf ("\ t 8 ---------------- name query \ n ");
Printf ("\ t 9 ---------------- room number QUERY \ n ");
Printf ("\ t 10 ---------------- exit system \ n ");
Printf ("\ t ------------------------------------------------------ \ n ");
Printf ("Enter your operation number (0 ~ 10 ):");
Scanf ("% d", & select );
Switch (select ){
Case 0:
Create ();
Break;
Case 1:
Insert_dorm ();
Break;
Case 2:
Insert_stu ();
Break;
Case 3:
Exchange ();
Break;
Case 4:
Change ();
Break;
Case 5:
Check_out ();
Break;
Case 6:
Display ();
Break;
Case 7:
Numsearch ();
Break;
Case 8:
Namesearch ();
Break;
Case 9:
Dormnumsearch ();
Break;
Case 10:
Return 0;
Defaut:
Break;
}
}
Return 0;
}