Address book (realized in pure C language)

I believe that everyone has an address book. Today I will take you to realize the simplest address book below. Through this article, I believe that you can have a better understanding of the C language.

Not much to say, let's put the implementation of the function first

#define  _CRT_SECURE_NO_WARNINGS 1
#include "Contact.h"


int CheakCapacity(Contact* ps);

void LoadContact(Contact* ps)
{
	FILE* pf = fopen("Contact.dat", "rb");
	if (pf == NULL)
	{
		perror("LoadContact");
		return;
	}
	PeoInfo tmp = { 0 };
	while (fread(&tmp,sizeof(PeoInfo),1,pf))
	{
		CheakCapacity(ps);
		ps->date[ps->size] = tmp;
		ps->size++;
	}

	fclose(pf);
	pf = NULL;

	
}

void InitContact(Contact* ps)
{
	assert(ps);
	ps->date = NULL;
	ps->size = ps->capacity = 0;
	LoadContact(ps);
}

int CheakCapacity(Contact* ps)
{
	assert(ps);
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		PeoInfo* tmp = (PeoInfo*)realloc(ps->date, newcapacity * sizeof(PeoInfo));
		if (tmp == NULL)
		{
			perror("CheakCapacity");
			return 0;
		}
		else
		{
			ps->date = tmp;
			ps->capacity = newcapacity;
			printf("增容成功\n");
			return 1;
		}
	}
	return 1;
}

void AddContact(Contact* ps)
{
	assert(ps);
	if (CheakCapacity(ps) == 0)
	{
		return;
	}

	printf("请输入增加的姓名:>");
	scanf("%s", ps->date[ps->size].name);
	printf("请输入增加的年龄:>");
	scanf("%d", &ps->date[ps->size].age);
	printf("请输入增加的性别:>");
	scanf("%s", ps->date[ps->size].sex);
	printf("请输入增加的电话:>");
	scanf("%s", ps->date[ps->size].tele);
	printf("请输入增加的地址:>");
	scanf("%s", ps->date[ps->size].addr);
	
	ps->size++;
	printf("增加成功\n");
}

void ShowContact(Contact* ps)
{
	assert(ps);

	printf("%-10s\t%-4s\t%-5s\t%-12s\t%-30s\n", "名字", "年龄", "性别", "电话", "地址");

	for (int i =0; i<ps->size; i++)
	{
		printf("%-10s\t%-4d\t%-5s\t%-12s\t%-30s\n",
			ps->date[i].name,
			ps->date[i].age,
			ps->date[i].sex,
			ps->date[i].tele,
			ps->date[i].addr
			);
	}
}

int FindByName(const Contact* ps,char name[])
{
	int i = 0;
	for (i =0; i<ps->size; i++)
	{
		if (strcmp(ps->date[i].name,name)==0)
		{
			return i;
		}
	}
	return -1;
}

void DeleteContact(Contact* ps)
{
	assert(ps);
	if (ps->size == 0)
	{
		printf("通讯录为空,无法删除\n");
		return;
	}

	char name[20] = { 0 };
	printf("请输入你要删除的姓名:>");
	scanf("%s", name);

	int ret = FindByName(ps, name);
	if (ret == -1)
	{
		printf("要删除的人不存在\n");
		return;
	}

	for (int i =ret; i<ps->size; i++)
	{
		ps->date[i] = ps->date[i + 1];
	}

	ps->size--;
	printf("删除成功\n");
}

void SearchContact(Contact* ps)
{
	assert(ps);
	char name[20] = { 0 };
	printf("请输入你要删除的姓名:>");
	scanf("%s", name);

	int pos = FindByName(ps, name);
	if (pos == -1)
	{
		printf("要查找的人不存在\n");
		return;
	}

	printf("%-10s\t%-4d\t%-5s\t%-12s\t%-30s\n",
		ps->date[pos].name,
		ps->date[pos].age,
		ps->date[pos].sex,
		ps->date[pos].tele,
		ps->date[pos].addr
	);
}

void ModifyContact(Contact* ps)
{
	assert(ps);
	char name[20] = { 0 };
	printf("请输入你要修改的姓名:>");
	scanf("%s", name);

	int pos = FindByName(ps, name);
	if (pos == -1)
	{
		printf("要修改的人不存在\n");
		return;
	}

	printf("请输入修改的姓名:>");
	scanf("%s", ps->date[pos].name);
	printf("请输入修改的年龄:>");
	scanf("%d", &ps->date[pos].age);
	printf("请输入修改的性别:>");
	scanf("%s", ps->date[pos].sex);
	printf("请输入修改的电话:>");
	scanf("%s", ps->date[pos].tele);
	printf("请输入修改的地址:>");
	scanf("%s", ps->date[pos].addr);
}

void SortContact(Contact* ps)
{
	int i = 0;
	int j = 0;
	for (i=0; i<ps->size-1; i++)
	{
		for (j =0; j<ps->size-1-i; j++)
		{
			if (strcmp(ps->date[j].name, ps->date[j+1].name) > 0)
			{
				PeoInfo tmp = ps->date[j];
				ps->date[j] = ps->date[j + 1];
				ps->date[j + 1] = tmp;
			}
		}
	}
	printf("排序成功\n");
}

void DestoryContact(Contact* ps)
{
	free(ps->date);
	ps->date = NULL;
	ps->capacity = ps->size = 0;
}

void SaveContact(Contact* ps)
{
	FILE* pf = fopen("Contact.dat", "wb");
	if (pf == NULL)
	{
		perror("SaveContact");
		return;
	}
	for (int i =0; i<ps->size; i++)
	{
		fwrite(ps->date+ i, sizeof(PeoInfo), 1, pf);
	}

	fclose(pf);
	pf = NULL;
}

#define  _CRT_SECURE_NO_WARNINGS 1
#include "Contact.h"

void menu()
{
	printf("**************************** *************\n");
	printf("*********** 1.add     2.del **************\n");
	printf("*********** 3.search  4.modify ***********\n");
	printf("*********** 5.show    6.sort *************\n");
	printf("*********** 0.exit  ********* ************\n");

}

void Text()
{
	int input = 0;
	Contact con;
	InitContact(&con);
	do 
	{
		menu();
		printf("请输入你的选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			AddContact(&con);
			break;
		case DEL:
			DeleteContact(&con);
			break;
		case SEARCH:
			SearchContact(&con);
			break;
		case MODIFY:
			ModifyContact(&con);
			break;
		case SHOW:
			ShowContact(&con);
			break;
		case SORT:
			SortContact(&con);
			break;
		case EXIT:
			SaveContact(&con);
			DestoryContact(&con);
			printf("退出通讯录成功\n");
			break;
		default:
			printf("你选择的有误,请重新输入\n");
			break;

		}
	} while (input);
}

int main()
{
	Text();
	return 0;
}

 Does it scare you to see this? ? Don't be afraid, follow my ideas, you can also realize it, I will take you to realize each function step by step

  •  We want to realize this function. First, let’s look at the following code. First, we should choose first. Here I choose the do while statement. To realize this function, we use the menu function to print the menu. In fact, it is very simple, and I use the printf function to realize it.
void Text()
{
	int input = 0;
	Contact con;
	InitContact(&con);
	do 
	{
		menu();
		printf("请输入你的选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			AddContact(&con);
			break;
		case DEL:
			DeleteContact(&con);
			break;
		case SEARCH:
			SearchContact(&con);
			break;
		case MODIFY:
			ModifyContact(&con);
			break;
		case SHOW:
			ShowContact(&con);
			break;
		case SORT:
			SortContact(&con);
			break;
		case EXIT:
			SaveContact(&con);
			DestoryContact(&con);
			printf("退出通讯录成功\n");
			break;
		default:
			printf("你选择的有误,请重新输入\n");
			break;

		}
	} while (input);
}

void menu()
{
	printf("**************************** *************\n");
	printf("*********** 1.add     2.del **************\n");
	printf("*********** 3.search  4.modify ***********\n");
	printf("*********** 5.show    6.sort *************\n");
	printf("*********** 0.exit  ********* ************\n");

}

  • Let's directly implement the basic functions of the address book. I first use struct to define a person's basic information, and then use struct to contain the person's basic information, but I also add size and capcacity. Size is used to record how many people are stored, and capacity is used to indicate how much space there is.
    typedef struct PeoInfo
    {
    	char name[MAX_NAME];
    	int age;
    	char sex[MAX_SEX];
    	char tele[MAX_TELE];
    	char addr[MAX_ADDR];
    }PeoInfo;
    
    typedef struct Contact
    {
    	PeoInfo *date;
    	int size;
    	int capacity;
    }Contact;

  •  Don’t forget to initialize the address book when we first use it. Here, I set the space pointed to by ps->date to NULL, and initialize the size and capacity to 0. Because we want to create a dynamic address book, we specially use the structure pointer date to design.
  • Let's implement an address book. Let's imagine a scenario first. If your mobile phone is turned off, will the information in the address book still exist after restarting, so we also need to implement such a function.
  • I used file operations here. I first created the tmp temporary variable of the structure, and I used fread to operate it. If the return value of freaed is not 0, we will copy the data to tmp.
  • while (fread(&tmp,sizeof(PeoInfo),1,pf))
    	{
    		CheakCapacity(ps);
    		ps->date[ps->size] = tmp;
    		ps->size++;
    	}

    Here is the full code for this function

  • void LoadContact(Contact* ps)
    {
    	FILE* pf = fopen("Contact.dat", "rb");
    	if (pf == NULL)
    	{
    		perror("LoadContact");
    		return;
    	}
    	PeoInfo tmp = { 0 };
    	while (fread(&tmp,sizeof(PeoInfo),1,pf))
    	{
    		CheakCapacity(ps);
    		ps->date[ps->size] = tmp;
    		ps->size++;
    	}
    
    	fclose(pf);
    	pf = NULL;
    
    	
    }
    
    void InitContact(Contact* ps)
    {
    	assert(ps);
    	ps->date = NULL;
    	ps->size = ps->capacity = 0;
    	LoadContact(ps);
    }

  •  Then let's look at the second function, adding people's information. When we add information at the beginning, we must think about expanding the capacity if the space is full. Therefore, I judge whether to expand the capacity at the beginning, because the size and capacity are both 0 at the beginning, so the capacity must be expanded from the beginning. I malloc a space, and if it is full, I will expand it twice. Finally, the return value is used to judge whether the expansion is successful.
int CheakCapacity(Contact* ps)
{
	assert(ps);
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		PeoInfo* tmp = (PeoInfo*)realloc(ps->date, newcapacity * sizeof(PeoInfo));
		if (tmp == NULL)
		{
			perror("CheakCapacity");
			return 0;
		}
		else
		{
			ps->date = tmp;
			ps->capacity = newcapacity;
			printf("增容成功\n");
			return 1;
		}
	}
	return 1;
}
  • Then, implement the add function, which is actually very simple. Our initial size is 0, so whenever we add a piece of information, ps->size needs ++, and ps->date points to the space of the person information. After ps->date [ps->size], add the information we want to add, and the function of our add function is completed.

void AddContact(Contact* ps)
{
	assert(ps);
	if (CheakCapacity(ps) == 0)
	{
		return;
	}

	printf("请输入增加的姓名:>");
	scanf("%s", ps->date[ps->size].name);
	printf("请输入增加的年龄:>");
	scanf("%d", &ps->date[ps->size].age);
	printf("请输入增加的性别:>");
	scanf("%s", ps->date[ps->size].sex);
	printf("请输入增加的电话:>");
	scanf("%s", ps->date[ps->size].tele);
	printf("请输入增加的地址:>");
	scanf("%s", ps->date[ps->size].addr);
	
	ps->size++;
	printf("增加成功\n");
}


  •  For the second deletion function, my idea is to create an array first, and then use the array to compare with the names in the address book to see if they are equal. Then return the subscript to delete
int FindByName(const Contact* ps,char name[])
{
	int i = 0;
	for (i =0; i<ps->size; i++)
	{
		if (strcmp(ps->date[i].name,name)==0)
		{
			return i;
		}
	}
	return -1;
}
  • The last operation to delete is to move from the back to the front, and then ps->size-- that's it.
void DeleteContact(Contact* ps)
{
	assert(ps);
	if (ps->size == 0)
	{
		printf("通讯录为空,无法删除\n");
		return;
	}

	char name[20] = { 0 };
	printf("请输入你要删除的姓名:>");
	scanf("%s", name);

	int ret = FindByName(ps, name);
	if (ret == -1)
	{
		printf("要删除的人不存在\n");
		return;
	}

	for (int i =ret; i<ps->size; i++)
	{
		ps->date[i] = ps->date[i + 1];
	}

	ps->size--;
	printf("删除成功\n");
}


  •  The implementation of the third search function is similar to the delete operation, but we are a search function, so we finally print out the information we want to find directly by printf. Here I also use the multiplexing of the FindByName function.

void SearchContact(Contact* ps)
{
	assert(ps);
	char name[20] = { 0 };
	printf("请输入你要删除的姓名:>");
	scanf("%s", name);

	int pos = FindByName(ps, name);
	if (pos == -1)
	{
		printf("要查找的人不存在\n");
		return;
	}

	printf("%-10s\t%-4d\t%-5s\t%-12s\t%-30s\n",
		ps->date[pos].name,
		ps->date[pos].age,
		ps->date[pos].sex,
		ps->date[pos].tele,
		ps->date[pos].addr
	);
}


  •  The fourth modification function, because we also need to find the subscript to be modified, so I used the reuse of functions, we only need to re-enter the value we want to modify in the subscript to be modified.
void ModifyContact(Contact* ps)
{
	assert(ps);
	char name[20] = { 0 };
	printf("请输入你要修改的姓名:>");
	scanf("%s", name);

	int pos = FindByName(ps, name);
	if (pos == -1)
	{
		printf("要修改的人不存在\n");
		return;
	}

	printf("请输入修改的姓名:>");
	scanf("%s", ps->date[pos].name);
	printf("请输入修改的年龄:>");
	scanf("%d", &ps->date[pos].age);
	printf("请输入修改的性别:>");
	scanf("%s", ps->date[pos].sex);
	printf("请输入修改的电话:>");
	scanf("%s", ps->date[pos].tele);
	printf("请输入修改的地址:>");
	scanf("%s", ps->date[pos].addr);
}


 Did you find that the implementation of many functions is actually similar, so don't be afraid, and then look back.

  •  The fifth function: we want to display the information, we just use the for loop to traverse it once, but we use left alignment to look better, and the length can be realized according to you. The final effect is as follows.

void ShowContact(Contact* ps)
{
	assert(ps);

	printf("%-10s\t%-4s\t%-5s\t%-12s\t%-30s\n", "名字", "年龄", "性别", "电话", "地址");

	for (int i =0; i<ps->size; i++)
	{
		printf("%-10s\t%-4d\t%-5s\t%-12s\t%-30s\n",
			ps->date[i].name,
			ps->date[i].age,
			ps->date[i].sex,
			ps->date[i].tele,
			ps->date[i].addr
			);
	}
}


  •  The sixth function: We want to sort the size of the name. Here I directly use bubble sorting, which can directly sort out the size of the name. It's just that the time complexity here is O(N^2), and the efficiency is very low. It can also be realized by quick sorting, and the efficiency can be higher
void SortContact(Contact* ps)
{
	int i = 0;
	int j = 0;
	for (i=0; i<ps->size-1; i++)
	{
		for (j =0; j<ps->size-1-i; j++)
		{
			if (strcmp(ps->date[j].name, ps->date[j+1].name) > 0)
			{
				PeoInfo tmp = ps->date[j];
				ps->date[j] = ps->date[j + 1];
				ps->date[j + 1] = tmp;
			}
		}
	}
	printf("排序成功\n");
}


  •  Because we implement the address book dynamically, I used malloc. When I finally exit, I also need to free the opened space. I used a DestoryContact function to achieve it.
  • void DestoryContact(Contact* ps)
    {
    	free(ps->date);
    	ps->date = NULL;
    	ps->capacity = ps->size = 0;
    }


  • The last function: Have you ever thought about such a problem, when we enter information, if we exit, the information will still be saved when we open it next time? ? ? After learning, I found that file operations can be used to achieve it.
  • I used fopen to open a binary file Contact.dat, and used the for loop fwrite to save the existing information in the file stream (that is, this file), and because at the beginning of initialization, the information of the file should be recorded, so that we realized the preservation of information.
void SaveContact(Contact* ps)
{
	FILE* pf = fopen("Contact.dat", "wb");
	if (pf == NULL)
	{
		perror("SaveContact");
		return;
	}
	for (int i =0; i<ps->size; i++)
	{
		fwrite(ps->date+ i, sizeof(PeoInfo), 1, pf);
	}

	fclose(pf);
	pf = NULL;
}

Guess you like

Origin blog.csdn.net/m0_72165281/article/details/131745871