当前位置:网站首页>Su embedded training - C language programming practice (implementation of address book)

Su embedded training - C language programming practice (implementation of address book)

2022-07-08 00:58:00 Light chasing rain

Implement address book

requirement

1. use makefile Management document
2. Each sub function

a: Add users ( Continuous addition )
b: To view the user ( Sort by initials )
c: Search users ( Two ways :id, user name )
d: Delete user information
e: Modify user information

3. Encapsulate the string processing function by yourself

Specific completion

1. Makefile

TARGET ?= run
OBJS := main.o tongxun.o
CC := gcc
CFLAGS := -c -o
CFLAGSs := -o

$(TARGET):$(OBJS)
	$(CC) $(OBJS) $(CFLAGSs) $(TARGET)
main.o:main.c
	$(CC) main.c $(CFLAGS) main.o
func.o:func.c
	$(CC) tongxun.c $(CFLAGS) tongxun.o
clean:
	rm -rf $(OBJS) $(TARGET)

2. main.c

#include "tongxunlu.h"
Person *g_into[MAX] = {
    0}; // Structure pointer array 
int g_Count;               // The current number of people 
int main(int argc, char const *argv[])
{
    
    Welcome();   // The welcome screen 
    int choice;  // Customers choose 
    g_Count = 0; // Number of people cleared 
    while (1)
    {
    
        menu();                             //  Menu interface 
        printf("Please enter a number:\n"); // Enter the type of service the customer needs 
        scanf("%d", &choice);
        serve(choice); // Determine the service type , And process it 
    }
    // You can add and write files 
    return 0;
}

3. tongxunlu.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
// Ban string.h,

#define MAX 1024

typedef struct ContactPerson
{
    
    int id;
    char name[32];
    int age;
    char tel[12];
} Person;

extern int g_Count;
extern Person *g_into[MAX];

void Welcome();
void menu();
void serve(int choice);
void Exit();
void Add_inf();
void Dis_inf();
void Mod_inf();
void Del_inf();
int Find_inf();
int Find_ID();
int Find_NAME();
int Add_ID(int);
int Add_NAME(int);
int Add_AGE(int);
int Add_TEL(int);
int Keep_Add(int);
int My_strlen(const char *name);
int My_strncmp(char *, char *, size_t);
int My_strcmp(char *,char *);
char *My_strcpy(char *, char *);

3. tongxun.c

#include "tongxunlu.h"

void Welcome()
{
    
    system("clear");
    printf("--------------------------------------------------------\n");
    printf("\t\tWelcome to Contact Person\n");
    printf("--------------------------------------------------------\n");
}

void menu()
{
    
    printf("--------------------------------------------------------\n");
    printf(" 1->Add information 2->Displays information\n");
    printf(" 3->Modify information 4->Delete information\n");
    printf(" 5->Find information 6->Exit\n");
    printf("--------------------------------------------------------\n");
    printf("--------------------------------------------------------\n");
}

void serve(int choice)
{
    
    switch (choice)
    {
    
    case 1:
        Add_inf();
        break;
    case 2:
        Dis_inf();
        break;
    case 3:
        Mod_inf();
        break;
    case 4:
        Del_inf();
        break;
    case 5:
        Find_inf();
        break;
    case 6:
        Exit();
        break;
    default:
        break;
    }
}

void Exit() //6-> sign out 
{
    
    system("clear");
    exit(0);
}

void Add_inf() //1-> Add information 
{
    
    //printf("g_Count = %d,LINE = %d\n", g_Count,__LINE__);
    while (1)
    {
    
        system("clear");
        //printf("g_Count = %d,LINE = %d\n", g_Count,__LINE__);
        g_into[g_Count] = (Person *)malloc(sizeof(Person) * 1);
        if (NULL == g_into || NULL == &g_into[g_Count])
        {
    
            printf("Add_inf Malloc Error!");
            exit(1);
        }
        int flag = 1;
        while (flag)
        {
    
            printf("You will enter the following information\n\nid\nname\nage\ntelephone number\n");
            putchar(10);
            flag = Add_ID(flag);
            putchar(10);
            flag = Add_NAME(flag);
            putchar(10);
            flag = Add_AGE(flag);
            putchar(10);
            flag = Add_TEL(flag);
            putchar(10);
            printf("g_into[%d]->id = %d\n", g_Count, g_into[g_Count]->id);
            printf("g_into[%d]->name = %s\n", g_Count, g_into[g_Count]->name);
            printf("g_into[%d]->age = %d\n", g_Count, g_into[g_Count]->age);
            printf("g_into[%d]->tel = %s\n", g_Count, g_into[g_Count]->tel);
            putchar(10);
            flag = Keep_Add(flag);
            //printf("flag = %d\n",flag);
            if (flag)
            {
    
                g_Count++;
                break;
            }
            else
            {
    
                g_Count++;
                return;
            }
        }
    }
    //printf("g_Count = %d,LINE = %d\n", g_Count,__LINE__);
}

int Add_ID(int flag) // Input ID
{
    
    flag = 1;
    while (flag)
    {
    
        long int try = 0;
        printf("Please enter id:\n");
        scanf("%ld", &try);
        //printf("g_Count = %d\n",g_into[g_Count]->id);
        if (INT_MIN < try && try < INT_MAX)
        {
    
            g_into[g_Count]->id = try;
            break; // Right is the end 
        }
        else if (INT_MAX < try || try < INT_MIN) // Judge whether you crossed the line 
        {
    
            printf("The ID is out of range\n");
        }
        else // Determine whether it is other characters 
        {
    
            printf("ID ERROR!\n");
        }
    }
    //printf("g_Count = %d\n",g_into[g_Count]->id);
    return 0;
}

int Add_NAME(int flag) // Enter a name k
{
    
    flag = 1;
    while (flag)
    {
    
        int str_len;
        char name_try[32];
        printf("Please enter name:\n");
        scanf("%s", name_try);
        str_len = My_strlen(name_try);
        if (str_len < 32 && str_len > 0)
        {
    
            My_strcpy(g_into[g_Count]->name, name_try);
            //printf("g_into[g_Count]->name = %s\n",g_into[g_Count]->name);
            break;
        }
        else if (str_len > 32 || str_len < 0)
        {
    
            printf("The name is out of range\n");
        }
        else
        {
    
            printf("Add_NAME ERROR\n");
        }
        //printf("%s\n", g_into[g_Count]->name);
    }
    return 0;
}

int My_strlen(const char *name) //strlen function ( Self writing )
{
    
    if (NULL == name)
    {
    
        printf("The char is NULL\n");
    }

    const char *str = name;
    int len = 0;

    while (*str != '\0')
    {
    
        len += 1;
        str++;
    }
    //printf("str = %d\n",len);
    return len;
}

char *My_strcpy(char *dest, char *src) //strcpy function ( Self writing )
{
    
    if (NULL == dest || NULL == src)
    {
    
        return NULL;
    }
    char *temp = dest; // Need to have a temp For mobile 

    while (*src != '\0')
    {
    
        *temp = *src;
        src++;
        temp++;
    }
    *temp = '\0';
    //printf("g_into[g_Count]->name = %d\n",*dest);
    return dest;
}

int Add_AGE(int flag) // Input ID
{
    
    flag = 1;
    while (flag)
    {
    
        long int try = 0;
        printf("Please enter age:\n");
        scanf("%ld", &try);
        //printf("g_Count = %d\n",g_into[g_Count]->id);
        if (INT_MIN < try && try < INT_MAX)
        {
    
            g_into[g_Count]->age = try;
            break; // Right is the end 
        }
        else if (INT_MAX < try || try < INT_MIN) // Judge whether you crossed the line 
        {
    
            printf("The AGE is out of range\n");
        }
        else // Determine whether it is other characters 
        {
    
            printf("AGE ERROR!\n");
        }
    }
    //printf("g_Count = %d\n",g_into[g_Count]->id);
    return 0;
}

int Add_TEL(int flag) // Enter the phone number 
{
    
    flag = 1;
    while (flag)
    {
    
        int str_len;
        char tel_try[32];
        printf("Please enter tel:\n");
        scanf("%s", tel_try);
        str_len = My_strlen(tel_try);
        if (11 == str_len)
        {
    
            My_strcpy(g_into[g_Count]->tel, tel_try);
            //printf("g_into[g_Count]->name = %s\n",g_into[g_Count]->name);
            break;
        }
        else if (11 != str_len)
        {
    
            printf("The tel is out of range\n");
        }
        else
        {
    
            printf("Add_TEL ERROR\n");
        }
    }
    //printf(" Son  %s\n", g_into[g_Count]->tel);
    return 0;
}

int Keep_Add(int flag) // Determine whether to continue to input information 
{
    
    while (1)
    {
    
        char keep;
        printf("Do you need to continue adding contacts(y/n)\n");
        putchar(10);
        getchar();
        scanf("%c", &keep);
        //
        if ('y' == keep)
        {
    
            return 1;
        }
        else if ('n' == keep)
        {
    
            system("clear");
            return 0;
        }
        else
        {
    
            printf("Please enter 'y' or 'n'\n");
            putchar(10);
        }
    }
}

void Dis_inf() // Output in alphabetical order 
{
    
    system("clear");
    //printf("g_Count = %d\n",g_Count);
    char temp[32];
    //printf("%d\n",__LINE__);
    int i, j;
    for (i = 0; i < g_Count - 1; i++)
    {
    
        //printf("%d\n",__LINE__);
        for (j = 0; j < g_Count - i - 1; j++)
        {
    
            //printf("%d\n",__LINE__);
            if (My_strncmp(g_into[j]->name, g_into[j + 1]->name, 1) > 0)
            {
    
                g_into[j]->id = g_into[j]->id ^ g_into[j + 1]->id;
                g_into[j + 1]->id = g_into[j]->id ^ g_into[j + 1]->id;
                g_into[j]->id = g_into[j]->id ^ g_into[j + 1]->id;

                My_strcpy(temp, g_into[j]->name);
                My_strcpy(g_into[j]->name, g_into[j + 1]->name);
                My_strcpy(g_into[j + 1]->name, temp);

                g_into[j]->age = g_into[j]->age ^ g_into[j + 1]->age;
                g_into[j + 1]->age = g_into[j]->age ^ g_into[j + 1]->age;
                g_into[j]->age = g_into[j]->age ^ g_into[j + 1]->age;

                My_strcpy(temp, g_into[j]->tel);
                My_strcpy(g_into[j]->tel, g_into[j + 1]->tel);
                My_strcpy(g_into[j + 1]->tel, temp);
            }
        }
    }
    for (size_t i = 0; i < g_Count; i++)
    {
    
        putchar(10);
        printf("g_into[%ld]->id = %d\n", i, g_into[i]->id);
        printf("g_into[%ld]->name = %s\n", i, g_into[i]->name);
        printf("g_into[%ld]->age = %d\n", i, g_into[i]->age);
        printf("g_into[%ld]->tel = %s\n", i, g_into[i]->tel);
        putchar(10);
    }
}

int My_strncmp(char *str1, char *str2, size_t n)
{
    
    char *temp_1 = str1;
    char *temp_2 = str2;
    //printf("%d\n",__LINE__);
    if (NULL == str1 || NULL == str2)
    {
    
        printf("My_strncmp is ERROR!\n");
    }
    //printf("%d\n",__LINE__);
    for (size_t i = 0; i < n; i++)
    {
    

        if (*temp_1 == *temp_2)
        {
    
            return 0;
        }
        else if ((*temp_1 - *temp_2) > 0)
        {
    
            return 1;
        }
        else if ((*temp_1 - *temp_2) < 0)
        {
    
            return -1;
        }
        int len = 0;
        if (My_strlen(str1) > My_strlen(str2))
        {
    
            len = My_strlen(str2);
            if ((i + 1) <= len)
            {
    
                str1++;
                str2++;
            }
            else
            {
    
                break;
            }
        }
        else
        {
    
            len = My_strlen(str1);
            if ((i + 1) <= len)
            {
    
                str1++;
                str2++;
            }
            else
            {
    
                break;
            }
        }
    }
}

void Mod_inf()
{
    
    char Mod_Name[32];
    char Mod_tel[12];
    int Find_g_Count = -1;
    Find_g_Count = Find_inf();
    if (Find_g_Count != -1)
    {
    
        putchar(10);
        printf("Please enter id\n");
        scanf("%d", &g_into[Find_g_Count]->id);
        putchar(10);
        printf("Please enter name\n");
        scanf("%s", Mod_Name);
        My_strcpy(g_into[Find_g_Count]->name, Mod_Name);
        putchar(10);
        printf("Please enter age\n");
        scanf("%d", &g_into[Find_g_Count]->age);
        putchar(10);
        printf("Please enter tel\n");
        scanf("%s", Mod_tel);
        My_strcpy(g_into[Find_g_Count]->tel, Mod_tel);
        printf("Modification successful!\n");
        putchar(10);
        printf("g_into[%d]->id = %d\n", Find_g_Count, g_into[Find_g_Count]->id);
        printf("g_into[%d]->name = %s\n", Find_g_Count, g_into[Find_g_Count]->name);
        printf("g_into[%d]->age = %d\n", Find_g_Count, g_into[Find_g_Count]->age);
        printf("g_into[%d]->tel = %s\n", Find_g_Count, g_into[Find_g_Count]->tel);
    }
}

void Del_inf()
{
    
    int Find_g_Count = -1;
    Find_g_Count = Find_inf();
    if (Find_g_Count != -1)
    {
    
        for (size_t i = Find_g_Count; i < g_Count - 1; i++)
        {
    
            g_into[Find_g_Count]->id = g_into[Find_g_Count + 1]->id;
            My_strcpy(g_into[Find_g_Count]->name, g_into[Find_g_Count + 1]->name);
            g_into[Find_g_Count]->age = g_into[Find_g_Count + 1]->age;
            My_strcpy(g_into[Find_g_Count]->tel, g_into[Find_g_Count + 1]->tel);
        }
        free(g_into[g_Count]);
        g_into[g_Count] = NULL;
        g_Count -= 1;
        printf("Delete Successful!\n");
    }
}

int Find_inf()
{
    
    system("clear");
    int choice;
    putchar(10);
    printf("Please enter the ID or name you want to find\n");
    printf("1->by ID or 2->by name (1/2)\n");
    scanf("%d", &choice);
    int Find_g_Gount = -1;
    switch (choice)
    {
    
    case 1:
        Find_g_Gount = Find_ID();
        //printf("%d %d\n",__LINE__,Find_g_Gount);
        break;
    case 2:
        Find_g_Gount = Find_NAME();
        break;
    default:
        printf("Please enter '1' or '2'\n");
        break;
    }
    return Find_g_Gount;
}

int Find_ID()
{
    
    long int find_ID;
    printf("Please enter id\n");
    scanf("%ld", &find_ID);
    int i;
    for (i = 0; i < g_Count; i++)
    {
    
        if (g_into[i]->id == find_ID)
        {
    
            printf("g_into[%d]->id = %d\n", i, g_into[i]->id);
            printf("g_into[%d]->name = %s\n", i, g_into[i]->name);
            printf("g_into[%d]->age = %d\n", i, g_into[i]->age);
            printf("g_into[%d]->tel = %s\n", i, g_into[i]->tel);
            //printf("Find %d\n",__LINE__);
            return i;
        }
    }
    printf("Sorry, this ID was not found\n");
    return -1;
}

int Find_NAME()
{
    
    char find_NAME[32];
    printf("Please enter name\n");
    scanf("%s", find_NAME);
    int i;
    for (i = 0; i < g_Count; i++)
    {
    
        if (My_strcmp(find_NAME, g_into[i]->name) == 0)
        {
    
            printf("g_into[%d]->id = %d\n", i, g_into[i]->id);
            printf("g_into[%d]->name = %s\n", i, g_into[i]->name);
            printf("g_into[%d]->age = %d\n", i, g_into[i]->age);
            printf("g_into[%d]->tel = %s\n", i, g_into[i]->tel);
            return i;
        }
    }
    printf("Sorry, this NAME was not found\n");
    return -1;
}

int My_strcmp(char *dest, char *sub)
{
    
    if (NULL == dest && NULL == sub)
    {
    
        return 0;
    }
    else if (NULL != dest && NULL == sub)
    {
    
        return 1;
    }
    else if (NULL == dest && NULL != sub)
    {
    
        return 1;
    }
    int len = 0;
    if (My_strlen(dest) < My_strlen(sub))
    {
    
        len = My_strlen(dest);
    }
    else
    {
    
        len = My_strlen(sub);
    }
    for (size_t i = 0; i < len + 1; i++)
    {
    
        if (*dest != *sub)
        {
    
            return 1;
        }
        dest++;
        sub++;
    }
    return 0;
}
//8274
原网站

版权声明
本文为[Light chasing rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130551477628.html