Implementation of address book in c language

Article catalog

catalogue

Article catalog

1, Introduction to address book function

2, Code part

1. Related parts of main function

2. Address book header file

3. Specific function implementation documents

summary

1, Introduction to address book function

        Write a simple address book in c language. The address book has five functions: addition, deletion, query, modification and display. Dynamic space storage. Subsequent file operations can be added to save information.

2, Code part

1. Related parts of main function

        Design the interface of page function and each function function.

#define _CRT_SECURE_NO_WARNINGS
#include"contact.h"

void menu()
{
	system("cls");
	printf("\n\n\n");
	printf("\t\t\t\t\t**************************************\n");
	printf("\t\t\t\t\t************Address book management system************\n");
	printf("\t\t\t\t\t======================================\n");
	printf("\t\t\t\t\t*          1.Enter contact information          *\n");
	printf("\t\t\t\t\t*          2.Delete contact information          *\n");
	printf("\t\t\t\t\t*          3.Find contact information          *\n");
	printf("\t\t\t\t\t*          4.Modify contact information          *\n");
	printf("\t\t\t\t\t*          5.Show contact information          *\n");
	printf("\t\t\t\t\t*          0.Exit the system                *\n");
	printf("\t\t\t\t\t======================================\n");
	printf("\t\t\t\t\t**************************************\n");
	printf("\t\t\t\t\t          Please select(0-5):");
}
enum Option
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SHOW
};
int main()
{
	int input = 0;
	struct contact con;
	InitCon(&con);
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			AddCon(&con);
			break; 
		case DEL:
			DelCon(&con);
			break;
		case SEARCH:
			SeaCon(&con);
			break;
		case MODIFY:
			ModCon(&con);
			break;
		case SHOW:
			ShowCon(&con);
			break;
		case EXIT:
			printf("sign out!");
			break;
		default:
			break;
		}
	} while (input);
	return 0;
}

2. Address book header file

        Each person's information storage type can use the structure type, and then create a structure type member, including the structure of person information, the current number and capacity of address books.

#pragma once

#define NAME_MAX 20
#define SEX_MAX 10
#define TEL_MAX 20
#define ADR_MAX 20
#define DEFAULT_CAP 3
#include<stdio.h>
#include<windows.h>
#include<string.h>
struct Peo
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char tel[TEL_MAX];
	char adr[ADR_MAX];
};
struct contact
{
	struct Peo* data;
	int nums;
	int capacity;
};
void InitCon(struct contact*);
void AddCon(struct contact*);
void ShowCon(struct contact*);
void DelCon(struct contact*);
void SeaCon(struct contact*);
void ModCon(struct contact*);

3. Specific function implementation documents

        In fact, each function is written in a similar way, which is to access the content in the structure. But each function also has some different details. In the addition function, realloc is used to expand the open space. The rule is how to add two spaces once the amount of information is equal to the capacity of the address book. In the deletion function, you need to find the deletion location, then overwrite the rear storage contents one by one, and then reduce the number of information by one to complete the deletion. As for the search, modification and display, as long as the circular access to the structure content is reasonably used, the function can be well realized.

#define _CRT_SECURE_NO_WARNINGS
#include"contact.h"
void InitCon(struct contact* pc)
{
	pc->nums = 0;
	pc->capacity = DEFAULT_CAP;
	pc->data = (struct Peo*)malloc(DEFAULT_CAP * sizeof(struct Peo));
}
void AddCon(struct contact* pc)
{
	if (pc->nums == pc->capacity)
	{
		struct Peo* ptr = realloc(pc->data, (pc->capacity + 2) * sizeof(struct Peo));
		if (ptr != NULL)
		{
			pc->data = ptr;
			pc->capacity += 2;
		}
		else
		{
			return;
		}
	}
	printf("Please enter the name of the person to add:");
	scanf("%s", pc->data[pc->nums].name);
	printf("Please enter the age of the person you want to increase:");
	scanf("%d", &pc->data[pc->nums].age);
	printf("Please enter the gender of the person you want to add:");
	scanf("%s", pc->data[pc->nums].sex);
	printf("Please enter the phone number of the person you want to add:");
	scanf("%s", pc->data[pc->nums].tel);
	printf("Please enter the address of the person to be added:");
	scanf("%s", pc->data[pc->nums].adr);
	pc->nums++;
	printf("Added successfully\n");
	system("pause");
}

int FindByName(struct contact* pc, char* name)
{
	int i = 0;
	for (i = 0; i < pc->nums; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}
void DelCon(struct contact* pc)
{
	if (pc->nums == 0)
	{
		printf("The address book is empty and cannot be deleted!\n");
		system("pause");
		return;
	}
	char name[NAME_MAX] = { 0 };
	printf("Enter the name of the person to delete:");
	scanf("%s", name);
	int pos = FindByName(pc,name);
	if (pos == -1)
	{
		printf("The person cannot be found!\n");
		system("pause");
		return;
	}
	else
	{
		int i = 0;
		for (i = pos; i < pc->nums; i++)
		{
			pc->data[i] = pc->data[i + 1];
		}
		pc->nums--;
		printf("Delete succeeded!\n");
		system("pause");
	}
}

void ShowCon(struct contact* pc)
{
	int i = 0;
	printf("%20s\t%5s\t%10s\t%20s\t%20s\n", "name", "age", "sex", "tel", "adr");
	for (i = 0; i < pc->nums; i++)
	{
		printf("%20s\t%5d\t%10s\t%20s\t%20s\n",
			pc->data[i].name, pc->data[i].age, pc->data[i].sex,
			pc->data[i].tel, pc->data[i].adr);
	}
	system("pause");
}

void SeaCon(struct contact* pc)
{
	if (pc->nums == 0)
	{
		printf("Address book is empty!\n");
		system("pause");
		return;
	}
	char name[NAME_MAX] = { 0 };
	printf("Please enter the name of the person you want to find:");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("The person cannot be found!\n");
		system("pause");
		return;
	}
	else
	{
		printf("eureka! The information is as follows:\n");
		printf("%20s\t%5s\t%10s\t%20s\t%20s\n", "name", "age", "sex", "tel", "adr");
		printf("%20s\t%5d\t%10s\t%20s\t%20s\n",
			pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex,
			pc->data[pos].tel, pc->data[pos].adr);
		system("pause");
		return;
	}
}

void ModCon(struct contact* pc)
{
	if (pc->nums == 0)
	{
		printf("Address book is empty!\n");
		system("pause");
		return;
	}
	printf("Please enter the name of the person to modify:");
	char name[NAME_MAX] = { 0 };
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("The person cannot be found!\n");
		system("pause");
		return;
	}
	else
	{
		printf("Enter the modified name");
		scanf("%s", pc->data[pos].name);
		printf("Enter the modified age");
		scanf("%d", &pc->data[pos].age);
		printf("Enter the modified gender");
		scanf("%s", pc->data[pos].sex);
		printf("Enter the modified phone number");
		scanf("%s", pc->data[pos].tel);
		printf("Enter the modified address");
		scanf("%s", pc->data[pos].adr);
		system("pause");
		return;
	}
}

summary

        Using c language to practice the function of address book can better help you get familiar with the use of structure and dynamic space development function. Later, you can add file operation to save the address book information in txt file.

Tags: C

Posted on Thu, 23 Sep 2021 04:01:03 -0400 by XeroXer