"C Language Madness Handout" (16) C-Language Easy Address Book (not optimized version)

Source: Internet
Author: User
Tags fread

Knowledge points used in the development of contacts

Use of constants and variables

Global variables

Branch statements

Function

Macro

Looping statements

s canf and Prin TF

Data

Structural body

File

Pointers *


Classic issues addressed in the Book of contacts


    1. how array elements are removed (without using a linked list)

Development Debug Environment: Mac OS x10.10 + xcode6.1

Contacts Main interface:


Add a Contact interface


Delete a contact interface


Modify the Contact interface


Search for contacts



#include <stdio.h> #include <string.h>/****** macro definition *******/#define Name_len 21#define tel_len 12#define N 1000 Declaration of the/****** function *******/void doadd (); void DoDelete (); void DoUpdate (); void Dolist (); void Searchpersonbyname (); void Init ()    void WriteFile ();/****** variable definition *******/typedef struct linker{//contact name Char Name[name_len];    Contact phone Char Mobilenum[tel_len]; }person;//define Contact Group person contacts[n];//defines a variable that holds the number of the current contact int totalcount=0;//file path variable char *filepath= "Telbook.data";    int main (int argc, const char * argv[]) {int flag = 1;        int num =-1;    Initializes the Init ();        printf ("Data initialization succeeded!... \ n");        while (flag) {printf ("**************************\n");        printf ("****** Welcome to use Address Book ******\n");        printf ("****** 1, add contact ******\n");        printf ("****** 2, delete contact ******\n");        printf ("****** 3, modify contact ******\n");        printf ("****** 4, see all Contact ****\n");        printf ("****** 5, search contact ******\n"); printf ("****** 6, exit system ******\n");        printf ("**************************\n\n");        printf ("Please select an action between 1-6 \ n");        scanf ("%d", &num);        if (num<=0 | | num >6) {printf ("illegal input \ n");                    }else{//Determine which feature the user has selected switch (num) {Case 1:                    Doadd ();                Break                    Case 2:dodelete ();                Break                    Case 3:doupdate ();                Break                    Case 4:dolist ();                Break                    Case 5:searchpersonbyname ();                Break                    Case 6:printf ("exiting system ... \ n"); printf ("Exit done!                    \ n ");                    Flag = 0;                                    Break            Default:break;          }                        }                                    }  return 0;} /** * Add Contact Ideas: 1) Prompt user to enter name and phone number 2) receive user input 3) Save to Contact Group 4) write to File */void Doadd () {printf ("you choose to add a contact, follow the instructions    ("n");    1) Prompt user to enter name and phone number printf ("Please enter contact Name: * Contact person name cannot have space \ n");    2) Receive user input//0 0//1 1 contacts[1]//3) Save to contact Group scanf ("%s", contacts[totalcount].name);    printf ("Please enter contact Phone: * Contact phone cannot have space \ n");        scanf ("%s", contacts[totalcount].mobilenum);        The total number of contacts to +1 totalcount++; 4) write to file WriteFile ();} /** * Delete a contact's function ideas: 1) Let the user enter the data to be deleted number 2) determine whether the number is legal 3) Let the user confirm the deletion 4) start Delete array element 5) Delete the file contents */void doDelete () {p    rintf ("You have chosen to delete the contact, please follow the instructions \ n");    Dolist ();    1) Let the user enter the number of the data to be deleted int no;    printf ("Please enter the number of the contact you want to delete: \ n");    scanf ("%d", &no);        2) Determine if the number is valid if (no<=0 | | no >totalcount) {printf ("number does not exist!\n");    Return    }//3) Let the user confirm the deletion of int No1 again;    printf ("Please reconfirm the information you want to delete: 0. Cancel 1. OK \ n");        scanf ("%d", &no1); if (No1) {//4) begins to delete the array element//1) The deleted element is the last element of the array if (No==totAlcount) {totalcount--; }else{//2) Delete is not the last element for (int i=no; i<totalcount; i++) {//After an element overrides the previous                            Elements Contacts[no-1] = Contacts[no];        } totalcount--;        5) Delete file contents WriteFile ();    }}}/** * Update contact information ideas: 1) Prompt to enter the contact number to be modified 2) determine whether the number is legal 3) Let the user enter a new name, Phone 4) Let the user confirm the change again 5) Start to modify user information    6) Update to file */void doupdate () {printf ("You have selected to modify the contact, please follow the instructions \ n");    Dolist ();    1) Let the user enter the number of the data to be modified int no;    printf ("Please enter the number of the contact you want to modify: \ n");    scanf ("%d", &no);        2) Determine if the number is valid if (no<=0 | | no >totalcount) {printf ("number does not exist!\n");    Return    }//3) Let the user enter a new name and phone number char Name[name_len];    Char Tel[tel_len];    printf ("Please enter a new user name: \ n");    scanf ("%s", name);    printf ("Please enter a new phone number: \ n");        scanf ("%s", tel);    4) Let the user confirm the modification of int No1 again;    printf ("Please reconfirm the information to be modified: 0. Cancel 1. OK \ n");        scanf ("%d", &no1); if (No1) {    5) Start to modify user information strcpy (contacts[no-1].name, name);                    strcpy (Contacts[no-1].mobilenum, tel);    6) Update to file WriteFile (); }}/** * View All Contact ideas: 1) First determine if the contact is empty 2) if it is not empty, iterate through the array, display all contacts */void dolist () {if (totalcount==0) {p            RINTF ("Your address book is empty, no one!\n");        }else{printf ("All contact information is as follows: \ n");        printf ("number \ t name \t\t phone \ n");        for (int i=0; i<totalcount; i++) {printf ("%d\t%s\t\t%s\n", i+1,contacts[i].name,contacts[i].mobilenum); }} printf ("\ n");} /** * Find contacts based on their name: 1) Prompt user to enter the name of the person to find 2) traverse the Contact group, find out if this person 3) find out, show the phone number 4) did not find, prompt no this contact */void Sear     Chpersonbyname () {printf ("You have selected a search contact, please follow the instructions: \ n");    Char Name[name_len];    1) Prompt the user to enter the name of the person to find printf ("Please enter the user name to find: \ n");    scanf ("%s", name);    int i; 2) traverse the contact group to find out if this person for (i=0; i<totalcount; i++) {//3) found it, and show the phone number if (strcmp (contacts[i). Name, name) ==0) {printf ("The phone number of the%s you are looking for is:%s\n", contacts[i].name,contacts[i].mobilenum);        Stop loop break;    }}//4) did not find a hint that there is no such contact if (I==totalcount) {printf ("Your address book does not have this person!\n"); } printf ("\ n");} /** initialization method for initializing data ideas: 1) Try to read the file 2) if successful, the description file exists, then read the file Contents 3) is unsuccessful, the description file does not exist, 1) Create file 2) write the number of contacts */vo        ID init () {//read data file *FP = fopen (FilePath, "RB"); if (fp!=null) {//read to, Fread read the file into the array//printf ("The Contacts file already exists!                \ n ");        Number of contacts first read fread (&totalcount, sizeof (int), 1, FP);//printf ("TotalCount =%d", totalcount);        for (int i=0; i<totalcount; i++) {//loop-read each piece of data fread (&contacts[i], sizeof (person), 1, FP);        }}else{//Does not exist, create file FP = fopen (FilePath, "w");        The number of current contacts is saved to file//int count=1;                4 1 fwrite (&totalcount, sizeof (int), 1, FP); printf ("ContactsThe file was created successfully!    \ n "); } fclose (FP);}  /** * Normal Write file operation (write contacts to file) idea: 1) First write the length of the contact, occupy the first 4 bytes of the file 2) and then iterate through the array, each element of the array is written to the file */void WriteFile () {file *fp    = fopen (FilePath, "WB");        if (fp!=null) {//1) write the length of the contact first, occupy the first 4 bytes of the file fwrite (&totalcount, sizeof (int), 1, FP); 2) Then iterate through the array, writing each element of the array to the file for (int i=0; i<totalcount; i++) {fwrite (&contacts[i], sizeof (Perso        n), 1, FP); } printf ("File updated successfully!")    \ n "); } fclose (FP);}


"C Language Madness Handout" (16) C-Language Easy Address Book (not optimized version)

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.