一、静态版本
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
void menu()
{
printf("*****************************\n");
printf("*****************************\n");
printf("*** 1.添加 2.删除 ***\n");
printf("*** 3.显示 4.修改 ***\n");
printf("*** 5.查找 0.退出 ***\n");
printf("*****************************\n");
printf("*****************************\n");
}
enum option
{
EXIT,
ADD,
DELETE,
SHOW,
MODIFY,
SEARCH
};
int main()
{
int input = 0;
struct Contact con;
Initcontact(&con);
do {
menu();
printf("请输入:");
scanf("%d", &input);
switch (input)
{
case ADD:
Addcontact(&con);
break;
case DELETE:
Delecontact(&con);
break;
case SHOW:
Showcontact(&con);
break;
case MODIFY:
Modicontact(&con);
break;
case SEARCH:
Searcontact(&con);
break;
case EXIT:
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
#define MAX 1000
void Initcontact(struct Contact* pc)
{
pc->sz = 0;
memset(pc->data, 0, sizeof(pc->data));
}
void Addcontact(struct Contact* pc)
{
if (MAX == pc->sz)
{
printf("通讯录已满\n");
return;
}
else
{
printf("请输入姓名:");
scanf("%s", pc->data[pc->sz].name);
printf("请输入年龄:");
scanf("%d", &(pc->data[pc->sz].age));
printf("请输入性别:");
scanf("%s", pc->data[pc->sz].sex);
printf("请输入电话号码:");
scanf("%s", pc->data[pc->sz].tele);
printf("请输入地址:");
scanf("%s", pc->data[pc->sz].addr);
}
pc->sz++;
printf("添加成功\n");
printf("\n");
}
void Showcontact(struct Contact* pc)
{
printf("%-15s\t%-5s\t%-5s\t%-15s\t%-20s\n", "姓名", "年龄", "性别", "电话号码", "地址");
int i = 0;
for (i = 0; i < pc->sz; i++)
{
printf("%-15s\t%-5d\t%-5s\t%-15s\t%-20s\n",
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
printf("\n");
}
int Throucontact(const struct Contact* pc,char* name)
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name,name)==0)
{
return i;
}
}
return -1;
}
void Delecontact(struct Contact* pc)
{
printf("请输入要删除联系人的姓名:");
char name[NAME_MAX] = {0};
scanf("%s", name);
int ret = Throucontact(pc, name);
if (0 == pc->sz)
{
printf("当前无联系人,无法删除\n");
}
else if (ret == -1)
{
printf("通讯录中无此联系人\n");
}
else
{
int i = 0;
for (i = ret; i < pc->sz - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->sz--;
printf("删除成功\n");
printf("\n");
}
}
void Searcontact(struct Contact* pc)
{
printf("请输入要查找联系人的姓名:");
char name[NAME_MAX] = { 0 };
scanf("%s", name);
int ret = Throucontact(pc, name);
if (ret == -1)
{
printf("通讯录中无此联系人\n");
}
else
{
printf("%-15s\t%-5s\t%-5s\t%-15s\t%-20s\n", "姓名", "年龄", "性别", "电话号码", "地址");
printf("%-15s\t%-5d\t%-5s\t%-15s\t%-20s\n",
pc->data[ret].name,
pc->data[ret].age,
pc->data[ret].sex,
pc->data[ret].tele,
pc->data[ret].addr);
}
printf("\n");
}
void Modicontact(struct Contact* pc)
{
printf("请输入要修改联系人的姓名:");
char name[NAME_MAX] = { 0 };
scanf("%s", name);
int ret = Throucontact(pc, name);
if (ret == -1)
{
printf("通讯录中无此联系人\n");
}
else
{
printf("请输入修改后的姓名:");
scanf("%s", pc->data[ret].name);
printf("请输入修改后的年龄:");
scanf("%d", &(pc->data[ret].age));
printf("请输入修改后的性别:");
scanf("%s", pc->data[ret].sex);
printf("请输入修改后的电话号码:");
scanf("%s", pc->data[ret].tele);
printf("请输入修改后的地址:");
scanf("%s", pc->data[ret].addr);
}
printf("修改成功\n");
printf("\n");
}
contact.h
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#define MAX 1000
#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 15
#define ADDR_MAX 30
void Initcontact(struct Contact* pc);
struct PeoInfo
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char tele[TELE_MAX];
char addr[ADDR_MAX];
};
struct Contact
{
struct PeoInfo data[MAX];
int sz;
};
//?
void Addcontact(struct Contact* pc);
void Showcontact(struct Contact* pc);
void Delecontact(struct Contact* pc);
void Searcontact(struct Contact* pc);
void Modicontact(struct Contact* pc);
二、动态版本
#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
void menu()
{
printf("*****************************\n");
printf("*****************************\n");
printf("*** 1.添加 2.删除 ***\n");
printf("*** 3.显示 4.修改 ***\n");
printf("*** 5.查找 0.退出 ***\n");
printf("*****************************\n");
printf("*****************************\n");
}
enum option
{
EXIT,
ADD,
DELETE,
SHOW,
MODIFY,
SEARCH
};
int main()
{
int input = 0;
struct Contact con;
Initcontact(&con);
do {
menu();
printf("请输入:");
scanf("%d", &input);
switch (input)
{
case ADD:
Addcontact(&con);
break;
case DELETE:
Delecontact(&con);
break;
case SHOW:
Showcontact(&con);
break;
case MODIFY:
Modicontact(&con);
break;
case SEARCH:
Searcontact(&con);
break;
case EXIT:
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
#define MAX 1000
#define DEFAULT_SZ 3
void Initcontact(struct Contact* pc)
{
pc->sz = 0;
pc->data=(struct PeoInfo*)malloc(DEFAULT_SZ*sizeof(struct PeoInfo));
pc->capacity = DEFAULT_SZ;
}
void Addcontact(struct Contact* pc)
{
if (pc->sz == pc->capacity)
{
struct PeoInfo* ptr = (struct PeoInfo*)realloc(pc->data, (pc->capacity + 2) * sizeof(struct PeoInfo));
if (ptr != NULL)
{
pc->data = ptr;
pc->capacity += 2;
printf("增容成功\n");
}
else
{
return;
}
}
else
{
printf("请输入姓名:");
scanf("%s", pc->data[pc->sz].name);
printf("请输入年龄:");
scanf("%d", &(pc->data[pc->sz].age));
printf("请输入性别:");
scanf("%s", pc->data[pc->sz].sex);
printf("请输入电话号码:");
scanf("%s", pc->data[pc->sz].tele);
printf("请输入地址:");
scanf("%s", pc->data[pc->sz].addr);
}
pc->sz++;
printf("添加成功\n");
printf("\n");
}
void Showcontact(struct Contact* pc)
{
printf("%-15s\t%-5s\t%-5s\t%-15s\t%-20s\n", "姓名", "年龄", "性别", "电话号码", "地址");
int i = 0;
for (i = 0; i < pc->sz; i++)
{
printf("%-15s\t%-5d\t%-5s\t%-15s\t%-20s\n",
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
printf("\n");
}
void DestroyContact(struct Contact* pc)
{
free(pc->data);
pc->data = NULL;
pc->capacity = 0;
pc->sz = 0;
}
int Throucontact(const struct Contact* pc, char* name)
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
void Delecontact(struct Contact* pc)
{
printf("请输入要删除联系人的姓名:");
char name[NAME_MAX] = { 0 };
scanf("%s", name);
int ret = Throucontact(pc, name);
if (0 == pc->sz)
{
printf("当前无联系人,无法删除\n");
}
else if (ret == -1)
{
printf("通讯录中无此联系人\n");
}
else
{
int i = 0;
for (i = ret; i < pc->sz - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->sz--;
printf("删除成功\n");
printf("\n");
}
}
void Searcontact(struct Contact* pc)
{
printf("请输入要查找联系人的姓名:");
char name[NAME_MAX] = { 0 };
scanf("%s", name);
int ret = Throucontact(pc, name);
if (ret == -1)
{
printf("通讯录中无此联系人\n");
}
else
{
printf("%-15s\t%-5s\t%-5s\t%-15s\t%-20s\n", "姓名", "年龄", "性别", "电话号码", "地址");
printf("%-15s\t%-5d\t%-5s\t%-15s\t%-20s\n",
pc->data[ret].name,
pc->data[ret].age,
pc->data[ret].sex,
pc->data[ret].tele,
pc->data[ret].addr);
}
printf("\n");
}
void Modicontact(struct Contact* pc)
{
printf("请输入要修改联系人的姓名:");
char name[NAME_MAX] = { 0 };
scanf("%s", name);
int ret = Throucontact(pc, name);
if (ret == -1)
{
printf("通讯录中无此联系人\n");
}
else
{
printf("请输入修改后的姓名:");
scanf("%s", pc->data[ret].name);
printf("请输入修改后的年龄:");
scanf("%d", &(pc->data[ret].age));
printf("请输入修改后的性别:");
scanf("%s", pc->data[ret].sex);
printf("请输入修改后的电话号码:");
scanf("%s", pc->data[ret].tele);
printf("请输入修改后的地址:");
scanf("%s", pc->data[ret].addr);
}
printf("修改成功\n");
printf("\n");
}
contact.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1000
#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 15
#define ADDR_MAX 30
void Initcontact(struct Contact* pc);
struct PeoInfo
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char tele[TELE_MAX];
char addr[ADDR_MAX];
};
struct Contact
{
struct PeoInfo data[MAX];
int sz;
int capacity;
};
//添加联系人
void Addcontact(struct Contact* pc);
void Showcontact(struct Contact* pc);
void Delecontact(struct Contact* pc);
void Searcontact(struct Contact* pc);
void Modicontact(struct Contact* pc);
|