Cainiao cultivation C language design-Address Book (2)

Source: Internet
Author: User

 

The address book has been improved based on Address Book (1) and added the ability to save contacts.

Important knowledge points of C language are:

File stream operations

Code:

Main. c

 

# Include <stdio. h>

# Include "record. h"

 

Int menu_select (void );

Void hand_menu (int cmd, int * flag );

 

Int main (int argc, char * argv [])

{

Int cmd = 0;

Int flag = 1;

While (1 ){

Cmd = menu_select ();

If (cmd = '0 ')

Return 0;

Hand_menu (cmd, & flag );

}

}

 

Int menu_select (void)

{

Int select;

Printf ("<------ communication thin --------> \ n ");

Printf ("1: add contact 2: Delete contact \ n ");

Printf ("3: show all contacts 4: Save \ n ");

Printf ("0: Exit \ n ");

Printf ("enter \ n ");

Select = getch ();

While (select <'0' | select> '4 '){

Printf ("input error, please input again: \ n ");

Select = getch ();

}

Return select;

}

 

Void hand_menu (int cmd, int * flag)

{

Static ADDR * list_head = NULL;

If (1 = * flag ){

List_head = init_person (list_head );

* Flag = 0;

}

Switch (cmd ){

Case '1 ':

List_head = add_person (list_head );

Break;

Case '2 ':

List_head = del_person (list_head );

Break;

Case '3 ':

Dis_person (list_head );

Break;

Case '4 ':

Save_person (list_head );

Break;

Default:

Break;

}

} <Strong>

</Strong>

 

Record. h

 

# Ifndef _ RECORD_H _

# Define _ RECORD_H _

Typedef struct {

Char name [8];

Char tel [20];

} DATA;

 

Typedef struct node {

DATA data;

Struct node * next;

} ADDR;

 

# Define ASK (p) do {\

P = (ADDR *) malloc (sizeof (ADDR ));\

If (p = NULL) {printf ("malloc memory failed! "); Exit (-1 );}\

} While (0)

 

# Endif

Opre. c

 

# Include <stdio. h>

# Include "record. h"

 

# Define FILE_NAME "phonebook. dat"

 

ADDR * add_person (ADDR * list_head)

{

ADDR * head = list_head;

ADDR * node = list_head;

ADDR * new_p;

ASK (new_p );

New_p-> next = NULL;

Printf ("enter your name :");

Scanf ("% s", new_p-> data. name );

Printf ("Enter the phone number :");

Scanf ("% s", new_p-> data. tel );

If (! Node ){

Head = new_p;

Return head;

}

While (node-> next)

Node = node-> next;

Node-> next = new_p;

Return head;

}

 

ADDR * del_person (ADDR * list_head)

{

ADDR * node = list_head;

ADDR * head = list_head;

Char name [8];

ADDR * pre = node;

Printf ("Enter the name to delete :");

Scanf ("% s", name );

If (! Strcmp (head-> data. name, name )){

Pre = head;

Head = head-> next;

Free (pre );

Return head;

}

While (node ){

If (! Strcmp (node-> data. name, name )){

Pre-> next = node-> next;

Free (node );

Printf ("deleted successfully! \ N ");

Return;

}

Pre = node;

Node = node-> next;

}

Printf ("this name is not found! \ N ");

Return head;

}

 

Void dis_person (ADDR * list_head)

{

ADDR * node = list_head;

If (! Node)

Return;

Printf ("name number \ n ");

While (node ){

Printf ("% s \ n", node-> data. name, node-> data. tel );

Node = node-> next;

}

}

 

Void save_person (ADDR * list_head)

{

FILE * pf;

ADDR * node = list_head;

Pf = fopen (FILE_NAME, "w + ");

While (node ){

Fprintf (pf, "% s \ n", node-> data. name, node-> data. tel );

Node = node-> next;

}

Fclose (pf );

Printf ("saved successfully! \ N ");

}

ADDR * init_person (ADDR * list_head)

{

ADDR * node = list_head;

ADDR * head = list_head;

ADDR * new_node;

FILE * pf;

Char name [8];

Char tel [20];

ASK (new_node );

Pf = fopen (FILE_NAME, "r ");

If (! Fscanf (pf, "% s", new_node-> data. name, new_node-> data. tel )){

Free (new_node );

Return head;

}

Rewind (pf );

While (fscanf (pf, "% s", name, tel) = 2 ){

ASK (new_node );

New_node-> next = NULL;

Strcpy (new_node-> data. name, name );

Strcpy (new_node-> data. tel, tel );

If (! Head)

Node = head = new_node;

Else {

While (node-> next)

Node = node-> next;

Node-> next = new_node;

}

}

Return head;

}

 

 

 

 

<Pre>




From K-Style technology house

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.