"C Language" implements an address book

Source: Internet
Author: User

Implement an address book;
Contacts can be used to store 1000 of people's information, and each person's information includes:
Name, gender, age, telephone, address

Available methods:
1. Add Contact information
2. Delete the specified contact information
3. Find the specified contact information
4. Modify the specified contact information
5. Show all contact information

6. Clear All Contacts

Header file:

#ifndef _contact__#define _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 pCon); void Modify (PCon pCon); void Show (PCon pCon); void cl Ear (PCon pCon), #endif function functions 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 contact void Add (PCon pCon) {//Determine if the phone book is full if (Pcon->count = = Sb_max) {printf ("phone book is full \"); return; else{printf ("Name:"); scanf ("%s", (Pcon->telen[pcon->count]). Name);p rintf ("Gender:"); scanf ("%s", pcon->telen[ Pcon->count]). Sex);p rintf ("Age:"), scanf ("%d",& (Pcon->telen[pcon->count]). Age);p rintf ("ContactWords: "); scanf ("%s ", (Pcon->telen[pcon->count]). Tele);p rintf (" Home Address: "); scanf ("%s ", pcon->telen[pcon-> Count]). addr);p con->count++;}} Delete contact void dele (PCon pCon) {int ret = 0;char Tname[name_max];p rintf ("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 ("Did not find the person you want to delete \");} Find contacts void Search (PCon pCon) {int ret = 0;char Tname[name_max];p rintf ("Please enter the name to be modified:"), scanf ("%s", tname); ret = Find (PCon, Tname); if (-1! = ret) {printf ("%s\t%s\t%d\t%s\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 ("Didn't find the person you're looking for");} Modify contact void Modify (PCon pCon) {int ret = 0;char Tname[name_max];p rintf ("Please enter the name to be modified:"), scanf ("%s", tname); ret = Find (PCon, Tname); if (-1! = ret) {printf ("Name:"); scanf ("%s", (Pcon->telen[ret]). Name);p rintf ("Gender:"); scanf ("%s", pcon-> Telen[ret]). Sex);p rintF ("Age:"); scanf ("%d",& (Pcon->telen[ret));p rintf ("Contact Phone:"); scanf ("%s", (Pcon->telen[ret]). Tele); printf ("Home Address:"); scanf ("%s", (Pcon->telen[ret]). addr);} elseprintf ("Didn't find the person you're looking for");} show void Show (PCon pCon) {int i = 0;for (; i < (pcon->count); i++) {printf ("%s\t%s\t%d\t%s\t%s\t\n", (pcon->telen[ I]). Name, (Pcon->telen[i]). Sex, (Pcon->telen[i]). Age, (Pcon->telen[i]). Tele, (Pcon->telen[i]). addr);}} Empty void Clear (PCon pCon) {pcon->count = 0;printf ("phone book is empty \ n");}

Main function:

#include "contact.h" void Shou_menue () {printf ("*********************************\n");p rintf ("*  1: Add contact    2: Delete Contact *\n ");p rintf (" *  3: Find contact    4: Modify Contact *\n ");p rintf (" *  5: Show Contact    6: Clear Contact *\n ");p rintf (" ************* \ n ");} int main () {struct contact My_contact;int input = 1;my_contact.count = 0;while (input) {shou_menue ();p rintf ("Select the action 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 following is the result of the operation:

Add contacts and show contacts:

To delete a contact:

Find a Contact:

To modify a contact:

Clear Contact:


"C Language" implements an address book

Related Article

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.