C language implements a simple student roll management system

  • 2020-06-03 07:29:28
  • OfStack

The example of this paper shares the specific code of C language to achieve student status management system for your reference. The specific content is as follows


#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 
#include<conio.h> 
#define N 100 /* storage 100 Student status information of three students */ 
int flag; /* Whether the tag is logged in */ 
struct date /* Date of birth */ 
{ 
  int year; 
  int month; 
  int day; 
}; 
struct student 
{ 
  char num[20];/* Student id */ 
  char name[25];/* The name */ 
  char sex[10];/* gender */ 
  struct date birthday;/* Date of birth */ 
  int age;/* age */ 
  char addr[50];/* Native place */ 
  char dept[50]; /* Is don't */ 
  char major[30]; /* professional */ 
  char classs[30]; /* The class */ 
  char phone[20];/* The phone */ 
}stu[N]; 
 
void login() /* The login */ 
{ 
  void menu(); 
  system("cls"); 
  if(flag) 
  { 
    printf("  Have login !\n"); 
    printf("Press any key to continue ... \n"); 
    getchar(); 
    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 Please select login identity: 1. The administrator    2. The user    [ ]\b\b"); 
  scanf("%d",&d); 
  if(d==1) 
  { 
    printf("\n\t\t\t Please enter your 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 if 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 mistake !\n"); 
      printf("What do you want to do next? 1. Continue to login  2. Return to the menu   [ ]\b\b"); 
      scanf("%d",&q); 
      if(q==1) /* Continue to login */ 
      { 
        system("cls"); 
        login(); 
      } 
      else if(q==2) /* Back to the main menu */ 
      { 
        system("cls"); 
        menu(); 
      } 
    } 
  } 
  else 
  { 
    printf("\n\t\t\t The user name :"); 
    scanf("%s",s.name); 
    printf("\t\t\t password :"); 
    int j=0; 
    while(1) 
    { 
      password[j]=getch(); 
      if(password[j]=='\r') 
       break; 
      printf("*"); 
      j++; 
    } 
    password[j]='\0'; 
    flag=0; /* Not login 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(); getchar(); 
     system("cls"); 
     menu(); /* Return to the main menu after successful login */ 
   } 
   else 
   { 
    m=0; /* Set up the m It's to avoid getting stuck   When login fails   Can't jump out of the login screen */ 
    w=1; /* Set up the w Is to avoid multiple outputs   "The user does not exist" */ 
    int q; /*q For the options */ 
    do 
    { 
      if(w>0) 
        printf(" The user name does not exist or the password is incorrect !\n"); /* The first 1 Displayed when a non-existent user is entered on the second login */ 
      printf("What do you want to do next? 1. Continue to login  2. Return to the menu   [ ]\b\b"); 
      scanf("%d",&q); 
      if(q==1) /* Continue to login */ 
      { 
        login(); 
        m=1; 
      } 
      else if(q==2) /* Back to the main menu */ 
      { 
        system("cls"); 
        menu(); 
        m=1; 
      } 
      else 
      { 
        printf(" The wrong choice ! Please reselect :\n"); 
        w=0; 
      } 
    }while(m==0); 
   } 
  } 
} 
 
void printf_one(int k) /* The output 1 Student information */ 
{ 
  system("color 4e"); 
  printf(" Student number: %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/%d\n",stu[k].birthday.year,stu[k].birthday.month,stu[k].birthday.day); 
  printf(" Age: %d\n",stu[k].age); 
  printf(" Native place: %s\n",stu[k].addr); 
  printf(" Don't: %s\n",stu[k].dept); 
  printf(" Professional: %s\n",stu[k].major); 
  printf(" Class: %s\n",stu[k].classs); 
  printf(" Telephone: %s\n",stu[k].phone); 
} 
 
void printf_n(int n) /* Output information for all students while browsing */ 
{ 
  system("color 2e"); 
  int j,i=1; 
  for(j=0;j<n;j++) 
  { 
    printf(" The first %d A student: \n",i++); 
    printf_one(j); 
    printf("\n"); 
  } 
 getchar(); 
} 
 
int load() /* Open the file , Read the data inside */ 
{ 
  FILE *fp; 
  int i; 
  if((fp=fopen("D:\\file.txt","rb"))==NULL) /* Read in an empty file, rb Said to open 1 a 2 Base file. Only read data is allowed */ 
  { 
    printf("\n File does not exist ! Please enter the :\n"); 
    return 0; 
  } 
  for(i=0;!feof(fp);i++) /* Process to end of file */ 
    fread(&stu[i],sizeof(struct student),1,fp); /*fread(buffer,size,cout,fp) Will be in the disk file 1 Batch data as 1 A whole 1 Read it subsexuallyout , 
  buffer Specify the location of the data block, sizeof Number of bytes of data block, count Read times, fp Point to the source disk file */ 
  fclose(fp); 
  return(i-1); 
} 
 
void input(int k) /* Enter student information */ 
{ 
  getchar(); 
  printf(" Student number: ");  gets(stu[k].num); 
  printf(" Name: ");  gets(stu[k].name); 
  printf(" Gender: ");  gets(stu[k].sex); 
  printf(" Date of birth: "); scanf("%d/%d/%d",&stu[k].birthday.year,&stu[k].birthday.month,&stu[k].birthday.day); 
  printf(" Age: ");  scanf("%d",&stu[k].age); getchar(); 
  printf(" Native place: ");  gets(stu[k].addr); 
  printf(" Don't: ");  gets(stu[k].dept); 
  printf(" Professional: ");  gets(stu[k].major); 
  printf(" Class: ");  gets(stu[k].classs); 
  printf(" Telephone: ");  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 1 a 2 Base files are only allowed to write data */ 
  { 
    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) /* will 1 Batch data as 1 A whole 1 Secondary writes to disk files */ 
      printf(" File write error !\n"); 
  fclose(fp); 
} 
 
void printf_back()/* "Browse or Return" after adding student information */ 
{ 
  int w,k=0; 
  void browse(); 
  void menu(); 
  printf("\n\n\t^_^. Insert the success . ^_^\n\n"); 
  do 
  { 
   printf("What do you want to do next?\n\n\t1). Through all of the \t2). return : [ ]\b\b"); 
   scanf("%d",&w); 
   if(w==1) 
   { 
     system("cls"); 
     browse(); 
   } 
   else if(w==2) 
   { 
     system("cls"); 
     menu(); 
   } 
   else 
     printf("  The wrong choice ! Please reselect !\n"); 
  }while(k==0); 
} 
 
void insert() /* Add student information */ 
{ 
  int i,n,k,t; 
  FILE *fp; 
  if((fp=fopen("D:\\file.txt","rb"))==NULL) /* Read in an empty file, rb Said to open 1 a 2 Base file. Only read data is allowed */ 
  { 
    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 first  %d  Student status information for each student .\n",i-n+1); 
    input(i); 
  } 
  save(k); /* Save all student information */ 
  printf_back(); 
} 
 
void deleter() /* delete */ 
{ 
  void menu(); 
  if(!flag) 
  { 
    printf("\n\n\t Please login first !\n"); 
    printf("\n\t Please click  Enter  Key to enter the login interface !"); 
    getchar(); 
    getchar(); 
    login(); 
  } 
  else 
  { 
    struct student s; 
    int i,n,k,w0,w1; 
    n=load(); 
    do 
    { 
      k=-1; 
      //printf_n(n); /* Display all student information before deleting */ 
      do 
      { 
         printf("\n\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\n There is no information about this student !\n"); 
           printf("What do you want to do next? 1. Continue to input  2. Back to the main menu   [ ]\b\b"); 
           scanf("%d",&m); 
           if(m==1) 
             deleter(); 
           else 
           { 
             k=0; 
             menu(); 
           } 
         } 
      }while(k==-1); 
      printf_one(k); 
      printf("\n\t Sure to delete ?\n\t1). is   2). return  [ ]\b\b");/* Ask again for deletion */ 
      scanf("%d",&w0); 
      if(w0==1) 
      { 
        stu[k].name[0]='\0'; 
        save(n); 
      } 
      else 
        break; 
      printf("\n\t^_^. Deleted successfully ^_^.\n"); 
      printf("What do you want to do?\n\t1). Continue to   2). return  [ ]\b\b"); 
      scanf("%d",&w1); 
    }while(w1==1); 
   system("cls"); 
   menu(); /* Return to the main menu when no longer deleted, w1!=1*/ 
  } 
} 
 
void search() /* To find the */ 
{ 
  void menu(); 
  int w0,k,i,n,w1,w2; 
  struct student s; 
  FILE *fp; 
  if((fp=fopen("D:\\file.txt","rb"))!=NULL) /* Not an empty file */ 
    n=load(); 
  do 
  { 
    printf(" Select find category : 1. The name  2. Student id   [ ]\b\b"); 
    scanf("%d",&w1); 
    if(w1<1||w1>2) 
    { 
      printf(" Input error ! Please re-enter !\n"); 
      w2=1; 
    } 
    else 
      w2=0; /* Note here: if you don't add this sentence, you will get stuck in a loop and won't 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 are looking for !\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) /* Could not find */ 
         { 
           int o; 
           printf("\n\n There is no information about this student !\n!"); 
           printf("What do you want to do?\n\t1. Continue to   2. Return to the menu  [ ]\b\b"); 
           scanf("%d",&o); 
           if(o==1) 
           { 
             system("cls"); 
             search(); /* Continue to find */ 
           } 
           else 
           { 
             system("cls"); 
             menu(); 
           } 
         } 
      }while(k==-1); 
      system("cls"); 
      printf_one(k); /* Find and export the student's information later */ 
      printf("\nWhat do you want to do next?\n\t1). Continue to   2). Return to the menu   [ ]\b\b"); 
      scanf("%d",&w0); 
    }while(w0==1); 
   system("cls"); 
   menu(); /* Returns the main menu when no longer looking */ 
  } 
  else /* Search by student number */ 
  { 
    do 
    { 
      k=-1; 
      do 
      { 
        printf("\n Please enter the student number you are looking for !\nNum:"); 
        scanf("%s",s.num); 
        for(i=0;i<n;i++) 
         if(strcmp(s.num,stu[i].num)==0) /* To find the */ 
         { 
          k=i; 
          s=stu[i]; 
         } 
        if(k==-1) /* Could not find */ 
        { 
         int o; 
         printf("\n\n There is no information about this student !\n!"); 
         printf("What do you want to do?\n\t1. Continue to   2. Return to the menu  [ ]\b\b"); 
         scanf("%d",&o); 
         if(o==1) 
         { 
           system("cls"); 
           search(); /* Continue to find */ 
         } 
         else 
         { 
           system("cls"); 
           menu(); 
         } 
        } 
      }while(k==-1); 
      system("cls"); 
      printf_one(k); /* Output the student's status information after finding */ 
      printf("\nWhat do you want to do?\n\t1). Continue to   2). Return to the menu   [ ]\b\b"); 
      scanf("%d",&w0); 
    }while(w0==1); 
   system("cls"); 
   menu();/* Returns the main menu when no longer looking */ 
  } 
} 
 
int modify_data(int i) /* Modify information function */ 
{ 
  int c,w1; 
  void menu(); 
  do 
  { 
    puts("\n Select the option you want to modify: \n\n1. Student id  2. The name  3. gender  4. Date of birth  5. age  6. Native place  7. Is don't  8. professional  9. The class  10. The phone   11. All the information  12. Cancel and return "); 
    printf(" Please select a ?: [ ]\b\b\b"); 
    scanf("%d",&c); 
    if(c>12||c<1) 
    { 
      puts("\n The wrong choice ! Please reselect !"); 
      getchar(); 
    } 
  }while(c>12||c<1); 
  do 
  { 
    switch(c) 
    { 
     case 1: printf(" Student id :"); scanf("%s",stu[i].num); break; 
     case 2: printf(" The 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/%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(" Native place :"); scanf("%s",stu[i].addr);break; 
     case 7: printf(" Is don't :"); scanf("%s",stu[i].dept);break; 
     case 8: printf(" professional :"); scanf("%s",stu[i].major);break; 
     case 9: printf(" The class :"); scanf("%s",stu[i].classs);break; 
     case 10: printf(" The phone :"); scanf("%s",stu[i].phone);break; 
     case 11: input(i); break; /* Edit all the information about the student */ 
     case 12: menu(); /* Back to the main menu */ 
    } 
    puts("\nNow:\n"); 
    printf_one(i); /* The output   The modified   Information about this student */ 
    printf("\n determine ?\n\n\t1). is  2). No, rework it  3). Return do not save  [ ]\b\b"); 
    scanf("%d",&w1); 
  }while(w1==2); 
  return(w1); /* Modify successfully and select 1 After the return 1*/ 
} 
 
void modify() /* Modify the */ 
{ 
  void menu(); 
  if(!flag) 
  { 
    printf("\n\n\t Please login first !\n"); 
    printf("\n\t Please click  Enter  Key to enter the login interface !"); 
    getchar(); 
    getchar(); 
    login(); 
  } 
  else 
  { 
    struct student s; 
    int i,n,k,w0=1,w1,w2=0; 
    n=load(); 
    do 
    { 
     k=-1; 
     //printf_n(n); /* Display all student information before deleting */ 
     do 
     { 
       printf("\n Please enter the name of the student you wish to modify !\nName:"); 
       scanf("%s",s.name); 
       for(i=0;i<n;i++) 
        if(strcmp(s.name,stu[i].name)==0) /* With the 1 Student matching */ 
        { 
          k=i; 
          s=stu[i]; 
        } 
       if(k==-1) /* Student whose input was not found */ 
       { 
         int o; 
         printf("\n\n There is no information about this student !\n"); 
         printf("What do you want to do?\n\t1. Continue to   2. Return to the menu  [ ]\b\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 if the modification was successful */ 
     { 
       printf("\n\t^_^. Modify the success  ^_^.\n\n Whether or not to continue ?\n\n\t1). is  2). Save the return \t[ ]\b\b"); 
       scanf("%d",&w0); 
       w2=1; 
     } 
     else 
     { 
       w0=0; 
       if(w2==0) 
         stu[k]=s; 
     } 
     if((w0!=1)&&(w2==1)) /* Modified saved successfully */ 
       save(n); 
    }while(w0==1); 
    system("cls"); 
   menu();/* Return to main menu no longer modified */ 
  } 
} 
 
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 to main function */ 
} 
 
void exit() /* exit */ 
{ 
  exit(0); 
} 
 
void face() /* To access the page */ 
{ 
  system("color 4e"); 
  printf("\n\t   u   u   u   u   u   u   u   u   u   u   u   u   u   u   u   u   u   u \n\n"); 
  puts("\t   Does does does does does does does does does does does does does does does does does does does does does does does does does does does "); 
  puts("\t   Does does                         Does does "); 
  puts("\t   Does does                         Does does "); 
  puts("\t   Does does        Welcome to the Student status management system !      Does does "); 
  puts("\t   Does does                         Does does "); 
  puts("\t   Does does                         Does does "); 
  puts("\t   Does does                         Does does "); 
  puts("\t   Does does                         Does does "); 
  puts("\t   Does does                 Producer: Li Yuanhang   Does does "); 
  puts("\t   Does does                   2013.6.20  Does does "); 
  puts("\t   Does does                         Does does "); 
  puts("\t   Does does does does does does does does does does does does does does does does does does does does does does does does does does does "); 
  printf("\n\tPress [ Enter ] key to continue ... \n"); 
  getchar(); 
  system("cls"); 
} 
 
void menu() /* The main menu */ 
{ 
  system("color 2e"); 
  printf("\n\t\t\t Welcome to use the Student status management system !\n"); 
  printf("\n"); 
  int n,w1; 
  do 
  { 
    puts("\t\t U u u u u u u u    The menu   U u u u u u u u "); 
    puts("\t\t u         1. deng   record          u "); 
    puts("\t\t u         2. insert   Into the          u "); 
    puts("\t\t u         3. delete   In addition to          u "); 
    puts("\t\t u         4. check   Looking for a          u "); 
    puts("\t\t u         5. repair   change          u "); 
    puts("\t\t u         6. clear   Browse the          u "); 
    puts("\t\t u         7. refund   Out of the          u "); 
    puts("\t\t U u u u u u u u u u u u u u u u u u u u u "); 
    printf(" Please select the type of service (1-7) : [ ]\b\b"); 
    scanf("%d",&n); 
    if(n<1||n>7) 
    { 
      system("cls"); 
      printf(" The wrong choice !  Please reselect !\n"); 
      w1=1; 
    } 
    else 
      w1=0; 
  }while(w1==1); 
  switch(n) 
  { 
    case 1: login(); break; /* The login */ 
    case 2: system("cls"); insert();  break; /* insert */ 
    case 3: system("cls"); deleter(); break; /* delete */ 
    case 4: system("cls"); search(); break; /* To find the */ 
    case 5: system("cls"); modify(); break; /* Modify the */ 
    case 6: system("cls"); browse(); break; /* browse */ 
    case 7: exit(); break; /* exit */ 
  } 
} 
 
int main() 
{ 
  face(); 
  menu(); 
  return 0; 
} 


Related articles: