C语言实现——通讯录管理系统

#include 
#include 
#include 

#define MAX_NAME_LENGTH 20
#define MAX_PHONE_LENGTH 15
#define MAX_CONTACTS 100

struct Contact {
	char name[MAX_NAME_LENGTH];
	char phone[MAX_PHONE_LENGTH];
};

struct Contact contacts[MAX_CONTACTS];
int numContacts = 0;

void addContact() {
	if (numContacts >= MAX_CONTACTS) {
		printf("通讯录已满,无法添加联系人。\n");
		return;
	}

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

	printf("请输入联系人电话号码: ");
	scanf("%s", contacts[numContacts].phone);

	printf("联系人添加成功!\n");
	numContacts++;
}

void deleteContact() {
	char name[MAX_NAME_LENGTH];
	int found = 0;
	int i;

	printf("请输入你想要删除的联系人姓名: ");
	scanf("%s", name);

	for (i = 0; i < numContacts; i++) {
		if (strcmp(name, contacts[i].name) == 0) {
			found = 1;
			break;
		}
	}

	if (!found) {
		printf("对不起,通讯录中查无此人。\n");
		return;
	}

	for (i = i + 1; i < numContacts; i++) {
		strcpy(contacts[i - 1].name, contacts[i].name);
		strcpy(contacts[i - 1].phone, contacts[i].phone);
	}

	numContacts--;
	printf("联系人删除成功!\n");
}

void findContact() {
	char name[MAX_NAME_LENGTH];
	int found = 0;
	int i;

	printf("请输入你想要查找的联系人姓名:\n ");
	scanf("%s", name);

	for (i = 0; i < numContacts; i++) {
		if (strcmp(name, contacts[i].name) == 0) {
			found = 1;
			printf("姓名: %s\n手机号码: %s\n", contacts[i].name, contacts[i].phone);
			break;
		}
	}

	if (!found) {
		printf("对不清,通讯录中查无此人.\n");
	}
}

void printContacts() {
	int i;

	printf("通讯录是:\n");

	for (i = 0; i < numContacts; i++) {
		printf("%s\t%s\n", contacts[i].name, contacts[i].phone);
	}
}

int main() {
	int choice;

	while (1) {
		printf("\n");
		printf("#####################欢迎来到通讯录管理系统####################\n");
		printf("\n");
		printf("#####################1. 增加联系人####################\n");
		printf("#####################2. 删除联系人####################\n");
		printf("#####################3. 查找联系人####################\n");
		printf("#####################4. 打印通讯录####################\n");
		printf("#####################5. 退出      ####################\n");
		printf("\n");
		printf("您的选择是: ");
		scanf("%d", &choice);

		switch (choice) {
			case 1:
				addContact();
				break;
			case 2:
				deleteContact();
				break;
			case 3:
				findContact();
				break;
			case 4:
				printContacts();
				break;
			case 5:
				exit(0);
			default:
				printf("您的输入不正确\n");
				break;
		}
	}

	return 0;
}

你可能感兴趣的:(c语言,c++,算法)