[C Language] implements an address book, C language address book

Source: Internet
Author: User

[C Language] implements an address book, C language address book
Implement an address book;
The address book can be used to store information of 1000 people. The information of each person includes:
Name, gender, age, phone number, and address

Method:
1. Add contact information
2. Delete the specified contact information
3. Search for the specified contact information
4. Modify the specified contact information
5. Show All contacts

6. Clear all contacts

// Header file:

# Ifndef _ CONTACT __# define NAME_MAX 20 # define SEX_MAX 5 # define TELE_MAX 20 # define ADDR_MAX 30 # define SB_MAX 1000 # include <stdio. h> # include <string. h> typedef struct sb {char name [NAME_MAX]; char sex [SEX_MAX]; int age; char tele [TELE_MAX]; char addr [ADDR_MAX];} * sbcon; typedef struct contact {struct sb telen [SB_MAX]; int count;} * pCon; void add (pCon pcon); void dele (pCon pcon); void search (pCon p Con); void modify (pCon pcon); void show (pCon pcon); void clear (pCon pcon); # endif function functions are as follows: <pre name = "code" class = "cpp"> # include "contact. h "int find (pCon pcon, char * name) {int I = 0; while (I <pcon-> count) {if (strcmp (name, pcon-> telen [I]. name) = 0) return I; I ++;} return-1;} // add the contact void add (pCon pcon) {// determine if the phone book is full if (pcon-> count = SB_MAX) {printf ("the phone book is full \ n"); return;} else {printf ("Name: "); scanf (" % s ", (pcon-> telen [Pcon-> count]). name); printf ("Gender:"); scanf ("% s", (pcon-> telen [pcon-> count]). sex); printf ("Age:"); scanf ("% d", & (pcon-> telen [pcon-> count]). age); printf ("contact number:"); scanf ("% s", (pcon-> telen [pcon-> count]). tele); printf ("Home Address:"); scanf ("% s", (pcon-> telen [pcon-> count]). addr); pcon-> count ++ ;}// Delete the contact void dele (pCon pcon) {int ret = 0; char tname [NAME_MAX]; printf ("Enter the name to delete:"); scanf ("% s", tname); ret = find (pcon, tname); if (- 1! = Ret) {int j = ret; for (; j <pcon-> count-1; j ++) {pcon-> telen [j] = pcon-> telen [j + 1];} pcon-> count --;} elseprintf ("the person you want to delete \ n" is not found);} // find the contact void search (pCon pcon) {int ret = 0; char tname [NAME_MAX]; printf ("Enter the name to be modified:"); scanf ("% s", tname); ret = find (pcon, tname); if (-1! = Ret) {printf ("% s \ t % d \ t % s \ t \ n ", (pcon-> telen [ret]). name, (pcon-> telen [ret]). sex, (pcon-> telen [ret]). age, (pcon-> telen [ret]). tele, (pcon-> telen [ret]). addr);} elseprintf ("the person you are looking for \ n" is not found);} // modify the contact void modify (pCon pcon) {int ret = 0; char tname [NAME_MAX]; printf ("Enter the name to be modified:"); scanf ("% s", tname); ret = find (pcon, tname ); if (-1! = Ret) {printf ("name:"); scanf ("% s", (pcon-> telen [ret]). name); printf ("Gender:"); scanf ("% s", (pcon-> telen [ret]). sex); printf ("Age:"); scanf ("% d", & (pcon-> telen [ret]). age); printf ("contact number:"); scanf ("% s", (pcon-> telen [ret]). tele); printf ("Home Address:"); scanf ("% s", (pcon-> telen [ret]). addr);} elseprintf ("the person you are looking for is not found \ n");} // display void show (pCon pcon) {int I = 0; (; I <(pcon-> count); I ++) {printf ("% s \ t % d \ t % s \ t \ n", (pcon-> telen [I]). name, (pcon-> telen [I]). sex, (pcon-> telen [I]). age, (pcon-> telen [I]). tele, (pcon-> telen [I]). addr) ;}}// clear void clear (pCon pcon) {pcon-> count = 0; printf ("the phone book is empty \ n ");}

// Main function:
 

# Include "contact. h "void shou_menue () {printf ("********************************* \ n" ); printf ("* 1: add contact 2: Delete contact * \ n"); printf ("* 3: Find contact 4: Modify contact * \ n "); printf ("* 5: Show contacts 6: Clear contacts * \ n "); printf ("********************************* \ n ") ;} int main () {struct contact my_contact; int input = 1; my_contact.count = 0; while (input) {shou_menue (); printf ("select the operation you want :"); scanf ("% d", & input); switch (input) {case 1: add (& my_contact); break; case 2: dele (& my_contact); break; case 3: search (& my_contact); break; case 4: modify (& my_contact); break; case 5: show (& my_contact); break; case 6: clear (& my_contact); break; default: break;} return 0 ;}

The running result is as follows:

Add and display contacts:

Delete contact:

Find contacts:

Modify contact:

Clear contacts:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.