# contains "string.h"
# contains "conio.h"
# contains "stdio.h"
# contains "dos.h"
FILE * fp
int I; //i is a global variable, not a length.
Structural address
{ char postnum[ 10];
char a[40]; //Home address
};
Structural birthday
{int year;
Int month;
Int day;
};
Structural friends
{int num// serial number
char name[20];
Charming sex;
Toll phone [13];
Structural birthday birthday;
Structure address address;
}
Friends [50];
Invalid load ()
{
int j;
Long k;
fp=fopen("friend.txt "," a+"); //Open the file friend.txt
if(fp! = empty)
{
for(I = 1; I & lt50; i++)
{
j = fgetc(FP);
If(j==EOF) // Judge whether the end of the file has been reached.
Return;
k = I- 1;
fseek(fp,k*sizeof(struct ffriend),SEEK _ SET);
Fred (& friends [i], sizeof(struct ffriend), 1, fp); //Read the record from the file
}
}
other
{
fp=fopen("friend.txt "," w ");
I = 1;
}
}
Empty display (int j)
{
//friends [j]. num = I;
Printf ("\ n \ n \ tNo .. -n number: %3d ",friend [j]. num);
Printf ("\ n \ tname -Name:%-20s ",friends[j]。 Name);
Printf ("\ n \ tGender -Sex:%c ",friends[j]。 Sex);
Printf(" \ n \ tTel- Tel: %- 13s ",friend [j]. Telephone);
Printf ("\ n \ tDate of birth-date of birth:% 4d-%2d-%3d", friends [j]. birthday Year, friend [j]. Birthday, month, friends. Birthday);
printf(" \ n \ tZip code-postnum:%- 10s ",friends [j]。 address . postnum);
Printf(" \ n \ t E-mail address-address: %-40s ",friends [j]. address . a);
}
Void addition (int j)
{
fflush(stdin); //Clear the file buffer in this area, and write the buffer to the file when the file is opened in write mode.
//stdin is a standard file * (file pointer), which usually points to the input stream with a keyboard.
Friend [j]. num = j;
Printf(" \ n \ t \ t \ t tSerial number-number: %d ",j);
printf(" \ n \ t \ t \ tname-Name:");
Scanf("%s ",friend [j]. Name);
fflush(stdin); //Why is there no fflush (stdin); Will the effect automatically skip?
printf(" \ t \ t \ t gender-Sex(m/w):"); //Why do men and women who enter Chinese characters automatically skip the contact number?
Scanf("%c ",& Friend [j]. Sex);
printf(" \ t \ t \ t tel-Phone:");
Scanf("%s ",friend [j]. Telephone);
Printf("\t Date of Birth-Birthday ");
Printf(" \ n \ t \ t \ t Year: ");
Scanf("%d ",& Friend [j]. Birthday. Year);
printf(" \ t \ t \ t month-month:");
Scanf("%d ",& Friend [j]. Birthday. Month);
printf(" \ t \ t \-day:");
Scanf("%d ",& Friend [j]. Birthday);
Printf ("\ t \ t \ tPostal Code-Postal Code: ");
scanf("%s ",friends[j]. address . postnum);
Printf(" \ t \ t \ t Email address-address: ");
scanf("%s ",friends[j]. address . a);
getchar();
}
Void delete ()
{
int k;
Printf ("\ n \ tDelete serial number-serial number:");
scanf("%d ",& ampk);
if(k & lt; =i)
{
for(int j = k; j & ltI+ 1; J++) /* Element order with backward insertion position */
{
Strcpy (friend [j]. Name, friend [j+ 1]. Name); /* Exchange element content */
Friend [j]. Sex = friends [j+ 1]. Sex;
Strcpy (friend [j]. Phone, friend [j+ 1]. Telephone);
Friend [j]. Birthday. Year = Friend [j+ 1]. Birthday. Year;
Friend [j]. Birthday. Month = Friend [j+ 1]. Birthday. Month;
Friend [j]. Birthday. Day = Friend [j+ 1]. Birthday. Day;
strcpy(friends[j]. address . postnum,friends[j+ 1]. address . postnum);
Strcpy (friend [j].address.a, friend [j+1]. address.a);
}
I-;
}
other
{
Printf ("The serial number entered is too large!" );
}
}
void Modify(int j)
{
Add (j);
}
Invalid save ()
{
int j;
fp=fopen("friend.txt "," w ");
for(j = 1; j & lt= I; j++)
{
fwrite(& amp; friends[j],sizeof(struct ffriend), 1,FP);
}
fclose(FP);
}
void main()
{
int j;
Carbonization grade;
char search name[ 10];
load();
I-;
do
{
Printf ("\ t \ t \ t \ t \ tSimple Address Book \ n \ n");
Printf ("function selection \ n");
Printf (" 1A。 Read \ n ");
Printf("\2B。 append \ n ");
Printf("\6C。 insert \ n ");
Printf("\5D。 Delete \ n ");
Printf("\5E。 Search \ n ");
Printf("。 Modify \ n ");
Printf("\2G。 Save \ n ");
Printf (" 1h。 Exit \ n ");
Printf ("please choice") \ nNote: Enter capital letters of A~H \ n ");
Scanf("%c ",& grade);
Switch (grade)
//You can add uppercase and lowercase letters.
{
Case' a': j =1; //Display function
while(getchar()! = 0x 1b & amp; & ampj & lt=i) // Add and press Esc to exit.
{
show(j++);
Printf ("\ nPlease press Enter to continue!" );
}
If (j-1<; 1)
{
Printf ("\ n \ tEmpty undocumented document-empty comment \ n ");
Printf ("\ nPlease press Enter to continue!" );
getchar();
}
Printf("\n\t\t\t\t\t\t This operation has ended ");
printf(" \ n \ t-\ t \ t \ n ");
Break;
Case "b"://Add a function
While (I & lt=50) // Add the function of pressing ESC to exit.
{
i++;
Add (1);
Printf ("\ tContinue to increase? y/n”);
//If you modify and add else, you will jump out or force a printing error.
char a = getchar();
if(a=='n'||a=='N ')
Break;
}
if(i==5 1)
Printf ("\ n \ tThe document is full, so the record cannot be added-please note that it is full");
Printf("\n\t\t\t\t\t\t This operation has ended ");
printf(" \ n \ t-\ t \ t \ n ");
getchar();
Break;
Case "c":
int k;
Printf ("\ n \ tEnter the location to insert:");
scanf("%d ",& ampk);
j = I+ 1;
Friend [j]. num = j;
for(j; j & gtk; J-)// Insert function
{
Strcpy (friend [j]. Name, friend [j- 1]. Name); //Data moves backwards
Friend [j]. Sex = friends [j- 1]. Sex;
Strcpy (friend [j]. Telephone, friend [j- 1]. Telephone);
Friend [j]. Birthday. Year = Friend [j- 1]. Birthday. Year;
Friend [j]. Birthday. Month = Friend [j- 1]. Birthday. Month;
Friend [j]. Birthday. Day = Friend [j- 1]. Birthday. Day;
strcpy(friends[j]. address . postnum,friends[j- 1]. address . postnum);
Strcpy (friend [j].address.a, friend [j-1]. address.a);
}
Add (k);
i++;
Printf("\n\t\t\t\t\t\t This operation has ended ");
printf(" \ n \ t-\ t \ t \ n ");
Break;
Case' d'://Delete function//Add the function of deleting multiple records at will.
delete();
If (I< 1)
Printf ("\ nNo record-No record \ n");
Printf ("\ nPlease press Enter to continue!" );
getchar();
getchar();
Printf("\n\t\t\t\t\t\t This operation has ended ");
printf(" \ n \ t-\ t \ t \ n ");
Break;
Case "e"://Query function
Printf ("\ n \ tPlease enter the relevant name of the record to be queried:"); //Add query by column number
scanf("%s ",search name);
for(j = 1; j & lt= I; j++)
{
If (strcmp (searchname, friends [j].name)= = 0)// Compare strings.
{
Display (j);
Break;
}
}
If (I< 1)
{
Printf ("\ nThere is no record you are looking for-no record");
Printf ("\ nPlease press Enter to continue!" );
getchar();
}
Printf("\n\t\t\t\t\t\t This operation has ended ");
printf(" \ n \ t-\ t \ t \ n ");
getchar();
Break;
Case "f"://Modify function//Add query by serial number.
Printf ("\ n \ tPlease enter the related name of the record to be modified:");
Get (search name);
scanf("%s ",search name);
for(j = 1; j & lt= I; j++)
{
If (strcmp (searchname, friends [j].name)= = 0)// Compare strings.
{
Modify (j);
//getchar(); //Add output confirmation modification?
}
}
If (I <1)//modify the function,
{printf ("\ nNo record to modify-no record");
Printf ("\ nPlease press Enter to continue!" );
getchar();
getchar();
}
Printf("\n\t\t\t\t\t\t This operation has ended ");
printf(" \ n \ t-\ t \ t \ n ");
Break;
Case "g":
save(); //Save function
getchar();
Printf("\n\t\t\t\t\t\t This operation has ended ");
printf(" \ n \ t-\ t \ t \ n ");
Break;
Case "H"://Exit function
char x;
Printf ("Do you want to save it? Yes/No \ n ");
scanf("%s ",& ampx);
//getchar();
//getchar();
if(x=='y ')
{
save();
Exit (0);
}
other
Exit (0);
Default value:
Printf ("\ n \ tWrong input, please enter the correct serial number!" );
Printf ("\ nPlease press Enter to continue!" );
getchar();
Printf("\n\t\t\t\t\t\t This operation has ended ");
printf(" \ n \ t-\ t \ t \ n ");
}
} while( 1);
}