实现通讯录
要求具体完成
1. Makefile2.main.c3. tongxunlu.h3. tongxun.c
实现通讯录 要求1.用makefile管理文件
2.各个子功能
a:添加用户(连续添加)
b:查看用户(按照姓名的首字母排序)
c:搜索用户(两种方式:id,用户名)
d:删除用户信息
e:修改用户信息
3.自己封装字符串处理函数
具体完成 1. MakefileTARGET ?= 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}; //结构体指针数组
int g_Count; //当前人数
int main(int argc, char const *argv[])
{
Welcome(); //欢迎界面
int choice; //客户选择
g_Count = 0; //人数清零
while (1)
{
menu(); // 菜单界面
printf("Please enter a number:n"); //输入客户所需服务类型
scanf("%d", &choice);
serve(choice); //判断服务类型,并进行处理
}
//可以加写入文件
return 0;
}
3. tongxunlu.h
#pragma once #include3. tongxun.c#include #include #include //禁用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 *);
#include "tongxunlu.h"
void Welcome()
{
system("clear");
printf("--------------------------------------------------------n");
printf("ttWelcome to Contact Personn");
printf("--------------------------------------------------------n");
}
void menu()
{
printf("--------------------------------------------------------n");
printf(" 1->Add information 2->Displays informationn");
printf(" 3->Modify information 4->Delete informationn");
printf(" 5->Find information 6->Exitn");
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->退出
{
system("clear");
exit(0);
}
void Add_inf()//1->添加信息
{
while (1)
{
system("clear");
//printf("g_Count = %dn", g_Count);
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 informationnnidnnamenagentelephone numbern");
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 = %dn", g_Count, g_into[g_Count]->id);
printf("g_into[%d]->name = %sn", g_Count, g_into[g_Count]->name);
printf("g_into[%d]->age = %dn", g_Count, g_into[g_Count]->age);
printf("g_into[%d]->tel = %sn", g_Count, g_into[g_Count]->tel);
putchar(10);
flag = Keep_Add(flag);
//printf("flag = %dn",flag);
if (flag)
{
g_Count++;
break;
}
else
{
return;
}
}
}
}
int Add_ID(int flag) //输入ID
{
flag = 1;
while (flag)
{
long int try = 0;
printf("Please enter id:n");
scanf("%ld", &try);
//printf("g_Count = %dn",g_into[g_Count]->id);
if (INT_MIN < try && try < INT_MAX)
{
g_into[g_Count]->id = try;
break; //正确则结束
}
else if (INT_MAX < try || try < INT_MIN) //判断是否越界
{
printf("The ID is out of rangen");
}
else //判断是否为其他字符
{
printf("ID ERROR!n");
}
}
//printf("g_Count = %dn",g_into[g_Count]->id);
return 0;
}
int Add_NAME(int flag) //输入姓名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 = %sn",g_into[g_Count]->name);
break;
}
else if (str_len > 32 || str_len < 0)
{
printf("The name is out of rangen");
}
else
{
printf("Add_NAME ERRORn");
}
//printf("%sn", g_into[g_Count]->name);
}
return 0;
}
int My_strlen(const char *name) //strlen函数(自写)
{
if (NULL == name)
{
printf("The char is NULLn");
}
const char *str = name;
int len = 0;
while (*str != ' ')
{
len += 1;
str++;
}
//printf("str = %dn",len);
return len;
}
char *My_strcpy(char *dest, char *src) //strcpy函数(自写)
{
if (NULL == dest || NULL == src)
{
return NULL;
}
char *temp = dest; //需要有个temp进行移动
while (*src != ' ')
{
*temp = *src;
src++;
temp++;
}
*temp = ' ';
//printf("g_into[g_Count]->name = %dn",*dest);
return dest;
}
int Add_AGE(int flag) //输入ID
{
flag = 1;
while (flag)
{
long int try = 0;
printf("Please enter age:n");
scanf("%ld", &try);
//printf("g_Count = %dn",g_into[g_Count]->id);
if (INT_MIN < try && try < INT_MAX)
{
g_into[g_Count]->age = try;
break; //正确则结束
}
else if (INT_MAX < try || try < INT_MIN) //判断是否越界
{
printf("The AGE is out of rangen");
}
else //判断是否为其他字符
{
printf("AGE ERROR!n");
}
}
//printf("g_Count = %dn",g_into[g_Count]->id);
return 0;
}
int Add_TEL(int flag) //输入电话号码
{
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 = %sn",g_into[g_Count]->name);
break;
}
else if (11 != str_len)
{
printf("The tel is out of rangen");
}
else
{
printf("Add_TEL ERRORn");
}
}
//printf("子 %sn", g_into[g_Count]->tel);
return 0;
}
int Keep_Add(int flag)//判断是否继续输入信息
{
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()//按照首字母排序输出
{
//printf("g_Count = %dn",g_Count);
char temp[32];
//printf("%dn",__LINE__);
int i, j;
for (i = 0; i <= g_Count - 1; i++)
{
//printf("%dn",__LINE__);
for (j = 0; j <= g_Count - i - 1; j++)
{
//printf("%dn",__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 = %dn", i, g_into[i]->id);
printf("g_into[%ld]->name = %sn", i, g_into[i]->name);
printf("g_into[%ld]->age = %dn", i, g_into[i]->age);
printf("g_into[%ld]->tel = %sn", 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("%dn",__LINE__);
if (NULL == str1 || NULL == str2)
{
printf("My_strncmp is ERROR!n");
}
//printf("%dn",__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();
putchar(10);
printf("Please enter id");
scanf("%d",&g_into[Find_g_Count]->id);
putchar(10);
printf("Please enter name");
scanf("%s",Mod_Name);
My_strcpy(g_into[Find_g_Count]->name,Mod_Name);
putchar(10);
printf("Please enter age");
scanf("%d",&g_into[Find_g_Count]->age);
putchar(10);
printf("Please enter tel");
scanf("%s",Mod_tel);
My_strcpy(g_into[Find_g_Count]->tel,Mod_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; 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;
}
}
int Find_inf()
{
int choice;
putchar(10);
printf("Please enter the ID or name you want to findn");
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();
break;
case 2:
Find_g_Gount = Find_NAME();
break;
default:
break;
}
return Find_g_Gount;
}
int Find_ID()
{
long int find_ID;
printf("Please enter idn");
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 = %dn", i, g_into[i]->id);
printf("g_into[%d]->name = %sn", i, g_into[i]->name);
printf("g_into[%d]->age = %dn", i, g_into[i]->age);
printf("g_into[%d]->tel = %sn", i, g_into[i]->tel);
return i;
}
}
printf("Sorry, this ID was not foundn");
return -1;
}
int Find_NAME()
{
char find_NAME[32];
printf("Please enter namen");
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 = %dn", i, g_into[i]->id);
printf("g_into[%d]->name = %sn", i, g_into[i]->name);
printf("g_into[%d]->age = %dn", i, g_into[i]->age);
printf("g_into[%d]->tel = %sn", i, g_into[i]->tel);
return i;
}
}
printf("Sorry, this NAME was not foundn");
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;
}
//7627



