Contacts-----First Edition

Source: Internet
Author: User

A simple address book based on sequential table-----1#define _crt_secure_no_warnings 1#include<stdio.h> #include <stdlib.h > #include <string.h>typedef struct stu                     //defines a structure type for student information {Char name[20];char &NBSP;SEX[6];INT&NBSP;AGE;CHAR&NBSP;TELE[20];CHAR&NBSP;ADDRESS[40];} stu;typedef struct                                                 //Create a sequential table {stu  Data[1000];int i;} Suquence;int find (suquence *book, char *names) {int n = 0;while  (n  < book->i) {if  (strcmp (book->data[n].name, names)  == 0) break;n++;} if  (n != book->i) RETURN&NBSP;N;&NBsp;                       //return to this location if found return -1;                             //not found return -1}void add (Suquence *book) {if  (book->i >= 1000)                                    //stores up to 1000 messages {printf ("Address Book is full \ n");} else{printf ("Enter Name:"), scanf ("%s",  book->data[book->i].name);p rintf ("Please enter Gender:"); scanf ("%s",  book- >data[book->i].sex);p rintf ("Please enter Age:"), scanf ("%d", &book->data[book->i].age);p rintf ("Please enter Phone:"); scanf ("%s",  book->data[book->i].tele);p rintf ("Please enter Address:"), scanf ("%s",  book->data[book->i]. address); ++book->i;printf ("AddSuccess \ ");}} Void delete (suquence *book,char *names) {if  (book->i <= 0)                                 {printf ("Address Book is empty \ n");} Else{int n = 0;n = find (book, names);                        //find If by name   (n==0)         {printf ("No person \ n");} else{while  (n < book->i - 1)                              //if found, delete and overwrite {book->data[n] = book->data[n + 1] in turn;} book->i--;              &nbsp      //Delete One, the book->i of the record position is also reduced by one printf ("Delete succeeded \ n");}} Void change (suquence *book , char *names) {int ret = 0;ret =  Find (book,names);               // Find this person's location first if  (ret == -1) {printf ("No person \ n");} else{printf ("Enter Name:"), scanf ("%s",  book->data[ret].name);p rintf ("Please enter Gender:"); scanf ("%s", book-> Data[ret].sex);p rintf ("Please enter Age:"), scanf ("%d",  &book->data[ret].age);p rintf ("Please enter Phone:"); scanf ("%s",  book->data[ret].tele);p rintf ("Please enter Address:"), scanf ("%s",  book->data[ret].address);p rintf ("modification succeeded \ n");}} Void output (Suquence *book) {if  (book->i <=0) {printf ("Address Book is empty \ n");} else{int n =0;while  (n < book->i) {printf ("Name:%s  sex: %s  age:  %d  phone: %s  Address:  %s\n ",  book->data[n].name, book->data[n].sex,book->data[n ].age,book->data[n].tele,book->data[n].address); n++;}}} Void init (Suquence *book) {book->i = 0;             //as long as the book->i of the record position is 0printf ("Initialize successfully \ n");} Void sort (Suquence *book) {if  (book->i >1)                    //when there are more than 1 records in the Address book and then sort {Int i= 0;int  j = 0;int flag = 0;for  (i = 0; i < book-> i-1; i++)             {flag = 1;for   (j = 0; j < book->i - i - 1; j++) {                         if  (strcmp (book->data[j].name, book->data[j + 1].name)  <  0)   //is sorted by name {char arr[50];strcpy (arr, book->data[j].name); strcpy (book->data[j].name,  Book->data[j + 1].name); strcpy (Book->data[j + 1].name, arr); strcpy (arr,  Book->data[j].sex); strcpy (book->data[j].sex, book->data[j + 1].sex); strcpy (book-> Data[j + 1].sex, arr); int tmp = book->data[j].age;book->data[j].age =  book->data[j + 1].age;book->data[j + 1].age = tmp;strcpy (arr,  Book->data[j].tele); strcpy (Book->data[j].tele, book->data[j + 1].tele); strcpy (book- >data[j + 1].tele, arr); strcpy (arr, book->data[j].address); strcpy (Book->data[j]. address, book->data[j + 1].address); strcpy (Book->data[j + 1].address, arr) ; flag = 0;}         //if  End}if  (flag)      // If you already haveFlag is a true break;}} printf ("Sort succeeded \ n");} Void execute (Suquence book) {int n = 0;char name[20];while  (1) {printf ("Please select:" ); scanf ("%d",  &n);switch  (n) {case 0:exit (1); Break;case 1:add (&book); break;case  2:printf ("Please enter Name:"), scanf ("%s",  name);d elete (&book, name); break;case 3:printf ("Please Enter name:" ); scanf ("%s",  name); change (&book, name); break;case 4:printf ("Please enter Name:"); scanf ("%s",  name ); Int ret = find (&book, name);if  (ret ==-1) printf ("Query no fruit \ n"); elseprintf ("Name:% s  Sex: %s  Age: %d  phone: %s  address:  %s\n ", Book.data[ret].name,book.data[ret]. sex,book.data[ret].age,book.data[ret].tele,book.data[ret].address); Break;case 5:init (&book); break; Case 6:output (&book); Break;case 7:sort (&book); break;default:printf ("Invalid selection \ n"); break;}} Int main () {printf ("*******************************\n");p rintf ("*0.exit              1.add    *\n ");p rintf (" * *. deletel           3.change *\n ");p rintf (" *4.find               5.init   *\ n ");p rintf (" *6.output            7.sort    *\n\n "); Suquence book;book.i = 0;execute (book); System (" pause "); return 0;}



Add more versions later

This article is from the "11132019" blog, please be sure to keep this source http://11142019.blog.51cto.com/11132019/1771437

Contacts-----First Edition

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.