通訊錄
- 聲明包含人的各種資訊的結構體類型
- 聲明包含所有人的資訊的結構體類型
-
- 靜态版本
- 動态版本
- 建立選項菜單
- 使用者進行選擇
- 初始化通訊錄
-
- 靜态版
- 動态版本
- 檔案版本
- 擴容
- 儲存到檔案中
- 顯示通訊錄裡面的資訊
- 錄入人的資訊
- 查找人姓名的函數
- 查找人的資訊
- 删除人的資訊
- 修改人的資訊
- 按名字排序
- 🔽
本通訊錄用來錄入,查找,删除,修改,顯示,排序等功能。
暫定這個通訊錄的人包括姓名,年齡,性别,電話,住址。
依然是分子產品實作
⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇
點選擷取通訊錄源碼
聲明包含人的各種資訊的結構體類型
-
姓名,性别,年齡,電話,位址
#define name_max 15
#define sex_max 5
#define tele_max 12
#define address_max 30
typedef struct people
{
char name[name_max];
char sex[sex_max];
int age;
char tele[tele_max];
char address[address_max];
}people;
聲明包含所有人的資訊的結構體類型
靜态版本
先預設最大可以儲存1000個人,
count
表示通訊錄裡面現有的人。
#define human_max 1000
typedef struct contacts
{
people human[human_max];
int count;
}contacts;
動态版本
typedef struct contacts
{
people* human;
int count;
int max;
}contacts;
建立選項菜單
void menu()
{
printf("############################################\n");
printf("###### 1.錄入 2.查找 #########\n");
printf("###### 3.删除 4.修改 #########\n");
printf("###### 5.排序 6.顯示 #########\n");
printf("########### 0.退出 #############\n");
printf("############################################\n");
}
使用者進行選擇
enum select
{
quit,
add,
find,
delete,
change,
sort,
print
};
void test()
{
int n;
contacts con;
do
{
menu();
printf("請選擇你的操作->");
scanf("%d", &n);
switch (n)
{
case quit:
printf("退出程式\n");
break;
/*
case quit:
printf("退出程式\n");
free(con.human);
con.human = NULL;
break; 動态版本修改部分
*/
case add://添加
break;
case find://查找
break;
case delete://删除
break;
case change://修改
break;
case sort://排序
break;
case print://列印
break;
default:
printf("操作非法,請重新選擇\n");
break;
}
} while (n);
}
初始化通訊錄
靜态版
void init_contacts(contacts* p)
{
p->count = 0;
memset(p->human, 0, sizeof(p->human));
}
動态版本
先預設可以存3個人的資訊
void init_contacts(contacts* p)
{
p->human = (people*)malloc(sizeof(people) * 3);
if (p->human == NULL)
{
printf("初始化失敗%s", strerror(errno));
return;
}
p->count = 0;
p->max = 3;
memset(p->human, 0, p->max*sizeof(p->human));
}
檔案版本
void init_contacts(contacts* p)
{
p->human = (people*)malloc(sizeof(people) * 3);
if (p->human == NULL)
{
printf("初始化失敗%s", strerror(errno));
return;
}
p->count = 0;
p->max = 3;
memset(p->human, 0, p->max*sizeof(p->human));
add_store(p);
}
void add_store(contacts* p)
{
FILE* s = fopen("通訊錄.txt", "rb");
if (s == NULL)
{
printf("檔案資訊讀入出錯%s", strerror(errno));
return;
}
people temp;
while (fread(&temp, sizeof(people), 1, s))
{
p->human[p->count] = temp;
p->count++;
if (p->count == p->max)
enlarge(p);
}
fclose(s);
s = NULL;
}
擴容
void enlarge(contacts* p)
{
p->max += 3;
people* temp=(people*)realloc(p->human,sizeof(people) * p->max);
if (temp == NULL)
{
printf("%s\n", strerror(errno));
return;
}
p->human = temp;
}
儲存到檔案中
void store(const contacts* p)
{
FILE* s = fopen("通訊錄.txt", "wb");
if (s == NULL)
{
printf("檔案資訊讀入出錯%s", strerror(errno));
return;
}
int i = 0;
for (i = 0; i < p->count; i++)
{
fwrite(p->human+i, sizeof(people), 1, s);
}
fclose(s);
s = NULL;
}
顯示通訊錄裡面的資訊
void print_contacts(const contacts* p)
{
printf("%-15s%-5s%-5s%-15s%-30s\n", "姓名", "性别", "年齡", "電話", "位址");
int i = 0;
for (i = 0; i < p->count; i++)
{
printf("%-15s%-5s%-5d%-15s%-30s\n", p->human[i].name, p->human[i].sex, p->human[i].age, p->human[i].tele, p->human[i].address);
}
}
錄入人的資訊
void add_contacts(contacts* p)
{
if (p->count == human_max)
{
printf("通訊錄已滿");
return;
}
/*
if (p->count == p->max)
{
enlarge(p);
}動态版修改部分
*/
printf("請輸入人的姓名->");
scanf("%s", p->human[p->count].name);
printf("請輸入人的性别->");
scanf("%s", p->human[p->count].sex);
printf("請輸入人的年齡->");
scanf("%d", &p->human[p->count].age);
printf("請輸入人的電話->");
scanf("%s", p->human[p->count].tele);
printf("請輸入人的位址->");
scanf("%s", p->human[p->count].address);
p->count++;
printf("錄入成功\n");
}
查找人姓名的函數
找到傳回對應的人的下标
找不到傳回-1
int find_people(char* name_x,const contacts* p)
{
int i = 0;
for (i=0;i<p->count;i++)
{
if (strcmp(name_x, p->human[i].name) == 0)
return i;
}
return -1;
}
查找人的資訊
void find_contacts(const contacts* p)
{
char name_x[name_max];
printf("請輸入人的姓名->");
scanf("%s", name_x);
int ret=find_people(name_x, p);
if (ret == -1)
printf("沒有查找到該人\n");
else
{
printf("%-15s%-5s%-5s%-15s%-30s\n", "姓名", "性别", "年齡", "電話", "位址");
printf("%-15s%-5s%-5d%-15s%-30s\n", p->human[ret].name, p->human[ret].sex, p->human[ret].age,
p->human[ret].tele, p->human[ret].address);
}
}
删除人的資訊
void delete_contacts(contacts* p)
{
char name_x[name_max];
printf("請輸入人的姓名->");
scanf("%s", name_x);
int ret = find_people(name_x, p);
if (ret == -1)
printf("沒有查找到該人\n");
else
{
int i;
for (i = ret; i < p->count-1; i++)
{
p->human[i] = p->human[i + 1];
}
p->count--;
printf("删除成功\n");
}
}
修改人的資訊
void change_contacts(contacts* p)
{
char name_x[name_max];
printf("請輸入人的姓名->");
scanf("%s", name_x);
int ret = find_people(name_x, p);
if (ret == -1)
printf("沒有查找到該人\n");
else
{
printf("請輸入人的姓名->");
scanf("%s", p->human[ret].name);
printf("請輸入人的性别->");
scanf("%s", p->human[ret].sex);
printf("請輸入人的年齡->");
scanf("%d", &p->human[ret].age);
printf("請輸入人的電話->");
scanf("%s", p->human[ret].tele);
printf("請輸入人的位址->");
scanf("%s", p->human[ret].address);
printf("修改成功\n");
}
}
按名字排序
int cmp_name(const void* e1, const void* e2)
{
return strcmp(((people*)e1)->name, ((people*)e2)->name);
}
void sort_contacts(contacts* p)
{
qsort(p->human, p->count, sizeof(p->human[0]), cmp_name);
printf("排序成功\n");
}