C語言通訊錄系統——C語言單向鏈表實現


實現的通訊錄功能有:查看通訊錄、添加聯系人、刪除聯系人、查詢聯系人、保存並退出。

通過txt文件保存和讀取通訊錄數據。

 

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

void showMenu();
void initL();
void addPerson();
void displayPersons();
void deletePerson();
void saveList();
void searchPerson();

struct person_node {
	char name[10];
	char tel_num[20];
	char company_or_school[20];
	char sex[5];
	person_node* next;
};

person_node* L = (person_node *)malloc(sizeof(person_node));

int main()
{
	initL();

	int k;
	while (1)
	{
		system("cls");
		showMenu();
		scanf("%d", &k);
		if (k == 1)
		{
			system("cls");
			displayPersons();
		}
		else if (k == 2)
		{
			system("cls");
			addPerson();
		}
		else if (k == 3)
		{
			system("cls");
			deletePerson();
		}
		else if (k == 4)
		{
			system("cls");
			searchPerson();
		}
		else if (k == 0)
		{
			saveList();
			exit(0);
		}
	}

	system("pause");
	return 0;
}

void showMenu()
{
	printf("------------------------------------------------------\n");
	printf("\t\t\t通訊錄系統\n\n");
	printf("\t\t  1.查看通訊錄\n");
	printf("\t\t  2.添加聯系人\n");
	printf("\t\t  3.刪除聯系人\n");
	printf("\t\t  4.查詢聯系人\n");
	printf("\t\t  0.保存並退出\n");
	printf("------------------------------------------------------\n");
}

void addPerson()
{
	int k = 1;

	while (k == 1)
	{
		printf("-------------------新建聯系人--------------------------------\n");
		person_node* new_person = (person_node *)malloc(sizeof(person_node));
		printf("請輸入聯系人姓名:");
		scanf("%s", new_person->name);
		printf("請輸入聯系人性別:");
		scanf("%s", new_person->sex);
		printf("請輸入聯系人電話:");
		scanf("%s", new_person->tel_num);
		printf("請輸入聯系人單位:");
		scanf("%s", new_person->company_or_school);
		printf("------------------------------------------------------\n");

		person_node* ptr = L;
		while (ptr->next != NULL) //ptr指向鏈表L末尾結點
			ptr = ptr->next;

		ptr->next = new_person;
		new_person->next = NULL;

		printf("...輸入'0'返回菜單...輸入'1'繼續添加...\n");
		scanf("%d", &k);
		if (k == 0)
			return;
	}
}

void displayPersons()
{
	person_node* ptr = L->next;
	if (ptr == NULL)
		printf("...暫未添加任何聯系人...\n");
	else
	{
		while (ptr != NULL)
		{
			printf("->姓名:%s", ptr->name);
			printf("\t性別:%s", ptr->sex);
			printf("\t電話:%s", ptr->tel_num);
			printf("\t學校或公司:%s<-", ptr->company_or_school);
			printf("\n");
			ptr = ptr->next;
		}
	}
	printf("...輸入'0'返回菜單...");
	int k = 10;
	while (k != 0)
	{
		scanf("%d", &k);
	}
	return;
}

void initL()
{
	L->next = NULL;

	FILE *fp;
	if ((fp = fopen("list_file.txt", "r")) == NULL)
	{
		printf("...打開聯系人文件時出錯...\n");
		return;
	}

	person_node* tail_ptr = L;
	char name[10], tel_num[20], company_or_school[20], sex[5];
	while (!feof(fp))
	{
		fscanf(fp, "%s%s%s%s", name, sex, tel_num, company_or_school);
		person_node* new_node = (person_node *)malloc(sizeof(person_node));
		strcpy(new_node->name, name);
		strcpy(new_node->sex, sex);
		strcpy(new_node->tel_num, tel_num);
		strcpy(new_node->company_or_school, company_or_school);
		tail_ptr->next = new_node;
		new_node->next = NULL;
		tail_ptr = new_node;
		getc(fp);
	}
	fclose(fp);
}

void deletePerson()
{
	int k = 1;
	while (k == 1)
	{
		printf("-------------------刪除聯系人--------------------------------\n");
		person_node* ptr = L->next;
		person_node* pre = L;
		if (ptr == NULL)
			printf("...通訊錄中暫無聯系人...\n");
		else
		{
			printf("請輸入刪除聯系人的姓名:");
			char del_name[10];
			scanf("%s", del_name);
			while (ptr != NULL)
			{
				if (strcmp(ptr->name, del_name) == 0)
				{
					printf("...刪除聯系人條目...\n");
					printf("->姓名:%s", ptr->name);
					printf("\t性別:%s", ptr->sex);
					printf("\t電話:%s", ptr->tel_num);
					printf("\t學校或公司:%s<-", ptr->company_or_school);

					printf("\n確認刪除嗎?y/n\n");
					char input = '0';
					while (input != 'y' && input != 'n')
					{
						scanf("%c", &input);
					}
					if (input == 'n')
					{
						ptr = ptr->next;
						pre = pre->next;
						continue;
					}
					else if (input == 'y')
					{
						pre->next = ptr->next;
						free(ptr);
						ptr = pre->next;
						continue;
					}
				}
				ptr = ptr->next;
				pre = pre->next;
			}
		}
		printf("------------------------------------------------------\n");
		printf("...輸入'0'返回菜單...輸入'1'繼續刪除...\n");
		scanf("%d", &k);
		if (k == 0)
			return;
	}
}

void saveList()
{
	if (L->next == NULL)
		return;

	FILE *fp;
	if ((fp = fopen("list_file.txt", "w")) == NULL)
		printf("...打開聯系人文件時出錯...\n");

	person_node* ptr = L->next;
	while (ptr != NULL)
	{
		fprintf(fp, "\n%s %s %s %s", ptr->name, ptr->sex, ptr->tel_num, ptr->company_or_school);
		ptr = ptr->next;
	}
	fclose(fp);
}

void searchPerson()
{
	int k = 1;
	while (k == 1)
	{
		printf("-------------------查詢聯系人--------------------------------\n");
		char search_name[10];
		printf("請輸入查詢姓名:");
		scanf("%s", search_name);
		person_node* ptr = L->next;
		if (ptr == NULL)
			printf("...通訊錄中暫無聯系人...\n");
		else
		{
			int count = 0;
			while (ptr != NULL)
			{
				if (strcmp(ptr->name, search_name) == 0)
				{
					count++;
					printf("...查詢到聯系人條目%d...\n", count);
					printf("->姓名:%s", ptr->name);
					printf("\t性別:%s", ptr->sex);
					printf("\t電話:%s", ptr->tel_num);
					printf("\t學校或公司:%s<-\n", ptr->company_or_school);
				}
				ptr = ptr->next;
			}
		}
		printf("------------------------------------------------------\n");
		printf("...輸入'0'返回菜單...輸入'1'繼續查詢...\n");
		scanf("%d", &k);
		if (k == 0)
			return;
	}
}

 截圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM