C language implementation of simple address book

Source: Internet
Author: User

C language implementation of simple address book
An address book can be used to store contact information. Each person's information includes the name, gender, age, phone number, and address. add Contact Information 2. delete the specified contact information. 3. search for the specified contact information. 4. modify the specified contact information. show all contacts 6. sort all contacts by name

# Define _ CRT_SECURE_NO_WARNINGS # include <stdio. h> # include <stdlib. h> # include <string. h> enum OP // enumeration defines the switch conversion interface location {NUL, ADD, // ADD = 1 DELETE, // DELETE = 2 FIND, // FIND = 3 ALTER, // ALTER = 4 PRINT, // PRINT = 5 SORT // SORT = 6}; void menu () {printf ("*************** simple address book ************ \ n "); printf ("*************** 1. add contact information ************ \ n "); printf (" **************** 2. delete contact information ************ \ n "); printf (" **************** 3. find the contact information ******* * ***** \ N "); printf (" **************** 4. modify the contact information *********** \ n "); printf (" ***************** 5. display All contacts ************ \ n "); printf (" **************** 6. sort all contacts ************ \ n "); printf (" **************** 0. return ************ \ n ");} typedef struct man // defines the structure of the contact information {char name [15]; char sex [6]; char adrr [30]; char phone_num [10];} Man; Man man [10]; // store 10 contact information int Add_man (Man man [], int n) // Add the contact function {int I = 0; char sign = 0; // Y /N input char x [10]; while (sign! = 'N' & sign! = 'N') {printf ("phone_number:"); scanf ("% s", & man [N + I]. phone_num); printf ("name:"); scanf ("% s", & man [n + I]. name); printf ("sex:"); scanf ("% s", & man [n + I]. sex); printf ("address:"); scanf ("% s", & man [n + I]. adrr); gets (x); printf ("\ n continue to add? (Y/N) "); scanf (" % c ", & sign); I ++;} return (n + I);} int Delete_man (Man man [], int n) // Delete the contact information {char a [10]; int I = 0; int j = 0; printf ("enter the name of the contact to be deleted :"); scanf ("% s", a); while (strcmp (man [I]. name, )! = 0 & I <n) {I ++;} if (I = n) {printf ("No such person found, incorrect input! "); Return (n) ;}for (j = I; j <n-1; j ++) {strcpy (man [j]. phone_num, man [j + 1]. phone_num); strcpy (man [j]. name, man [j + 1]. name); strcpy (man [j]. sex, man [j + 1]. sex); strcpy (man [j]. adrr, man [j + 1]. adrr);} printf ("Delete success! \ N "); return (n-1);} void Alter_man (Man [], int n) // modify the contact information {char a [10]; int I = 0; printf ("enter the name of the contact to be modified:"); scanf ("% s", a); while (strcmp (man [I]. name, )! = 0 & I <n) {I ++;} if (I = n) {printf ("No such person found, incorrect input! \ N ");} printf (" phone_number: "); scanf (" % s ", & man [I]. phone_num); printf ("name:"); scanf ("% s", & man [I]. name); printf ("sex:"); scanf ("% s", & man [I]. sex); printf ("address:"); scanf ("% s", & man [I]. adrr); printf ("Alter success! \ N ");} void Find_man (Man man [], int n) // find the relevant contact {int choose = 0; printf ("******************************* \ n "); printf ("******************* \ n"); printf ("******* 1. people_name ***** \ n "); printf ("******************************* \ n "); printf ("***** 2. phone_number ***** \ n "); printf ("******************************* \ n "); printf ("choose one find way:"); scanf ("% d", & choose); while (choose! = 1 & choose! = 2) {printf ("incorrect input. Please try again! "); Scanf (" % d ", & choose) ;}if (choose = 1) {char a [10]; int I = 0; printf ("enter the name of the contact to be searched:"); scanf ("% s", a); while (strcmp (man [I]. name, )! = 0 & I <n) {I ++;} if (I = n) {printf ("\ n no such person found! \ N ");} else {printf (" phone_number: % s \ n ", man [I]. phone_num); printf ("name: % s \ n", man [I]. name); printf ("sex: % s \ n", man [I]. sex); printf ("address: % s \ n", man [I]. adrr) ;}} if (choose = 2) {char a [10]; int I = 0; printf ("Enter the contact number to search :"); scanf ("% s", a); while (strcmp (man [I]. phone_num, )! = 0 & I <n) {I ++;} if (I = n) {printf ("\ n! \ N ");} else {printf (" phone_number: % s \ n ", & man [I]. phone_num); printf ("name: % s \ n", man [I]. name); printf ("sex: % s \ n", man [I]. sex); printf ("address: % s \ n", man [I]. adrr) ;}}void Print_man (Man man [], int n) // display all contact information {int I = 0; printf ("-------------------------------------------------- \ n "); printf ("phone name gender address \ n"); printf ("-------------------------------------------- \ n"); for (I = 0; I <n; I ++) {printf ("% s \ t % s \ n", man [I]. phone_num, man [I]. name, man [I]. sex, man [I]. adrr);} printf ("---------------------------------------------- \ n");} void Sort_man (Man man [], int n) // sort all contacts by contact name {int I = 0; int j = 0; struct man tmp; for (I = 0; I <n-1; I ++) {for (j = I; j <n; j ++) {if (strcmp (man [I]. name, man [j]. name)> 0) {tmp = man [I]; man [I] = man [j]; man [j] = tmp ;}} for (I = 0; I <n; I ++) {printf ("% s \ t % s \ n", man [I]. phone_num, man [I]. name, man [I]. sex, man [I]. adrr) ;}// main function int main () {int input = 1; int ret = 0; while (input) {menu (); printf ("Enter your options:>"); scanf ("% d", & input); switch (input) {case ADD: // use the function name defined by enumeration to replace the 1, 2, 3, 4, and so on after case, so that each function call entry is meaningful, rather than the meaningless pure number ret = Add_man (man, ret); break; case DELETE: ret = Delete_man (man, ret); break; case FIND: Find_man (man, ret); break; case ALTER: Alter_man (man, ret); break; case PRINT: Print_man (man, ret); break; case SORT: Sort_man (ret); break; case 0: return 0; break; default: break;} return 0 ;}

 


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.