1. Function introduction of student management system

The previous article introduced the structure type, know that the structure type can store different data types, belong to an ordered collection.

This article uses the structure knowledge to complete a small exercise, using the structure + array design of a simple student management system, as the consolidation of the structure knowledge exercises.

The functions are as follows:

(1). Welcome the prompt

(2). Enter the password to log in

(3). Functions: input student information, sort according to student number, sort according to grades, output all student information, output the specified student information (student number, name, grade), calculate the average value of grade output print, delete the specified student information, add new student information.

(4) Function modules are selected by menu

2. Frame design

#include <stdio.h>
#include <stdlib.h>void  Display_info(void);
​
char buff_user[50];
char buff_password[50];
int main(a)
{
    int a,i;
    printf("------ Welcome to XXXX student Management system ------\n");
​
    for(i=1; i<=3; i++) {printf("Please enter user name :");
        scanf("%s",buff_user);
        printf("Please enter your password :");
        system("stty -echo"); // System functions -- call system commands to turn off the echo
        scanf("%s",buff_password);
        printf("\n");
        system("stty echo"); // System function -- call system command open echo
        // Check whether the password is correct
        if(strcmp(buff_user,"wbyq")! =0 || strcmp(buff_password,"123456")! =0)
        {
            printf("Password or user error. Please re-enter, remaining times :%d\n".3-i);
        }
        else break;
    }
    
    // Check whether the password is correct
    if(strcmp(buff_user,"wbyq")! =0 || strcmp(buff_password,"123456")! =0)
    {
        printf("System safe exit.\n");
        return 0;  //exit(); // Terminate the process
    }
​
/* #include 
      
        void exit(int status); For example: the exit (0); * /
      
    while(1)
    {
        Display_info(a);scanf("%d",&a);
        switch (a)
        {
        case 1:
            printf("Select admission information \n");
            break;
        case 2:
            printf("Output student information \n");
            break;
         / /...
        default:
            break; }}return 0;
}
​
// Displays a prompt message
void  Display_info(void)
{
    printf("------ Welcome to XXXX student Management system ------\n");
    printf("1. Admission information \n");
    printf("2. Output student information \n");
    printf("3. Print average score \n");
    printf("4. Add student information \n");
    printf("5. Output student information sorted by grade.\n");
    printf("6. Delete specified student \n");
    printf("Please select function :\n");
}
Copy the code

3. Complete code

#include <stdio.h>
#include <stdlib.h>struct STU
{
    char name[100]; / / name
    int age; / / age
    char number[100]; / / student id
    float grade;/ / result
};
​
// The global variable defines the region
char buff_user[50];
char buff_password[50];
struct STU stu_info[100].
int stu_cnt=0;  // Total number of students// Function declaration area
void  Display_info(void);
int SystemLogin(void);
void add_info(struct STU *stu,int flag);
void grade_sort(struct STU *stu);
void del_info(struct STU *stu);
void print_info(struct STU *stu);
​
int main(a)
{
    int number;
    Log in to the system
    if(SystemLogin())
    {
        printf("System safe exit.\n");
        exit(0);
    }
​
    //2. Execute the function module
    while(1)
    {
        Display_info(a);scanf("%d",&number);
        switch (number)
        {
        case 1: // Input student information
            add_info(stu_info,1);
            break;
        case 2: // Output student information
            print_info(stu_info);
            break;
        case 3: // Print the average score
            break;
        case 4: // Add student information
            add_info(stu_info,0);
            break;
        case 5: // Outputs student information sorted by grade
            grade_sort(stu_info);
            break;
        case 6: // Delete the specified student information
            del_info(stu_info);
            break;
        case 7: // Exit the system
            printf("----- System exit safely ------\n");
            exit(0); Exit the current process
        default:
            break; }}return 0;
}
​
// Displays a prompt message
void  Display_info(void)
{
    printf("\n\n");
    printf("------ Welcome to XXXX student Management system ------\n");
    printf("1. Input student information (input information as required)\n");
    printf("2. Output student information \n");
    printf("3. Print average score \n");
    printf("4. Add student information \n");
    printf("5. Output student information sorted by grade.\n");
    printf("6. Delete specified student \n");
    printf("7. Log out \n");
    printf("Please select function :");
}
​
Return value: 0: successful 1: failed */
int SystemLogin(void)
{
    printf("------ Welcome to XXXX student Management system ------\n");
    int i;
    for(i=1; i<=3; i++) {printf("Please enter user name :");
        scanf("%s",buff_user);
        printf("Please enter your password :");
        system("stty -echo"); // System functions -- call system commands to turn off the echo
        scanf("%s",buff_password);
        printf("\n");
        system("stty echo"); // System function -- call system command open echo
        // Check whether the password is correct
        if(strcmp(buff_user,"wbyq")! =0 || strcmp(buff_password,"123456")! =0)
        {
            printf("Password or user error. Please re-enter, remaining times :%d\n".3-i);
        }
        else break;
    }
    // Check whether the password is correct
    if(strcmp(buff_user,"wbyq")! =0 || strcmp(buff_password,"123456")! =0)
    {
        return 1; 
    }
    return 0;
}
​
Int flag :1 indicates the continuous increase of student information 0 indicates the increase of a single student information */
void add_info(struct STU *stu,int flag)
{
    int i;
    int tmp;
    int addr=stu_cnt; // Total number of students
    if(flag)
    {
        for(i=addr; i<sizeof(stu_info)/sizeof(stu_info[0]); i++) {printf("Please enter student name :");
            scanf("%s",stu[i].name);
            printf("Please enter student age :");
            scanf("%d",&stu[i].age);
            printf(Please enter your student number:);
            scanf("%s",stu[i].number);
            printf("Please enter the result :");
            scanf("%f",&stu[i].grade);
            
            stu_cnt++;  // Record the total number of people
            printf("Do you want to continue typing? Zero means continue and one means quit.");
            scanf("%d",&tmp);
            printf("\n\n");
            if(tmp)break; }}else
    {
        printf("Please enter student name :");
        scanf("%s",stu[addr].name);
        printf("Please enter student age :");
        scanf("%d",&stu[addr].age);
        printf(Please enter your student number:);
        scanf("%s",stu[addr].number);
        printf("Please enter the result :");
        scanf("%f",&stu[addr].grade);
        stu_cnt++;  // Record the total number of people}}// Output student information
void print_info(struct STU *stu)
{
    int i;
    printf("--------------------------------------------------\n");
    for(i=0; i<stu_cnt; i++) {printf("Student name :%s\n",stu[i].name);
        printf("Student Age :%d\n",stu[i].age);
        printf("Student id: % s \ n",stu[i].number);
        printf("Grade: % f \ n",stu[i].grade);
        printf("\n");
    }
    printf("--------------------------------------------------\n");
}
​
// Outputs student information sorted by grade
void grade_sort(struct STU *stu)
{
    int i,j;
    struct STU tmp;
    for(i=0; i<stu_cnt- 1; i++) {for(j=0; j<stu_cnt- 1-i; j++) {if(stu[j].grade<stu[j+1].grade)
            {
                tmp=stu[j];
                stu[j]=stu[j+1];
                stu[j+1]=tmp; }}}}/ / 1234567890
// Delete the specified student information
void del_info(struct STU *stu)
{
    char number[100];
    int i,j;
    printf(Please enter the student ID of the student you want to delete:);
    scanf("%s",number);
    printf("--------------------------------------------------\n");
    for(i=0; i<stu_cnt; i++) {if(strcmp(stu[i].number,number)==0)
        {
            printf("The following student information was successfully deleted :");
            printf("Student name :%s\n",stu[i].name);
            printf("Student Age :%d\n",stu[i].age);
            printf("Student id: % s \ n",stu[i].number);
            printf("Grade: % f \ n",stu[i].grade);
            for(j=i; j<stu_cnt- 1; j++) { stu[j]=stu[j+1];
            }
            stu_cnt--; // Subtract the total quantity
            break; }}if(i==stu_cnt+1)
    {
        printf("Student id not found.\n");
    }
    printf("--------------------------------------------------\n");
}
Copy the code