C language simple address book implementation code

  • 2020-11-25 07:27:39
  • OfStack

This article shares the specific code of C language simple address book for your reference, the specific content is as follows

Address book implementation:

1. Introduction:

Using C language array, pointer, structure and other related knowledge: to achieve a simple address book:

The general contents of this directory are as follows:
The address book can store information about 1,000 people:

The basic information of each person is: name + age + gender + telephone + QQ + address
This address book has the function: add + delete + query + modify + sort + exit

This address book is designed at the time of design: Three files are designed in total:

contact. h: Declarations of types and functions

contact.c: The concrete realization of the function function

test. c: Test file

2. Specific implementation:

1. contact.h:

This file defines the function declaration and two structures for the functions designed in the address book:

PeoInfo: Stores basic information about each person in the address book

Contact: Stores the data in the address book and the number of people stored in the address book

Specific code implementation is as follows:


//
// Type declaration + Declaration of a function 
//
 
#include <stdio.h>
#include <string.h>
 
#define MAX 1000
 
#define MAX_NAME 20
#define MAX_TELE 12
#define MAX_ADDR 100
#define MAX_QQ 12
#define MAX_SEX 5
 
typedef struct PeoInfo
{
 char name[MAX_NAME];
 char tele[MAX_TELE];
 char addr[MAX_ADDR];
 char qq[MAX_QQ];
 char sex[MAX_SEX];
 short age;
}PeoInfo;
 
// The address book 
typedef struct Contact
{
 PeoInfo data[MAX];// data 
 int sz;// The number of effective 
}Contact;
 
 
// add 1 Personal Information 
void add_contact(Contact* pc);
 
// Displays the information in the address book 
void show_contact(Contact* pc);
 
// Deletes the specified contact 
void del_contact(Contact* pc);
 
// Find the specified contact 
void search_contact(Contact* pc);
 
// Modify the specified contact 
void modify_contact(Contact* pc);
 
// Sort address book data 
void sort_contact(Contact* pc); 

2. contact.c:

This file is the specific implementation of the functions designed by the address book: specifically, it includes:

(1) Add a personal information void add_contact(Contact* pc);

(2) Display information in the address book void show_contact(Contact* pc);

(3) Delete the specified contact void del_contact(Contact* pc);

(4) Find the specified contact void search_contact(Contact* pc);

(5) Modify the designated contact void modify_contact(Contact* pc);

(6) Sorting address book data void sort_contact(Contact* pc);

Specific code implementation is as follows:


#define _CRT_SECURE_NO_WARNINGS 1
 
#include "contact.h"
 
void add_contact(Contact* pc)
{
 if (pc->sz == MAX)
 {
 printf(" The address book is full \n");
 }
 else
 {
 printf(" Please enter your name :>");
 scanf("%s", pc->data[pc->sz].name);
 printf(" Please enter the telephone number :>");
 scanf("%s", pc->data[pc->sz].tele);
 printf(" Please enter the address :>");
 scanf("%s", pc->data[pc->sz].addr);
 printf(" Please enter the QQ:>");
 scanf("%s", pc->data[pc->sz].qq);
 printf(" Please enter gender :>");
 scanf("%s", pc->data[pc->sz].sex);
 printf(" Please enter your age :>");
 scanf("%d", &(pc->data[pc->sz].age));
 
 pc->sz++;
 printf(" Add a success \n");
 }
}
 
void show_contact(Contact* pc)
{
 int i = 0;
 printf("%10s %12s %20s %5s %12s %5s\n", " The name ", " The phone ", " address ", " age ", "QQ", " gender ");
 for (i = 0; i < pc->sz; i++)
 {
 printf("%10s %12s %20s %5d %12s %5s\n", pc->data[i].name,
 pc->data[i].tele,
 pc->data[i].addr,
 pc->data[i].age,
 pc->data[i].qq,
 pc->data[i].sex);
 }
}
 
static int find_peo_by_name(Contact* pc, char name[])
{
 int i = 0;
 for (i = 0; i < pc->sz; i++)
 {
 if (strcmp(name, pc->data[i].name) == 0)
 {
 return i;// Found it. Return the index 
 }
 }
 return -1;// Can't find 
}
 
void del_contact(Contact* pc)
{
 
 if (pc->sz == 0)
 {
 printf(" Sorry, the address book is empty \n");
 }
 else
 {
 char name[MAX_NAME] = { 0 };
 printf(" Please enter the name of the person you want to delete :>");
 scanf("%s", name);
 //1.  Locate the location of the specified contact 
 int pos = find_peo_by_name(pc, name);
 if (pos == -1)
 {
 printf(" Unfortunately, the deleted person does not exist \n");
 }
 else
 {
 //2.  delete 
 int j = 0;
 for (j = pos; j < pc->sz - 1; j++)
 {
 pc->data[j] = pc->data[j + 1];
 }
 pc->sz--;
 printf(" Delete the success \n");
 }
 }
}
 
 
void search_contact(Contact* pc)
{
 char name[MAX_NAME] = { 0 };
 printf(" Please enter the name of the person you are looking for :>");
 scanf("%s", name);
 int pos = find_peo_by_name(pc, name);
 if (pos == -1)
 {
 printf(" No such person \n");
 }
 else
 {
 printf("%10s %12s %20s %5s %12s %5s\n",
 " The name ", " The phone ", " address ", " age ", "QQ", " gender ");
 printf("%10s %12s %20s %5d %12s %5s\n", pc->data[pos].name,
 pc->data[pos].tele,
 pc->data[pos].addr,
 pc->data[pos].age,
 pc->data[pos].qq,
 pc->data[pos].sex);
 }
}
 
 
void modify_contact(Contact* pc)
{
 char name[MAX_NAME] = { 0 };
 printf(" Please enter the name of the person to be modified :>");
 scanf("%s", name);
 int pos = find_peo_by_name(pc, name);
 if (pos == -1)
 {
 printf(" No such person \n");
 }
 else
 {
 printf(" Please enter a new name :>");
 scanf("%s", pc->data[pos].name);
 printf(" Please enter a new number :>");
 scanf("%s", pc->data[pos].tele);
 printf(" Please enter a new address :>");
 scanf("%s", pc->data[pos].addr);
 printf(" Please enter new QQ:>");
 scanf("%s", pc->data[pos].qq);
 printf(" Please enter the new gender :>");
 scanf("%s", pc->data[pos].sex);
 printf(" Please enter your new age :>");
 scanf("%d", &(pc->data[pos].age));
 }
}
 
void sort_contact(Contact* pc)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < pc->sz - 1; i++)
 {
 int flag = 1;// Let's say it's already ordered 
 for (j = 0; j < pc->sz - 1 - i; j++)
 {
 if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0)
 {
 PeoInfo tmp = pc->data[j];
 pc->data[j] = pc->data[j + 1];
 pc->data[j + 1] = tmp;
 flag = 0;
 }
 }
 if (1 == flag)
 {
 break;
 }
 }
}

3.test.c

This file is a test file for the implementation of the address book function: including the menu printing, according to the user's input, output related information and the implementation of the complete process of the address book function.

Specific code implementation is as follows:


#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
 
// The test file 
// The address book 1000 Personal Information: Name +  age +  The phone  +  address + QQ +  gender  
// increase   delete   check   change   The sorting   exit 
#define _CRT_SECURE_NO_WARNINGS 1
 
 
void menu()
{
 printf("*******************************\n");
 printf("****** 1. add  2. del * ***\n");
 printf("****** 3. search 4. modify ***\n");
 printf("****** 5. sort 6. show ***\n");
 printf("******  0. exit  ***\n");
 printf("*******************************\n");
}
 
enum Option
{
 EXIT,
 ADD,
 DEL,
 SEARCH,
 MODIFY,
 SORT,
 SHOW
};
 
//first_name
//FirstName
 
void test()
{
 // The address book created 
 Contact con = { 0 };
 int input = 0;
 do
 {
 menu();
 printf(" Please select a :>");
 scanf("%d", &input);
 switch (input)
 {
 case ADD:
 add_contact(&con);
 break;
 case DEL:
 del_contact(&con);
 break;
 case SORT:
 sort_contact(&con);
 break;
 case SHOW:
 show_contact(&con);
 break;
 case SEARCH:
 search_contact(&con);
 break;
 case MODIFY:
 modify_contact(&con);
 break;
 case EXIT:
 printf(" Exit address book \n");
 break;
 default:
 printf(" The wrong choice \n");
 break;
 }
 } while (input);
}
int main()
{
 test();//
 return 0;
}

Related articles: