简单C语言通讯录的实现(非动态内存管理)

本文将介绍一个基于C语言的命令行通讯录管理系统。该系统支持联系人信息的增删改查、排序和清空等核心功能,采用模块化设计便于维护和扩展。

一、程序结构

程序由三个文件组成:

  1. contact.h数据结构和函数声明

  2. contact.c - 函数具体实现

  3. main.c - 程序入口和主循环

二、核心数据结构

typedef struct PeoInf {
	char name[20];
	char gender[7];
	int age;
	char phone[12];
	char address[50];
} PeoInf;

typedef struct Contact {
	PeoInf data[1000];
	int size;
}Contact;

定义两个结构体,用于存储和管理联系人信息:

PeoInf 结构体(人员信息):

包含5个成员:

address[50]:50字符的地址

phone[12]:12字符的电话号码

age:整型的年龄

gender[7]:7字符的性别

name[20]:20字符的姓名

Contact 结构体(通讯录):

包含:

size:记录当前存储的联系人数量

data[1000]:可存储1000个PeoInf结构体的数组

三、功能实现详解

1.初始化通讯录

// Initialize the contact list
void InitContact(Contact* con) {
	assert(con != NULL);
	con->data[0] = (PeoInf){ "","",0,"","" };
	con->size = 0;
}

2.增加联系人信息

// Add a new contact
void AddContact(Contact* con) {
	assert(con != NULL);
	if (con->size >= 1000) {
		printf("Contact list is full, cannot add more contacts.\n");// Check if the contact list is full
	}
	else {
		printf("Enter name: ");	
		scanf("%s", con->data[con->size].name);
		printf("Enter gender:");
		scanf("%s", con->data[con->size].gender);
		printf("Enter age: ");
		scanf("%d", &con->data[con->size].age);
		printf("Enter phone: ");
		scanf("%s", con->data[con->size].phone);
		printf("Enter address: ");
		scanf("%s", con->data[con->size].address);
		con->size++;
		printf("Contact added successfully!\n");	
	}
}

3.显示所有联系人

// Show all contacts
void ShowContact(const Contact* con) {
	assert(con != NULL);
	if (con->size <= 0) {
		printf("No contacts available.\n");
	}
	else {
		printf("Name\tGender\tAge\tPhone\t\tAddress\n");
		for (int i = 0; i < con->size; i++) {
			printf("%s\t%s\t%d\t%s\t\t%s\n", con->data[i].name, con->data[i].gender,
				con->data[i].age, con->data[i].phone, con->data[i].address);
		}
	}
	
}

4.通过名字查找联系人

//Find a contact by name
void SearchContact(const Contact* con) {
	assert(con != NULL);
	if (con->size <= 0) {
		printf("No contacts available to search.\n");
		return;
	}
	char name[20];
	printf("Enter the name of the contact to search: ");
	scanf("%s", name);
	int found = 0;
	for (int i = 0; i < con->size; i++) {
		if (strcmp(con->data[i].name, name) == 0) {
			printf("Found contact: %s\t%s\t%d\t%s\t\t%s\n", con->data[i].name,
				con->data[i].gender, con->data[i].age, con->data[i].phone, con->data[i].address);
			found = 1;
			break;
		}
	}
	if (!found) {
		printf("Contact with name '%s' not found.\n", name);
	}
}

5.删除指定联系人

// Delete a contact by name
void DelContact(Contact* con) {
	assert(con != NULL);
	if (con->size <= 0) {
		printf("No contacts available to delete.\n");
		return;
	}
	char name[20];
	printf("Enter the name of the contact to delete ");
	scanf("%s", name);
	for (int i = 0; i < con->size; i++) {
		if (strcmp(con->data[i].name, name) == 0) {
			for (int j = i; j < con->size - 1; j++) {
				con->data[j] = con->data[j + 1]; // Shift contacts left
			}
			con->size--;
			printf("Contact '%s' deleted successfully.\n", name);
		}
	}
}

6.修改指定联系人

//modify a contact by name
void ModifyContact(Contact* con) {
	assert(con != NULL);
	if (con->size <= 0) {
		printf("No contacts available to modify.\n");
		return;
	}
	char name[20];
	printf("Enter the name of the contact to modify: ");
	scanf("%s", name);
	for (int i = 0; i < con->size; i++) {
		if (strcmp(con->data[i].name, name) == 0) {
			printf("Enter new name: ");
			scanf("%s", con->data[i].name);
			printf("Enter new gender:");
			scanf("%s", con->data[i].gender);
			printf("Enter new age: ");
			scanf("%d", &con->data[i].age);
			printf("Enter new phone: ");
			scanf("%s", con->data[i].phone);
			printf("Enter new address: ");
			scanf("%s", con->data[i].address);
			printf("Contact modify successfully!\n");
		}
	}
}

7.排序现有联系人

// Sort contacts by name
void SortContact(Contact* con) {
	assert(con != NULL);
	if (con->size <= 0) {
		printf("No contacts available to sort.\n");
		return;
	}
	for (int i = 0; i < con->size - 1; i++) {
		for (int j = i + 1; j < con->size; j++) {
			if (strcmp(con->data[i].name, con->data[j].name) > 0) {
				PeoInf temp = con->data[i];
				con->data[i] = con->data[j];
				con->data[j] = temp;
			}
		}
	}
	printf("Contacts sorted successfully!\n");
}

8.清除所有联系人

// Clear all contacts
void ClearContact(Contact* con) {
	assert(con != NULL);
	if (con->size <= 0) {
		printf("No contacts available to clear.\n");
		return;
	}
	con->size = 0; // Reset size to 0
	for (int i = 0; i < 1000; i++) {
		con->data[i] = (PeoInf){"", "", 0, "", ""}; // Reset each contact
	}
	printf("All contacts cleared successfully!\n");
}

四、主程序逻辑

enum OPtions {
	EXIT,
	ADD,
	SHOW,
	SEARCH,
	DEL,
	MODIFY,
	SORT,
	CLEAR
};

int main() {
	int input;
	Contact con;
	InitContact(&con);
	while (1) {
		PrintMenu();
		printf("Enter your choice: ");
		scanf("%d", &input);
		switch (input) {
			case ADD:
				AddContact(&con);
				break;
			case SHOW:
				ShowContact(&con);
				break;
			case SEARCH:
				SearchContact(&con);
				break;
			case DEL:
				DelContact(&con);
				break;
			case MODIFY:
				ModifyContact(&con);
				break;
			case SORT:
				SortContact(&con);
				break;
			case CLEAR:
				ClearContact(&con);
				break;
			case EXIT:
				printf("Exiting the contact management system.\n");
				return 0;
			default:
				printf("Invalid choice, please try again.\n");
		}
	}

	return 0;
}

五、使用示例和代码展示

简单C语言通讯录的实现(非动态内存管理)_第1张图片

contact.h

#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 
#include 
#include

typedef struct PeoInf {
	char name[20];
	char gender[7];
	int age;
	char phone[12];
	char address[50];
} PeoInf;

typedef struct Contact {
	PeoInf data[1000];
	int size;
}Contact;

void InitContact(Contact* con);

void AddContact(Contact* con);

void ShowContact(const Contact* con);

void DelContact(Contact* con);

void SearchContact(const Contact* con);

void ModifyContact(Contact* con);

void SortContact(Contact* con);

void ClearContact(Contact* con);

 main.c

#include"contact.h"

enum OPtions {
	EXIT,
	ADD,
	SHOW,
	SEARCH,
	DEL,
	MODIFY,
	SORT,
	CLEAR
};

int main() {
	int input;
	Contact con;
	InitContact(&con);
	while (1) {
		PrintMenu();
		printf("Enter your choice: ");
		scanf("%d", &input);
		switch (input) {
			case ADD:
				AddContact(&con);
				break;
			case SHOW:
				ShowContact(&con);
				break;
			case SEARCH:
				SearchContact(&con);
				break;
			case DEL:
				DelContact(&con);
				break;
			case MODIFY:
				ModifyContact(&con);
				break;
			case SORT:
				SortContact(&con);
				break;
			case CLEAR:
				ClearContact(&con);
				break;
			case EXIT:
				printf("Exiting the contact management system.\n");
				return 0;
			default:
				printf("Invalid choice, please try again.\n");
		}
	}

	return 0;
}

contact.c

#include"contact.h"

//Print the mnue of the contact management system
void PrintMenu() {
	printf("1. Add Contact\n");
	printf("2. Show Contacts\n");
	printf("3. Search Contact\n");
	printf("4. Delete Contact\n");
	printf("5. Modify Contact\n");
	printf("6. Sort Contacts\n");
	printf("7. Clear Contacts\n");
	printf("0. Exit\n");
}

// Initialize the contact list
void InitContact(Contact* con) {
	assert(con != NULL);
	con->data[0] = (PeoInf){ "","",0,"","" };
	con->size = 0;
}

// Add a new contact
void AddContact(Contact* con) {
	assert(con != NULL);
	if (con->size >= 1000) {
		printf("Contact list is full, cannot add more contacts.\n");// Check if the contact list is full
	}
	else {
		printf("Enter name: ");	
		scanf("%s", con->data[con->size].name);
		printf("Enter gender:");
		scanf("%s", con->data[con->size].gender);
		printf("Enter age: ");
		scanf("%d", &con->data[con->size].age);
		printf("Enter phone: ");
		scanf("%s", con->data[con->size].phone);
		printf("Enter address: ");
		scanf("%s", con->data[con->size].address);
		con->size++;
		printf("Contact added successfully!\n");	
	}
}

// Show all contacts
void ShowContact(const Contact* con) {
	assert(con != NULL);
	if (con->size <= 0) {
		printf("No contacts available.\n");
	}
	else {
		printf("Name\tGender\tAge\tPhone\t\tAddress\n");
		for (int i = 0; i < con->size; i++) {
			printf("%s\t%s\t%d\t%s\t\t%s\n", con->data[i].name, con->data[i].gender,
				con->data[i].age, con->data[i].phone, con->data[i].address);
		}
	}
	
}

//Find a contact by name
void SearchContact(const Contact* con) {
	assert(con != NULL);
	if (con->size <= 0) {
		printf("No contacts available to search.\n");
		return;
	}
	char name[20];
	printf("Enter the name of the contact to search: ");
	scanf("%s", name);
	int found = 0;
	for (int i = 0; i < con->size; i++) {
		if (strcmp(con->data[i].name, name) == 0) {
			printf("Found contact: %s\t%s\t%d\t%s\t\t%s\n", con->data[i].name,
				con->data[i].gender, con->data[i].age, con->data[i].phone, con->data[i].address);
			found = 1;
			break;
		}
	}
	if (!found) {
		printf("Contact with name '%s' not found.\n", name);
	}
}

// Delete a contact by name
void DelContact(Contact* con) {
	assert(con != NULL);
	if (con->size <= 0) {
		printf("No contacts available to delete.\n");
		return;
	}
	char name[20];
	printf("Enter the name of the contact to delete ");
	scanf("%s", name);
	for (int i = 0; i < con->size; i++) {
		if (strcmp(con->data[i].name, name) == 0) {
			for (int j = i; j < con->size - 1; j++) {
				con->data[j] = con->data[j + 1]; // Shift contacts left
			}
			con->size--;
			printf("Contact '%s' deleted successfully.\n", name);
		}
	}
}

//modify a contact by name
void ModifyContact(Contact* con) {
	assert(con != NULL);
	if (con->size <= 0) {
		printf("No contacts available to modify.\n");
		return;
	}
	char name[20];
	printf("Enter the name of the contact to modify: ");
	scanf("%s", name);
	for (int i = 0; i < con->size; i++) {
		if (strcmp(con->data[i].name, name) == 0) {
			printf("Enter new name: ");
			scanf("%s", con->data[i].name);
			printf("Enter new gender:");
			scanf("%s", con->data[i].gender);
			printf("Enter new age: ");
			scanf("%d", &con->data[i].age);
			printf("Enter new phone: ");
			scanf("%s", con->data[i].phone);
			printf("Enter new address: ");
			scanf("%s", con->data[i].address);
			printf("Contact modify successfully!\n");
		}
	}
}

// Sort contacts by name
void SortContact(Contact* con) {
	assert(con != NULL);
	if (con->size <= 0) {
		printf("No contacts available to sort.\n");
		return;
	}
	for (int i = 0; i < con->size - 1; i++) {
		for (int j = i + 1; j < con->size; j++) {
			if (strcmp(con->data[i].name, con->data[j].name) > 0) {
				PeoInf temp = con->data[i];
				con->data[i] = con->data[j];
				con->data[j] = temp;
			}
		}
	}
	printf("Contacts sorted successfully!\n");
}

// Clear all contacts
void ClearContact(Contact* con) {
	assert(con != NULL);
	if (con->size <= 0) {
		printf("No contacts available to clear.\n");
		return;
	}
	con->size = 0; // Reset size to 0
	for (int i = 0; i < 1000; i++) {
		con->data[i] = (PeoInf){"", "", 0, "", ""}; // Reset each contact
	}
	printf("All contacts cleared successfully!\n");
}

六、改进方向

数据持久化:添加文件存储功能

动态内存:改用动态数组扩展容量

多字段搜索:支持电话/地址等搜索

七、总结

整个代码的编写过程可以帮助对模块化的理解,可以加深对C语言中指针、结构体的理解。下一篇文章我回将上面的代码改进。请期待我的下一篇文章。

你可能感兴趣的:(c语言,数据结构,开发语言)