C language implementation of student information management system (single linked list)

  • 2020-06-01 10:33:40
  • OfStack

The example of this paper is to share the specific code of C language to realize the student information management system for your reference. The specific content is as follows


/*copyright(c)2016. School of computer science, yantai university  
 * All rights reserved, 
 *  File name: text.Cpp 
 *  Author: wu jingchao  
 *  Completion date: 2016 years 7 month 1 day  
 *  The version number: codeblock 
 * 
 *  Problem description :  Student information management system  
 *  The input describing : 
 *  Program output:   The output  
 */ 
 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <windows.h> 
#include <time.h> 
# define LEN sizeof(struct Student) 
struct Student { 
char num[100];   // Student id  
char name[200];  // The name  
char sex[100];  // gender  
int age;   // age  
char phone[120];  // The phone  
char address[120]; // address  
char birthday[200]; // birthday  
char mail[200] ; // mail  
struct Student *next; 
}; 
void menu(); 
void start(); 
void welcome(); 
char filename[30];// Global variable to hold the name of the file you want to open  
struct Student *Creat(int n); 
// Generate the list  
struct Student *Creat(int n) 
{ 
 void menu_print_in(void); 
 struct Student *head; 
 struct Student *p1, *p2; 
 
 system("cls"); 
 for(int i=1;i<n+1;i++) { 
 p1 = (struct Student*)malloc(LEN); 
 menu_print_in(); 
 scanf("%s%s%s%d%s%s%s%s",p1->num,p1->name,p1->sex, 
 &p1->age,p1->phone,p1->address,p1->birthday,p1->mail); 
 p1->next = NULL; 
 if(i==1) 
 { 
  head = p2 = p1; 
 } 
 else { 
  p2->next = p1; 
  p2 = p1; 
 } 
 } 
 return(head); 
} 
 
// Inventory data (wb Just write ) 
void WriteData_wb(struct Student *head) { 
 FILE *fp; 
 struct Student *p; 
 if((fp = fopen(filename, "wb"))==NULL) 
 printf("\a  File opening error "); 
 p = head; 
 while(p!=NULL) { 
 if(fwrite(p,LEN,1,fp)!=1) { 
  printf(" Error writing data \n"); 
  fclose(fp); 
  return; 
 } 
 p=p->next; 
 } 
 fclose(fp); 
} 
 
// Inventory data (ab additional ) 
void WriteData_ab(struct Student *head) 
{ 
 FILE *fp; 
 struct Student *p; 
 if((fp = fopen(filename, "ab"))==NULL) 
 printf("\a  File opening error !"); 
 p = head; 
 while(p!=NULL) { 
 if(fwrite(p,LEN,1,fp)!=1) { 
  printf(" Error writing data \n"); 
  fclose(fp); 
  return; 
 } 
 p=p->next; 
 } 
 fclose(fp); 
} 
 
 
// The read data file is saved to the linked list   , returns a pointer to the header of the chain  
struct Student *ReadData(void) 
{ 
 struct Student *head = NULL; 
 struct Student *p1, *p2;//s = p1;p = p2; 
 
 FILE *fp; 
 if((fp=fopen(filename,"rb+"))==NULL) 
 { 
 printf(" Error opening file \n"); 
 exit(0); 
 } 
 while(!feof(fp)) { 
 if((p1=(struct Student*)malloc(LEN))==NULL){ 
  printf(" Memory request error \n"); 
  fclose(fp); 
  exit(0); 
 } 
 if(fread(p1,LEN,1,fp)!=1){ 
  free(p1); 
  break; 
 } 
 if(head==NULL) 
 head=p2=p1; 
 else{ 
  p2->next=p1; 
  p2=p1; 
 } 
 } 
 fclose(fp); 
 return (head); 
} 
 
// All of the query  
void display() 
{ 
 system("cls"); 
 void menu_print_out(void); 
 struct Student *p; 
 p = ReadData(); 
 menu_print_out(); 
 do 
 { 
 printf("%-10s%6s%8s%4d%13s%11s %4s %4s",p->num,p->name,p->sex, 
 p->age,p->phone,p->address,p->birthday,p->mail); 
 p = p->next; 
 printf("\n\n"); 
 }while(p!=NULL); 
 printf("\n\n"); 
} 
 
// Student number query  
int query_num() 
{ 
 system("cls"); 
 void menu_print_out(); 
 struct Student *p; 
 char str_num[10]; 
 printf(" Please enter the student number you want to query :"); 
 scanf("%s", str_num); 
 p = ReadData(); 
 menu_print_out(); 
 do { 
 if(strcmp(p->num,str_num)==0) { 
  printf("%-10s%6s%8s%4d%13s%11s %4s %4s",p->num,p->name,p->sex, 
 p->age,p->phone,p->address,p->birthday,p->mail); 
  printf("\n\n"); 
  return 0; 
 } 
 p = p->next; 
 }while(p!=NULL); 
 printf(" The database does not store the data you want to query! \n"); 
 printf("\n\n"); 
 return 0; 
} 
 
// Name query  
int query_name() 
{ 
 system("cls"); 
 void menu_print_out(void); 
 struct Student *p; 
 char str_name[20]; 
 printf(" Please enter the name you want to query :"); 
 scanf("%s", str_name); 
 p = ReadData(); 
 menu_print_out(); 
 do { 
 if(strcmp(p->name,str_name)==0) 
 { 
  printf("%-8s%6s%8s%4d%13s%11s %4s %4s",p->num,p->name,p->sex, 
 p->age,p->phone,p->address,p->birthday,p->mail); 
  printf("\n\n"); 
  return 0; 
 } 
 p = p->next; 
 }while(p!=NULL); 
 printf(" The database does not store the data you want to query! \n"); 
 printf("\n\n"); 
 return 0; 
} 
 
// 【 1 Modify data deletion record  
int delStudent() 
{ 
 struct Student *p1, *p2, *head; 
 char str_num[20]; 
 printf("\n Please enter the student number you want to delete :"); 
 scanf("%s", str_num); 
 p1 = ReadData(); 
 p2 = p1->next; 
 head = p1; 
 while(p2!=NULL) 
 { 
 if(strcmp(p1->num,str_num)==0) { 
  WriteData_wb(p2); 
 } 
 else if(strcmp(p2->num,str_num)==0) { 
  p1->next = p2->next; 
  WriteData_wb(head); 
 } 
 p2 = p2->next; 
 p1 = p1->next; 
 } 
 if(p2!=NULL) 
 printf(" The data you want to delete is not stored in the database! \n"); 
 printf("\n\n"); 
 return 0; 
} 
 
// 【 2 Modification record of modified data  
int change1() 
{ 
 void menu_print_in(void); 
 struct Student *p1, *p2, *head; 
 char str_num[20]; 
 printf(" Please enter the student number you want to modify :"); 
 scanf("%s", str_num); 
 p1 = ReadData(); 
 p2 = p1->next; 
 head = p1; 
 while(p2!=NULL) 
 { 
 if(strcmp(p1->num,str_num)==0) { 
  menu_print_in(); 
  scanf("%s%s%s%d%s%s%s%s",p1->num,p1->name,p1->sex, 
 &p1->age,p1->phone,p1->address,p1->birthday,p1->mail); 
  WriteData_wb(head); 
 } 
 else if(strcmp(p2->num,str_num)==0) 
 { 
  menu_print_in(); 
  scanf("%s%s%s%d%s%s%s%s",p2->num,p2->name,p2->sex, 
 &p2->age,p2->phone,p2->address,p2->birthday,p2->mail); 
  WriteData_wb(head); 
 } 
 p2 = p2->next; 
 p1 = p1->next; 
 } 
 if(p2!=NULL) 
 printf(" The data you want to delete is not stored in the database! \n"); 
 return 0; 
} 
void start() // The welcome screen  
{ 
 system("color 1E"); 
 printf("\n\n\n\n\n\n\n\n\n\t\t\t It even though it's you you you you you you you you you you you you you you you \n"); 
 printf("\t\t\t Even though      Even though \n"); 
 printf("\t\t\t Even though   Welcome to the student information management system   Even though \n"); 
 printf("\t\t\t Even though      Even though \n"); 
 printf("\t\t\t It even though it's you you you you you you you you you you you you you you you \n"); 
 printf("\n\t\t\t\t System start up .........\n"); 
 printf("===============================================================================\r"); 
 for(int i=1;i<80;i++) 
 { 
  Sleep(40); 
  printf(">"); 
 } 
 system("CLS"); 
 welcome(); 
} 
void welcome()// Login screen  
{ 
 system("CLS"); 
 system("color 2"); 
 printf("\n\n\n\n\n\n\t\t\t U u u u u u u u u u u u u u u u \n"); 
 printf("\t\t\t u      u \n"); 
 printf("\t\t\t u   Welcome to the student information management system   u \n"); 
 printf("\t\t\t u      u \n"); 
 printf("\t\t\t u   Designer: wu jingchao   u \n"); 
 printf("\t\t\t u      u \n"); 
 printf("\t\t\t u   Date: 2016.7.1  u \n"); 
 printf("\t\t\t u      u \n"); 
 printf("\t\t\t U u u u u u u u u u u u u u u u \n"); 
 
 printf("\n\t\t\t Press any key to continue ..."); 
 getchar(); 
 system("CLS"); 
 menu(); 
} 
// The main menu  
void menu() 
{ 
 void addStudent(); 
 void query(); 
 int File_name(); 
 void change(); 
 system("CLS"); 
 system("color D"); 
 printf("\t\t\t Main menu * * * * * * * * * * * * * * * * * * \n"); 
 printf("\n\n\n\n\n\t\t\t Does does does does does does does does does does does does does does \n"); 
 printf("\t\t\t does     does \n"); 
 printf("\t\t\t does  1. Input student information   does \n"); 
 printf("\t\t\t does ------------------------ does \n"); 
 printf("\t\t\t does  2. Search for student information   does \n"); 
 printf("\t\t\t does ------------------------ does \n"); 
 printf("\t\t\t does  3. Modify student information   does \n"); 
 printf("\t\t\t does ------------------------ does \n"); 
 printf("\t\t\t does  4.  Log out   does \n"); 
 printf("\t\t\t does ------------------------ does \n"); 
 printf("\t\t\t Does does does does does does does does does does does does does does \n"); 
 printf("\n\t\t Please enter the sequence number before the function to select the service item :"); 
 
 int a = 0; 
 a = getchar(); 
 
 while(a!='1'&&a!='2'&&a!='3'&&a!='4') { 
 printf(" Error, please enter the correct number !\n"); 
 putchar('\a'); 
 getchar(); 
 printf("\n\t\t Please enter the sequence number before the function to select the service item :"); 
 a = getchar(); 
 } 
 switch(a) { 
 case '1': File_name();addStudent(); 
 break; 
 case '2': File_name();query(); 
 break; 
 case '3': File_name();change(); 
 break; 
 case '4': exit(0); 
 break; 
 } 
 getchar(); 
} 
 
int File_name() 
{ 
 printf("\n\t\t Please enter the file you want to open :"); 
 if(scanf("%s", filename)!=1) 
 printf("\a error!"); 
 return 0; 
} 
//2 Add data to level menu  
void addStudent() 
{ 
 int Creat_num(); 
 system("cls"); 
 getchar(); 
 system("CLS"); 
 system("color B"); 
 printf("\t\t\t * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n"); 
 printf("\n\n\n\n\n\t\t\t Does does does does does does does does does does does does does \n"); 
 printf("\t\t\t does     does \n"); 
 printf("\t\t\t does  1.  The new file   does \n"); 
 printf("\t\t\t does ---------------------- does \n"); 
 printf("\t\t\t does  2.  Add data   does \n"); 
 printf("\t\t\t does ---------------------- does \n"); 
 printf("\t\t\t does  3.  Return to the menu   does \n"); 
 printf("\t\t\t does ---------------------- does \n"); 
 printf("\t\t\t Does does does does does does does does does does does does does \n"); 
 printf("\n\t\t Please enter the sequence number before the function to select the service item :"); 
 
 int a = 0; 
 a = getchar(); 
 
 while(a!='1'&&a!='2'&&a!='3') 
 { 
 printf(" Error, please enter the correct number !\n"); 
 putchar('\a'); 
 getchar(); 
 printf("\n\t\t Please enter the sequence number before the function to select the service item :"); 
 a = getchar(); 
 } 
 switch(a) { 
 case '1': WriteData_wb(Creat(Creat_num())); 
  printf("\n The new file was created successfully and the data was saved successfully \n"); 
  system("pause"); 
  system("cls"); 
  addStudent(); 
 break; 
 case '2': WriteData_ab(Creat(Creat_num())); 
  printf("\n The data was added successfully \n"); 
  system("pause"); 
  system("cls"); 
  addStudent(); 
 break; 
 case '3': system("cls"); 
  getchar(); 
  menu(); 
 break; 
 } 
} 
 
//2 Level menu query data  
void query() 
{ 
 system("cls"); 
 getchar(); 
 while(1) 
 { 
 system("CLS"); 
 system("color C"); 
 printf("\t\t\t * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n"); 
 printf("\n\n\n\n\n\t\t\t Does does does does does does does does does does does does does does \n"); 
 printf("\t\t\t does     does \n"); 
 printf("\t\t\t does  1. All of the query   does \n"); 
 printf("\t\t\t does ------------------------ does \n"); 
 printf("\t\t\t does  2. Student number query   does \n"); 
 printf("\t\t\t does ------------------------ does \n"); 
 printf("\t\t\t does  3. Name query   does \n"); 
 printf("\t\t\t does ------------------------ does \n"); 
 printf("\t\t\t does  4. Return to the menu   does \n"); 
 printf("\t\t\t does ------------------------ does \n"); 
 printf("\t\t\t Does does does does does does does does does does does does does does \n"); 
 printf("\n\t\t Please enter the sequence number before the function to select the service item :"); 
 int a = 0; 
 a = getchar(); 
 
 while(a!='1'&&a!='2'&&a!='3'&&a!='4') 
 { 
  printf(" Error, please enter the correct number !\n"); 
  putchar('\a'); 
  getchar(); 
  printf("\n\t\t Please enter the sequence number before the function to select the service item :"); 
  a = getchar(); 
 } 
 switch(a) { 
  case '1': display();system("pause");getchar(); 
  break; 
  case '2': query_num();system("pause");getchar(); 
  break; 
  case '3': query_name();system("pause");getchar(); 
  break; 
  case '4': system("cls");getchar();menu(); 
  break; 
 } 
 } 
} 
int Creat_num() { 
 printf("\n\t\t Please enter the number of data you want to add :"); 
 int n; 
 if(scanf("%d", &n)!=1) { 
 printf("\a error!"); 
 } 
 return n; 
} 
//2 Level menu modification data / 
void change() 
{ 
 system("cls"); 
 getchar(); 
 while(1) 
 { 
 system("CLS"); 
 system("color A"); 
 printf("\t\t\t * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n"); 
 printf("\n\n\n\n\n\t\t\t Does does does does does does does does does does does does does does \n"); 
 printf("\t\t\t does     does \n"); 
 printf("\t\t\t does  1. Delete records   does \n"); 
 printf("\t\t\t does ------------------------ does \n"); 
 printf("\t\t\t does  2. Modify the record   does \n"); 
 printf("\t\t\t does ------------------------ does \n"); 
 printf("\t\t\t does  3. Return to the menu   does \n"); 
 printf("\t\t\t does ------------------------ does \n"); 
 printf("\t\t\t Does does does does does does does does does does does does does does \n"); 
 printf("\n\t\t Please enter the sequence number before the function to select the service item :"); 
 
 int a = 0; 
 a = getchar(); 
 
 while(a!='1'&&a!='2'&&a!='3') 
 { 
 printf(" Error, please enter the correct number !\n"); 
 putchar('\a'); 
 getchar(); 
 printf("\n\t\t Please enter the sequence number before the function to select the service item :"); 
 a = getchar(); 
 } 
 switch(a) { 
  case '1': delStudent(); 
  printf("\n\n The specified data was successfully deleted \n"); 
  system("pause"); 
  getchar(); 
  break; 
  case '2': change1(); 
   printf("\n\n The specified data was successfully modified \n"); 
  system("pause"); 
   getchar(); 
  break; 
  case '3': system("cls"); 
  getchar(); 
  menu(); 
  break; 
 } 
 } 
} 
 
// I/o prompt bar  
void menu_print_in() { 
 printf("------------------------------------------------------------------------\n"); 
 printf(" Student id   The name   gender   age   The phone    address   birthday   mail  \n"); 
 printf("------------------------------------------------------------------------\n"); 
} 
void menu_print_out(void) { 
 printf("--------------------------------------------------------------------------\n"); 
 printf(" Student id   The name   gender   age   The phone    address   birthday   mail  \n"); 
 printf("--------------------------------------------------------------------------\n"); 
} 
 
// The main function  
int main() 
{ 
 start(); 
 return 0; 
} 

For more information, please refer to the topic "management system development".


Related articles: