通讯录(动态与静态,文件操作版)实现 w字 C语言进阶

举报
原来45 发表于 2022/03/22 19:34:46 2022/03/22
【摘要】 2. 静态实现首先我们要知道我们要实现的功能,首先还是增删查改,然后是显示成员,排序,最后是退出。2.1. 效果展示图2.2. 接口实现2.2.01. 首先是我们的框架int main(){ do { menu(); switch () { case: default: } } while (); return 0;}2.2.02. 要实现的接口2.2.03. 初始化2.2.0...

2. 静态实现

首先我们要知道我们要实现的功能,首先还是增删查改,然后是显示成员,排序,最后是退出。

2.1. 效果展示图

2.2. 接口实现

2.2.01. 首先是我们的框架

int main()
{
	do
	{
		menu();
 
		switch ()
		{
		case:
		default:
		}
	} while ();
	return 0;
}

2.2.02. 要实现的接口

2.2.03. 初始化

2.2.04. 增加联系人

2.2.05. 删除

2.2.06. 查找 

2.3. 源代码

test.c

#define _CRT_SECURE_NO_WARNINGS 1
 
#include"Contact.h"
 
 
void menu()
{
	printf("**************************************************************\n");
	printf("******************** 1.Add    2.Delet  ***********************\n");
	printf("******************** 3.Search 4.Modify ***********************\n");
	printf("******************** 5.Show   6.Sort   ***********************\n");
	printf("******************** 0.exitcontact     ***********************\n");
	printf("**************************************************************\n");
}
 
int main()
{
	int input=0;
	Contact con;
	char name[10] = { 0 };
	InitContact(&con);
	do
	{
		menu();
		printf("请输入你要选择的选项:\n");
		scanf("%d", &input);
		switch (input)
		{
		case Add:
			printf("增加通讯录成员:\n");
			AddContact(&con);
			break;
		case Delet:
			printf("删除通讯录成员:\n");
			printf("请输入要删除人员姓名:");
			char name[NAME_MAX] = { 0 };
			scanf("%s", name);
			DeletContact(&con, name);
			break;
		case Search:
			printf("查找通讯录成员:\n");
			printf("请输入要查找人员姓名:");
			scanf("%s", name);
			SearchContact(&con, name);
			break;
		case Modify:
			printf("更改通讯录成员信息:\n");
			printf("请输入要查找人员姓名:");
			scanf("%s", name);
			ModifyContact(&con, name);
			break;
		case Show:
			printf("显示通讯录成员:\n");
			ShowContact(&con);
			break;
		case Sort:
			printf("排序通讯录成员:\n");
			SortContact(&con);
			break;
		case exitcontact:
			//DestoryContact(&con);
			printf("退出通讯录:\n");
			break;
		default:
			printf("输入错误,请重新输入:");
			break;
		}
	} while (input);
	return 0;
}
 

Contact.h

#pragma once
 
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<stdlib.h>
 
#define CONTACT_MAX 1000
#define NAME_MAX 10
#define SEX_MAX 5
#define TELEP_MAX 13
#define ADDRESS_MAX 20
 
typedef struct Student
{
	char name[NAME_MAX];
	char sex[SEX_MAX];
	int age;
	char telep[TELEP_MAX];
	char address[ADDRESS_MAX];
}Student;
 
typedef struct Contact
{
	Student date[CONTACT_MAX];
	int sz;
}Contact;
 
enum contactmenu
{
	exitcontact,
    Add,
	Delet,
	Search,
	Modify,
	Show,
	Sort,
};
 
//初始化
void InitContact(Contact* pc);
//增加
void AddContact(Contact* pc);
//删除
void DeletContact(Contact* pc, char name[]);
//查找
void SearchContact(Contact* pc, char name[]);
//修改
void ModifyContact(Contact* pc, char name[]);
//显示
void ShowContact(Contact* pc);
//排序
void SortContact(Contact* pc);
//void DestoryContact(Contact* pc);
int FindContact(Contact* pc, char name[]);
 
 

Contact.c

#define _CRT_SECURE_NO_WARNINGS 1
 
#include"Contact.h"
 
void InitContact(Contact* pc)
{
	assert(pc);
	pc->sz = 0;
	memset(pc->date, 0, sizeof(pc->date));
}
 
void AddContact(Contact* pc)
{
	assert(pc);
	if (pc->sz == CONTACT_MAX)
	{
		printf("通讯录满了,无法添加\n");
		return;
	}
	printf("输入要添加联系人的姓名:");
	scanf("%s", pc->date[pc->sz].name);
	printf("输入要添加联系人的性别:");
	scanf("%s", pc->date[pc->sz].sex);
	printf("输入要添加联系人的年龄:");
	scanf("%d", &(pc->date[pc->sz].age));
	printf("输入要添加联系人的电话:");
	scanf("%s", pc->date[pc->sz].telep);
	printf("输入要添加联系人的地址:");
	scanf("%s", pc->date[pc->sz].address);
 
	pc->sz++;
	printf("添加联系人成功\n");
}
 
void ShowContact(Contact* pc)
{
	assert(pc);
	printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\t\n", "姓名", "性别", "年龄", "电话", "地址");
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n", 
			pc->date[i].name,pc->date[i].sex,pc->date[i].age,pc->date[i].telep,pc->date[i].address);
	}
}
 
int FindContact(Contact* pc,char name[])
{
	assert(pc);
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->date[i].name, name)==0)
		{
			return i;
		}
	}
	return -1;
}
 
void DeletContact(Contact* pc, char name[])
{
	assert(pc);
	if (pc->sz == 0)
	{
		printf("已经没有联系人,不可删除\n");
		return;
	}
	int i = FindContact(pc, name);
	if (i != -1)
	{
		int j = 0;
		for (j=i; j < pc->sz-1; j++)
		{
			pc->date[j] = pc->date[j + 1];
		}
		pc->sz--;
		printf("删除成功!\n");  
	}
	else
	{
		printf("查无此人\n");
	}
}
 
void SearchContact(Contact* pc, char name[])
{
	assert(pc);
	int ret=FindContact(pc, name);
	if (ret != -1)
	{
		printf("找到了!\n");
		printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n",
			pc->date[ret].name, pc->date[ret].sex, pc->date[ret].age, pc->date[ret].telep, pc->date[ret].address);
	}
	else
	{
		printf("没有找到\n");
	}
	
}
 
 
void ModifyContact(Contact* pc, char name[])
{
	assert(pc);
	int ret=FindContact(pc, name);
	if (ret == -1)
	{
		printf("查无此人\n");
	}
	else
	{
		printf("找到了!\n");
		printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n",
			pc->date[ret].name, pc->date[ret].sex, pc->date[ret].age, pc->date[ret].telep, pc->date[ret].address);
		printf("请修改:\n");
		printf("输入要添加联系人的姓名:");
		scanf("%s", pc->date[ret].name);
		printf("输入要添加联系人的性别:");
		scanf("%s", pc->date[ret].sex);
		printf("输入要添加联系人的年龄:");
		scanf("%d", &(pc->date[ret].age));
		printf("输入要添加联系人的电话:");
		scanf("%s", pc->date[ret].telep);
		printf("输入要添加联系人的地址:");
		scanf("%s", pc->date[ret].address);
		printf("修改成功!\n");
	}
}
 
int cmp_name(const void* e1, const void* e2)
{
	return strcmp(((Student*)e1)->name, ((Student*)e2)->name);
}
 
void SortContact(Contact* pc)
{
	assert(pc);
	qsort(pc->date, pc->sz, sizeof(pc->date[0]), cmp_name);
}

3. 动态版

test.c

#define _CRT_SECURE_NO_WARNINGS 1
 
#include"Contact.h"
 
 
void menu()
{
	printf("**************************************************************\n");
	printf("******************** 1.Add    2.Delet  ***********************\n");
	printf("******************** 3.Search 4.Modify ***********************\n");
	printf("******************** 5.Show   6.Sort   ***********************\n");
	printf("******************** 0.exitcontact     ***********************\n");
	printf("**************************************************************\n");
}
 
int main()
{
	int input = 0;
	Contact con;
	char name[10] = { 0 };
	InitContact(&con);
	do
	{
		menu();
		printf("请输入你要选择的选项:\n");
		scanf("%d", &input);
		switch (input)
		{
		case Add:
			printf("增加通讯录成员:\n");
			AddContact(&con);
			break;
		case Delet:
			printf("删除通讯录成员:\n");
			printf("请输入要删除人员姓名:");
			char name[NAME_MAX] = { 0 };
			scanf("%s", name);
			DeletContact(&con, name);
			break;
		case Search:
			printf("查找通讯录成员:\n");
			printf("请输入要查找人员姓名:");
			scanf("%s", name);
			SearchContact(&con, name);
			break;
		case Modify:
			printf("更改通讯录成员信息:\n");
			printf("请输入要查找人员姓名:");
			scanf("%s", name);
			ModifyContact(&con, name);
			break;
		case Show:
			printf("显示通讯录成员:\n");
			ShowContact(&con);
			break;
		case Sort:
			printf("排序通讯录成员:\n");
			SortContact(&con);
			break;
		case exitcontact:
			DestoryContact(&con);
			printf("退出通讯录:\n");
			break;
		default:
			printf("输入错误,请重新输入:");
			break;
		}
	} while (input);
	return 0;
}
 
 

Contact.h 

#define _CRT_SECURE_NO_WARNINGS 1
 
#include"Contact.h"
 
void InitContact(Contact* pc)
{
	assert(pc);
	Student* new = (Student*)malloc(DEFAULT_SIZE * sizeof(Student));
	if (new != NULL)
	{
		pc->date = new;
		pc->sz = 0;
		pc->capacity = DEFAULT_SIZE;
	}
	else
	{
		printf("InitContact:%s\n", strerror(errno));
		return;
	}
}
 
void Add_Capacity(Contact* pc)
{
	Student* new = (Student*)realloc(pc->date, sizeof(Student) * (pc->capacity + 2));
	if (new != NULL)
	{
		pc->date = new;
		pc->capacity += 2;
		printf("增容成功!\n");
	}
	else
	{
		printf("Add_Capccity():%s\n", strerror(errno));
		return;
	}
}
 
void AddContact(Contact* pc)
{
	assert(pc);
 
	if (pc->capacity == pc->sz)
	{
		Add_Capacity(pc);
	}
 
	printf("输入要添加联系人的姓名:");
	scanf("%s", pc->date[pc->sz].name);
	printf("输入要添加联系人的性别:");
	scanf("%s", pc->date[pc->sz].sex);
	printf("输入要添加联系人的年龄:");
	scanf("%d", &(pc->date[pc->sz].age));
	printf("输入要添加联系人的电话:");
	scanf("%s", pc->date[pc->sz].telep);
	printf("输入要添加联系人的地址:");
	scanf("%s", pc->date[pc->sz].address);
 
	pc->sz++;
	printf("添加联系人成功\n");
}
 
void ShowContact(Contact* pc)
{
	assert(pc);
	printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\t\n", "姓名", "性别", "年龄", "电话", "地址");
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n",
			pc->date[i].name, pc->date[i].sex, pc->date[i].age, pc->date[i].telep, pc->date[i].address);
	}
}
 
int FindContact(Contact* pc, char name[])
{
	assert(pc);
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->date[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}
 
void DeletContact(Contact* pc, char name[])
{
	assert(pc);
	if (pc->sz == 0)
	{
		printf("已经没有联系人,不可删除\n");
		return;
	}
	int i = FindContact(pc, name);
	if (i != -1)
	{
		int j = 0;
		for (j = i; j < pc->sz - 1; j++)
		{
			pc->date[j] = pc->date[j + 1];
		}
		pc->sz--;
		printf("删除成功!\n");
	}
	else
	{
		printf("查无此人\n");
	}
}
 
void SearchContact(Contact* pc, char name[])
{
	assert(pc);
	int ret = FindContact(pc, name);
	if (ret != -1)
	{
		printf("找到了!\n");
		printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n",
			pc->date[ret].name, pc->date[ret].sex, pc->date[ret].age, pc->date[ret].telep, pc->date[ret].address);
	}
	else
	{
		printf("没有找到\n");
	}
 
}
 
 
void ModifyContact(Contact* pc, char name[])
{
	assert(pc);
	int ret = FindContact(pc, name);
	if (ret == -1)
	{
		printf("查无此人\n");
	}
	else
	{
		printf("找到了!\n");
		printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n",
			pc->date[ret].name, pc->date[ret].sex, pc->date[ret].age, pc->date[ret].telep, pc->date[ret].address);
		printf("请修改:\n");
		printf("输入要添加联系人的姓名:");
		scanf("%s", pc->date[ret].name);
		printf("输入要添加联系人的性别:");
		scanf("%s", pc->date[ret].sex);
		printf("输入要添加联系人的年龄:");
		scanf("%d", &(pc->date[ret].age));
		printf("输入要添加联系人的电话:");
		scanf("%s", pc->date[ret].telep);
		printf("输入要添加联系人的地址:");
		scanf("%s", pc->date[ret].address);
		printf("修改成功!\n");
	}
}
 
int cmp_name(const void* e1, const void* e2)
{
	return strcmp(((Student*)e1)->name, ((Student*)e2)->name);
	// return strcmp(((Contact*)e1)->date->name, ((Student*)e2)->date->name);
}
 
void SortContact(Contact* pc)
{
	assert(pc);
 
	qsort(pc->date, pc->sz, sizeof(pc->date[0]), cmp_name);
}
 
void DestoryContact(Contact* pc)
{
	assert(pc);
	free(pc->date);
	pc->date = NULL;
	pc->capacity = 0;
	pc->sz = 0;
}

Contact.c

#define _CRT_SECURE_NO_WARNINGS 1
 
#include"Contact.h"
 
void InitContact(Contact* pc)
{
	assert(pc);
	Student* new = (Student*)malloc(DEFAULT_SIZE * sizeof(Student));
	if (new != NULL)
	{
		pc->date = new;
		pc->sz = 0;
		pc->capacity = DEFAULT_SIZE;
	}
	else
	{
		printf("InitContact:%s\n", strerror(errno));
		return;
	}
}
 
void Add_Capacity(Contact* pc)
{
	Student* new = (Student*)realloc(pc->date, sizeof(Student) * (pc->capacity + 2));
	if (new != NULL)
	{
		pc->date = new;
		pc->capacity += 2;
		printf("增容成功!\n");
	}
	else
	{
		printf("Add_Capccity():%s\n", strerror(errno));
		return;
	}
}
 
void AddContact(Contact* pc)
{
	assert(pc);
 
	if (pc->capacity == pc->sz)
	{
		Add_Capacity(pc);
	}
 
	printf("输入要添加联系人的姓名:");
	scanf("%s", pc->date[pc->sz].name);
	printf("输入要添加联系人的性别:");
	scanf("%s", pc->date[pc->sz].sex);
	printf("输入要添加联系人的年龄:");
	scanf("%d", &(pc->date[pc->sz].age));
	printf("输入要添加联系人的电话:");
	scanf("%s", pc->date[pc->sz].telep);
	printf("输入要添加联系人的地址:");
	scanf("%s", pc->date[pc->sz].address);
 
	pc->sz++;
	printf("添加联系人成功\n");
}
 
void ShowContact(Contact* pc)
{
	assert(pc);
	printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\t\n", "姓名", "性别", "年龄", "电话", "地址");
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n",
			pc->date[i].name, pc->date[i].sex, pc->date[i].age, pc->date[i].telep, pc->date[i].address);
	}
}
 
int FindContact(Contact* pc, char name[])
{
	assert(pc);
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->date[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}
 
void DeletContact(Contact* pc, char name[])
{
	assert(pc);
	if (pc->sz == 0)
	{
		printf("已经没有联系人,不可删除\n");
		return;
	}
	int i = FindContact(pc, name);
	if (i != -1)
	{
		int j = 0;
		for (j = i; j < pc->sz - 1; j++)
		{
			pc->date[j] = pc->date[j + 1];
		}
		pc->sz--;
		printf("删除成功!\n");
	}
	else
	{
		printf("查无此人\n");
	}
}
 
void SearchContact(Contact* pc, char name[])
{
	assert(pc);
	int ret = FindContact(pc, name);
	if (ret != -1)
	{
		printf("找到了!\n");
		printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n",
			pc->date[ret].name, pc->date[ret].sex, pc->date[ret].age, pc->date[ret].telep, pc->date[ret].address);
	}
	else
	{
		printf("没有找到\n");
	}
 
}
 
 
void ModifyContact(Contact* pc, char name[])
{
	assert(pc);
	int ret = FindContact(pc, name);
	if (ret == -1)
	{
		printf("查无此人\n");
	}
	else
	{
		printf("找到了!\n");
		printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n",
			pc->date[ret].name, pc->date[ret].sex, pc->date[ret].age, pc->date[ret].telep, pc->date[ret].address);
		printf("请修改:\n");
		printf("输入要添加联系人的姓名:");
		scanf("%s", pc->date[ret].name);
		printf("输入要添加联系人的性别:");
		scanf("%s", pc->date[ret].sex);
		printf("输入要添加联系人的年龄:");
		scanf("%d", &(pc->date[ret].age));
		printf("输入要添加联系人的电话:");
		scanf("%s", pc->date[ret].telep);
		printf("输入要添加联系人的地址:");
		scanf("%s", pc->date[ret].address);
		printf("修改成功!\n");
	}
}
 
int cmp_name(const void* e1, const void* e2)
{
	return strcmp(((Student*)e1)->name, ((Student*)e2)->name);
	// return strcmp(((Contact*)e1)->date->name, ((Student*)e2)->date->name);
}
 
void SortContact(Contact* pc)
{
	assert(pc);
 
	qsort(pc->date, pc->sz, sizeof(pc->date[0]), cmp_name);
}
 
void DestoryContact(Contact* pc)
{
	assert(pc);
	free(pc->date);
	pc->date = NULL;
	pc->capacity = 0;
	pc->sz = 0;
}

4. 文件操作版 

void InitContact(Contact* pc)
{
	assert(pc);
	Student* new = (Student*)malloc(DEFAULT_SIZE * sizeof(Student));
	if (new != NULL)
	{
		pc->date = new;
		pc->sz = 0;
		pc->capacity = DEFAULT_SIZE;
	}
	else
	{
		printf("InitContact:%s\n", strerror(errno));
		return;
	}
	FILE* pf = fopen("contact.txt", "rb");
	if (pf == NULL)
	{
		printf("InitContact::%s", strerror(errno));
		return;
	}
	Student buf = { 0 };
	while (fread(&buf, sizeof(Student), 1, pf))
	{
		Add_Capacity(pc);
 
		pc->date[pc->sz] = buf;
		pc->sz++;
	}
}

void SaveContact(Contact* pc)
{
	FILE* pf = fopen("contact.txt", "wb");
	if (pf == NULL)
	{
		printf("SaveContact::%s\n", strerror(errno));
		return 0;
	}
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		fwrite(pc->date + i, sizeof(Student), 1, pf);
	}
	fclose(pf);
	pf = NULL;
}

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。