当前位置:网站首页>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】
List of articles
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
边栏推荐
- Password recovery vulnerability of foreign public testing
- Introduction to paddle - using lenet to realize image classification method II in MNIST
- How is it most convenient to open an account for stock speculation? Is it safe to open an account on your mobile phone
- 5g NR system messages
- Get started quickly using the local testing tool postman
- 取消select的默认样式的向下箭头和设置select默认字样
- fabulous! How does idea open multiple projects in a single window?
- How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
- v-for遍历元素样式失效
- 基于人脸识别实现课堂抬头率检测
猜你喜欢
[go record] start go language from scratch -- make an oscilloscope with go language (I) go language foundation
Binder core API
【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
v-for遍历元素样式失效
Cancel the down arrow of the default style of select and set the default word of select
基于人脸识别实现课堂抬头率检测
取消select的默认样式的向下箭头和设置select默认字样
Image data preprocessing
1293_ Implementation analysis of xtask resumeall() interface in FreeRTOS
随机推荐
What if the testing process is not perfect and the development is not active?
50Mhz产生时间
基于人脸识别实现课堂抬头率检测
4.交叉熵
Lecture 1: the entry node of the link in the linked list
DNS series (I): why does the updated DNS record not take effect?
语义分割模型库segmentation_models_pytorch的详细使用介绍
What has happened from server to cloud hosting?
Which securities company has a low, safe and reliable account opening commission
Reentrantlock fair lock source code Chapter 0
国内首次,3位清华姚班本科生斩获STOC最佳学生论文奖
Fofa attack and defense challenge record
ReentrantLock 公平锁源码 第0篇
Codeforces Round #804 (Div. 2)(A~D)
7.正则化应用
Course of causality, taught by Jonas Peters, University of Copenhagen
【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
Introduction to paddle - using lenet to realize image classification method I in MNIST
Is it safe to open an account on the official website of Huatai Securities?
串口接收一包数据