本文主要是介绍通讯录Contact,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
利用C语言编写通讯录程序
Utili.h
#ifndef _UTILI_H_
#define _UTILI_H_
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#endif /* _UTILI_H_ */
Contact.h
#ifndef _CONTACT_H_
#define _CONTACT_H_
#include"utili.h"
enum {QUIT,ADD,DEL,FIND,MODIFY,CLEAR,SORT,SHOW};
#define MAX_NAME_SIZE 20
#define MAX_SEX_SIZE 3
#define MAX_TEL_SIZE 12
#define MAX_ADD_SIZE 256
#define MAX_CONTACT_SIZE 256
//定义信息结构
typedef struct PersonInfo
{
char name[MAX_NAME_SIZE];
char sex[MAX_SEX_SIZE];
int age;
char tel[MAX_TEL_SIZE];
char add[MAX_ADD_SIZE];
}PersonInfo;
//定义通讯录结构
typedef struct Contact
{
PersonInfo Cont[MAX_CONTACT_SIZE];
size_t capacity;
size_t size;
}Contact;
bool IsEmpty(Contact* pcnt);
bool IsFull(Contact* pcnt);
int FindByName(Contact* pcnt,char name[]);
void InitContact(Contact* pcnt);
void AddContact(Contact* pcnt);
void ShowContact(Contact* pcnt);
void FindContact(Contact* pcnt);
void ModifyContact(Contact* pcnt);
void DelContact(Contact* pcnt);
void ClearContact(Contact* pcnt);
void SortContact(Contact* pcnt);
#endif /* _CONTACT_H_ */
Contact.c
#include"contact.h"
void InitContact(Contact* pcnt)
{
memset(pcnt->Cont, 0, sizeof(PersonInfo) * MAX_CONTACT_SIZE);
pcnt->capacity = MAX_CONTACT_SIZE;
pcnt->size = 0;
}
bool IsFull(Contact* pcnt)
{
return pcnt->size >= pcnt->capacity;
}
void AddContact(Contact* pcnt)
{
//检查容量
if (IsFull(pcnt))
{
printf("通讯录已满!");
return;
}
printf("姓名:");
scanf("%s", pcnt->Cont[pcnt->size].name);
printf("性别:");
scanf("%s", pcnt->Cont[pcnt->size].sex);
printf("年龄:");
scanf("%d", &pcnt->Cont[pcnt->size].age);
printf("电话:");
scanf("%s", pcnt->Cont[pcnt->size].tel);
printf("地址:");
scanf("%s", pcnt->Cont[pcnt->size].add);
pcnt->size++;
printf("增加人员信息成功!\n");
}
int FindByName(Contact* pcnt,char name[])
{
for (int i = 0; i < pcnt->size; ++i)
{
if (strcmp(name, pcnt->Cont[i].name) == 0)
{
return i;
}
}
return -1;
}
void FindContact(Contact* pcnt)
{
if (IsEmpty(pcnt))
{
printf("通讯录为空!\n");
return;
}
char name[MAX_NAME_SIZE];
printf("请输入姓名:");
scanf("%s", name);
int index = FindByName(pcnt,name);
if (index == -1)
{
printf("此人不存在!\n");
return;
}
printf("%-15s%-15s%-15s%-15s%-15s", "姓名", "性别", "年龄", "电话", "地址");
printf("\n");
printf("%-13s%-13s%-13d%-13s%-13s\n", pcnt->Cont[index].name, pcnt->Cont[index].sex, pcnt->Cont[index].age, pcnt->Cont[index].tel, pcnt->Cont[index].add);
}
bool IsEmpty(Contact* pcnt)
{
return pcnt->size == 0;
}
void ShowContact(Contact* pcnt)
{
printf("%-15s%-15s%-15s%-15s%-15s","姓名","性别","年龄","电话","地址");
printf("\n");
for (int i = 0; i < pcnt->size; ++i)
{
printf("%-13s%-13s%-13d%-13s%-13s\n", pcnt->Cont[i].name, pcnt->Cont[i].sex, pcnt->Cont[i].age, pcnt->Cont[i].tel, pcnt->Cont[i].add);
}
}
void ModifyContact(Contact* pcnt)
{
if (IsEmpty(pcnt))
{
printf("通讯录为空!\n");
return;
}
printf("请输入姓名:");
char name[MAX_NAME_SIZE];
scanf("%s", name);
int index = FindByName(pcnt,name);
if (index == -1)
{
printf("此人不存在!\n");
return;
}
printf("你要修改什么信息(1-姓名 2-性别 3-年龄 4-电话 5-住址)\n");
int sel;
scanf("%d", &sel);
switch (sel)
{
case 1:
printf("请输入要修改的姓名:");
scanf("%s", pcnt->Cont[index].name);
break;
case 2:
printf("请输入要修改的性别:");
scanf("%s", pcnt->Cont[index].sex);
break;
case 3:
printf("请输入要修改的年龄:");
scanf("%d",&pcnt->Cont[index].age);
break;
case 4:
printf("请输入要修改的电话:");
scanf("%s", pcnt->Cont[index].tel);
break;
case 5:
printf("请输入要修改的地址:");
scanf("%s", pcnt->Cont[index].add);
break;
}
}
void DelContact(Contact* pcnt)
{
printf("请输入要删除的名字:");
char name[MAX_NAME_SIZE];
scanf("%s", name);
int index = FindByName(pcnt, name);
if (index == -1)
{
printf("此人不存在!\n");
return;
}
for (int i = index; i < pcnt->size - 1; ++i)
{
pcnt->Cont[i] = pcnt->Cont[i + 1];
}
pcnt->size--;
printf("删除完成!\n");
}
void ClearContact(Contact* pcnt)
{
printf("是否确定 <Y/N> ?:\n");
getchar();
char ch = getchar();
if (ch == 'Y' || ch == 'y')
{
pcnt->size = 0;
printf("清除成功!\n");
return;
}
else if(ch == 'N' || ch == 'n')
{
printf("清楚取消!\n");
return;
}
printf("输入错误!\n");
}
void SortContact(Contact* pcnt)
{
for (int i = 0; i < pcnt->size - 1; ++i)
{
for (int j = 0; j < pcnt->size - i - 1; ++j)
{
if (strcmp(pcnt->Cont[j].name, pcnt->Cont[j + 1].name) > 0)
{
PersonInfo tmp = pcnt->Cont[j];
pcnt->Cont[j] = pcnt->Cont[j + 1];
pcnt->Cont[j + 1] = tmp;
}
}
}
printf("排序成功!\n");
}
ContactMain.c
#include"contact.h"
int main(int argc, char* argv[])
{
//初始化通讯录
Contact cont;
InitContact(&cont);
int select = 1;
while (select)
{
printf("***********************\n");
printf("* [1]Add [2]Del *\n");
printf("* [3]Find [4]Modify *\n");
printf("* [5]Clear [6]Sort *\n");
printf("* [7]Show [0]Quit *\n");
printf("***********************\n");
printf("请选择:");
scanf("%d", &select);
if (select == QUIT)
{
break;
}
switch (select)
{
case ADD:
AddContact(&cont);
break;
case DEL:
DelContact(&cont);
break;
case FIND:
FindContact(&cont);
break;
case MODIFY:
ModifyContact(&cont);
break;
case CLEAR:
ClearContact(&cont);
break;
case SORT:
SortContact(&cont);
break;
case SHOW:
ShowContact(&cont);
break;
}
}
printf("再见");
return 0;
}
这篇关于通讯录Contact的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!