Actual combat of C language project based on Linux--local account management system

C language development project combat:

C language is a general-purpose computer programming language, which is widely used in low-level development. The design goal of the C language is to provide a programming language that can be compiled in an easy way, handle low-level memory, generate a small amount of machine code, and can run without any operating environment support. Although the C language provides many low-level processing functions, it still maintains a good cross-platform feature. A C language program written in a standard specification can be compiled and run normally on many platforms.

C language is a process-oriented language. Its inherent characteristics determine that it is not suitable for writing a beautiful GUI interface. In this place, the author chooses to use C language to realize the design of user login just to let everyone feel the C language project. The actual development process. Therefore, the follow-up description will not be as official and accurate as the actual project development document description in the company.

one. Project requirements:

1. User Console;

2. Functional requirements, able to register, store and compare user account passwords; 

two. Project Design:

According to the requirements we can know:

1. The interface layout should have account and password input, which means that we need user and password identification in the code. We can use the printf function provided by the standard C library to print on the terminal; at the same time, the interface that requires user and password input, this We can use the scanf or gets function provided by the standard C library to realize it; similarly, the login, registration, and exit options can also be realized directly by using the above functions, and the specific layout can be determined by the individual.

2. According to the functional requirements, it is better to use the existing mature database to realize the comparison of user data, but here we only talk about the practical application of C language, so we can use some existing data types of C language to achieve such a function. Of course, this will also limit the performance of our program. We can find that the relationship between the user and the password should be one-to-one, so we can use the form of a structure to realize the "binding" of the user and its corresponding password. And we should not have only one user, so we can use the form of a structure array to store user data. (This kind of design will have an unavoidable problem. When the program ends, the memory is released, and all the data inside the structure array disappears. If you also learn file IO, I think you can already solve this problem.)

3. When the user logs in, data comparison At present, we can realize the login function by traversing and comparing. In the future, when the scale of the problem is large, the database should still be used to manage relevant data and achieve optimization.

three. Item code:

1. We can use a more professional approach to divide the file into multiple source files. This is relatively basic and convenient for subsequent display. The author decided to adopt the most "LOW" method, simply and rudely put everything in one file middle;

2. Specific code implementation:

#include 

#include 

#include 

#include 

typedef struct data{

    char name[20]; //store username

    char pass[20]; //store password

}user_data;

user_data user[20] = {0}; //A structure array capable of saving 20 users

int last = 0; //flag bit, marking the number of registered accounts

bool reg_string(void)

{

    int i = 0, n;

    if(last >= 20){

        return false;

    }

    char name[20] = {0}, pass[20] = {0};

printf("please input your usrname:\n");

    xxx:    

    scanf("%s", name);

getchar(); // read dirty characters

    for(i = 0; i < last; i++){

        n = strcmp(name, user[i].name);

        if(0 == n){

            printf("The user has registered\n");

            goto xxx; //If the user name is the same, jump to re-enter

        }

}

    printf("please input your usrpassword:\n");

    scanf("%s", pass);

    strcpy(user[last].name, name);

    strcpy(user[last].pass, pass);

    last++;

    printf("Successful registration\n");

    return true;

}

bool log_string(void)

{

    if(0 == last){

        return false;

    }

    int i, n, m;

    char name[20] = {0}, pass[20] = {0};

    printf("please input your usrname:\n");

    scanf("%s", name);

    getchar();

    printf("please input your usrpassword:\n");

    scanf("%s", pass);

    getchar();

for(i = 0; i < last; i++){

//strcmp function return value "0" means the two strings are the same

        n = strcmp(user[i].name, name);         

m = strcmp(user[i].pass, pass);

        if(n == 0 && m == 0){

//In the future, other interfaces can be provided to realize various custom functions

            printf("Successful login!\n"); 

            return true;

        }

}

//When the program executes to this step, it means that the structure array has been traversed and no matching user and //password have been found

    printf("User or password error\n"); 

    return false;

}

int main()

{

    int n;

while(1){

//Implement interface layout

        printf("***************************************\n");

printf("*****1. Register*****2. Login*****3. Exit*****\n"); printf("********** *******************************\n");

xxx:   

        scanf("%d", &n);

        getchar();

        switch(n){

            case 1:

// You should make a good judgment on the return value when calling to see if the corresponding function is called correctly

                reg_string(); 

                break; 

            case 2:

                log_string(); //login

                break;

            case 3:

                return 0;

            default:

                goto xxx;

        }

    }

}

Note:

This time is mainly to introduce the actual development process of C language projects, so the selection of projects is relatively simple, and the code is not optimal. If you want an in-depth and professional understanding, please contact us.

Embedded Internet of Things needs to learn a lot. Don't learn the wrong route and content, which will cause your salary to go up!

Share a data package with everyone, about 150 G. The learning content, face-to-face scriptures, and projects in it are relatively new and complete! (Click to find a small assistant to receive)

Guess you like

Origin blog.csdn.net/m0_70911440/article/details/131633011