当前位置:网站首页>C language takes you to tear up the address book
C language takes you to tear up the address book
2022-07-27 09:16:00 【Blue cat Knight】
To achieve an address book , First , We need to know What we want to achieve is what functions the address book has ,
First Think about this address book What is it , The key is —— Store and display contacts ; Store and display its name ; Age ; Gender ; Address ; also Phone number ; No phone number ; How to practice others ; How can I call an address book ?
In addition to storing and displaying contacts We need to have at least Add, delete, search and change ; Finally, we hope to have one Sort The function of .
I know so much , We have A general direction
First
Have a simple initial interface like this :
#include<stdio.h>
void Add()
{}
void Del()
{}
void Search()
{}
void Show()
{}
void Modify()
{}
void Sort()
{}
void menu()
{
printf("***1.(Add) ***\n");
printf("***2.(Del) ***\n");
printf("***3.(Search)***\n");
printf("***4.(Modify)***\n");
printf("***5.(Show) ***\n");
printf("***6.(Sort) ***\n");
printf("***0.(Exit) ***\n");
}
int main()
{
int input = 0;
do
{
menu(); // Give me a menu Prompt the function performed
printf(" Please enter the function to be performed : ");
scanf("%d", &input);
switch(input)
{
case 1:Add();
break;
case 2:Del();
break;
case 3:Search();
break;
case 4:Modify();
break;
case 5:Show();
break;
case 6:Sort();
break;
case 0: printf(" Exit address book \n");
break;
default:printf(" Input error \n");
break;
}
} while (input);
}Here are the results of the run :

So we have completed the first step ;
The second step Is to create variables ;
It is convenient for us to create variables with structures :
struct People
{
char name[10];
int age; // age Is the integer variable
char sex[10];
char tele[11];
char address[10];
};
struct Contact
{
people date[100]; // Initially we store 100 Personal information So we give people type
int count; // Record the number of people in the current address book
};
//——————————————————————————
typedef struct People
{
char name[10];
int age[10];
char sex[10];
char tele[11];
char address[10];
}People;
typedef struct Contact
{
int date[100];
int count;
}Contact;
For the convenience of later reference to the structure We make use of typedef To name the structural weight .
The above is the basic framework of this address book Now? We construct the function step by step to realize our function .
Mention functions ; Let's not worry about writing those function functions directly
Think about it our Contact Is it not initialized yet ;
So let's build one Contact Variable
Contact con; // Construct a Contact Variable
initContact(&con); // initialization Then start writing initialization functions
void initContact(Contact*pc) // initialization pc The type is Contact*
{
assert(pc); // prevent pc Null pointer
pc->count = 0;
memset(pc->date, 0, sizeof(pc->date));
//memset Copy characters 0)( An unsigned character ) To ginseng
// Count pc->date The front of the string to which it refers sizeof(pc->date) Characters .
}Initialization finished , Let's write Add function
case 1:Add(&con);
break;Pay special attention when printing here A little carelessness is wrong ( Painful lesson )
// name It's an array name Itself is the address
// age Is an integer variable Need to get the address
void Add(Contact* pc)
{
assert(pc);
// First of all Yes pc Judge Whether it is a null pointer Null pointers cannot be dereferenced
// Judge count Have you exceeded date The range that can be stored
if (pc->count == 100)
{
printf(" The number is full , Please try again after processing ");
return;
}
printf(" Please enter a name :");
scanf("%s", pc->date[pc->count].name); // name It's an array name Itself is the address
printf(" Please enter age :");
scanf("%d", &(pc->date[pc->count].age)); // age Is an integer variable Need to get the address
printf(" Please enter gender :");
scanf("%s", &pc->date[pc->count].sex);
printf(" Please input the phone number :");
scanf("%s", pc->date[pc->count].tele);
printf(" Please enter the address :");
scanf("%s", pc->date[pc->count].address);
pc->count = pc->count + 1; // count Record the number of people stored Remember to add 1
printf(" Increase success \n");
}Here is Del function
case 2:Del(&con);
break;int find_name(Contact* pc,char name1[])
{
assert(pc);
int i = 0; // for Loop through to find
for (i = 0; i < pc->count; i++)
{
if (0 == strcmp(name1, pc->date[i].name))
return i; // return i Convenient deletion
}
return -1;
}void Del(Contact* pc)
{
assert(pc);
// First of all, judge Whether the address book is empty
if(pc->count == 0)
{
printf(" Address book is empty , Not delete \n");
}
printf(" Please enter the name of the contact to be deleted :");
char name1[20] = { 0 };
scanf("%s", &name1);
// Know before deleting We know the location of this contact
// You cannot apply for deletion directly
// So we have to find The location of this contact And determine whether there is
// We assume that No one in this contact has the same name
// Let's follow Name to find
int ret = find_name(pc, name1); // Give me one ret To accept the return value
// Use overwrite to delete
if (ret == 0)
{
printf(" No such contact , Please check and try again \n");
return 0;
}
else if (ret != -1)
{
int i = 0;
for (i = ret; i < pc->count - 1; i++)
{
pc->date[ret] = pc->date[ret + 1];
}
}
pc->count = pc->count - 1;
printf(" Delete successful \n");
}The next step is Search function
case 3:Search(&con);
break;Before the same search We know the location of this contact
We need to find The location of this contact And determine whether there is
We assume that No one in this contact has the same name Let's follow Name to find
Still use find_name This function
void Search(Contact* pc)
{
assert(pc);
printf(" Please enter the name of the person you want to find :");
// Old rules We must first determine whether the contact we are looking for exists
char name1[20] = { 0 };
scanf("%s", &name1);
int ret = find_name(pc, name1); // Give me one ret To accept the return value
if (ret == -1)
{
printf(" No such contact , Please check and try again \n");
return ;
}
// According to the width of each character , Align left to output them .
printf("%-20s\t%-10s\t%-10s\t%-11s\t%-10s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-20s\t%-10d\t%-10s\t%-11s\t%-10s\n",
pc->date[ret].name,
pc->date[ret].age,
pc->date[ret].sex,
pc->date[ret].tele,
pc->date[ret].address);
}And then it's Modify function
case 4:Modify(&con);
break;Before the same modification We know the location of this contact
We need to find The location of this contact And determine whether there is
We still assume No one in this contact has the same name Let's follow Name to find
Still use find_name This function
void Modify(Contact* pc)
{
assert(pc);
printf(" Please enter the name of the person to modify :");
// Old rules We must first judge whether the modified contact exists
char name1[20] = { 0 };
scanf("%s", &name1);
int ret = find_name(pc, name1); // Give me one ret To accept the return value
if (ret == -1)
{
printf(" No such contact , Please check and try again \n");
return;
}
printf(" Please enter a name :");
scanf("%s", pc->date[ret].name);
printf(" Please enter age :");
scanf("%d", pc->date[ret].age);
printf(" Please enter gender :");
scanf("%s", &pc->date[ret].sex);
printf(" Please input the phone number :");
scanf("%s", pc->date[ret].tele);
printf(" Please enter the address :");
scanf("%s", pc->date[ret].address);
printf(" Modification successful \n");
}Here is Show function
case 5:Show(&con);
break;void Show(Contact* pc)
{
assert(pc);
int i = 0; // for Loop through print
printf("%-20s\t%-10s\t%-10s\t%-11s\t%-10s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
for (i = 0; i < pc->count; i++)
{
printf("%-20s\t%-10d\t%-10s\t%-11s\t%-10s\n",
pc->date[i].name,
pc->date[i].age,
pc->date[i].sex,
pc->date[i].tele,
pc->date[i].address);
}
}the last one function
case 6:Sort(&con);
break;Want to use qsort function I don't know the usage here
int com_by_name(const void* e1,const void*e2)
{
strcmp(((People*)e1)->name, ((People*)e2)->name);
}void Sort(Contact* pc)
{
assert(pc);
// We still sort by name
qsort(pc->date,pc->count, sizeof(People),com_by_name);
printf(" Sort success \n");
}———————————————————————————————————————————
Manual split line
Above we will take the simple version of the address book It's done
The complete code is attached below
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
typedef struct People
{
char name[10];
int age;
char sex[10];
char tele[11];
char address[100];
}People;
typedef struct Contact
{
People date[100];
int count; // Record the number of people in the current address book
}Contact;
void initContact(Contact*pc) // initialization pc The type is Contact*
{
assert(pc); // prevent pc Null pointer
pc->count = 0;
memset(pc->date, 0, sizeof(pc->date));
//memset Copy characters 0)( An unsigned character ) To parameter pc->date The front of the string to which it refers sizeof(pc->date) Characters .
}
void Add(Contact* pc)
{
assert(pc);
// First of all Yes pc Judge
// Judge count Have you exceeded date The range that can be stored
if (pc->count == 100)
{
printf(" The number is full , Please try again after processing \n");
return;
}
printf(" Please enter a name :");
scanf("%s", pc->date[pc->count].name); // name It's an array name Itself is the address
printf(" Please enter age :");
scanf("%d", &(pc->date[pc->count].age)); // age Is an integer variable Need to get the address
printf(" Please enter gender :");
scanf("%s", pc->date[pc->count].sex);
printf(" Please input the phone number :");
scanf("%s", pc->date[pc->count].tele);
printf(" Please enter the address :");
scanf("%s", pc->date[pc->count].address);
pc->count = pc->count + 1;
printf(" Increase success \n");
return;
}
int find_name(Contact* pc,char name1[])
{
assert(pc);
int i = 0; // for Loop through to find
for (i = 0; i < pc->count; i++)
{
if (0 == strcmp(name1, pc->date[i].name))
return i; // return i Convenient deletion
}
return -1;
}
void Del(Contact* pc)
{
assert(pc);
// First of all, judge Whether the address book is empty
if(pc->count == 0)
{
printf(" Address book is empty , Not delete \n");
}
printf(" Please enter the name of the contact to be deleted :");
char name1[20] = { 0 };
scanf("%s", &name1);
// Know before deleting We know the location of this contact
// You cannot apply for deletion directly
// So we have to find The location of this contact And determine whether there is
// We assume that No one in this contact has the same name
// Let's follow Name to find
int ret = find_name(pc, name1); // Give me one ret To accept the return value
// Use overwrite to delete
if (ret == -1)
{
printf(" No such contact , Please check and try again \n");
return ;
}
else if (ret != -1)
{
int i = 0;
for (i = ret; i < pc->count - 1; i++)
{
pc->date[ret] = pc->date[ret + 1];
}
}
pc->count = pc->count - 1;
printf(" Delete successful \n");
}
void Search(Contact* pc)
{
assert(pc);
printf(" Please enter the name of the person you want to find :");
// Old rules We must first determine whether the contact we are looking for exists
char name1[20] = { 0 };
scanf("%s", name1);
int ret = find_name(pc, name1); // Give me one ret To accept the return value
if (ret == -1)
{
printf(" No such contact , Please check and try again \n");
return ;
}
// According to the width of each character , Align left to output them .
printf("%-20s\t%-3s\t%-10s\t%-11s\t%-10s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
printf("%-20s\t%-3d\t%-10s\t%-11s\t%-10s\n",
pc->date[ret].name,
pc->date[ret].age,
pc->date[ret].sex,
pc->date[ret].tele,
pc->date[ret].address);
}
void Show(Contact* pc)
{
assert(pc);
int i = 0; // for Loop through print
printf("%-20s\t%-3s\t%-10s\t%-11s\t%-10s\n", " name ", " Age ", " Gender ", " Telephone ", " Address ");
for (i = 0; i < pc->count; i++)
{
printf("%-20s\t%-3d\t%-10s\t%-11s\t%-10s\n",
pc->date[i].name,
pc->date[i].age,
pc->date[i].sex,
pc->date[i].tele,
pc->date[i].address);
}
}
void Modify(Contact* pc)
{
assert(pc);
printf(" Please enter the name of the person to modify :");
// Old rules We must first judge whether the modified contact exists
char name1[20] = { 0 };
scanf("%s", &name1);
int ret = find_name(pc, name1); // Give me one ret To accept the return value
if (ret == -1)
{
printf(" No such contact , Please check and try again \n");
return;
}
printf(" Please enter a name :");
scanf("%s", pc->date[ret].name);
printf(" Please enter age :");
scanf("%d", &(pc->date[ret].age));
printf(" Please enter gender :");
scanf("%s", pc->date[ret].sex);
printf(" Please input the phone number :");
scanf("%s", pc->date[ret].tele);
printf(" Please enter the address :");
scanf("%s", pc->date[ret].address);
printf(" Modification successful \n");
}
int com_by_name(const void* e1,const void*e2)
{
strcmp(((People*)e1)->name, ((People*)e2)->name);
}
void Sort(Contact* pc)
{
assert(pc);
// We still sort by name
qsort(pc->date,pc->count, sizeof(People),com_by_name);
printf(" Sort success \n");
}
void menu()
{
printf("***1.(Add) ***\n");
printf("***2.(Del) ***\n");
printf("***3.(Search)***\n");
printf("***4.(Modify)***\n");
printf("***5.(Show) ***\n");
printf("***6.(Sort) ***\n");
printf("***0.(Exit) ***\n");
}
int main()
{
int input = 0;
Contact con; // Construct a Contact Variable
initContact(&con); // initialization
do
{
menu(); // Give me a menu Prompt the function performed
printf(" Please enter the function to be performed : ");
scanf("%d", &input);
switch(input)
{
case 1:Add(&con);
break;
case 2:Del(&con);
break;
case 3:Search(&con);
break;
case 4:Modify(&con);
break;
case 5:Show(&con);
break;
case 6:Sort(&con);
break;
case 0: printf(" Exit address book \n");
break;
default:printf(" Input error \n");
break;
}
} while (input);
}边栏推荐
- The wechat installation package has soared from 0.5m to 260m. Why are our programs getting bigger and bigger?
- Thermal renewal and its principle
- [C language - zero foundation lesson 6] input and output sentence format and compound sentence
- BEVFormer: Learning Bird’s-Eye-View Representation from Multi-Camera Images via Spatiotemporal Trans
- D3.v3.js data visualization -- pictures and tips of force oriented diagram
- async/await的执行顺序以及宏任务和微任务
- Mangodb simple to use
- 软件测试功能测试全套常见面试题【功能测试-零基础】必备4-1
- Replace restricts the text box to regular expressions of numbers, numbers, letters, etc
- Wechat applet 5 - foundation strengthening (not finished)
猜你喜欢

Mangodb简单使用

ES6 new - array part

BEVFormer: Learning Bird’s-Eye-View Representation from Multi-Camera Images via Spatiotemporal Trans

NPM install error forced installation

Some practical, commonly used and increasingly efficient kubernetes aliases

How to deploy yolov6 with tensorrt

async/await的执行顺序以及宏任务和微任务

Mangodb simple to use

Solve the problem of Chinese garbled code on the jupyter console

pollFirst(),pollLast(),peekFirst(),peekLast()
随机推荐
PyTorch自定义CUDA算子教程与运行时间分析
NPM and yarn update dependent packages
Summary of traversal methods
Is the operation of assigning values to int variables atomic?
MATLAB data import -- importdata and load functions
对 int 变量赋值的操作是原子的吗?
qt中使用sqlite同时打开多个数据库文件
[C language - zero foundation _ study _ review _ lesson 4] data types and operations
Nutrecipes developed based on arkui ETS
[interprocess communication IPC] - semaphore learning
Pyqt5 rapid development and practice 4.1 qmainwindow
CUDA programming-04: CUDA memory model
The lifecycle of arkui development framework components
易语言编程: 让读屏软件可获取标签控件的文本
【云驻共创】华为云:全栈技术创新,深耕数字化,引领云原生
Babbitt | yuan universe daily must read: Guangzhou Nansha released the "Yuan universe nine" measures, and the platform can obtain up to 200million yuan of financial support
ES6 new symbol data type
Svg drawing curve
NPM install error forced installation
[leetcode -- the first day of introduction to programming ability] basic data type [statistics of odd numbers within the range / average wage after removing the minimum wage and maximum wage)