Const int MaxLen = 200// can store up to 200 contact information.
char fName[20]= " tel . dat "; //Disk file: address book file name
Int Len// Global variable, the total number of current contacts in the address book.
// 1. Design of address book data structure
Typedef structure telephone
{
char name[20]; //Name
Int group; //Group: 1. Friends 2. Colleague 3. Family 4. others
Char House [15]; //Home phone
Char mobile phone [15]; //mobile phone
char email[20]; //Email
char add[20]; //address
char memo[20]; //Remarks
} telephone;
//Pointer array structure, used for sorting.
Typedef structural index
{
Int len// array length
tel * tel[MaxLen]; //Pointer array,
} index;
//display module xuanshi.cpp
Invalid list (tel[], index lianxren); //Display all contacts according to the sort of pointer array in lianxren.
Void bidding (Void); //Print the title
Void wins the standard prestige (Void); //Print footer
void PrintOne(Tel r,int I); //Only print the information of a contact R with serial number I..
Invalid printing (phone number); //Print header and contact information.
Void Yin Huan (); //Welcome interface
Int menu (void); //Main menu, displaying first-level menus: View Address Book (1), Update Address Book (2), Backup Address Book (3) and Exit (0).
int menu 2(void); //Update the address book submenu and display: add contacts (2 1), edit contacts (22), delete contacts (23) and return to the main menu (20).
//file processing module wenjiancl.cpp
Void New (telephone number [], index * lianxren); //Add a contact. Add to the first empty position in the array. After that, you need to reorder.
void Edit(Tel tel[],Index * lianx ren); //Edit the contact. Need to reorder
void Delete(Tel tel[],Index * lianx ren); //Delete the contact. Logical deletion, only assigning the name to the empty string. You need to reorder after deletion.
Void input (Tel *r, Tel tel[], int menu); //Enter the information of the contact person and identify it through the menu: Add (1 1) or Modify (13).
int chongcha(char *name,Tel tel[],char * original); //Check the weight. Please check whether there are duplicate names before adding or editing employees.
int Select(); //Select the contact serial number to avoid mistakes.
int Load(Tel tel[],Index * lianx ren); //read. Read the address book file fName into memory. classify
int Save(Tel Tel[]); //Save. Save the address book to disk with the name fName (global variable). Physical deletion is performed at this time: a contact with an empty string indicates that the contact has been deleted and will not be stored in the address book file.
int Copy(void); //Back up the address book. Copy the address book file.
Void pingypaix (telephone [], index * lianxren); //Sort by sound order.
void Xuan paixi(Index * p); //Select the sort of pointer array.
//main file module zhuwenjian.cpp
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
# include & ltconio.h & gt
# include & ltstring.h & gt
# Including "Chuangyong.h"
# contains "xuanshi.cpp"
# contains "wenjiancl.cpp"
int main()
{
Yin Huan ();
int select = 0;
tel tel[MaxLen]; //Store up to 200 contacts.
Index lianxren// An array of pointers sorted by sound order.
Load (telephone & Amplia nxren); //Read information from the address book file
//Circularly display the menu for the user to choose.
Do {
If (select & lt 10 || select==20)// main menu item < 10, or return 20 from the submenu. Then the main menu is displayed.
select = Menu();
Else if (Select & GT20 & Select & lt30)// Select the item of submenu 2 to continue displaying submenu 2.
select = menu 2();
Switch (selection)
{
Case 1:// View the address book in alphabetical order.
List (telephone, lianxren);
Printf ("\ n \ n \ tPress any key to continue ...");
getch(); //Wait for the user to see the screen clearly, conio.h
Break;
Case 2 1: // Add a contact.
New (Tel & Amplia Nxren);
Break;
Case 22: // Editing Contacts
Editor (telephone & Amplia Nxren);
Break;
Case 23: // Deleting a Contact
Delete (telephone & Amplia nxren);
Break;
Case 20: // Return to the main menu
Break;
Case 3: // Backing up Address Book
System ("CLS"); //If there is no submenu, clear the menu stdlib.h at the next higher level first.
Save (telephone); //The user may have updated it. Please save the current phone number to the address book file first.
copy();
Break;
Case 0: // Exit the program
Break;
}
} and (choose! =0);
Save (telephone); //Store the memory data in the address book file before exiting the system.
Returns 0;
}
//xuanshi.cpp menu module. Independent of other modules of the program. The menu code returned by corresponds to the operation.
//Welcome section
Invalid Yin Huan ()
{
Char A[ 100]={ "Welcome to this address book. This program was written by Fei Yao, o(∩_∩)o ...! Get in the car! ”};
int i,j,k;
for(I = 0; I<6; i++)
{
printf(" \ n ");
}
puts(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
printf(" \ t \ t ");
for(I = 0; A [me]! ='\0'; i++)
{
if(I = = 49)printf(" \ n \ n \ t \ t ");
printf("%c ",A[I]);
for(j = 0; j & lt 10000; j++)
for(k = 0; k & lt2000; k++)
;
}
Puts ("\ n \ n \ n \ t \ t 07 Wang, Computer Department Class C ");
puts(" \ n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ a ");
getch();
}
//Main menu, displaying first-level menus: View Address Book (1), Update Address Book (2), Backup Address Book (3) and Exit (0).
Int menu (invalid)
{
int I;
Char menu;
Do {
System ("CLS"); //Clear the screen, stdlib.h
for(I = 0; I<6; i++)
{
printf(" \ n ");
}
Puts ("\ n * * * * * * * main menu * * * * * * ");
puts(" * * ");
Puts(" * 1。 View address book * ";
Puts(" * 2。 Update address book * ";
Puts(" * 3。 Backup address book * ";
Puts(" * 0。 Exit the program * ";
puts(" * * ");
puts(" * * * * * * * * * * * * * * * * * * * * * * * \ n ");
Printf ("\ tPlease select:");
menu = getche();
Switch (menu)
{
Case "0":
Puts ("\ n \ n \ tExit the program!" );
Menu = menu-'0';
Break; //Jump out of the loop
Case "1":
Case "3":
Menu = menu-'0';
Break;
Case "2":
menu = menu 2();
Break;
Default value:
Puts ("\ n \ n \ tWrong choice!" );
menu = ' 9
}
} while(menu = = ' 9 '); //Returns the menu code,' 0',' 1', 20,21,22,23 and' 3' are all valid values.
Return to the menu;
}
//Update the address book submenu and display: Add contact (2 1), edit contact (22), delete contact (23) and return to (20).
int Menu2()
{
Char menu;
int I;
Do {
System ("CLS"); //Clear the screen, stdlib.h
for(I = 0; I<6; i++)
{
printf(" \ n ");
}
Puts ("\ n * * * * * * Update Address Book * * * * * ");
puts(" * * ");
Puts(" * 1。 Add contact * ";
Puts(" * 2。 Edit contact * ";
Puts(" * 3。 Delete contact * ";
Puts(" * 0。 Return to the main menu * ";
puts(" * * ");
puts(" * * * * * * * * * * * * * * * * * * * * * * * \ n ");
Printf ("\ tPlease select:");
menu = getche(); //Enter a character with no response, without pressing Enter, conio.h
Switch (menu)
{
Case "0":
Will ("\ n \ n \ return to the main menu!" );
Break; //Jump out of the loop
Case "1":
Case "2":
Case "3":
Break;
Default value:
Puts ("\ n \ n \ tWrong choice!" );
menu = ' 9
}
} while(menu = = ' 9 '); //To return the menu code, 0, 1, 2 and 3 are valid values.
Return to 20+ menu-'0';
}
//Browse section
Voidlist (tel tel [], index lianx ren)// Displays all nodes according to the sorting method of lianxren.
{
int I; //i: array subscript
if(Len==0)
{
Printf ("\ n \ tNo contact");
Return;
}
Daying bid ();
for(I = 0; I & ltLenI++) // Traverse all contacts in alphabetical order, with the serial number i+ 1.
PrintOne( *(lianxren.tel[i]),I+ 1);
dayingbiaowei();
}
Void dayingbiaotou(void) // print header
{
Printf ("\ n \ n \ tAddress Book * Contact");
printf(" \ n \ t-");
Printf ("\ n \ t%-10s%-6s%-10s%-10s", "name", "group", "home phone" and "mobile phone");
Printf ("%-10s%-10s%-10s", "email", "address" and "remarks");
printf(" \ n \ t-");
}
Void dayingbiaowei(void)// Print footer
{
printf(" \ n \ t-\ n ");
}
Void PrintOne(Tel r, int i) // Only print the information of a contact r with serial number i.
{
If (me! =0) // The contact is preceded by the serial number.
printf("\n%6d\t%- 10s ",I,r . name);
Otherwise//No serial number is added before the contact.
printf("\n\t%- 10s ",r . name);
//Group: 1. Friends 2. Colleague 3. Family 4. others
Switch (right group)
{
Case 1:
Printf("%-6s ","friend ");
Break;
Case 2:
Printf("%-6s ","colleague ");
Break;
Case 3:
Printf("%-6s ","family ");
Break;
Default value:
Printf("%-6s ","other ");
}
printf(" %- 10s %- 10s %- 10s ",r.home,r.mobile,r . email);
printf("%- 10s%- 10s ",r.add,r . memo);
}
Void Print(Tel r) // Print header and contact information.
{
Daying bid ();
PrintOne(r,0); //Only one person is printed, and the serial number is not output (the second parameter is 0).
dayingbiaowei();
}
//file processing module wenjiancl.cpp
//file processing section
Intload (teltel [],index * lianxren)//read。 Read the address book file fName into memory. classify
{
FILE * fin
int I = 0;
fin=fopen(fName," Rb ");
If (! Fin)
{
Printf ("\ n \ tAddress book does not exist, please add a contact first! \ n \ n \ Press any key to continue ... ");
getch();
Returns 0;
}
while( 1)
{
Fred (&tel [i], sizeof(Tel), 1, fin);
if(feof(fin))
Break;
i++;
}
fclose(fin);
len = I; //Update the total number of people in the address book.
Pingypaix (telephone, Lianxren); //Sort by sound order
Return I;
}
Int Save(Tel tel[]) // save。 Save the address book to disk with the name fName (global variable). Physical deletion is performed at this time: a contact with an empty string indicates that the contact has been deleted and will not be stored in the address book file.
{
FILE * fout
int i,j; //I:tel array subscript, J: actual number of people in the address book (excluding elements with empty names in TEL).
fout=fopen(fName,“WB”);
If (! fout)
{
Printf ("\ n \ tAddress book cannot be saved correctly! Press any key to continue ... ");
getch();
Returns 0;
}
//i is the subscript of the tel array, and J is the actual number of people (there are logically deleted contacts in the tel array with empty names).
for(i=0,j = 0; j & ltLen)
{
//Find a valid contact and expel logical deletion (name is empty string).
While( strcmp(tel[i]. Name, ""== 0)
i++;
fwrite(& amp; tel[i],sizeof(Tel), 1,fout);
i++;
j++;
}
fclose(fout);
Return j;
}
Int Copy(void) // Back up the address book. Copy the address book file.
{
FILE *fout,* fin
Tel * p =(Tel *)malloc(sizeof(Tel)); //Store temporary data read from the address book.
char name[20];
fin=fopen(fName," Rb ");
If (! Fin)
{
Printf ("\ n \ tNo contact!" );
Returns 0;
}
Do {
Printf ("\ n \ tPlease enter a backup file name:");
Scanf("%s ",name);
Strcat (name. bak”);
Fout=fopen (name, "WB");
If (! fout)
Printf ("\ tInvalid file name or path! \ n ");
}while(! fout);
while( 1)
{
fread(p,sizeof(Tel), 1,fin);
if(feof(fin))
Break;
fwrite(p,sizeof(Tel), 1,fout);
}
Printf ("\ n \ tBackup succeeded!" );
Free (p);
Returns1;
}
//Basic operation section
Voidnew (tel tel [], index * lianxren)// Add a contact. Add to the first empty position in the array. After that, you need to reorder.
{
int t = 0; //Find the first empty location in tel to store the newly entered contact.
if(Len & gt; =MaxLen)
{
Printf ("\ n \ n \ tAddress book is full!" );
Return;
}
//Find the first empty location t in the tel.
while( strcmp(tel[t])。 Name, ""! =0 )
t++;
Input (& phone [t], phone, 21); //Add a contact (2 1) and enter the contact information.
len++; //Contacts added 1
Pingypaix (telephone, Lianxren); //Reorder
}
Voidededit (tel tel [], index * lianx ren)// Edit contact. Need to reorder
{
int I;
List (telephone, * lianxren); //Modify in phonetic sorting order.
If(Len==0) returns;
I = Select();
print(*(lianx ren-& gt; tel[I])); //Displays the contact i selected by the user.
Input (lianxren->; Telephone [i], telephone, 23); //Enter contact information when editing (23)
Pingypaix (telephone, Lianxren); //Reorder
}
Void delete (tel tel [], index * lianx ren)// Delete the contact. Logical deletion, only assigning the name to the empty string. You need to reorder after deletion.
{
int I;
Character selection; //User selection: whether to continue to enter information other than name.
List (telephone, * lianxren); //Delete in alphabetical order
If(Len==0) returns;
I = Select();
print(*(lianx ren-& gt; tel[I])); //Displays the contact i selected by the user.
Printf ("\ n \ tAre you sure you want to delete this person? (y/n)");
select = getche();
if(select=='Y' || select=='y ')
{
strcpy(lianx ren-& gt; Telephone number [I]-> Name, ""; //Delete contact information (logical deletion): assign the name as an empty string, which meansno..
len-; //Contact minus 1
Pingypaix (telephone, Lianxren); //Reorder
}
}
Void input (tel * r, tel tel [], int menu)// Enter the information of a contact, and the menu will identify: Add (1 1) or Modify (13).
{
int select
char name[20],original[20];
If (menu = = 21)/Add, originally without a name.
strcpy(original," ");
Else // editor, it used to have a name.
strcpy(original,r-& gt; Name);
Printf ("\ n \ n \ tPlease enter contact name:");
Do {
Scanf("%s ",name);
}while(chongcha(name,tel,orignal)); //Duplicate name check. When modified, it can be the same as the original name.
strcpy(r-& gt; Name, name);
Printf ("\ tDo you want to continue typing (y/n)?" );
select = getche();
if(select=='y' || select=='Y ')
{
//group
Printf("\n\t Group ( 1。 Friends 2. Colleague 3. Family 4. Other), please enter the serial number: ");
scanf("%d ",& amp(r-& gt; Group));
while(r->; Group & lt1|| r->; group & gt4)
{
Printf ("\ n \ tInput error! Please re-enter the group serial number (1. Friends 2. Colleague 3. Family 4. Others):) ";
scanf("%d ",& amp(r-& gt; Group));
}
Printf ("\ tTel:");
scanf("%s ",r-& gt; Home);
Printf ("\ tMobile phone:");
scanf("%s ",r-& gt; Mobile);
printf(" \ tEmail:");
scanf("%s ",r-& gt; Mailbox);
Printf("\t Address: ");
scanf("%s ",r-& gt; Add);
Printf(" \ t tag: ");
scanf("%s ",r-& gt; Memo);
}
Elseif (menu = = 21)/When adding a contact (menu=2 1), the user does not enter it, and the default value is set.
{
r-& gt; Group = 4; //Default Group: Other
strcpy(r-& gt; Home, "";
strcpy(r-& gt; Move, "";
strcpy(r-& gt; Mailbox,'';
strcpy(r-& gt; Plus, "";
strcpy(r-& gt; Memo, "";
}
}
Intchongcha (char * name, tel tel [], char * original)// Check for duplicates. Before adding or editing contacts, please check whether there are duplicate names.
{
int i,j; //i: array subscript j: The actual number of people except the name is an empty string.
int dup = 0; //Duplicate name sign: 1 Duplicate name, 0 does not duplicate name.
if(strcmp(name,original)! =0)//name is not the original name.
for(i=0,j = 0; J & ltLen)// Traverse all contacts.
{
//Find a valid contact and expel logical deletion (name is empty string).
While( strcmp(tel[i]. Name, ""== 0)
i++;
If (strcmp (name, tel [I].name) = = 0)//name is not the original name, but it is a duplicate name in the address book.
{
dup = 1;
printf(" \ n \ t name! Please re-enter: ");
Break;
}
i++;
j++;
}
Return to dup
}
Int Select() // Select the contact serial number to avoid errors.
{
int I = 0;
int c; //c: the return value entered by scanf (). If the input is successful, return1; Otherwise, 0 is returned.
Printf ("\ n \ tPlease select a contact by serial number:");
c=scanf("%d ",& ampI);
While (I<1|| I > Len)
{
Printf ("\ n \ tInput error! Please select a contact by serial number: ");
if(c = = 0)fflush(stdin); //Input failed. Empty the contents of the input buffer related to the input stream stdin, stdio.h
c=scanf("%d ",& ampI);
}
Return I-1;
}
//sorting section
Void pingpaix (tel tel [], index * lianx ren)// Sort by sound order.
{
int i,j; //i: array subscript j: The actual number of people except the name is an empty string.
if( Len==0)
Return;
//Initialize the sort pointer array
lianx ren-& gt; len = 0;
for(i=0,j = 0; j & ltLen)
{
//Find a valid contact and expel logical deletion (name is empty string).
While( strcmp(tel[i]. Name, ""== 0)
i++;
lianx ren-& gt; Telephone [j] =&; Telephone [I];
j++;
i++;
lianx ren-& gt; len++;
}
//Select sorting
Lianxren;
}
Void Xuan paixi(Index * p)// Select and sort the pointer array.
{
int i,j,n = p-& gt; len
int min
Telephone * temperature;
If( n==0) returns; //There are no contacts, so sorting is not needed.
//Select sorting
for(I = 0; I<n-1; i++)
{
//Find the smallest element
min = I;
for(j = I+ 1; j & ltn; j++)
{
if(strcmp(p-& gt; Telephone number [j]-> Name, p-> Phone call [minutes]-> Name)<0)
min = j;
}
//Exchange the smallest element min, the first element i of the current unordered sequence.
If (min! =i)
{
temp = p-& gt; Telephone [I];
p->; tel[I]= p-& gt; Telephone [minutes];
p->; tel[min]= temp;
}
The end of }//I/i.
}