C language-Address Book, C language address book

Source: Internet
Author: User

C language-Address Book, C language address book
Black/white address book -- 1 -- Requirement Analysis1.1 requirements1.2 prototype display1.3 Function Analysis-- 2 -- code implementation2.1 external declarations, variables, macros2.2 module implementation

 

 

[Written at the beginning :]

"The UI interface is used to it. It's pretty good to use a command line Address Book ...』

 

-- 1 -- Demand Analysis 1.1 requirements

Implements the computing function of a simple calculator and selects specific operations using the command line method.

 

1.2 prototype display

Welcome Page:

 

1) Add a contact:

 

2) delete a contact:

 

3) modify the contact:

 

4) view contacts:

 

5) Search for contacts:

 

Database files:

 

1.3 Function Analysis

Compile the corresponding process functions according to the prototype.

/**

Analysis:

0. Initialization

Initialization Method for initializing data

Ideas:

1) attempted File Reading

2) If the operation succeeds, the file exists and the file content is read.

3) failed. The file does not exist.

1) create a file

2) number of contacts written

 

1. Add a contact

Ideas:

1) prompt the user to enter the name and phone number

2) receive user input

3) Save to the contact Array

4) write to file

2. delete a contact

Ideas:

1) Ask the user to enter the number of the data to be deleted

2) Determine whether the serial number is valid

3) Ask the user to confirm the deletion again

4) start to delete array elements.

5) delete the file content

 

3. Modify the contact

Update contact information

Ideas:

1) prompt the user to enter the number of the contact to be deleted

2) Determine whether the serial number is valid

3) Ask the user to confirm the modification again

4) start modifying user information

5) Update to file

 

4. View contacts

View All contacts

Ideas:

1) first determine whether the contact is empty

2) if it is not empty, traverse the array to list all contacts.

 

5. Search for contacts

Search for contacts by name

Ideas:

1) prompt the user to enter the name of the person to search

2) traverse the contact array to find whether the person exists.

3) if the phone number is found, the phone number is displayed.

4) if no contact information is found, no contact information is displayed.

 

6. Exit the system

*/

Knowledge points used:

Usage of constants and variables; global variables; array; branch statements; functions; macros; cyclic statements; scanf and printf; data; struct; file operations; pointer *

Difficulties:

Delete array elements (variable array, better with linked list)

 

-- 2 -- code implementation 2.1 external declarations, variables, macros

Macro to be defined

// ***** Macro definition # define N 100 // contact array length # define NAMELEN 13 // Name Length # define NUMLEN 12 // mobile phone number length //*** *******

Global Variables

// ***** Defines the variable int inputNum; // receives the input int telCount on the keyboard; // The number of contacts int flag; // receives the confirmation mark char * telBook = "telNumBook. data "; // database file //****

Function Declaration

// ***** Function declaration void doAdd (); void doDel (); void doEdit (); void doList (); void doSearch (); void startView (); int writeFile (); void init (); int validateInput (int num, int min, int max );//********

Contact struct

// ***** Contact structure typedef struct {char telName [NAMELEN]; char telNum [NUMLEN];} Person; Person contacts [N]; // contact array //*********
2.2 module implementation

1) Add a contact Module

1/** 2 * add contact 3 */4 void doAdd () {5 printf ("you have selected add contact operation \ n "); 6 printf ("enter the name of the contact to be added: (* Note that the contact name cannot contain spaces) \ n"); 7 scanf ("% s", contacts [telCount]. telName); // save to the array 8 9 printf ("Enter the phone number of the contact to be added: (* Note that the contact phone number cannot contain spaces) \ n "); 10 scanf ("% s", contacts [telCount]. telNum); 11 12 // prompt to confirm 13 printf ("are you sure you want to add \" % s \ "contact? 1. confirm 0. cancel \ n ", contacts [telCount]. telName); 14 scanf ("% d", & flag); 15 if (flag) {16 // number of contacts + 117 telCount ++; 18 // write 19 writeFile (); 20 21 printf ("\ n"); 22} 23 24}

 

2) Delete the contact Module

1/** 2 * delete a contact 3 */4 void doDel () {5 printf ("You selected to delete a contact operation \ n"); 6 doList (); 7 printf ("Enter the contact number to be deleted: \ n"); 8 9 scanf ("% d", & inputNum); 10 if (validateInput (inputNum, 1, telCount) {11 // If the input is invalid, 12 return is returned; 13} else {14 printf ("data cannot be restored after deletion, are you sure you want to delete the \ "% s \" information? 1. confirm 0. cancel \ n ", 15 contacts [inputNum-1]. telName); 16 scanf ("% d", & flag); 17 if (flag) {18 if (inputNum = telCount) {// The last element to be deleted is 19 telCount --; 20} else {// if it is not the last element, move the position of the array element 21 for (int I = inputNum; I <telCount; I ++) {22 contacts [inputNum-1] = contacts [inputNum]; 23 telCount --; // contact-124} 25} 26 // Write File 27 writeFile (); 28} 29} 30 31}

 

3) modify the contact Module

1/** 2 * edit contact 3 */4 void doEdit () {5 printf ("you have selected modify contact operation \ n"); 6 7 doList (); 8 printf ("Enter the contact number to be modified: \ n"); 9 scanf ("% d", & inputNum ); 10 11 // define a temporary array to save the name and phone number of the new contact. Do not directly modify the array 12 char tempName [NAMELEN]; 13 char tempTelNum [NUMLEN]; 14 if (validateInput (inputNum, 1, telCount) {15 return; 16} else {17 printf ("are you sure you want to modify the contact \" % s \ "information? 1. confirm 0. cancel ", 18 contacts [inputNum-1]. telName); 19 scanf ("% d", & flag); 20 21 if (flag) {22 printf ("enter a new contact name: (* Note that there cannot be spaces in the name) \ n"); 23 scanf ("% s", tempName ); 24 printf ("Enter the new contact's phone number: (* Note that the phone number cannot contain spaces) \ n"); 25 scanf ("% s", tempTelNum ); 26 27 // update array information 28 strcpy (contacts [inputNum-1]. telName, tempName); 29 strcpy (contacts [inputNum-1]. telNum, tempTelNum); 30 31 // Save the input file 32 writeFile (); 33 34} 35} 36 37}

 

4) view the contact Module

1/** 2 * Show contacts 3 */4 void doList () {5 if (telCount = 0) {6 printf ("your current contact is empty, add a partner first ~~ \ N "); 7 return; 8} 9 printf (" current contact list: \ n "); 10 for (int I = 0; I <telCount; I ++) {11 printf ("No. % d name: % s, phone number: % s \ n", I + 1, contacts [I]. telName, contacts [I]. telNum); 12} 13}

 

5) Search for contacts

1/** 2 * search for contacts 3 */4 void doSearch () {5 printf ("You are currently selecting search for contacts \ n"); 6 7 doList (); 8 char tempName [NAMELEN]; // temporary array 9 printf ("enter the name of the contact to be searched: \ n"); 10 scanf ("% s", tempName ); 11 12 int I; 13 for (I = 0; I <telCount; I ++) {14 if (strcmp (tempName, contacts [I]. telName) = 0) {15 printf ("the phone number of the contact you searched \" % s \ "is % s \ n", contacts [I]. telName, contacts [I]. telNum); 16 break; // if it is found, the loop ends 17} 18} 19 // 20 if (I = tel Count) {21 printf ("Sorry! \ "% S \" record information \ n ", tempName); 22} 23} not found}

 

6) custom validation of invalid values and file writing functions

/*** Write the number of arrays and contacts to the FILE ** @ return 1 Abnormal 0 normal */int writeFile () {// open the FILE * fp = fopen (telBook, "wb"); // write if (fp! = NULL) {// write the number of contacts to the file fwrite (& telCount, sizeof (telCount), 1, fp); // write the array to the file for (int I = 0; I <telCount; I ++) {fwrite (& contacts [I], sizeof (Person), 1, fp) ;}printf ("data updated successfully! \ N ");} else {printf (" Data Writing failed! \ N "); return 1 ;}// close the pointer fclose (fp); return 0 ;} /*** verify whether the input is valid ** @ param num the integer to be verified * @ param min minimum * @ param max maximum ** @ return 0. valid 1. invalid */int validateInput (int num, int min, int max) {if (num >=min & num <= max) {return 0 ;} else {printf ("illegal input! \ N "); return 1 ;}}

Analysis:

Why do we need to persist the number of contacts in telCount?

If the number of contacts is not persistent, no contacts will be read after each Program Restart, because the variable array is used here. Therefore, you must specify the number of persistences.

 

7) custom initialization functions

/** Initialize reading contacts and arrays from a FILE */void init () {FILE * fp = fopen (telBook, "r"); // open the FILE if (fp! = NULL) {// read the contact fread (& telCount, sizeof (telCount), 1, fp) from the file; // read the array for (int I = 0; I <telCount; I ++) {fread (& contacts [I], sizeof (Person), 1, fp) ;}} else {// if no file exists, create FILE * fp = fopen (telBook, "wr"); fwrite (telBook, sizeof (Person), 1, fp); printf ("address book created successfully ~ \ N ") ;}// close the pointer fclose (fp); printf (" Address Book initialization successful !~~ \ N ");}

Analysis:

Why is there an initialization method?

Each time the system starts, it must read the contact array and number of contacts from the file. Therefore, there must be an initialization method.

 

8) Main Interface and Main Function

/*** Main interface */void startView () {printf ("******************************** \ n "); printf ("********* welcome to C Address Book ******* \ n "); printf ("********** 1. Add a contact ******* \ n "); printf ("********* 2. delete a contact ******* \ n "); printf ("********** 3. Modify the contact ******* \ n "); printf ("********** 4. View All contacts ******* \ n "); printf ("********** 5. Search for contacts ******* \ n "); printf ("********* 6. Exit the system ******* \ n "); printf ("******************************** \ n "); printf ("select the operation between 1 and 6 \ n ");} /*** main function *** @ param argc * @ param argv ** @ return 0 program Exits normally */int main (int argc, const char * argv []) {init (); while (1) {startView (); scanf ("% d", & inputNum); switch (inputNum) {case 1: doAdd (); break; case 2: doDel (); break; case 3: doEdit (); break; case 4: doList (); break; case 5: doSearch (); break; case 6: printf ("the system is exiting... \ n "); printf (" the system exits safely. Thank you for using \ n "again); return 0; default: break ;}} return 0 ;}

 

 

[Written at the end :]

"At that time, I really want to say something. Sorry, thank you 』

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.