[C language crazy handout] (16) C language simple Address Book (not optimized version), handout address book

Source: Internet
Author: User

[C language crazy handout] (16) C language simple Address Book (not optimized version), handout address book

Knowledge points used in Address Book Development

 

Usage of constants and variables

Global Variables

Branch statement

Function

Macro

Loop statement

Scanf and printf

Data

Struct

File

Pointer *


Classic problems solved in address book


  1. How to delete array elements (do not use linked lists)

Development and debugging environment: Mac OS x10.10 + xcode6.1

Address book main interface:


Add contact page


Delete contact page


Modify contact page


Search for contacts



# Include <stdio. h> # include <string. h>/******* macro definition *******/# define NAME_LEN 21 # define TEL_LEN 12 # define N 1000/***** function declaration * ******/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; // defines the contact array Person contacts [N]; // defines a variable for storing Int totalCount = 0; // The variable char * filePath = "telbook. data "; int main (int argc, const char * argv []) {int flag = 1; int num =-1; // initialize init (); printf ("Data initialization successful !... \ N "); while (flag) {printf ("************************** \ n "); printf ("******* welcome to 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 an operation between 1 and 6 \ n"); scanf ("% d", & num); if (num <= 0 | num> 6) {printf ("illegal input \ n");} else {// determines which function 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 complete! \ N "); flag = 0; break; default: break; }}return 0;}/*** add contact idea: 1) prompt user input name and phone number 2) receive user input content 3) Save to contact Array 4) write to file */void doAdd () {printf ("You selected to add a contact, please follow the prompts \ n"); // 1) prompt the user to enter the name and phone number printf ("Please enter the contact name: * contact names cannot contain spaces \ n "); // 2) receive user input content // 0 0 // 1 contacts [1] // 3) save to the contact array scanf ("% s", contacts [totalCount]. name); printf ("Enter the contact number: * the contact number cannot contain spaces \ n"); scanf ("% s", contacts [totalCount]. mobileNum); // The total number of contacts must be + 1 TalCount ++; // 4) write to the file writeFile ();}/*** Delete contact Function Idea: 1) Let the user enter the number of the data to be deleted 2) determine whether the number is valid 3) Ask the user to confirm the deletion again 4) start to delete the array element 5) delete the file content */void doDelete () {printf ("You selected to delete the contact, follow the prompts to operate \ n "); doList (); // 1) Ask the user to enter the number of the data to be deleted int no; printf (" Enter the number of the contact to be deleted: \ n "); scanf (" % d ", & no); // 2) Determine whether the number is legal if (no <= 0 | no> totalCount) {printf ("No. does not exist! \ N "); return;} // 3) Ask the user to confirm the deletion of int no1; printf (" Please confirm the information to be deleted again: 0. cancel 1. OK \ n "); scanf (" % d ", & no1); if (no1) {// 4) start to delete the array element // 1) the deleted element is the last element of the array, if (no = totalCount) {totalCount --;} else {// 2), which is not the last element for (int I = no; I <totalCount; I ++) {// The last element overwrites the previous element contacts [no-1] = contacts [no];} totalCount --; // 5) delete the file content writeFile () ;}}/ *** train of thought for updating the contact information: 1) prompt to enter the contact number to be modified 2) check whether the number is valid 3) ask the user to enter a new name and phone number. 4) Ask the user to confirm the change. 5) Start modifying user information 6) Update to file */void doUpdate () {printf ("You selected to modify the contact, follow the prompts to operate \ n"); doList (); // 1) Ask the user to enter the data number int no; printf ("Enter the contact number to be modified: \ n"); scanf ("% d ", & no); // 2) Determine whether the number is legal if (no <= 0 | no> totalCount) {printf ("the number does not exist! \ N "); return ;}// 3) Ask the user to enter a new name and phone number char name [NAME_LEN]; char tel [TEL_LEN]; printf ("enter a new user name: \ n"); scanf ("% s", name); printf ("enter a new phone number: \ n "); scanf ("% s", tel); // 4) Ask the user to re-confirm the modification of int no1; printf ("Please re-confirm the information to be modified: 0. cancel 1. OK \ n "); scanf (" % d ", & no1); if (no1) {// 5) start modifying user information strcpy (contacts [no-1]. name, name); strcpy (contacts [no-1]. mobileNum, tel); // 6) update to the writeFile ();}/*** to view all contact ideas: 1) first determine whether the contact is empty. 2) if not empty, the traversal count Show all contacts */void doList () {if (totalCount = 0) {printf ("your address book is empty! \ N ") ;}else {printf (" all contact information is as follows: \ n "); printf (" No. \ t name \ t phone \ n "); for (int I = 0; I <totalCount; I ++) {printf ("% d \ t % s \ n", I + 1, contacts [I]. name, contacts [I]. mobileNum) ;}} printf ("\ n") ;}/ *** find a contact based on the contact name. Thoughts: 1) prompt the user to enter the name of the person to be searched. 2) traverse the contact array and check whether the contact is found. 3) Find the contact and display the phone number. 4) If no contact is found, the following message is displayed: */void searchPersonByName () {printf ("you selected a search contact. Follow the prompts to operate: \ n"); char name [NAME_LEN]; // 1) prompt the user to enter the name of the person to search printf ("Enter the user name to search: \ N "); scanf (" % s ", name); int I; // 2) traverse the contact array to find whether the person for (I = 0; I <totalCount; I ++) {// 3) found, display the phone number if (strcmp (contacts [I]. name, name) = 0) {printf ("Your % s phone number is: % s \ n", contacts [I]. name, contacts [I]. mobileNum); // stop loop break;} // 4) No found, prompting if (I = totalCount) {printf ("you do not have this address book! \ N ");} printf (" \ n ");}/** initialization method, used to initialize data ideas: 1) try to read the file 2) if it succeeds, if the file exists, the file content is read. 3) if the file fails, the file does not exist. 1) create a file. 2) number of contacts written to the file */void init () {// read the data FILE * fp = fopen (filePath, "rb"); if (fp! = NULL) {// read, fread read the file to the array // printf ("the address book file already exists! \ N "); // first read the number of contacts fread (& totalCount, sizeof (int), 1, fp); // printf (" totalCount = % d ", totalCount); for (int I = 0; I <totalCount; I ++) {// read each piece of data cyclically fread (& contacts [I], sizeof (Person ), 1, fp) ;}} else {// creation file fp = fopen (filePath, "w "); // The number of current contacts is saved to the file // int count = 1; // 4 1 fwrite (& totalCount, sizeof (int), 1, fp ); printf ("Address Book File Created successfully! \ N ") ;}fclose (fp) ;}/ *** common file write operations (write contacts to files) Ideas: 1) write the contact length first, when the FILE is occupied, the first 4 bytes of the FILE 2) then traverses the array and writes every element of the array to the FILE */void writeFile () {FILE * fp = fopen (filePath, "wb"); if (fp! = NULL) {// 1) first write the length of the contact. When the file is occupied, the first 4 bytes of fwrite (& totalCount, sizeof (int), 1, fp ); // 2) then traverse the array and write every element of the array into the file for (int I = 0; I <totalCount; I ++) {fwrite (& contacts [I], sizeof (Person), 1, fp);} printf ("file updated successfully! \ N ") ;}fclose (fp );}


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.