The realization of the method of address book structure and analysis of some problems existing in VS

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


The functions are as follows:
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




Modular Design:




header file struct and corresponding function definition, declaration

#include <stdio.h>  #include <string.h>  #include <stdlib.h>  #include <memory.h>  #define MAX  #define NAME_LENGTH  #define SEX_LENGTH 5 #define AGE_LENGTH 3 #define TELE_LENGTH  #define ADDR_LENGTH/  * struct for storing contact person information */struct Contactsuser{char Name[name_length];char sex[sex_length];/* vs compiler scanf_s for length protection so use a character array to save the age */char Age[age_length];char Tele[tele_length];char addr[addr_length];};/ * Structure to load the previous structure and create variable record number of */struct contacts{struct contactsuser person[max];int user_count;}; typedef struct CONTACTS *pcontacts;int add_contacts (pcontacts pcon);//Add function int dele_contacts (pcontacts pcon);//delete function int Clear_contacts (pcontacts Pcon);//empty function int find_contacts (pcontacts pcon);//Lookup function int modify_contacts (pcontacts pcon);// Modify the function void Show_contacts (pcontacts pcon);//Show function void menu ();//Main Menu </span>


#include "contacts.h"/* function functions *//* menus */void menu () {printf ("Contacts \ n");p rintf ("\ n");p rintf ("1.  Add the Users_info \ n ");p rintf (" 2.  Delete the Users_info \ n ");p rintf (" 3.  Clean all the users_info\n ");p rintf (" 4.  Find the Users_info \ n ");p rintf (" 5.  Modify the users_info\n ");p rintf (" 6.  Show all the users_info\n ");p rintf (" 7. exit\n ");p rintf (" \ n ");} /* Query entity functions are used to compare input user characteristics and storage (strcmp) to facilitate the invocation of other function functions */int find_entry (pcontacts pcon) {int i = 0;char Name[name_length];p rintf ("Please input name:"); scanf_s ('%s ', name,name_length); for (i = 0; I < pcon->user_count; i++) {if (strcmp (pcon->person[i].name, name) = = 0)//input and store for comparison {return i;}} return-1;} /* Add function */int add_contacts (pcontacts pcon) {if (Pcon->user_count = MAX) {printf ("Telephone book is full! \ n "); return-1;} else{printf ("Please input name:"); the/*scanf_s security function should add a control length parameter */scanf_s ("%s", Pcon->person[pcon->user_count]. Name, name_length);/* Array from subscript 0 to subscript user_count-1, then operate */printf at User_count ("Please input sex:"); SCanf_s ("%s", Pcon->person[pcon->user_count].sex, Sex_length);p rintf ("Please input:"); scanf_s ("%s", pcon- >person[pcon->user_count].age, Age_length);p rintf ("Please input Tele:"); scanf_s ("%s", pcon->person[pcon- >user_count].tele,tele_length);p rintf ("Please input addr:"); scanf_s ("%s", Pcon->person[pcon->user_count] . addr, Addr_length);p con->user_count++;//add end of staff increase by 1return 1;}}  /* Delete function */int dele_contacts (pcontacts pcon) {int i = 0;int ret = find_entry (Pcon);//define RET receive find_entry return position if (ret! =-1) {for (i = ret; i < pcon->user_count-1; i++) {Pcon->person[i] = pcon->person[i + 1];} Pcon->user_count--;return 1;} else{printf ("not exist!\n"); return-1;}} /* Empty function */int clear_contacts (pcontacts pcon) {memset (Pcon->person,0,max),/*memset function fills in memory for a given value is the quickest way to clear a large struct or array */pcon->user_count = 0; Count assignment 0 to clear 0 return 1;} /* Lookup function */int find_contacts (pcontacts pcon) {int ret = find_entry (Pcon);//define the return position of the RET receive find_entry if (ret! =-1) {printf (" Name:%-5s ", pcon->Person[ret].name, Name_length);p rintf ("sex:%-5s", Pcon->person[ret].sex, Sex_length);p rintf ("age:%-5s", pcon- >person[ret].age, Age_length);p rintf ("tele:%-5s", Pcon->person[ret].tele, Tele_length);p rintf ("addr:%-5s", PCON-&GT;PERSON[RET].ADDR, Addr_length); return 1;} else{printf ("not exist!\n"); return-1;}} /* Modify the function */int modify_contacts (pcontacts pcon) {int ret = find_entry (Pcon);//define the return position of the RET receive find_entry if (ret! =-1) {printf (" Please input name: "), scanf_s ("%s ", Pcon->person[ret].name, Name_length);p rintf (" Please input sex: "); scanf_s ("%s ", Pcon->person[ret].sex, Sex_length);p rintf ("Please input:"); scanf_s ("%s", Pcon->person[ret].age, Age_ LENGTH);p rintf ("Please input Tele:"); scanf_s ('%s ', Pcon->person[ret].tele, tele_length);p rintf ("Please input addr : "); scanf_s ("%s ", Pcon->person[ret].addr, Addr_length); return 1;} else{printf ("not exist!\n"); return-1;}} /* Display function */void show_contacts (pcontacts pcon) {int i = 0;printf ("\tname\tsex\t\tage\t\ttele\t\t\taddr\n"); for (i = 0; i < pcon->user_count; i++) {printf ("%10s\t", Pcon->person[i].name);p rintf ("%5s\t", Pcon->person[i].sex);p rintf ("%10s\t", pcon-> Person[i].age);p rintf ("%15s\t", Pcon->person[i].tele);p rintf ("%20s\t", pcon->person[i].addr);} printf ("\ n");} </span>


#include "contacts.h"  /* Main function */int main () {int input = 1;   Defines an input  initialization struct Contacts user;user.user_count = 0;//initializes the menu () for User_count, while (input) {    printf ("\ n  Enter you choice (0-7): \ n "), scanf_s ("%d ", &input), switch (input)  //switch-case use a different functional function {case 1:add_ Contacts (&user); Break;case 2:dele_contacts (&user); Break;case 3:clear_contacts (&user); Break;case 4: Find_contacts (&user); Break;case 5:modify_contacts (&user); Break;case 6:show_contacts (&user); break; Case 7:printf ("Thanks for use!\n"); break;default:printf ("Input error!\n"); return 0;}



Operation Result:





The realization of the method of address book structure and analysis of some problems existing in VS

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.