C Entry-level student management system

Source: Internet
Author: User

Trainer, a student management system that is no longer simple. Hope the experts do not spray.

# Include <stdio. h> # include <stdlib. h> # include <string. h> # define num 20 // total number of users # define Add 1 // added tag # define del 2 // deleted tag # define UPD 3 // updated tag # define ser 4 // tag for a single search # define all 5 // tag for all search # define exit 6 // tag for exit # define Yes 1 // operation successful # define no 0 // operation failed // Student Score structure struct info {float Chinese; float math; float English ;}; // basic student information struct, nested Student Score struct Stu {int ID; char * Name; char sex; struct info * In ;}; typedef struct Stu stu_s; Typedef struct info info_ I; // use the global variable stu_s * lib [num] for the student information library; int Len = sizeof (LIB)/sizeof (LIB [0]); int add (stu_s * t) // Add a student function. I use (ID-1) to indicate the position in the student information library, saves traversal trouble {If (LIB [(t-> ID)-1] = NULL) // if the information library is empty, add it directly, return Yes {lib [T-> ID-1] = (stu_s *) malloc (sizeof (struct Stu) * 1 ); lib [T-> ID-1]-> name = (char *) malloc (sizeof (char) * 10 ); lib [T-> ID-1]-> In = (info_ I *) malloc (sizeof (struct info) * 1); LIB [T-> ID-1]-> I D = T-> ID; LIB [T-> ID-1]-> name = T-> name; lib [T-> ID-1]-> sex = T-> sex; lib [T-> ID-1]-> In-> Chinese = T-> In-> Chinese; lib [T-> ID-1]-> In-> math = T-> In-> math; lib [T-> ID-1]-> In-> English = T-> In-> English; return yes;} // if the information library is not empty, the student already exists. Return noprintf ("** failed to add ** \ n"); printf ("^ sorry! This student already exists! ^ \ N "); return no;} int Delete (int id) // deletes a student by passing in the ID {If (LIB [ID-1]! = NULL) // if the student information is not empty, delete it directly, and return YES {free (LIB [ID-1]); LIB [ID-1] = NULL; // memmove (LIB + J, lib + J + 1, len-j); Return yes;} // otherwise, the student does not exist, return noprintf ("** failed to delete ** \ n"); printf ("^ sorry, this student does not exist! ^ \ N "); return no;} int beforeupd (int id) // before updating student information, check whether the student exists before proceeding to the next step, yes {If (LIB [ID-1] = NULL) {printf ("** update failed ** \ n") is returned "); printf ("^ sorry! This student does not exist! ^ \ N "); return no;} return yes;} int Update (int id, stu_s * t) // The student already exists, directly update the student information and return YES {lib [ID-1]-> id = T-> ID; LIB [ID-1]-> name = T-> name; lib [ID-1]-> sex = T-> sex; LIB [ID-1]-> In-> Chinese = T-> In-> Chinese; lib [ID-1]-> In-> math = T-> In-> math; lib [ID-1]-> In-> English = T-> In-> English; return yes;} int search (int id) // search for {If (LIB [ID-1]! = NULL) // if the student information is not empty, print it directly, and return YES {printf ("ID = % d student information: \ n", ID ); printf ("student ID: % d, name: % s, Gender: % C, language: %. 2f, mathematics: %. 2f, English: %. 2f \ n ", lib [ID-1]-> ID, lib [ID-1]-> name, lib [ID-1]-> sex, lib [ID-1]-> In-> Chinese, lib [ID-1]-> In-> math, lib [ID-1]-> In-> English ); return yes;} // otherwise, the student does not exist. noprintf ("** query failed ** \ n") is returned; printf ("^ sorry! This student does not exist! ^ \ N "); return no;} void printall () // query all student information {Int J; For (j = 0; j <Len; ++ J) // traverse the {If (LIB [J]! = NULL) // print all the information not empty. {printf ("student ID: % d, name: % s, Gender: % C, language: %. 2f, mathematics: %. 2f, English: %. 2f \ n ", lib [J]-> ID, lib [J]-> name, lib [J]-> sex, lib [J]-> In-> Chinese, lib [J]-> In-> math, lib [J]-> In-> English) ;}} int menu () // menu function, {printf ("@ select a menu label: \ n"); printf ("1: Add student information. \ N "); printf (" 2: delete student information. \ N "); printf (" 3: Update student information. \ N "); printf (" 4: Query individual student information. \ N "); printf (" 5: Query all student information. \ N "); printf (" 6: exit the system. \ N "); printf ("========================================== =============================\ N "); int num; scanf ("% d", & num); getchar (); Return num;} int main (void) {// declare a student information constructor, and allocate the dynamic memory stu_s * s; S = (stu_s *) malloc (sizeof (struct Stu) * 1); s-> name = (char *) malloc (sizeof (char) * 10); s-> In = (info_ I *) malloc (sizeof (struct info) * 1 ); printf ("=============================== * student management system * ================== ===========\ N "); int num; // set a menu label to operate label: num cyclically = Menu (); Switch (Num) // capture the menu label entered by the user {Case Add: // Add the label printf ("Enter the student ID :"); scanf ("% d", & S-> ID); getchar (); printf ("Enter name:"); s-> name = (char *) malloc (sizeof (char) * 10); scanf ("% s", S-> name); getchar (); printf ("Enter Gender :"); scanf ("% C", & S-> sex); getchar (); printf ("enter Chinese score:"); scanf ("% F ", & S-> In-> Chinese); getchar (); printf ("Enter the mathematical score:"); scanf ("% F ", & S-> In-> math); getchar (); printf ("Enter the English score:"); scanf ("% F ", & S-> In-> English); getchar (); If (S-> ID> 0 & S-> id <= num) {int isadd = add (s); If (isadd = Yes) printf ("***** congratulations, student information has been added! * ***** \ N ");} else {printf (" ****** sorry, the ID you entered is out of the range! * ***** \ N ");} printf ("========================================== =============================\ N "); goto label; Case DEL: // Delete the label printf ("Enter the student ID to delete:"); int IDD; scanf ("% d", & IDD ); getchar (); If (IDD> 0 & IDD <= num) {int isdel = Delete (IDD); If (isdel = Yes) {printf ("***** congratulations, student information has been deleted! * ***** \ N ") ;}} else {printf (" ****** sorry, the ID you entered is out of the range! * ***** \ N ");} printf ("========================================== =============================\ N "); goto label; Case UPD: // update the label printf ("Enter the student ID to be updated:"); int IDU; scanf ("% d", & IDU ); getchar (); If (IDU> 0 & IDU <= num) {int isok = beforeupd (IDU); If (isok = Yes) {S-> id = IDU; printf ("Enter name:"); s-> name = (char *) malloc (sizeof (char) * 10); scanf ("% s ", s-> name); getchar (); printf ("Enter Gender:"); scanf ("% C", & S-> sex); getchar (); printf ("Enter the Chinese score :"); Scanf ("% F", & S-> In-> Chinese); getchar (); printf ("Enter the mathematical score:"); scanf ("% F ", & S-> In-> math); getchar (); printf ("Enter the English score:"); scanf ("% F ", & S-> In-> English); getchar (); int isupd = Update (IDU, S); If (isupd = Yes) printf ("***** congratulations, student information has been updated! * ***** \ N ") ;}} else {printf (" ****** sorry, the ID you entered is out of the range! * ***** \ N ");} printf ("========================================== =============================\ N "); goto label; Case SER: printf ("Enter the student ID to query:"); int IDs; scanf ("% d", & IDs ); if (IDs> 0 & IDs <num) {int isser = search (IDS);} else {printf ("***** sorry, the ID you entered is out of the range! * ***** \ N ");} printf ("========================================== =============================\ N "); goto label; case all: printf ("all student information: \ n"); printall (); printf ("========================================== =============================\ N "); goto label; goto label; Case Exit: break;} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.