本通訊錄具體實作以下功能:

實作結果如下

其中排序分為姓名排序和年齡排序

附原始碼:
測驗部分——
void menu()
{
printf("*******************************************\n");
printf("*******1.添加聯系人 2.洗掉聯系人*******\n");
printf("*******3.查找聯系人 4.修改聯系人*******\n");
printf("*******5.查看通訊錄 6.排序聯系人*******\n");
printf("***************** 0.退出 ******************\n");
}
int main()
{
int input = 0;
contacts x;//創建通訊錄結構體變數x
Init_contacts(&x);//初始化通訊錄
do {
menu();
printf("請選擇: >");
scanf("%d", &input);
switch (input)
{
case 添加聯系人:
contacts_add(&x);
break;
case 洗掉聯系人:
contacts_delete(&x);
break;
case 查找聯系人:
contacts_search(&x);
break;
case 修改聯系人:
contacts_modify(&x);
break;
case 查看通訊錄:
contacts_show(&x);
break;
case 排序聯系人:
contacts_sort(&x);
break;
case 退出:
//銷毀通訊錄,銷毀動態開辟的記憶體
Savecontact(&x);
Destroy(&x);
printf("退出.");
break;
default:
printf("輸入錯誤數字!\n");
break;
}
} while (input);
return 0;
}
函式實作部分——
void Checkcapacity(struct contacts* p)
{
if (p->capacity <= p->sz)
{
//增加容量
info* tmp=(info*)realloc(p->x, (p->capacity * 2) * sizeof(info));
if (tmp != NULL)
{
p->x = tmp;
p->capacity *= 2;
}
}
}
void Readcontact(struct contacts* p)
{
info tmp = { 0 };
FILE* pf = fopen("contacts.data", "rb");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return;
}
while (fread(&tmp, sizeof(struct info), 1, pf))
{
Checkcapacity(p);
p->x[p->sz] = tmp;
p->sz++;
}
}
void Init_contacts(struct contacts* p)
{
p->x = (info*)malloc(DEFAULT_SZ * sizeof(info));//默認通訊錄存放三個聯系人
if (p->x == NULL)
{
return;
}
else
{
p->sz = 0;
p->capacity = DEFAULT_SZ;
}
Readcontact(p);
}
void contacts_add(struct contacts* p)
{
//檢測通訊錄容量
//滿了就增加容量
Checkcapacity(p);
printf("請輸入姓名:>");
scanf("%s", p->x[p->sz].name);
printf("請輸入年齡:>");
scanf("%d", &p->x[p->sz].age);
printf("請輸入性別:>");
scanf("%s", p->x[p->sz].sex);
printf("請輸入電話:>");
scanf("%s", p->x[p->sz].tele);
printf("請輸入住址:>");
scanf("%s", p->x[p->sz].address);
printf("添加成功!\n");
p->sz++;
}
void contacts_delete(struct contacts*p)
{
if (p->sz == 0)
{
printf("無聯系人可洗掉\n");
}
else
{
char name[MAX_NAME] = { 0 };
printf("請輸入要洗掉的聯系人姓名:>");
scanf("%s", name);
int ret=Findbyname(p, name);//查找聯系人
//洗掉聯系人
if (ret == -1)
{
printf("查無此聯系人\n");
}
else
{
/*memmove(&(p->x[i]),&(p->x[i+1]),(p->sz-i)*sizeof(struct contacts));*/
int j = ret;
for (j = ret; j < p->sz-1; j++)
{
p->x[j] = p->x[j + 1];
}
p->sz--;
}
}
}
void contacts_search(struct contacts* p)
{
char name[MAX_NAME] = { 0 };
printf("請輸入聯系人姓名:>");
scanf("%s", name);
int ret = Findbyname(p, name);
if (ret == -1)
{
printf("查無此人!\n");
}
else
{
printf("%-5s\t%-4s\t%-5s\t%-15s\t%-20s\n", "姓名", "年齡", "性別", "電話", "住址");
printf("%-5s\t%-4d\t%-5s\t%-15s\t%-20s\n",
p->x[ret].name,
p->x[ret].age,
p->x[ret].sex,
p->x[ret].tele,
p->x[ret].address
);
}
}
void contacts_modify(struct contacts* p)
{
char name[MAX_NAME] = { 0 };
printf("請輸入要修改的聯系人姓名:>");
scanf("%s", name);
int ret = Findbyname(p, name);
if (ret == -1)
printf("查無此人\n");
else
{
printf("請輸入姓名:>");
scanf("%s", p->x[ret].name);
printf("請輸入年齡:>");
scanf("%d", &p->x[ret].age);
printf("請輸入性別:>");
scanf("%s", p->x[ret].sex);
printf("請輸入電話:>");
scanf("%s", p->x[ret].tele);
printf("請輸入住址:>");
scanf("%s", p->x[ret].address);
printf("修改成功!\n");
}
}
void contacts_show(const struct contacts*p)
{
if (p->sz == 0)
{
printf("聯系人為空!\n");
}
else
{
printf("%-5s\t%-4s\t%-5s\t%-15s\t%-20s\n","姓名","年齡","性別","電話","住址");
int i = 0;
for (i = 0; i < p->sz; i++)
{
printf("%-5s\t%-4d\t%-5s\t%-15s\t%-20s\n",
p->x[i].name,
p->x[i].age,
p->x[i].sex,
p->x[i].tele,
p->x[i].address
);
}
}
}
void contacts_sort(struct contacts* p)
{
int input = 0;
printf("請選擇排序方式:>\n");
printf("1.姓名 2.年齡\n");
scanf("%d", &input);
switch (input)
{
case 1:
qsort(p->x, p->sz,sizeof(info), name_cmp);
break;
case 2:
qsort(p->x, p->sz, sizeof(info), age_cmp);
break;
default:
printf("選擇錯誤\n");
break;
}
}
int name_cmp(const void* e1,const void*e2)
{
return strcmp(((info*)e1)->name, ((info*)e2)->name);
}
int age_cmp(const void* e1, const void* e2)
{
return ((info*)e1)->age - ((info*)e2)->age;
}
void Savecontact(struct contacts* p)
{
FILE* pf = fopen("contacts.data", "wb");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return;
}
int i = 0;
for (i = 0; i < p->sz; i++)
{
fwrite(&(p->x[i]),sizeof(struct info),1,pf);
}
fclose(pf);
pf = NULL;
}
void Destroy(struct contacts* p)
{
free(p->x);
p->x = NULL;
}
static int Findbyname(const struct contacts* p, char* name)
{
int i = 0;
for (i = 0; i < p->sz; i++)
{
if (strcmp(p->x[i].name, name) == 0)
{
return i;
}
}
if (i == p->sz)
{
return -1;
}
}
頭檔案部分——
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define DEFAULT_SZ 3
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 15
#define MAX_ADDRESS 30
enum menu
{
退出,
添加聯系人,
洗掉聯系人,
查找聯系人,
修改聯系人,
查看通訊錄,
排序聯系人
};
typedef struct info {
char name [MAX_NAME] ;
int age ;
char sex [MAX_SEX] ;
char tele [MAX_TELE] ;
char address[MAX_ADDRESS];
}info;
typedef struct contacts {
info* x;
int capacity;//當前通訊錄的最大容量
int sz;//記錄當前已有聯系人數
}contacts;
void Init_contacts(struct contacts* p);
void contacts_add(struct contacts* p);
void contacts_delete(struct contacts* p);
void contacts_search(struct contacts* p);
void contacts_modify(struct contacts* p);
void contacts_show(const struct contacts* p);
void contacts_sort(struct contacts* p);
void Destroy(struct contacts* p);
void Savecontact(struct contacts* p);
int name_cmp(const void* e1, const void* e2);
int age_cmp(const void* e1, const void* e2)
//作者定期分享C語言學習路上的經驗,歡迎關注哦
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/264525.html
標籤:其他
