C language uses structure to implement an address book

Source: Internet
Author: User
Tags strcmp

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 following methods are available:

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. sort all contacts by name

7. Clear All Contacts

Student.h:

#ifndef  __crt#define _crt_secure_no_warnings 1#endif#ifndef __student#ifndef __stdio_ h#include <stdio.h> #endif #ifndef __stdlib_h#include <stdlib.h> #endif #ifndef __ Srtrig_h#include <string.h> #endif #ifndef __maxsize#define maxsize 1000#endiftypedef &NBSP;STRUCT&NBSP;STU{CHAR&NBSP;NAME[20];CHAR&NBSP;SEX[3];INT&NBSP;AGE;CHAR&NBSP;TEL[12];CHAR&NBSP;ADDR[20];} Stu;void menu ()/* menu */{printf ("\ n");p rintf ("************************************************************** \ n ");p rintf (" **   ☆☆☆☆★★★★★       Welcome Student Information Management System        ★★★★★☆☆☆☆**\n ");p rintf (" **    ☆☆☆★★★★★          1, add contact information         ★★★★★☆☆☆ **\ n ");p rintf (" **    ☆☆☆★★★★★       2, delete the specified contact information        ★★★★★☆☆☆ **\n ");p rintf (" **    ☆☆☆★★★★★        3, find the specified contact information       ★★★★★☆☆☆ **\n ");p rintf (" **     ☆☆☆★★★★★       4, modify the specified contact information       ★★★★★☆☆☆  **\n ");p rintf (" **    ☆☆☆★★★★★       5, show all contact information       ★★★★★☆☆☆ **\n ");p rintf (" **    ☆☆☆★★★★★       6, sort all contacts by name      ★★★★★☆☆☆ **\n ");p rintf (" **     ☆☆☆★★★★★         7, clear all contacts          ★★★★★☆☆☆ **\n ");p rintf (" **    ☆☆☆★★★★★             0, exit system             ★★★★★☆☆☆ **\ n ");p rintf (" ***************************************************************************\n ");p rintf (" \ n ");} Void add (Stu *stu, int *count)/* Add contact Information */{if  ((*count)  >= maxsize -  1) {printf ("The system has reached the maximum occupancy, can not add students!\n"); return; printf ("Please enter the name of the student to be added:> "); scanf ("%s",  (stu +  (*count))->name); flag:printf (" Please enter the name of the student you want to add (male/female):>  "), scanf ("%s ",  (stu +  (*count))->sex);if  ((strcmp  +  (*count)->sex,  "male")  != 0)  && (strcmp (stu +  (*count))- >sex,  "female")  != 0) {printf ("Please enter the correct gender!\n"); goto flag;} printf ("Please enter the age of the student to be added:> "), scanf ("%d", & (stu +  (*count))->age);p rintf (" Please enter the phone number of the student you want to add:>  "); scanf ("%s ",  (stu +  (*count))->tel);p rintf (" Enter the address of the student you want to add:>  scanf ("%s",  (stu +  (*count))->addr);p rintf ("Add success!\n");(*count) ++;/* The number of people plus 1*/}void  Delete (stu *stu,  int *count)/* Delete the specified contact information */{char _name[20];if  ((*count)  <= 0) {printf ("There are no students in this student system!\n "); return;} printf ("Please enter the name of the student you want to delete:> "); scanf ("%s",  _name);for  (int i = 0; i  <  (*count);  i++) {if  (strcmp ((stu + i)->name, _name)  == 0) {for   (int j = i; j <  (*count)  - 1; j++) {strcpy (stu + &NBSP;J)->name,  (stu + j + 1)->name); strcpy ((stu + j)->sex,  ( stu + j + 1)->sex);(stu + j)->age =  (stu + j +  1)->age;strcpy ((stu + j)->tel,  (stu + j + 1)->tel); strcpy (( STU&NBSP;+&NBSP;J)->addr,  (stu + j + 1)->addr);} (*count)--;p rintf ("delete success!\n"); return;} /*if*/}/*for*/printf ("There is no such student in this student system!\n");} Void search (const stu *stu, const int count)/*Find specified contact information */{char _name[20];p rintf ("Please enter the name of the student you are looking for:> ") scanf ("%s",  _name);for  (int  i = 0; i < count; i++) {if  (strcmp (stu + i)->name, _ Name) { == 0) {printf ("*********======= you check the student's information as =======*********\n");p rintf ("    *     Name: > %s\n ",  (stu + i)->name);p rintf ("      *********    Gender: > %s\n ",  (stu + i)->sex);p rintf ("      *********    Age: > %d\n ",  (stu + i)->age);p rintf ("      *********    Tel: > %s\n ",  (stu + i)->tel);p rintf ("     *********    Address: > %s\n ",  (stu + i)->addr); return;}} /*for*/printf ("Did not find the student you are looking for!\n");} Void alter (stu *stu, const int count)/* Modify the specified contact information */{char _name[20];printf ("Please enter the name of the student you want to modify:> "); scanf ("%s",  _name);for  (int i = 0; i  < count; i++) {if  (strcmp ((stu + i)->name, _name)  == 0) {printf (" Please enter the modified name:>  "), scanf ("%s ",  (stu + i)->name);p rintf (" Please enter the modified gender:>  "); scanf ("%s ",  (stu + i)->sex);p rintf (" Please enter modified age:>  "); scanf ("%d ", & (stu + i)- >age);p rintf ("Please enter the modified phone:> "), scanf ("%s",  (stu + i)->tel);p rintf ("Please enter the modified address:>   scanf ("%s",  (stu + i)->addr);p rintf ("Modify success!\n"); return;}} /*for*/printf ("There is no student!\n you want to modify in this student system"); Void show (const stu *stu, const int count)/* Displays all contact information */{if  (count ==  0) {printf ("No student!\n in this student System");} else{printf ("   name   |   sex   |   age   |       Phone     |     Address      \n ");for  (int i = 0; i < count; i++) {printf ("%2s   |%5s  |%6d  |%13s  |%s\n ",  (stu + i)->name,  (stu +  i)->sex,  (stu + i)->age,  (stu + i)->tel,  (stu + i) -&GT;ADDR);}}} INT&NBSP;STCCMP (CONST&NBSP;VOID*NUM1,&NBSP;CONST&NBSP;VOID&NBSP;*NUM2)///quick-row comparison function */{return  (strcmp (((Stu  *) NUM1)->name,  ((stu *) num2)->name)  > 0)  ? 1 : -1;} Void sort (stu *stu, const int count)/* Sort all contacts by name */{if  (count == 0) { printf ("No student!\n in this student system"); return;} Qsort (Stu, count, sizeof (stu[0]),  stccmp);} Void empty (Stu *stu, int *count)/* Clear all Contacts */{if  (count == 0) {printf (" There is no student!\n in this student system; return;} for  (int i = 0; i < *count; i++) {* (stu + i)->name  = null;* (stu + i)->sex = null; (stu + i)->age = 0;* (stu + i) ->tel = null;* (Stu + i)->addr = null;} *count = 0;printf ("EMPTY success!");} #endif

Main program: TEST.C

#define  _CRT_SECURE_NO_WARNINGS 1#include <stdio.h> #include  <stdlib.h> #include   "Student.h" #define  maxsize 1000typedef enum en{exit,    //exit Add,      //Add delete,  //Delete search,   //find alter,     //Modify show,     //display sort,     //sort empty,     //empty}en;int main () {The existing number of people in the stu stu[maxsize];int count = 0; //system int  input = 1;while  (Input) {menu ();p rintf ("Please select the service item:> "); scanf_s ("%d", & Input) {case exit:printf ("Thank you for trying this service system, welcome to your next use of!\n");switch  ("pause"); return 0;case  add:      //adding Add (stu, &count);break;case delete:    //remove Delete (stu, &count); break;case search:    //Find Search (Stu,  count); Break;case&nbsP alter:    //Modify Alter (STU,&NBSP;COUNT); break;case show:     // Show Show (Stu, count); break;case sort:    //sort (stu, count); Show (stu, count); break;case empty:   //empty Empty (Stu, &count); Break;default: printf ("Please select the correct service item!\n"); /*switch*/}/*while*/printf ("\ n"); system ("pause"); return 0;}

Operating interface:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/76/B3/wKiom1ZZqmnRoYg3AABM6U46yHg008.png "title=" gd% (0) ' D2c]qxm (D~JR) lx4.png "alt=" Wkiom1zzqmnroyg3aabm6u46yhg008.png "/>

After entering the running interface, you can choose the right service item according to your needs.

C language uses structure to implement an address book

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.