[c language] address book (static)

Xiao Zhang has just finished learning structure, enumeration, combining relevant knowledge, practicing practice, and writing an address book!


Features of the address book

1. The address book can store 100 pieces of information
2. The content of the information is name, gender, age, phone number, address
3. Support adding contacts
4. Support deleting contacts
5. Support searching contacts
6. Support modifying contacts
7. Support Sort contacts
8. Print data

file type

test.c (test address book-related functions)
contanct.c (realization of address book functions)
contanct.h (declaration)

A person's information is defined in the structure

struct peoinfo {
char name[20];Name
char sex[5];gender
int age;age
char tel[12];Telephone
char addr[20];};address

为了方便代码的可读性,我们在宏里面定义每个字符串数组的长度,修改如下

struct peoinfo {
char name[MAX_NAME];Name
char sex[MAX_SEX];gender
int age;age
char tel[MAX_TEL];Telephone
char addr[MAX_ADDR];address};

宏定义如下

#define MAX_NAME 20
#define MAX_SEX 6
#define MAX_TEL 12
#define MAX_ADDR 20
#define MAX 1000

Contact record structure

typedef struct pp{ struct peoinfo arr[MAX]; int sz;}pp; Use typedef to rename the address book structure type to pp for easy operation



insert image description here

menu function menu

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

}

Select function function test()

void test()
{
    
    pp pro;//定义一个通讯录
Initcontanct(&pro);//通讯录初始化,具体在下面
int input;
	do {
    
    
		menu();
		scanf_s("%d", &input);
		switch (input)
		{
    
    
		case 1:
			Addcontanct(&pro);
			break;
		case 2:
			Delcontanct(&pro);
			break;
		case 3:
			findcontanct(&pro);
			break;
		case 4:
			modifycontanct(&pro);
			break;
		case 5:
			Sortcontanct(&pro);
			break;
		case 6:
			Printcontanct(&pro);
			break;
		case 7:
			
			break;
		case 0:
			printf("退出通讯录\n");
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
}

The case1, 2, 3, and 4 in the switch statement are not suitable for corresponding functions. In order to improve the readability of the code, we can define an enumeration type
enum opion
{ EXIT, // its value is 0, and the following are 1, 2, and 3 in sequence , 4, 5, 6 ADD, DEL, SEARCH, MODIFY, SORT, PRINT };







test() function modification

void test()
{
    
    pp pro;
Initcontanct(&pro);
int input;
	do {
    
    
		menu();
		scanf_s("%d", &input);
		switch (input)
		{
    
    
		case  ADD:
			Addcontanct(&pro);
			break;
		case DEL:
			Delcontanct(&pro);
			break;
		case SEARCH:
			findcontanct(&pro);
			break;
		case MODIFY:
			modifycontanct(&pro);
			break;
		case SORT:
			Sortcontanct(&pro);
			break;
		case PRINT:
			Printcontanct(&pro);
			break;
		
		case EXIT:
			printf("退出通讯录\n");
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
}

Initialize address book function

effect: Initialize sz to 0, initialize struct peoinfo arr[] to 0 to prevent initialization to a random value, the memset function can set the number of bytes starting from which address to the number you want, and the first parameter is from which The address starts to reset, the second parameter is what to set, the third parameter is how many bytes to set, the memset header file stdlib.h

void Initcontanct(pp*p)
{
    
    
	p->sz = 0;
	memset(p->arr, 0, sizeof(p->arr));
}

The structure pointer p receives the address of the address book &pro

Add contacts

void Addcontanct(pp* p)
{
    
    
	if (p->sz == 1000)
	{
    
    
		printf("通讯录已经存满\n");
		return;
	}
	printf("请输入姓名\n");
	scanf("%s",p->arr[p->sz].name);
	printf("请输入性别\n");
	scanf("%s",p->arr[p->sz].sex);
	printf("请输入年龄\n");
	scanf("%d",&(p->arr[p->sz].age));
	printf("请输入电话\n");
	scanf("%s",p->arr[p->sz].tel);
	printf("请输入地址\n");
	scanf("%s",p->arr[p->sz].addr);
	p->sz++;
	printf("录入成功\n");

}

NoticeAge is not an array, you must add & to get the address character when scanningf, add one each time, p->sz+1; if ==1000, it means that the storage is full, return directly jumps out, and no longer add process

print data function

void Printcontanct(pp* p)
{
    
    
	int i = 0;
	printf("******************************************************\n");
	printf("%-10s %-5s %-5s %-12s %-20s\n", "姓名","性别","年龄","电话","地址");
	printf("******************************************************\n");
	for (i = 0; i < p->sz; i++)
	{
    
    
		printf("%-10s %-5s %-5d %-12s %-20s\n", p->arr[i].name, p->arr[i].sex, p->arr[i].age, p->arr[i].tel, p->arr[i].addr);
		printf("******************************************************\n");
	}
}

Notice: In order to make the printing more beautiful, the printing adopts left alignment, and each line of printing * separates the users and looks better. It loops through each member of the address book and prints it. After each user is printed, remember to change the line

delete contact

void Delcontanct(pp* p)
{
    
    
	char name[MAX_NAME];
	printf("请输入要删除朋友的名字\n");
	scanf("%s",name);
	if (find(p, name) == -1)
	{
    
    
		printf("查无此人\n");
	}
	else
	{
    
    
		int k = find(p, name);
		for (int j = k; j < p->sz - 1; j++)
		{
    
    
			p->arr[j] = p->arr[j + 1];
		}
		p->sz--;
		printf("删除成功\n");

}
}

Find function find()

int find(pp* p, char name[])
{
    
    
	int i = 0;
	for (int i = 0; i < p->sz; i++)
	{
    
    
		if (!strcmp(p->arr[i].name, name))
			return i;
	}
	return -1;
}

NoticeThe function of the search function is to traverse the address book, if there is such a person, return the subscript of the person array, if there is no such person, return -1; use the strcmp comparison function to remember the header file string.h, to delete a person, enter the name to be deleted The name of that person, pass the array address of the input name and the address of the address book to the find() function, call the find() function to traverse the address book, if there is no such person, the find() function returns -1; print and find no such person , otherwise use k to receive the subscript of the user to be deleted, delete the user with the subscript of k, and then move the subsequent ones forward one by one, delete one, and the corresponding p->sz- -
insert image description here

Find a contact

void findcontanct(pp* p)
{
    
    
	char name[MAX_NAME];
	if (p->sz == 0)
	{
    
    
		printf("通讯录为空\n");
		return;
	}
	printf("请输入要查找朋友的名字\n");
	scanf("%s", name);
	if (find(p, name) == -1)
	{
    
    
		printf("查无此人\n");
	}
	else
	{
    
    
		int k = find(p, name);
		printf("******************************************************\n");
		printf("%-10s %-5s %-5s %-12s %-20s\n", "姓名", "性别", "年龄", "电话", "地址");
		printf("%-10s %-5s %-5d %-12s %-20s\n", p->arr[k].name, p->arr[k].sex, p->arr[k].age, p->arr[k].tel, p->arr[k].addr);
		printf("******************************************************\n");
	}
}

Notice: Finding is basically similar to deleting. After entering the name, you need to call the find function to find the subscript of the corresponding name, and return -1; it means not found. If the subscript is returned, the information corresponding to the subscript will be printed out

Modify contact information

void modifycontanct(pp* p)
{
    
    
	char name[MAX_NAME];
	printf("请输入修改朋友的名字\n");
	scanf("%s", name);
	if (find(p, name) == -1)
	{
    
    
		printf("查无此人\n");
	}
	else
	{
    
    
		int k = find(p, name);
		printf("请输入要修改朋友的信息\n");
		printf("修改性别->");
		scanf("%s", p->arr[k].sex);
		printf("修改年龄->");
		scanf("%d", &(p->arr[k].age));
		printf("修改电话->");
		scanf("%s", p->arr[k].tel);
		printf("修改地址->");
		scanf("%s", p->arr[k].addr);
		printf("修改成功\n");
     }
}

NoticeModifying contact information still requires the find() function to find the contact name, and the find() function returns its subscript. When modifying, directly operate on the user corresponding to the subscript. Age is not an array, and scanf needs to add & to get the address character

Sort contacts (sort by age)

void Sortcontanct(pp* p)
{
    
    
	int i = 0;
	for (int i = 0; i < p->sz-1; i++)
	{
    
    
		for (int j = 0; j < p->sz - i-1; j++)
		{
    
    
			if (p->arr[j].age > p->arr[j + 1].age)
			{
    
    
				int tmp = p->arr[j].age;
				p->arr[j].age = p->arr[j + 1].age;
				p->arr[j + 1].age = tmp;
				char name[MAX_NAME];
				strcpy(name, p->arr[j].name);
				strcpy(p->arr[j].name, p->arr[j+1].name);
				strcpy(p->arr[j + 1].name, name);

				char sex[MAX_SEX];
				strcpy(sex, p->arr[j].sex);
				strcpy(p->arr[j].sex, p->arr[j + 1].sex);
				strcpy(p->arr[j + 1].sex, sex);

				char tel[MAX_TEL];
				strcpy(tel, p->arr[j].tel);
				strcpy(p->arr[j].tel, p->arr[j + 1].tel);
				strcpy(p->arr[j + 1].tel, tel);

				char addr[MAX_ADDR];
				strcpy(addr, p->arr[j].addr);
				strcpy(p->arr[j].addr, p->arr[j + 1].addr);
				strcpy(p->arr[j + 1].addr, tel);


             }



		}
}
	printf("按年龄排序成功,快去打印吧\n");
}

NoticeUse bubble sorting to sort by age from small to large. When exchanging ages, you need to exchange the names, gender, phone numbers, and addresses of the two people. For string arrays, we use strcpy, strcpy header file string.h, exchanged The idea is also inspired by the exchange of three numbers

But my last article was about qsort sorting, we can also use qsort for sorting, rejecting Shishan code

Modify: Sort Contacts

int int_cmp_age(const void* p1, const void* p2)//按年龄比较
{
    
    
	return ((struct peoinfo*)p1)->age - ((struct peoinfo*)p2)->age;
}
void Sortcontanct(pp* p)
{
    
    
	qsort(p->arr, p->sz, sizeof(peoinfo), int_cmp_age);
    printf("按年龄排序成功,快去打印吧\n");
}

Overall code display
contanct.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NAME 20
#define MAX_SEX 6
#define MAX_TEL 12
#define MAX_ADDR 20
#define MAX 1000
enum opion
{
    
    
	EXIT,
    ADD,
	DEL,
    SEARCH,
	MODIFY,
	SORT,
	PRINT
};
typedef struct peoinfo {
    
    
	char name[MAX_NAME];
	char sex[MAX_SEX];
	int age;
	char tel[MAX_TEL];
	char addr[MAX_ADDR];

}peoinfo;

typedef struct pp{
    
    
	struct peoinfo arr[MAX];
	int sz;

}pp;
void Initcontanct(pp*p);
void Addcontanct(pp* p);
void Printcontanct(pp* p);
void Delcontanct(pp* p);
void findcontanct(pp* p);
void modifycontanct(pp* p);
void Sortcontanct(pp* p);

contanct.c

#include"contanct.h"
void Initcontanct(pp*p)
{
    
    
	p->sz = 0;
	memset(p->arr, 0, sizeof(p->arr));
}
void Addcontanct(pp* p)
{
    
    
	if (p->sz == 1000)
	{
    
    
		printf("通讯录已经存满\n");
		return;
	}
	printf("请输入姓名\n");
	scanf("%s",p->arr[p->sz].name);
	printf("请输入性别\n");
	scanf("%s",p->arr[p->sz].sex);
	printf("请输入年龄\n");
	scanf("%d",&(p->arr[p->sz].age));
	printf("请输入电话\n");
	scanf("%s",p->arr[p->sz].tel);
	printf("请输入地址\n");
	scanf("%s",p->arr[p->sz].addr);
	p->sz++;
	printf("录入成功\n");

}
void Printcontanct(pp* p)
{
    
    
	int i = 0;
	printf("******************************************************\n");
	printf("%-10s %-5s %-5s %-12s %-20s\n", "姓名","性别","年龄","电话","地址");
	printf("******************************************************\n");
	for (i = 0; i < p->sz; i++)
	{
    
    
		printf("%-10s %-5s %-5d %-12s %-20s\n", p->arr[i].name, p->arr[i].sex, p->arr[i].age, p->arr[i].tel, p->arr[i].addr);
		printf("******************************************************\n");
	}
}
int find(pp* p, char name[])
{
    
    
	int i = 0;
	for (int i = 0; i < p->sz; i++)
	{
    
    
		if (!strcmp(p->arr[i].name, name))
			return i;
	}
	return -1;
}
void Delcontanct(pp* p)
{
    
    
	char name[MAX_NAME];
	printf("请输入要删除朋友的名字\n");
	scanf("%s",name);
	if (find(p, name) == -1)
	{
    
    
		printf("查无此人\n");
	}
	else
	{
    
    
		int k = find(p, name);
		for (int j = k; j < p->sz - 1; j++)
		{
    
    
			p->arr[j] = p->arr[j + 1];
		}
		p->sz--;
		printf("删除成功\n");

}
}
void findcontanct(pp* p)
{
    
    
	char name[MAX_NAME];
	if (p->sz == 0)
	{
    
    
		printf("通讯录为空\n");
		return;
	}
	printf("请输入要查找朋友的名字\n");
	scanf("%s", name);
	if (find(p, name) == -1)
	{
    
    
		printf("查无此人\n");
	}
	else
	{
    
    
		int k = find(p, name);
		printf("******************************************************\n");
		printf("%-10s %-5s %-5s %-12s %-20s\n", "姓名", "性别", "年龄", "电话", "地址");
		printf("%-10s %-5s %-5d %-12s %-20s\n", p->arr[k].name, p->arr[k].sex, p->arr[k].age, p->arr[k].tel, p->arr[k].addr);
		printf("******************************************************\n");
	}
}
void modifycontanct(pp* p)
{
    
    
	char name[MAX_NAME];
	printf("请输入修改朋友的名字\n");
	scanf("%s", name);
	if (find(p, name) == -1)
	{
    
    
		printf("查无此人\n");
	}
	else
	{
    
    
		int k = find(p, name);
		printf("请输入要修改朋友的信息\n");
		printf("修改性别->");
		scanf("%s", p->arr[k].sex);
		printf("修改年龄->");
		scanf("%d", &(p->arr[k].age));
		printf("修改电话->");
		scanf("%s", p->arr[k].tel);
		printf("修改地址->");
		scanf("%s", p->arr[k].addr);
		printf("修改成功\n");
     }
}
int int_cmp_age(const void* p1, const void* p2)//按年龄比较
{
    
    
	return ((struct peoinfo*)p1)->age - ((struct peoinfo*)p2)->age;
}
void Sortcontanct(pp* p)
{
    
    
	qsort(p->arr, p->sz, sizeof(peoinfo), int_cmp_age);
    printf("按年龄排序成功,快去打印吧\n");
}

test.c

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

}
void test()
{
    
    pp pro;
Initcontanct(&pro);
int input;
	do {
    
    
		menu();
		scanf_s("%d", &input);
		switch (input)
		{
    
    
		case  ADD:
			Addcontanct(&pro);
			break;
		case DEL:
			Delcontanct(&pro);
			break;
		case SEARCH:
			findcontanct(&pro);
			break;
		case MODIFY:
			modifycontanct(&pro);
			break;
		case SORT:
			Sortcontanct(&pro);
			break;
		case PRINT:
			Printcontanct(&pro);
			break;
		
		case EXIT:
			printf("退出通讯录\n");
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
}

void main()
{
    
    
	test();

}

Result display
insert image description here

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
You can point out the existing problems, and I will share the dynamic address book with you later. Please support me a lot, and my visit finally broke 2000 hahahahaha
eggs
insert image description here
That’s all for today’s sharing, see you in the next one

Guess you like

Origin blog.csdn.net/yyqzjw/article/details/132243298