通訊錄(一)(靜態結構體)
阿新 • • 發佈:2018-11-01
結構體實現通訊錄
我們通常使用的手機通訊錄,可以實現增加,刪除,查詢,修改聯絡人資訊這幾個重要的版塊,當然我們手機裡儲存的列表也是按照首字母的順序排列的,非常的實用,那麼我們今天來用C語言簡單了實現一下通訊錄。
增加聯絡人
void AddContact(pContact pc)
{
assert(pc);
if (pc->sz == DATA_MAX)
{
printf("通訊錄已滿,插入失敗\n");
return;
}
printf("請輸入姓名:\n");
scanf("%s" , pc->data[pc->sz].name);
printf("請輸入年齡:\n");
scanf("%d", &pc->data[pc->sz].age);
printf("請輸入性別:\n");
scanf("%s", pc->data[pc->sz].sex);
printf("請輸入電話:\n");
scanf("%s", pc->data[pc->sz].tel);
printf("請輸入地址:\n");
scanf("%s" , pc->data[pc->sz].add);
pc->sz++;
printf("新增成功\n");
}
刪除聯絡人
static int Find(pContact pc,char name[])//static可以有效的保護程式碼
{
int i = 0;
assert(pc);
for (i = 0; i < pc->sz; i++)
{
if (strcmp(name, pc->data[i].name) == 0)
{
return i;//找到了返回下標
}
}
return -1;//沒找到
}
void DelContact(pContact pc)
{
char name[NAME_MAX] = { 0 };
int j = 0;
int pos = 0;
assert(pc);
if (pc->sz == 0)
{
printf("通訊錄為空,無法刪除\n");
return;
}
printf("請輸入要刪除的人的姓名:\n");
scanf("%s", name);
//1.查詢位置
pos=Find(pc, name);
if (pos == -1)
return;
//2.刪除
for (j = pos; j < pc->sz - 1; j++)
{
pc->data[j] = pc->data[j + 1];
}
pc->sz--;
printf("刪除成功\n");
}
查詢聯絡人
void SearchContact(pContact pc)
{
char name[NAME_MAX] = { 0 };
int pos = 0;
assert(pc);
if (pc->sz == 0)
{
printf("通訊錄為空,無法查詢\n");
return;
}
printf("請輸入要查詢的人的姓名:\n");
scanf("%s", name);
//查詢位置
pos = Find(pc, name);
if (pos == -1)
{
printf("要查詢的人不存在\n");
return;
}
else
{
printf("%10s\t%4s\t%5s\t%12s\t%20s\n", "name", "age", "sex", "tel", "add");
printf("%10s\t%4d\t%5s\t%12s\t%20s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tel, pc->data[pos].add);
}
}
修改聯絡人
void ModifyContact(pContact pc)
{
char name[NAME_MAX] = { 0 };
int pos = 0;
assert(pc);
if (pc->sz == 0)
{
printf("通訊錄為空,無法修改\n");
return;
}
printf("請輸入要修改的人的姓名:\n");
scanf("%s", name);
//查詢
pos = Find(pc, name);
if (pos == -1)
{
printf("要修改的人不存在\n");
return;
}
else
{
printf("請輸入姓名:\n");
scanf("%s", pc->data[pos].name);
printf("請輸入年齡:\n");
scanf("%d", &pc->data[pos].age);
printf("請輸入性別:\n");
scanf("%s", pc->data[pos].sex);
printf("請輸入電話:\n");
scanf("%s", pc->data[pos].tel);
printf("請輸入地址:\n");
scanf("%s", pc->data[pos].add);
printf("修改成功\n");
}
}
排序
void SortContact(pContact pc)
{
int i = 0;
int j = 0;
char tmp = 0;
int flag = 0;
assert(pc);
for (i = 0; i < pc->sz - 1; i++)
{
for (j = 0; j < pc->sz - 1 - i; j++)
{
if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0)
{//注意這裡是字串進行比較,不能直接比較
PeoInfo tmp = pc->data[j];
pc->data[j] = pc->data[j + 1];
pc->data[j + 1] = tmp;
flag = 1;
}
}
if (flag == 0)//提高排序的效率
return;
}
printf("排序成功!\n");
}
清空
void EmptyContact(pContact pc)
{
assert(pc);
pc->sz = 0;//清空
}
顯示
void ShowContact(pContact pc)
{
int i = 0;
assert(pc);
printf("%10s\t%4s\t%5s\t%12s\t%20s\n","name","age","sex","tel","add");
for (i = 0; i < pc->sz; i++)
{
printf("%10s\t%4d\t%5s\t%12s\t%20s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tel, pc->data[i].add);
}
}
標頭檔案
#ifndef __CONTACT_H__
#define __CONTACT_H__
#include<stdio.h>
#include<string.h>
#include<assert.h>
#define NAME_MAX 20
#define SEX_MAX 5
#define TEL_MAX 12
#define ADD_MAX 25
#define DATA_MAX 1000
typedef struct PeoInfo
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char tel[TEL_MAX];
char add[ADD_MAX];
}PeoInfo;
typedef struct Contact
{
PeoInfo data[DATA_MAX];//資料存放的位置
int sz ;//表示當前有sz個有效資料
}Contact,*pContact;
void InitContact(pContact pc);
void AddContact(pContact pc);
void DelContact(pContact pc);
void SearchContact(pContact pc);
void ModifyContact(pContact pc);
void SortContact(pContact pc);
void ShowContact(pContact pc);
void EmptyContact(pContact pc);
#endif // __CONTACT_H__
Contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
void InitContact(pContact pc)
{
assert(pc);
pc->sz = 0;
memset(pc->data, 0, sizeof(pc->data));//初始化
}
void AddContact(pContact pc)
{
assert(pc);
if (pc->sz == DATA_MAX)
{
printf("通訊錄已滿,插入失敗\n");
return;
}
printf("請輸入姓名:\n");
scanf("%s", pc->data[pc->sz].name);
printf("請輸入年齡:\n");
scanf("%d", &pc->data[pc->sz].age);
printf("請輸入性別:\n");
scanf("%s", pc->data[pc->sz].sex);
printf("請輸入電話:\n");
scanf("%s", pc->data[pc->sz].tel);
printf("請輸入地址:\n");
scanf("%s", pc->data[pc->sz].add);
pc->sz++;
printf("新增成功\n");
}
static int Find(pContact pc,char name[])//static可以有效的保護程式碼
{
int i = 0;
assert(pc);
for (i = 0; i < pc->sz; i++)
{
if (strcmp(name, pc->data[i].name) == 0)
{
return i;//找到了返回下標
}
}
return -1;//沒找到
}
void DelContact(pContact pc)
{
char name[NAME_MAX] = { 0 };
int j = 0;
int pos = 0;
assert(pc);
if (pc->sz == 0)
{
printf("通訊錄為空,無法刪除\n");
return;
}
printf("請輸入要刪除的人的姓名:\n");
scanf("%s", name);
//1.查詢位置
pos=Find(pc, name);
if (pos == -1)
return;
//2.刪除
for (j = pos; j < pc->sz - 1; j++)
{
pc->data[j] = pc->data[j + 1];
}
pc->sz--;
printf("刪除成功\n");
}
void SearchContact(pContact pc)
{
char name[NAME_MAX] = { 0 };
int pos = 0;
assert(pc);
if (pc->sz == 0)
{
printf("通訊錄為空,無法查詢\n");
return;
}
printf("請輸入要查詢的人的姓名:\n");
scanf("%s", name);
//查詢位置
pos = Find(pc, name);
if (pos == -1)
{
printf("要查詢的人不存在\n");
return;
}
else
{
printf("%10s\t%4s\t%5s\t%12s\t%20s\n", "name", "age", "sex", "tel", "add");
printf("%10s\t%4d\t%5s\t%12s\t%20s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tel, pc->data[pos].add);
}
}
void ModifyContact(pContact pc)
{
char name[NAME_MAX] = { 0 };
int pos = 0;
assert(pc);
if (pc->sz == 0)
{
printf("通訊錄為空,無法修改\n");
return;
}
printf("請輸入要修改的人的姓名:\n");
scanf("%s", name);
//查詢
pos = Find(pc, name);
if (pos == -1)
{
printf("要修改的人不存在\n");
return;
}
else
{
printf("請輸入姓名:\n");
scanf("%s", pc->data[pos].name);
printf("請輸入年齡:\n");
scanf("%d", &pc->data[pos].age);
printf("請輸入性別:\n");
scanf("%s", pc->data[pos].sex);
printf("請輸入電話:\n");
scanf("%s", pc->data[pos].tel);
printf("請輸入地址:\n");
scanf("%s", pc->data[pos].add);
printf("修改成功\n");
}
}
void SortContact(pContact pc)
{
int i = 0;
int j = 0;
char tmp = 0;
int flag = 0;
assert(pc);
for (i = 0; i < pc->sz - 1; i++)
{
for (j = 0; j < pc->sz - 1 - i; j++)
{
if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0)
{
PeoInfo tmp = pc->data[j];
pc->data[j] = pc->data[j + 1];
pc->data[j + 1] = tmp;
flag = 1;
}
}
if (flag == 0)
return;
}
printf("排序成功!\n");
}
void ShowContact(pContact pc)
{
int i = 0;
assert(pc);
printf("%10s\t%4s\t%5s\t%12s\t%20s\n","name","age","sex","tel","add");
for (i = 0; i < pc->sz; i++)
{
printf("%10s\t%4d\t%5s\t%12s\t%20s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tel, pc->data[i].add);
}
}
void EmptyContact(pContact pc)
{
assert(pc);
pc->sz = 0;//清空
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
void menu()
{
printf("********************************\n");
printf("*** 1.add 2.del ***\n");
printf("*** 3.search 4.modify ***\n");
printf("*** 5.show 6.sort ***\n");
printf("*** 7.empty 0.exit ***\n");
printf("********************************\n");
}
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT,
EMPTY
};
void test()
{
int input = 0;
struct Contact con;
InitContact(&con);
do
{
menu();
printf("請選擇");
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case DEL:
DelContact(&con);
break;
case SEARCH:
SearchContact(&con);
break;
case MODIFY:
ModifyContact(&con);
break;
case SHOW:
ShowContact(&con);
break;
case SORT:
SortContact(&con);
break;
case EMPTY:
EmptyContact(&con);
break;
case EXIT:
break;
default:
printf("選擇錯誤\n");
}
} while (input);
}
int main()
{
test();
return 0;
}
執行結果
下一篇將實現動態通訊錄:https://blog.csdn.net/qq_40550018/article/details/82320723