Rookie how to quickly understand the implementation of the Address Book-static method

Source: Internet
Author: User
Tags strcmp

There is not a sentence called students to students as teachers, students are the most easy to understand? Yes, I am the rookie to tell you about the implementation of this address Book project, so that you can more clearly understand the mystery.

This code is not too much optimization, just for the code can let more people read, so it is not the best code, you can understand the future to do the optimization.

The knowledge of code is the most basic operation in C language.

Project requirements:

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

7. Sort all contacts by name


Code implementation:

AddRess.h

 

#ifndef  __ADDRESS_H__#define __ADDRESS_H__#include <stdio.h> #include  <stdlib.h > #include  <assert.h> #include  <string.h> #define   MAX 10      //Here I take a smaller value for debugging convenience enum op{ exit = 0,//exit  ADD,      //Add contacts  del,     //Delete contacts  seek,    //find contacts  revise,  //Modify Contact  show,    //Show All contacts  empty,   // Clear all contacts  sort,    //by name all contacts};//name, gender, age, phone, address typedef struct pesonmessage{  char name[20]; char sex[5]; int age; char tel[15]; char  ADDRESS[20];} pesonmessage;//directory member structure; typedef struct peson{ pesonmessage people[max]; int size ;} Peson,*ppeson;void print_address (); void add_address (Ppeson peson); Void Show_AddRess (PPeson  peson); void empty_address (Ppeson peson); void del_address (Ppeson peson); Void Seek_AddRess ( Ppeson peson); void revise_address (Ppeson peson); void sort_address (PPeson peson); void  cheak (Ppeson peson); #endif//__address_h__

Address.c

#define  _CRT_SECURE_NO_WARNINGS 1#include  "AddRess.h" void print_address () { printf ("* * \//output to ensure that the screen is clean and tidy  printf ("*****0.    exit system    *** **\n ");//Let others have read the interest  printf (" *****1. Add contact information *****\n ");  printf (" *****2. Delete contact information *****\n ");  printf (" * 3. Find contact information *****\n ");  printf (" *****4. Modify contact information *****\n ");  printf (" *****5. Show contact Information *****\n ");  printf ("*****6. Empty all Contacts *****\n"),  printf ("*****7. Name sort contact *****\n");  printf ("************************** \ n ");} Void add_address (Ppeson peson) { assert (Peson);//assert that the pointer is valid &NBSP;IF (peson->size <  MAX)//Determine if peson->size is legal  {  //simple input and output   printf ("Add contact information \ n");//name, gender, age, phone, address    printf ("Please enter contact name: \ n"); &NBSP;&NBSP;SCANF ("%s",& (peson->people[peson->size].name));   printf ("Please enter the gender of the contact: \ n"); &NBSP;&NBSP;SCANF ("%s",& (Peson->people[peson->size].sex));   printf ( "Please enter the age of the contact person: \n "); &NBSP;&NBSP;SCANF ("%d ",& (peson->people[peson->size].age));    printf (" Please enter a contact's phone: \ n "); &NBSP;&NBSP;SCANF ("%s ",& (Peson->people[peson->size].tel));   printf (" Please enter the address of the contact: \ n "); &NBSP;&NBSP;SCANF ("%s",& (peson->people[peson->size].address));   peson->size++;// Each time you add a contact, the valid size is +1 } else {  printf ("Address Book is full \ n");//If peson->size >=  Max }}void show_address (Ppeson peson) { int i = 0; assert (Peson);// Asserts whether the pointer is valid  printf ("%5s\t", "name");//%xs   the way the string is printed from the far right to align   x but it's a random value, here's a smaller number   printf ("%5s\t", "Sex"),  printf ("%5s\t", "Age"),  printf ("%5s\t", "tel"),  printf ("%5s\t", "address");  printf ("\ n"); for  (i = 0;i <peson->size;i++)  {  printf ("% 5s\t ", peson->people[i].name);   printf ("%5s\t ", Peson->people[i].sex);   printf ("%5d\t " , peson->people[i].age);  printf ("%5s\t", Peson->people[i].tel);   printf ("%5s\t", peson->people[i].address);   printf ("\ n");  }}void empty_address (Ppeson peson) { assert (Peson);//assert pointer is valid  peson->size = 0;} Void del_address (Ppeson peson) { char name[20] = {0}; int i =  0; assert (Peson);//assert that the pointer is valid  if  (peson->size == 0)  {  printf (" The address book is empty \ n "),  } printf (" Enter the name of the contact you want to delete "),  scanf ("%s ", &name); for  (i = 0;i  < peson->size; i++)  {  if  (strcmp (Name,peson->people[i].name) &NBSP;==&NBSP;0)//Determine whether the name entered is consistent with the names of the contacts   {   for  (;i < peson->size; i++)    {    peson->people[i] = peson->people[i +  1];//when a consistent name is found, all valid values from the next subscript of the current underlying are assigned to the previous &NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;                                    // Overwrite the values that need to be removed, note that there is no forward assignment from the last valid value, the    peson->size--;//loses one data at a time, and the size of the data drops one   }   } if  (i == peson->size)  {  printf ("Sorry, could not find the contact you want to delete \ n");  }}void seek_address (Ppeson peson) { char name[20] = {0}; int i  = 0; assert (Peson);//assert that the pointer is valid  if  (peson->size == 0)  {   printf ("Address Book is empty \ n"),  } printf ("Enter the name of the contact you want to find"),  scanf ("%s", &name); for  (i =  0;i < peson->size; i++)  {  if  (strcmp (Name,peson->people[i]. Name)  == 0)   {   printf ("%5s\t", "name"),    printf ("%5s\t", " Sex ");    printf("%5s\t", "Age"),    printf ("%5s\t", "tel"),    printf ("%5s\t", "Address");    printf ("\ n");    printf ("%5s\t", Peson->people[i].name);    printf (" %5s\t ", peson->people[i].sex);    printf ("%5d\t ", Peson->people[i].age);    printf ("%5s\t", Peson->people[i].tel);    printf ("%5s\t", peson->people[i].address);    printf ("\ n");   }  }}void revise_address (Ppeson peson) { char  name[20] = {0}; char char_tmp[20] = {0}; int int_tmp =  0; int i = 0; int num = 0; assert (Peson);//asserts whether the pointer is valid &NBSP;IF   (peson->size == 0)  {  printf ("Address Book is empty \ n"),  } printf ("Please enter the name of the contact you want to modify" ); &NBSP;SCANF ("%s", &name); for  (i = 0;i < peson->size; i++)   {  if  (STRCMP (name,peson->people[i].name)  == 0)   {   printf ("1.name\n");    printf ("2.sex\n");    printf ("3.age\n");    printf ("4.tel\n");    printf ("5.address\n");    printf ("Please enter the corresponding sequence number to be modified: \ n"); &NBSP;&NBSP;&NBSP;SCANF ("%d", &num) ;   switch  (num)    {   case 1:     printf ("Please enter a contact name \ n"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", &char_tmp);     strcpy (peson- &GT;PEOPLE[I].NAME,CHAR_TMP);    break;   case 2:     printf ("Please enter the contact gender \ n"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", &char_tmp);     strcpy ( PESON-&GT;PEOPLE[I].SEX,CHAR_TMP);    break;   case 3:     printf ("Please enter contact age \ n"),     scanf ("%d", &int_tmp);     peson->people[i].age  = int_tmp;    break;   case 4:    printf (" Please enter the contact phone \ n "); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s ", &char_tmp);     strcpy (peson->people [i].tel,char_tmp);     break;   case 5:    printf (" Please enter the contact address \ n "); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s ", &char_tmp);     strcpy (peson->people [i].address,char_tmp);     break;   default:    printf (" Sorry, input error \ n ");     break;   }  }  }}void sort_ AddRess (Ppeson peson)//bubble sort { char name[20] = {0}; int i = 0;  int j = 0; pesonmessage tmp; assert (Peson);//assert that the pointer is valid  if  (peson- >size == 0)  {  printf ("Address Book is empty \ n"); } for  (i = 0; i  < peson->size-1; i++)//Sort the number of trips  {  for  (j = 0;j < peson->size -  i - 1;j++)//Comparison   {   if  (strcmp (Peson->people[j].name, Peson->people[j + 1].name)  < 0)    {    //strcpy ( Tmp,peson->people[j].name);     //just started missing out on two sets of names.     //strcpy ( Peson->people[j].name,peson->people[j + 1].name);     //strcpy (peson-> PEOPLE[J&NBSP;+&NBSP;1].NAME,TMP);    tmp = peson->people[j];     peson->people[j] = peson->people[j+1];    peson->people[j+1 ] = tmp;   }  } }}void cheak (Ppeson peson) { int  Index = 0; while (1)  {  print_address ()   printf ("Please select: \ n");   scanf ("%d", &index);  &Nbsp;switch (Index)   {  case exit:   exit (0);   break;   case add:   add_address (Peson);   break;  case  Del:   del_address (Peson);   break;  case seek:    Seek_address (Peson);    break;  case revise:   revise_address ( Peson);    break;  case show:   show_address (Peson);    break;  case empty:   empty_address (Peson);   break;   case sort:   sort_address (Peson);   break;  default:    printf ("Incorrect input \ n");    break;  } }}

TEST.c

#define _crt_secure_no_warnings 1#include "AddRess.h" int main () {Peson peson; peson.size = 0; Cheak (&peson); System ("pause"); return 0;}

650) this.width=650; "style=" Float:none; "title=" Select the function to be implemented "src=" http://s5.51cto.com/wyfs02/M02/80/F7/ Wkiom1dfcq7bmq3saaammqlsg8a627.png "alt=" Wkiom1dfcq7bmq3saaammqlsg8a627.png "/>

650) this.width=650; "style=" Float:none; "title=" Add Contact "src=" http://s5.51cto.com/wyfs02/M00/80/F7/ Wkiom1dfcq7aikbzaaavkyppv-a941.png "alt=" Wkiom1dfcq7aikbzaaavkyppv-a941.png "/>

650) this.width=650; "style=" Float:none; "title=" Display enter three contact information "src=" http://s5.51cto.com/wyfs02/M00/80/F7/ Wkiom1dfcq-gkdumaaahrqaeiss700.png "alt=" Wkiom1dfcq-gkdumaaahrqaeiss700.png "/>

650) this.width=650; "style=" Float:none; "title=" Find information named Xiao "src=" http://s2.51cto.com/wyfs02/M01/80/F5/ Wkiol1dfcheiadbeaaaib1fiqhu626.png "alt=" Wkiol1dfcheiadbeaaaib1fiqhu626.png "/>

650) this.width=650; "style=" Float:none; "title=" Modify contact information "src=" http://s5.51cto.com/wyfs02/M02/80/F7/ Wkiom1dfcsoz1z26aaajy0sbazq625.png "alt=" Wkiom1dfcsoz1z26aaajy0sbazq625.png "/>

650) this.width=650; "style=" Float:none; "title=" After modifying the address show contact information "src=" http://s5.51cto.com/wyfs02/M02/80/F5/ Wkiol1dfchfa0b3zaaasj5okffw503.png "alt=" Wkiol1dfchfa0b3zaaasj5okffw503.png "/>

650) this.width=650; "style=" Float:none; "title=" Sort Contacts "src=" with bubbles http://s1.51cto.com/wyfs02/M00/80/F5/ Wkiol1dfchrtraquaaafzcpvjq0794.png "alt=" Wkiol1dfchrtraquaaafzcpvjq0794.png "/>

650) this.width=650; "style=" Float:none; "title=" Delete contact "src=" http://s2.51cto.com/wyfs02/M01/80/F5/ Wkiol1dfchyc0-yiaaahqddyweg643.png "alt=" Wkiol1dfchyc0-yiaaahqddyweg643.png "/>

650) this.width=650; "style=" Float:none; "title=" clear Address Book "src=" http://s3.51cto.com/wyfs02/M02/80/F5/ Wkiol1dfch3t6axsaaafys4psyg153.png "alt=" Wkiol1dfch3t6axsaaafys4psyg153.png "/>

This article is from the "11500574" blog, please be sure to keep this source http://11510574.blog.51cto.com/11500574/1783105

Rookie how to quickly understand the implementation of the Address Book-static method

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.