Dormitory Management System

Source: Internet
Author: User

#include <stdio.h> #include <string.h> #include <stdlib.h> #define M 100int dirty=0;//To determine if the operation has been saved Define a store that stores information about studentsStructural bodytypedef struct{CHAR s_name[31];//student name Char s_number[16];//Student study number int d_number;//Dormitory number of the student's dorm int total;//number of students}student [M], st;//to determine whether the number of the table with the number of the repetition void S_number_judge (Student s,int t) {int i; for (i=1;i<= (s->total) -1;i++) while (strcmp ( S[i]. S_number,s[t]. S_number) {==0) {printf ("Student input failed, the number already exists, please re-enter the study number!\n"); printf ("Please enter students ' School number (15 characters):"); scanf ("%s", S[t]. S_number); GetChar (); I=1; }}//Adding student informationFunctionvoid Add (Student S) {printf ("Please enter student name (within 30 characters):"), scanf ("%s", s[++ (S->total)]. S_name); GetChar ();//GetLine break printf ("Please enter student's number (within 15 characters):"); scanf ("%s", S[s->total]. S_number); GetChar (); S_number_judge (s,s->total);//Determine whether the input number and the number of the table in the school is repeated printf ("Please enter the dorm number:"); scanf ("%d", &s[s->total]. D_number); GetChar (); Dirty=1; printf ("Add success!\n\n");} Modify student information function void Alter (Student S) {int i; int flag=0;//used to determine if the table has information about the student to be modified Char name[20]; printf ("Please enter the name of the student you want to modify:"); scanf ("%s", name); GetChar (); for (i=1;i<=s->total;i++) if (strcmp (S[i]. S_name,name) ==0) flag=i; if (!flag) printf ("The Student information you want to modify does not exist in the table!\n"); else {printf ("New information as follows: \ n"); printf ("Please enter student name (within 30 characters):"); scanf ("%s", S[flag]. S_name); GetChar (); printf ("Please enter student's number (within 15 characters):"); scanf ("%s", S[flag]. S_number); GetChar (); S_number_judge (S,flag); printf ("Please enter dorm number:"); scanf ("%d", &s[flag]. D_number); GetChar (); Dirty=1; printf ("Modified successfully!\n"); } putchar (' \ n ');} Delete Student information void Delete (Student S) {int i,j; int flag=0;//used to determine if the table has information about the student being deleted char name[20]; printf ("Please enter the name of the student you want to delete:"); scanf ("%s", name); GetChar (); for (i=1;i<=s->total;i++) if (strcmp (S[i]. S_name,name) ==0) FLag=i; if (!flag) printf ("The student you want to delete does not exist in the table!"); else {for (i=flag;i<s->total;i++) {j=i+1; strcpy (S[i]. S_NAME,S[J]. S_name); strcpy (S[i]. S_NUMBER,S[J]. S_number); S[i]. D_NUMBER=S[J]. D_number; } (S->total)--; Dirty=1; printf ("Delete succeeded!"); } printf ("\ n");} Show all student information function void Display_all (Student S) {int i; printf ("All students ' information is as follows: \ n"); printf ("Student name, student number, dormitory number \ n"); for (i=1;i<=s-> total;i++) printf ("%-20s%-15s%-5d\n", S[i]. S_name,s[i]. S_number,s[i]. D_number); Putchar (' \ n ');} Sorting functions According to the dorm number from small to large (bubbling method) void Sort_d_number (Student S) {int i,j,t; char name[30]; char number[15]; for (i=1;i<=s-> total;i++) for (j=i;j<=s->total;j++) if (S[i]. D_NUMBER&GT;S[J]. D_number) {strcpy (name,s[i]. S_name); strcpy (Number,s[i]. S_number); T=s[i]. D_number; strcpy (S[i]. S_NAME,S[J]. S_name); strcpy (S[i]. S_NUMBER,S[J]. S_number); S[i]. D_NUMBER=S[J]. D_number; strcpy (S[j]. S_name,name); strcpy (S[j]. S_number,number); S[J]. d_number=t; }}//sorting functions are ordered from small to large (bubbling method) void Sort_s_number (Student S) {int i,j,t; char name[30]; char number[15]; for (i=1;i<=s->total;i++) for (j=i;j<=s->total;j++) if (strcmp (s[i). S_NUMBER,S[J]. S_number) >0) {strcpy (name,s[i]. S_name); strcpy (Number,s[i]. S_number); T=s[i]. D_number; strcpy (S[i]. S_NAME,S[J]. S_name); strcpy (S[i]. S_NUMBER,S[J]. S_number); S[i]. D_NUMBER=S[J]. D_number; strcpy (S[j]. S_name,name); strcpy (S[j]. S_number,number); S[J]. d_number=t; The}}//query function queries with the name of the keyword (Sequential Lookup void Query_s_name (Student S) {int i,j=0; char name[31]; printf ("Enter the name of the student you are looking for (within 30 characters):"); scanf ("%s", name); GetChar () ; printf ("Find student information as follows: \ n"); printf ("Student name, student number, dormitory number \ n"); for (i=1;i<=s->total;i++) if (strcmp (Name,s[i]. S_name) {==0) {printf ("%-20s%-15s%-5d\n", S[i]. S_name,s[i]. S_number,s[i]. D_number); J=1; } if (!j) printf ("\ n lookup failed, table does not exist for the student's information!\n\n");} Query function query (binary lookup) void Query_s_number (Student S) {int i,j,top,base,mid; char number[15]; j=0; base=1; top=s-> Total; printf ("Please enter the student number you want to find:"); scanf ("%s", number); GetChar (); Sort_s_number (S); The table data in accordance with the school number from small to large order printf ("The students are looking for information as follows: \ n"); printf ("Student name, student number, dormitory number \ n"); if (strcmp (number,s[1). S_number) >=0&&strcmp (Number,s[s->total]. S_number) <=0) {while (base<=top) {mid= (base+top)/2; if (strcmp (Number,s[mid). S_number) {==0) {printf ("%-20s%-15s%-5d\n", S[i]. S_name,s[i]. S_number,s[i]. D_number); Putchar (' \ n '); J=1; Break } else if (strcmp (Number,s[mid). S_number) >0) base=mid+1; else top=mid-1; }} if (!j) printf ("\ n lookup failed, the study does not exist in the tableInformation!\n\n ");} Query function query with dorm number (binary lookup) void Query_d_number (Student S) {int i,j,m,n,base,top,mid; j=0; base=1; top=s->total; printf ("Please enter the dorm number you want to inquire:"); scanf ("%d", &i); GetChar (); Sort_d_number (S);//The table data in accordance with the dormitory number from small to large sort printf ("The search bedroom information as follows: \ n"); printf ("Student name, student number, dormitory number \ n"); if (I>=s[1]. D_number&&i<=s[s->total]. D_number) {while (base<=top) {mid= (base+top)/2; if (I==s[mid]. D_number) {m=mid; n=mid-1; while (S[m]. d_number==i) {printf ("%-20s%-15s%-5d\n", S[m]. S_NAME,S[M]. S_NUMBER,S[M]. D_number); m++; if (m>s->total) break; } if (n>0) {while (S[n]. d_number==i) {printf ("%-20s%-15s%-5d\n", S[n]. S_name,s[n]. S_number,s[n]. D_number); n--; if (n<1) break; }} j=1; Putchar (' \ n '); Break } else if (I>s[mid]. D_number) base=mid+1; else top=mid-1; }} if (!j) printf ("\ n lookup failed, the table does not have information!\n\n for the Dorm");} storage function void Save (Student S) {St Std; int i; int flag1=0,flag2=0;//Determine if the store succeeds FILE *fp; if (Fp=fopen ("Dorm_manage", "W") ==nul L) {printf ("Open file failed!\n\n"); flag1=1; exit (0);//End program} for (i=1;i<=s->total;i++) if (Fwrite (&s[i],sizeof (STD), 1,FP)!=1) {printf ("Data write error \ n \ nthe"); flag2=1; exit (0);} if (!flag1 &AMP;&AMP;!FLAG2) {printf ("Data storage succeeded!\n\n"); dirty=0;} fclose (FP);} Load record function void load (Student S) {St Std; FILE *FP; if (Fp=fopen ("Dorm_manage", "R") ==null) {printf ("Open file failed!\n\n"), exit (0),} while (!feof (FP)) fread (&s[++ (s-> Total)],sizeof (STD), 1,FP); Fclose (FP); printf ("Load data successfully!\n\n"); (s->total)--; Determine whether to save the function void Judge_save (int i,student S) {char ch, if (i) {printf ("The data in the table has been changed or not to be saved and then exited (y/n)") because the number of}//to be subtracted from the list of problems to be read out of the program; Ch=getchar (); GetChar (); while (ch!= ' n ' &&ch!= ' n ' &&ch!= ' y ' &&ch!= ' y ') {printf ("Please enter n (n) or Y (y):"); Ch=getchar (); GetChar (); } if (ch== ' Y ' | | ch== ' Y ') Save (S); }}//menus void menu () {//Menu printf ("Dormitory management \ n"); printf ("************************************* menu *********************** \ n "); printf ("1. Adding Student Information | 7. According to the dorm number from small to large order \ n "); printf ("2. Modify Student Information | 8. According to the school number from small to large order \ n "); printf ("3. Delete Student Information | 9. Show all student's information \ n "); printf ("4. Search student Information by name | 10. Save operation \ n "); PrinTF ("5. Search for student information by study number | 11. Load record \ n "); printf ("6. Search for all student information in the dorm by dormitory number | 12. Exit program \ n "); printf ("******************************************************************************\n");} void Main () {int i; Student S; s->total=0; do {menu (); printf ("Select the function you want to implement (please enter any number in the ~):"); scanf ("%d", &i); GetChar ();//Get line feed Putchar (' \ n '); switch (i) { Case 1:add (S); Break Case 2:alter (S); Break Case 3:delete (S); Break Case 4:query_s_name (S); Break Case 5:query_s_number (S); Break Case 6:query_d_number (S); Break Case 7:sort_d_number (S); printf ("Sort complete!\n\n"); Break Case 8:sort_s_number (S); printf ("Sort complete!\n\n"); Break Case 9:display_all (S); Break Case 10:save (S); Break Case 11:load (S); Break Case 12:judge_save (dirty,s); Exit (0); Break default:printf ("Select Error: Please select!\n\n between options"); Break }}while (i!=12);}

Dormitory Management System

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.