当前位置:网站首页>C language simple version address book (static version)
C language simple version address book (static version)
2022-07-19 15:43:00 【m0_ seventy-one million two hundred and fifty-nine thousand eig】
Functions of address book
Menu display
void print()
{
printf("****************************************\n");
printf("*****1. Add contacts 2. Delete Contact *****\n");
printf("*****3. Find contacts 4. Information changes *****\n");
printf("*****5. display information 6. Clear address book *****\n");
printf("*****7. Sort 0. sign out *****\n");
printf("****************************************\n");
}
Add contacts
void add(contact* con)
{
assert(con);
if (con->count > 1000)
{
printf(" The address book is full !\n");
return;
}
printf(" Please enter the contact name :>\n");
scanf("%s", con->data[con->count].name);
printf(" Please enter the gender of the contact person :>\n");
scanf("%s", con->data[con->count].sex);
printf(" Please enter the contact age :>\n");
scanf("%d", &(con->data[con->count].age));
printf(" Please enter the contact number :>\n");
scanf("%s", con->data[con->count].tele);
printf(" Please enter contact address :>\n");
scanf("%s", con->data[con->count].addr);
con->count++;
}
To add a contact here is to enter the contact information , At the same time, let the counting times of the structure +1.
Delete Contact
void del(contact* con)
{
char name[20] = {
0 };
printf(" Please enter the name of the contact you want to delete :>\n");
scanf("%s", name);
int s = search(con,name);
int i = 0;
for (i = s; i < con->count - 1; i++)
{
con->data[i] = con->data[i + 1];
}
con->count--;
printf(" Delete successful !\n");
}
Deleting contacts is simple , I delete contacts here by looking up contacts , If you write it yourself , It can be done by phone number or other methods
Find contacts
int search(contact* con,char name[])
{
assert(con);
int i = 0;
for (i = 0; i < con->count; i++)
{
if ((strcmp(name, con->data[i].name))==0)
{
return i;
}
}
return -1;
}
The function of finding contacts is very simple , Is to enter the name , Then compare the entered name with the name of the existing contact
Information changes
// Menu options
void menu1()
{
printf("*************************************\n");
printf("*********1. full name 2. Gender *********\n");
printf("*********3. Age 4. Phone number *****\n");
printf("*********5. Address 0. No modification *****\n");
printf("*************************************\n");
}
// Modify contact information
void modify(contact* con)
{
char name[max_name] = {
0 };
int choice = 0;
printf(" Please enter the contact name to modify :>\n");
scanf("%s", name);
int s = search(con, name);
if (s != -1)
{
do
{
printf(" Please modify the contact information according to your needs !\n");
menu1();
printf(" Please enter the options of the contact information you want to modify :>\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf(" Please modify the contact name :>\n");
scanf("%s", con->data[s].name);
break;
case 2:
printf(" Please modify the contact gender :>\n");
scanf("%s", con->data[s].sex);
break;
case 3:
printf(" Please modify the age of the contact :>\n");
scanf("%d", &(con->data[s].age));
break;
case 4:
printf(" Please modify the contact phone number :>\n");
scanf("%s", con->data[s].tele);
break;
case 5:
printf(" Please modify the contact address :>\n");
scanf("%s", con->data[s].addr);
break;
default:
printf(" There is no such option , Please reselect :>\n");
break;
}
} while (choice);
}
else
{
printf(" The contact information you want to modify does not exist !\n");
return ;
}
}
Also by calling the lookup function , Then modify the contact information according to your own needs
Show contact information
void show(contact* con)
{
int i = 0;
printf("%-10s%-5s%-6s%-15s%-20s\n", " full name ", " Gender ", " Age ", " Phone number ", " Address ");
for (i = 0; i < con->count; i++)
{
printf("%-10s%-5s%-6d%-15s%-20s\n", con->data[i].name,
con->data[i].sex,
con->data[i].age,
con->data[i].tele,
con->data[i].addr);
}
}
Traverse the contact data in this structure , Then print them one by one
Clear address book
// Clear address book
void clear(contact* con)
{
con->count = 0;
printf(" Clear successfully !\n");
}
This function is the simplest , Let the number of contacts become 0, Unable to read address book information .
Sort address book
// Comparison function
int cmp(void* e1, void* e2)
{
return (strcmp(((info*)e1)->name ,((info*)e2)->name));
}
// Exchange function
void Swap(char* e1, char* e2, int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
char tmp = *e1;
*e1 = *e2;
*e2 = tmp;
e1++;
e2++;
}
}
// Sort function based on bubble sort idea
void bubble_sort(void* base,
size_t num,
size_t size,
int(*cmp)(void* e1, void* e2))
{
int i = 0;
int j = 0;
for (i = 0; i < num - 1; i++)
{
for (j = 0; j < num - 1 - i; j++)
{
int sz = cmp(((char*)base + j * size), ((char*)base + (j + 1) * size));
if (sz>0)
{
Swap(((char*)base + j * size), ((char*)base + (j + 1) * size), size);
}
}
}
}
// Sort address book
void sort(contact* con)
{
bubble_sort(con->data,
con->count,
sizeof(info),
cmp);
printf(" Sort success !\n");
}
Here I use the idea of bubble sorting to sort the address book , At the same time, my sorting function is applicable to all types of sorting , If readers are bothered, they can use library functions qsort Function to sort .
The main function
enum choe {
EXIT,
ADD,
DELETE,
SEARCH,
MODIFY,
SHOW,
CLEAR,
SORT
};
int main()
{
int input = 0;
contact con;
memset(&con, 0, sizeof(con));
do
{
print();
printf(" Please select the function :>\n");
scanf("%d", &input);
switch (input)
{
case ADD:
printf(" Please enter information :>\n");
add(&con);
break;
case DELETE:
del(&con);
break;
case SEARCH:
printf(" Please enter the contact you want to find :>\n");
char name[max_name] = {
0 };
inputname(name);
int x = search(&con, name);
if (x >= 0)
{
printf(" Find success !\n");
}
else
{
printf(" Check no one !\n");
}
show1(&con, x);
break;
case MODIFY
:
modify(&con);
break;
case SHOW:
show(&con);
break;
case CLEAR:
clear(&con);
break;
case SORT:
sort(&con);
break;
case EXIT:
printf(" sign out \n");
break;
}
} while (input);
return 0;
}
Enumeration is used here , So that every function can see the name and know the meaning , clear .
There is no effect display here , This address book is relatively simple , You can copy it yourself , And then run , The compiler is vs2019, Thank you !
边栏推荐
- 通过jmeter压测surging
- 恒泰证券网上开户安全吗?
- 2022/7/17
- Raw Socket
- 【自定义类型:结构体,枚举,联合】
- Is it safe for Hengtai securities to open an account online?
- Adn public welfare acceleration - jsdelivr NPM (domestic), a high-quality alternative to elmcdn
- PostgreSQL在Linux和Windows安装和入门基础教程
- 232. Implement queue with stack
- Wpa_supplicant WiFi连接
猜你喜欢
随机推荐
Build intranet mail service through Qunhui Suite
浅谈ISP-噪声模型1
Through JMeter pressure measurement surging
es6解构赋值一学就会
启牛一键打新债靠谱吗,真的安全吗
Devops tool chain: open and free to choose the tools most suitable for the needs of the team and business
Selenium元素操作
证券账户上买基金安全吗。可以做短线吗
Cloud native - orchestration and management
数字IC-1.11.1 静态时序分析 - 单周期静态时序分析
Radiotap
Servlet+JDBC表白墙
深度学习系列资料总结
Is online account opening safe? Then choose which securities to open a securities account
可以写进简历的软件测试项目实战经验(包含电商、银行、app等)
LBP特征笔记
[user article] examples of P4 consolidation practice guide disassemble resolve
PostgreSQL在Linux和Windows安裝和入門基礎教程
初来乍到,多多关照(其实不是初来了^_^,Hello CSDN,我来了)
Which bank outlet in Chongqing can buy Ritz fund products?
![[dynamic memory management]](/img/53/65653523fa52315ecff5d6881d0382.png)








