C语言通讯录

150 篇文章 0 订阅


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 100      // 通讯录容量 100
#define NAMELEN 22 //姓名长度 考虑 \0 占用一个字节
#define NUMLEN 12  // 11位电话号码 + 1个 \0  一共 12

typedef struct c {
	// 联系人
	char name[NAMELEN];
	// 电话号码
	char phone[NUMLEN];
} Person;

Person contacts[N]; //能够存储 100个联系人

int totalContactCount = 0;

//接收用户输入的编号
int fno;

// 定义数据文件保存的位置
char * filePath = "/Users/stone/Desktop/contacts.data";

//****** 函数声明 ****************** stone ***
int validateInput(int num, int min, int max);
void doAdd();
void doDelete();
void doUpdate();
void doShowAll();
void doSearchByName();
void init();
void writeToFile();

int main(int argc, const char * argv[])
{
	init();
	printf("通讯录初始化完成\n");
	while (1) {

		printf("\n");
		printf("**************************\n");
		printf("****** 欢迎使用通讯录 ******\n");
		printf("****** 1.添加联系人  ******\n");
		printf("****** 2.删除联系人  ******\n");
		printf("****** 3.修改联系人  ******\n");
		printf("****** 4.查看所有人  ******\n");
		printf("****** 5.搜索联系人  ******\n");
		printf("****** 6.退出系统    ******\n");
		printf("**************************\n\n");

		printf("请选择1-6之间的功能编号:\n");
		scanf("%d", &fno);

		// 判断编号是否合法
		if (validateInput(fno, 1, 6)) {
			printf("请重新输入正确的编号\n");
			continue;
		}

		switch (fno) {
			//添加
			case 1:
				doAdd();
				break;
			//删除
			case 2:
				doDelete();
				break;
			//修改
			case 3:
				doUpdate();
				break;
			//显示所有联系人
			case 4:
				doShowAll();
				break;
			//查找
			case 5:
				doSearchByName();
				break;
			//退出
			case 6:
				return 0;
				break;
			default:
				break;
		}
	}
	return 0;
}
void init()
{
	FILE * fp = fopen(filePath, "r");
	if (fp != NULL) {
		fread(&totalContactCount, sizeof(totalContactCount), 1, fp);

		for (int i = 0; i < totalContactCount; i++) {
			fread(&contacts[i], sizeof(Person), 1, fp);
		}
	}
	else {
		//printf("open file failed!\n");
		fp = fopen(filePath, "wb");
		fwrite(&totalContactCount, sizeof(totalContactCount), 1, fp);
		printf("通讯录文件创建成功\n");
	}
	fclose(fp);
}
/**
    添加联系人
 */
void doAdd()
{
	printf("您选择的是添加联系人,请按提示操作:\n");

	printf("请输入联系人姓名:\n");
	scanf("%s", contacts[totalContactCount].name);

	printf("请输入联系人电话:\n");
	scanf("%s", contacts[totalContactCount].phone);

	int flag;
	printf("确认要添加数据吗? 1.确认 0.取消\n");
	scanf("%d", &flag);

	totalContactCount++;

	if (flag) {
		// 写入文件, totalContactCount +1
		writeToFile();
	}
}

void writeToFile()
{
	FILE * fp = fopen(filePath, "wb");
	if (fp != NULL) {
		fwrite(&totalContactCount, sizeof(totalContactCount), 1, fp);

		// 不是追加方式 , 而是 重新写一遍 全部内容??? 这文件大了 就完蛋咯
		for (int i = 0; i < totalContactCount; i++) {
			fwrite(&contacts[i], sizeof(Person), 1, fp);
		}
		printf("写文件数据更新成功\n");
	}
	else {
		printf("open file failed\n");
	}
	fclose(fp);
}

/**
    删除联系人
 */
void doDelete()
{
	for (int i = 0; i < totalContactCount; i++) {
		printf("编号:%d\t 联系人:%s --- 电话:%s\n", i + 1, contacts[i].name, contacts[i].phone);
	}
	printf("您选择的是删除联系人,请按提示操作:\n");
	int no;
	printf("请输入要删除的联系人的编号:\n");
	scanf("%d", &no);
	if (validateInput(no, 1, totalContactCount)) {
		return;
	}
	int flag;
	printf("删除联系人后将无法恢复, 您确认要删除吗? 1.确认 0.取消\n");
	scanf("%d", &flag);
	if (flag) {
		if (no == totalContactCount) {
			totalContactCount--;
		}
		else {
			for (int i = no; i < totalContactCount; i++) {
				contacts[i - 1] = contacts[i];
			}
			totalContactCount--;
		}
		// 同步更新到文件中
		writeToFile();
	}
}
/**
    修改联系人
 */
void doUpdate()
{
	for (int i = 0; i < totalContactCount; i++) {
		printf("编号:%d\t 联系人:%s --- 电话:%s\n", i + 1, contacts[i].name, contacts[i].phone);
	}

	printf("您选择的是修改联系人,请按提示操作:\n");
	int no;
	printf("请输入要修改的联系人的编号:\n");
	scanf("%d", &no);
	if (validateInput(no, 1, totalContactCount)) {
		return;
	}
	//    contacts[no-1].name = "kkk"; contacts[no-1].name 是数组名, 是常量值 不能直接赋值...
	// 使用 strcpy函数
	char nameTemp[NAMELEN];
	char phoneTemp[NUMLEN];
	printf("请输入新联系人姓名:\n");
	scanf("%s", nameTemp);

	printf("请输入新联系人电话:\n");
	scanf("%s", phoneTemp);

	int flag;
	printf("确认要覆盖数据吗? 1.确认 0.取消\n");
	scanf("%d", &flag);

	if (flag) {
		// 写入文件
		strcpy(contacts[no - 1].name, nameTemp);
		strcpy(contacts[no - 1].phone, phoneTemp);
		writeToFile();
	}
	for (int i = 0; i < totalContactCount; i++) {
		printf("编号:%d\t 联系人:%s --- 电话:%s\n", i + 1, contacts[i].name, contacts[i].phone);
	}
}
/**
    显示所有联系人
 */
void doShowAll()
{
	printf("您选择的是显示所有联系人,联系人数据如下:\n");
	if (totalContactCount == 0) {
		printf("通讯录还没有联系人\n");
	}
	else {
		for (int i = 0; i < totalContactCount; i++) {
			printf("编号:%d\t 联系人:%s --- 电话:%s\n", i + 1, contacts[i].name, contacts[i].phone);
		}
	}
}
/**
 * 搜索联系人
 */
void doSearchByName()
{
	printf("您选择的是搜索联系人,请按提示操作:\n");
	char name[NAMELEN];
	printf("请输入要查找的联系人\n");
	scanf("%s", name);
	int i;
	for (i = 0; i < totalContactCount; i++) {
		// strcmp 返回值 0 表示相等
		if (!strcmp(name, contacts[i].name)) {
			//找到了
			printf("编号:%d\t 联系人:%s --- 电话:%s\n", i + 1, contacts[i].name, contacts[i].phone);
			break;
		}
	}

	if (i == totalContactCount) {
		printf("查无此人\n");
	}
}

//判断 用户输入的num合法性
int validateInput(int num, int min, int max)
{
	if (num < min || num > max) {
		printf("非法的输入\t");
		return 1;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值