C语言通讯录小程序

#include<stdio.h>
#include<stdlib.h>
#define MAX_PERSON_INFO_SIZE 300
	//实现一个通讯录程序,完成联系人信息的存储
	//1、新增 
	//2、删除
	//3、修改记录
	//4、查找记录
	//5、打印全部记录
	//6、排序记录
	//7、清空全部记录
	//管理
	//1、把基本信息抽象并描述出来(结构体)
	//2、需要管理很多数据,就需要组织起来(数据结构)
	typedef struct PersonInfo {
		char name[1024];
		char phone[1024];
	}PersonInfo;
	typedef struct AddressBook {
		PersonInfo persons[MAX_PERSON_INFO_SIZE];
		int size;
	}AddressBook;
	AddressBook g_address_book;
	void Init() { 
		g_address_book.size = 0;
		for (int i = 0; i < MAX_PERSON_INFO_SIZE; ++i) {
			g_address_book.persons[i].name[0] = '\0';
			g_address_book.persons[i].phone[0] = '\0';

		}
	}
	int Menu() {
		printf("*****************************\n");
		printf("   1、新增联系人\n");
		printf("   2、删除联系人\n");
		printf("   3、查找联系人\n");
		printf("   4、修改联系人\n");
		printf("   5、打印全部联系人\n");
		printf("   6、排序联系人\n");
		printf("   7、清空联系人\n");
		printf("   0、退出\n");
		printf("*****************************\n");
		printf("请输入您的选择:");
		int choice = 0;
		scanf("%d", &choice);
		return choice;
	}
	void Empty() {
		//这个函数啥也不干,就是用来凑数的
	}
	void AddPersonInfo() {
		printf("新增联系人\n");
		if (g_address_book.size >= MAX_PERSON_INFO_SIZE) {
			printf("新增联系人失败!\n");

		}
		printf("请输入联系人姓名: ");
		//必须获取到一个指针,修改的内容是一个预期的内容
		PersonInfo* person_info = &g_address_book.persons[g_address_book.size];
		scanf("%s", person_info->name);
		printf("请输入联系人电话:");
		scanf("%s", person_info->phone);
		++g_address_book.size;
		printf("新增联系人成功!\n");
	}
	void DelPersonInfo() {
		printf("删除联系人\n");
		if (g_address_book.size <= 0) {
			printf("删除失败!通讯录为空!\n");
			return;
		}
		printf("请输入要删除的序号:");
		int id = 0;
		scanf("%d", &id);
		if (id < 0 || id >= g_address_book.size) {
			printf("删除失败!输入的序号有误!\n");
			return;
		}
		g_address_book.persons[id] =
			g_address_book.persons[g_address_book.size - 1];
		--g_address_book.size;
		printf("删除联系人成功!\n");
	}
	void FindPersonInfo() {
		printf("查找联系人\n");
		if (g_address_book.size <= 0) {
			printf("查找失败,通讯录为空!\n");
			return;
		}
		//根据姓名找电话
		printf("请输入要查找的姓名: ");
		char name[1024] = { 0 };
		scanf("%s", name);
		for (int i = 0; i < g_address_book.size; ++i) {
			PersonInfo* info = &g_address_book.persons[i];
			if (strcmp(info->name, name) == 0) {
				printf("[%d] %s\t%s\n", i, info->name, info->phone);
			}
			break;
		}
		printf("查找联系人成功!\n");
	}
	void UpdatePersonInfo() {
		printf("更新联系人\n");
		if (g_address_book.size <= 0) {
			printf("修改失败,通讯录为空!\n");
		}
		printf("请输入要修改的序号:");
		int id = 0;
		scanf("%d", &id);
		if (id < 0 || id >= g_address_book.size) {
			printf("修改失败,输入的序号有误!\n");
			return;
		}
		PersonInfo* info = &g_address_book.persons[id];
		printf("请输入新的姓名:(%s)\n",info->name);
		char name[1024] = { 0 };
		scanf("%s", name);
		if (strcmp(name, "*") != 0) {
			strcpy(info->name, name);
		}
		char phone[1024] = { 0 };
		printf("请输入新的电话:(%s)\n", info->phone);
		scanf("%s", phone);
		if (strcmp(phone, "*") != 0) {
			strcpy(info->phone, phone);
		}
		printf("更新联系人成功!\n");
	}
	void PrintAllPersonInfo() {
		printf("打印全部联系人\n");
		for (int i = 0; i < g_address_book.size; ++i) {
			PersonInfo* info = &g_address_book.persons[i];
			printf("[%d] %s\t%s\n", i, info->name, info->phone);
		}
		printf("共打印了 %d 条数据!\n,", g_address_book.size);
		printf("打印全部联系人成功\n");
	}
	void SortPersonInfo() {
		char Sort_Name[1024] = { 0 };
		char Sort_Phone_Number[1024] = { 0 };
		int bound, size;
		int i = 0;
		for (i = 0; i < g_address_book.size; i++) {
			for (bound = 0; bound < g_address_book.size; bound++) {
				for (size = bound; size < g_address_book.size - 1; size++) {
					if (strcmp(g_address_book.persons[size].name, g_address_book.persons[size + 1].name) > 0) {
						strcpy(Sort_Name, g_address_book.persons[size].name);
						strcpy(g_address_book.persons[size].name, g_address_book.persons[size + 1].name); 					
						strcpy(g_address_book.persons[size + 1].name, Sort_Name); 					//copy 电话号码 				
						strcpy(Sort_Phone_Number,g_address_book.persons[size].phone);
						strcpy(g_address_book.persons[size].phone,g_address_book.persons[size+1].phone); 				
						strcpy(g_address_book.persons[size+1].phone, Sort_Phone_Number);
					} 			
				} 		
			} 	
		} 

		printf("排序所有联系人:\n"); 
		printf("排序成功!\n");

	}

	
	void ClearAllPersonInfo() {
		printf("清空全部数据\n");
		printf("您真的要清空全部数据吗?Y/N\n");
		char choice[1024] = { 0 };
		scanf("%s", choice);
		if (strcmp(choice,"Y") == 0) {
			g_address_book.size = 0;
			printf("清空全部数据成功!\n");
		}
		else {
			printf("清空操作取消!\n");
		}
	}
	typedef void(*Func)();
int main() {
	Func arr[] = {
		Empty,
		AddPersonInfo,
		DelPersonInfo,
		FindPersonInfo,
		UpdatePersonInfo,
		PrintAllPersonInfo,
		SortPersonInfo,
		ClearAllPersonInfo
	};
	Init();
	while (1) {
		int choice = Menu();
		if (choice<0 || choice >sizeof(arr) / sizeof(arr[0])) {
			printf("您的输入有误,请您重新输入!\n");
			continue;
		}
		if (choice == 0) {
			printf("退出游戏!\n");
			break;
		}
		arr[choice]();
	}
	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的用C语言模拟小型通讯录的程序示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 100 struct Contact { char name[20]; // 联系人姓名 char phone[12]; // 联系人电话 }; struct Contact contacts[MAX]; // 最多存储100个联系人 int count = 0; // 当前联系人数量 void add_contact() { if (count >= MAX) { printf("通讯录已满,无法添加新联系人!\n"); return; } struct Contact new_contact; printf("请输入联系人姓名:"); scanf("%s", new_contact.name); printf("请输入联系人电话:"); scanf("%s", new_contact.phone); contacts[count++] = new_contact; printf("联系人已添加!\n"); } void list_contacts() { if (count == 0) { printf("通讯录为空!\n"); return; } printf("联系人列表:\n"); for (int i = 0; i < count; i++) { printf("%d. %s\t%s\n", i+1, contacts[i].name, contacts[i].phone); } } void search_contact() { if (count == 0) { printf("通讯录为空!\n"); return; } char name[20]; printf("请输入要查找的联系人姓名:"); scanf("%s", name); for (int i = 0; i < count; i++) { if (strcmp(name, contacts[i].name) == 0) { printf("%s\t%s\n", contacts[i].name, contacts[i].phone); return; } } printf("未找到联系人 %s!\n", name); } void delete_contact() { if (count == 0) { printf("通讯录为空!\n"); return; } char name[20]; printf("请输入要删除的联系人姓名:"); scanf("%s", name); for (int i = 0; i < count; i++) { if (strcmp(name, contacts[i].name) == 0) { for (int j = i; j < count-1; j++) { contacts[j] = contacts[j+1]; } count--; printf("联系人 %s 已删除!\n", name); return; } } printf("未找到联系人 %s!\n", name); } int main() { int choice = 0; while (1) { printf("\n"); printf("请选择操作:\n"); printf("1. 添加联系人\n"); printf("2. 显示联系人列表\n"); printf("3. 查找联系人\n"); printf("4. 删除联系人\n"); printf("5. 退出\n"); scanf("%d", &choice); switch(choice) { case 1: add_contact(); break; case 2: list_contacts(); break; case 3: search_contact(); break; case 4: delete_contact(); break; case 5: printf("谢谢使用!\n"); exit(0); default: printf("无效的选择!\n"); } } return 0; } ``` 这个程序使用了结构体来存储联系人的信息,可以添加、显示、查找和删除联系人。您可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值