# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <conio. h> # define N 100/* stores the student status information of 100 students */int flag;/* Indicates whether to log on */struct date/* date of birth */{int year; int month; int day;}; struct student {char num [20];/* student ID */char name [25];/* name */char sex [10]; /* Gender */struct date birthday;/* date of birth */int age;/* age */char addr [50];/* Nationality */char dept [50]; /* Department */char major [30];/* major */char classs [30];/* class */char phone [20];/* phone */} st U [N]; void login ()/* log on */{void menu (); system ("cls"); if (flag) {printf ("logged on! \ N "); printf (" Press any key to continue... \ N "); getchar (); system (" cls "); menu ();} int load (); int n, I, m, w; FILE * fp; if (fp = fopen ("D: \ file.txt", "rb "))! = NULL)/* not an empty file */n = load ();/* open the file */else n = 0; struct student s; char password [20]; int d, q; printf ("\ n \ t select a logon identity: 1. administrator 2. user [] \ B "); scanf (" % d ", & d); if (d = 1) {printf ("\ n \ t please enter the password:"); getchar (); int j = 0; while (1) {password [j] = getch (); if (password [j] = '\ R') break; printf ("*"); j ++ ;} password [j] = '\ 0'; if (! Strcmp (password, "930322")/* determine whether the password is correct */{flag = 1; printf ("\ n \ t login successful! \ N "); printf (" \ tPress [Enter] key to continue... "); Getchar (); system (" cls "); menu ();} if (! Flag) {printf ("\ t \ n Password error! \ N "); printf (" What do you want to do next? 1. continue to log on 2. return menu [] \ B "); scanf (" % d ", & q); if (q = 1) /* continue logging on */{system ("cls"); login ();} else if (q = 2) /* return to the main menu */{system ("cls"); menu () ;}} else {printf ("\ n \ t Username :"); scanf ("% s", s. name); printf ("\ t password:"); int j = 0; while (1) {password [j] = getch (); if (password [j] = '\ R') break; printf ("*"); j ++;} password [j] =' \ 0 '; flag = 0;/* flag = 0 */for (I = 0; I <n; I ++) if (! Strcmp (s. name, stu [I]. name )&&! Strcmp (s. name, password) {flag = 1;/* login successful flag = 1 */break;} if (flag) {printf ("\ n \ t login successful! \ N "); printf (" \ tPress any key to continue... \ N "); getchar (); system (" cls "); menu ();/* return to the main menu */} else {m = 0 after successful logon; /* Set m to avoid failing to log on. */w = 1;/* set w to avoid outputting "user does not exist" */int q multiple times; /* q is the option */do {if (w> 0) printf ("the user name does not exist or the password is incorrect! \ N ");/* displays */printf (" What do you want to do next? 1. continue to log on 2. return to the menu [] \ B "); scanf (" % d ", & q); if (q = 1)/* continue logging on */{login (); m = 1;} else if (q = 2)/* return to the main menu */{system ("cls"); menu (); m = 1 ;} else {printf ("incorrect selection! Please reselect: \ n "); w = 0 ;}} while (m = 0) ;}} void printf_one (int k) /* output a student's information */{system ("color 4e"); printf ("student ID: % s \ n", stu [k]. num); printf ("Name: % s \ n", stu [k]. name); printf ("Gender: % s \ n", stu [k]. sex); printf ("Date of Birth: % d/% d \ n", stu [k]. birthday. year, stu [k]. birthday. month, stu [k]. birthday. day); printf ("Age: % d \ n", stu [k]. age); printf ("Nationality: % s \ n", stu [k]. addr); printf ("Don't: % s \ n", stu [k]. dept); printf ("Major: % s \ n", stu [k]. major); printf ("Class: % s \ N ", stu [k]. classs); printf ("Tel: % s \ n", stu [k]. phone);} void printf_n (int n)/* output information of all students during browsing */{system ("color 2e"); int j, I = 1; for (j = 0; j <n; j ++) {printf ("% d student: \ n", I ++); printf_one (j ); printf ("\ n") ;}getchar () ;}int load ()/* open the FILE and read the data in it */{FILE * fp; int I; if (fp = fopen ("D: \ file.txt", "rb") = NULL)/* reads an empty file. rb indicates opening a binary file, read data only */{printf ("\ n file does not exist! Enter \ n "); return 0;} for (I = 0 ;! Feof (fp); I ++)/* process to the end of the file */fread (& stu [I], sizeof (struct student), 1, fp ); /* fread (buffer, size, cout, fp) reads a batch of data in the disk file as a whole at a time. buffer specifies the location of the data block and the number of bytes of the data block, count reads, fp refers to the source Disk File */fclose (fp); return (I-1);} void input (int k)/* enter student information */{getchar (); printf ("student ID:"); gets (stu [k]. num); printf ("name:"); gets (stu [k]. name); printf ("Gender:"); gets (stu [k]. sex); printf ("Date of Birth:"); scanf ("% d/% d", & stu [k]. birthday. year, & stu [k]. birt Hday. month, & stu [k]. birthday. day); printf ("Age:"); scanf ("% d", & stu [k]. age); getchar (); printf ("Nationality:"); gets (stu [k]. addr); printf ("Don't:"); gets (stu [k]. dept); printf ("Major:"); gets (stu [k]. major); printf ("class:"); gets (stu [k]. classs); printf ("Phone:"); gets (stu [k]. phone);} void save (int n)/* save student information */{void exit (); FILE * fp; int I; if (fp = fopen ("D: \ file.txt "," wb ") = NULL)/* only open or create a binary file, only allow Data Writing */{printf (" \ nCan not open File! \ N "); exit () ;}for (I = 0; I <n; I ++) if (stu [I]. name [0]! = '\ 0') if (fwrite (& stu [I], sizeof (struct student), 1, fp )! = 1)/* write a batch of data into the disk file as a whole */printf ("file write error! \ N "); fclose (fp);} void printf_back ()/* displays" Browse or return "after adding student information */{int w, k = 0; void browse (); void menu (); printf ("\ n \ t ^_^. inserted successfully. ^ _ ^ \ n "); do {printf (" What do you want to do next? \ N \ t1 ). browse all \ t2 ). return Value: [] \ B "); scanf (" % d ", & w); if (w = 1) {system (" cls "); browse ();} else if (w = 2) {system ("cls"); menu ();} else printf ("Incorrect choice! Please select again! \ N ") ;}while (k = 0);} void insert ()/* Add student information */{int I, n, k, t; FILE * fp; if (fp = fopen ("D: \ file.txt", "rb") = NULL)/* reads an empty file. rb indicates opening a binary file, read data only */{printf ("How many people do you want to add (1-% d )?: ", N-1); n = 0;} else {n = load (); printf (" How many student do you want to insert (1-% d )?: ", N-n-1);} scanf (" % d ", & t); k = n + t; for (I = n; I <k; I ++) {printf ("\ n enter the % d student information. \ n ", I-n + 1); input (I);} save (k);/* save all student information */printf_back ();} void deleter () /* Delete */{void menu (); if (! Flag) {printf ("\ n \ t please log on first! \ N "); printf (" \ n \ t press Enter to Enter the logon interface! "); Getchar (); login () ;}else {struct student s; int I, n, k, w0, w1; n = load (); do {k =-1; // printf_n (n ); /* display the information of all students before deletion */do {printf ("\ n please enter the name of the student you want to delete! \ NName: "); scanf (" % s ", s. name); for (I = 0; I <n; I ++) if (strcmp (s. name, stu [I]. name) = 0) {k = I; s = stu [I];} if (k =-1) {int m; printf ("\ n does not have the student information! \ N "); printf (" What do you want to do next? 1. continue input 2. return to the main menu [] \ B "); scanf (" % d ", & m); if (m = 1) deleter (); else {k = 0; menu () ;}}while (k =-1); printf_one (k); printf ("\ n \ t are you sure you want to delete? \ N \ t1 ). yes 2 ). return [] \ B ");/* re-query whether to delete */scanf (" % d ", & w0); if (w0 = 1) {stu [k]. name [0] = '\ 0'; save (n);} else break; printf ("\ n \ t 1__^. successfully deleted ^_^. \ n "); printf (" What do you want to do? \ N \ t1 ). continue 2 ). returns [] \ B "); scanf (" % d ", & w1) ;}while (w1 = 1); system (" cls "); menu ();/* return to the main menu, w1! = 1 */} void search ()/* search */{void menu (); int w0, k, I, n, w1, w2; struct student s; FILE * fp; if (fp = fopen ("D: \ file.txt", "rb "))! = NULL)/* not empty file */n = load (); do {printf ("select search category: 1. name 2. student ID [] \ B "); scanf (" % d ", & w1); if (w1 <1 | w1> 2) {printf (" input error! Enter again! \ N "); w2 = 1;} elsew2 = 0;/* Note: if you do not add this sentence, it will be in an endless loop, cannot jump out */} while (w2 = 1); if (w1 = 1)/* search by name */{do {k =-1; do {printf ("\ n please enter the name of the student you want to find! \ NName: "); scanf (" % s ", s. name); for (I = 0; I <n; I ++) if (strcmp (s. name, stu [I]. name) = 0) {k = I; s = stu [I];} if (k =-1)/* not found */{int o; printf ("\ n does not have the student information! \ N! "); Printf (" What do you want to do? \ N \ t1. continue 2. return to the menu [] \ B "); scanf (" % d ", & o); if (o = 1) {system (" cls "); search ();/* continue searching */} else {system ("cls"); menu () ;}} while (k =-1 ); system ("cls"); printf_one (k);/* Find and output the student's information */printf ("\ nWhat do you want to do next? \ N \ t1 ). continue 2 ). return to the menu [] \ B "); scanf (" % d ", & w0);} while (w0 = 1); system (" cls "); menu ();/* return to the main menu */} else/* search by student ID */{do {k =-1; do {printf ("\ n please enter the student ID you want to find! \ NNum: "); scanf (" % s ", s. num); for (I = 0; I <n; I ++) if (strcmp (s. num, stu [I]. num) = 0)/* found */{k = I; s = stu [I];} if (k =-1) /* not found */{int o; printf ("\ n no student information! \ N! "); Printf (" What do you want to do? \ N \ t1. continue 2. return to the menu [] \ B "); scanf (" % d ", & o); if (o = 1) {system (" cls "); search ();/* continue searching */} else {system ("cls"); menu () ;}} while (k =-1 ); system ("cls"); printf_one (k);/* Find and output the student's student status information */printf ("\ nWhat do you want to do? \ N \ t1 ). continue 2 ). return to the menu [] \ B "); scanf (" % d ", & w0);} while (w0 = 1); system (" cls "); menu ();/* return the main menu */} int modify_data (int I)/* modify information function */{int c, w1; void menu (); do {puts ("\ n select the option to be modified: \ n \ n1. student ID 2. name 3. gender 4. date of birth 5. age 6. nationality 7. 8. professional 9. class 10. phone number 11. all information 12. cancel and return "); printf (" Select?: [] \ B "); scanf (" % d ", & c); if (c> 12 | c <1) {puts ("\ n selection error! Please select again! "); Getchar () ;}}while (c> 12 | c <1); do {switch (c) {case 1: printf (" student ID :"); scanf ("% s", stu [I]. num); break; case 2: printf ("name:"); scanf ("% s", stu [I]. name); break; case 3: printf ("Gender:"); scanf ("% s", stu [I]. sex); break; case 4: printf ("Date of Birth:"); scanf ("% d/% d", & stu [I]. birthday. year, & stu [I]. birthday. month, & stu [I]. birthday. day); break; case 5: printf ("Age:"); scanf ("% d", & stu [I]. age); break; case 6: printf ("Nationality Guan: "); scanf (" % s ", stu [I]. addr); break; case 7: printf ("Don't:"); scanf ("% s", stu [I]. dept); break; case 8: printf ("professional:"); scanf ("% s", stu [I]. major); break; case 9: printf ("class:"); scanf ("% s", stu [I]. classs); break; case 10: printf ("Tel:"); scanf ("% s", stu [I]. phone); break; case 11: input (I); break;/* modify all information of this student */case 12: menu (); /* return to the main menu */} puts ("\ nNow: \ n"); printf_one (I ); /* output the modified student information */printf ("\ n OK? \ N \ t1 ). yes 2 ). no, modify it again. 3 ). [] \ B "); scanf (" % d ", & w1) ;}while (w1 = 2); return (w1 ); /* after the modification is successful and 1 is selected, 1 */} void modify ()/* modify */{void menu (); if (! Flag) {printf ("\ n \ t please log on first! \ N "); printf (" \ n \ t press Enter to Enter the logon interface! "); Getchar (); login () ;}else {struct student s; int I, n, k, w0 = 1, w1, w2 = 0; n = load (); do {k =-1; // printf_n (n ); /* display the information of all students before deletion */do {printf ("\ n please enter the name of the student you want to modify! \ NName: "); scanf (" % s ", s. name); for (I = 0; I <n; I ++) if (strcmp (s. name, stu [I]. name) = 0)/* match with one of the students */{k = I; s = stu [I];} if (k =-1) /* The input student is not found */{int o; printf ("\ n does not have the student information! \ N "); printf (" What do you want to do? \ N \ t1. continue 2. return to the menu [] \ B "); scanf (" % d ", & o); if (o = 1) {system (" cls "); modify ();/* continue to modify */} else {system ("cls"); menu () ;}} while (k =-1 ); system ("cls"); printf_one (k);/* output the student's information */w1 = modify_data (k); if (w1 = 1) /* determine whether the modification is successful */{printf ("\ n \ t success _^. successfully modified ^_^. \ n continue? \ N \ t1 ). yes 2 ). save \ t [] \ B "); scanf (" % d ", & w0); w2 = 1 ;}else {w0 = 0; if (w2 = 0) stu [k] = s;} if (w0! = 1) & (w2 = 1)/* save modified successfully */save (n);} while (w0 = 1); system ("cls "); menu ();/* no modification returned to the main menu */} void browse ()/* browse */{system ("color 5f"); void menu (); int n; n = load (); printf_n (n); printf ("A total of % d student records. \ n ", n); printf (" \ nPress [ENTER] key to back... "); getchar (); system (" cls "); menu ();/* return main function */} void exit () /* exit */{exit (0);} void face ()/* access page */{system ("color 4e"); printf ("\ n \ t★★★★★★★★★★★★★★★★★★\ N "); puts ("\ t ※※※※※※※※※※※※※※※※※※※※※※※※※※※"); puts ("\ t ※※※※※"); puts (" \ t ※※※※※"); puts ("\ t ※※welcome to the student status management system! ※※"); Puts ("\ t ※※※※※"); puts (" \ t ※※※※"); puts ("\ t ※※※※※"); puts (" \ t ※※※※※"); puts ("\ t ※※※producer: Li yuan ※※"); puts ("\ t ※※2013.6.20 ※※※"); puts (" \ t ※※※※"); puts ("\ t ※※※※※※※※※※※※※※※※※※※※※※※※※※※"); printf ("\ n \ tPress [Enter] key to continue ...... \ N "); getchar (); system (" cls ");} void menu ()/* Main menu */{system (" color 2e "); printf ("\ n \ t welcome to use the student status management system! \ N "); printf (" \ n "); int n, w1; do {puts (" \ t★★★★★★★★Menu★★★★★★★★"); Puts (" \ t★1. Login★"); Puts (" \ t★2. Insert★"); Puts (" \ t★3. Delete★"); Puts (" \ t★4. Search★"); Puts (" \ t★5. Repair and Modification★"); Puts (" \ t★6. Overview★"); Puts (" \ t★7. Return★"); Puts (" \ t★★★★★★★★★★★★★★★★★★★★★"); Printf (" select a service type (1-7): [] \ B "); scanf (" % d ", & n ); if (n <1 | n> 7) {system ("cls"); printf ("Incorrect choice! Please select again! \ N "); w1 = 1;} else w1 = 0;} while (w1 = 1); switch (n) {case 1: login (); break; /* log on */case 2: system ("cls"); insert (); break;/* insert */case 3: system ("cls"); deleter (); break;/* Delete */case 4: system ("cls"); search (); break;/* search */case 5: system ("cls "); modify (); break;/* modify */case 6: system ("cls"); browse (); break;/* browse */case 7: exit (); break;/* exit */} int main () {face (); menu (); return 0 ;}