Address Book Management System (original)

Source: Internet
Author: User

This is the address book management system designed at the end of this semester. It is completely conceived and coded by itself, and does not refer to any other programs on the network. The program implementation is relatively simple and there is no application linked list.

/*************************************** *****************************
* Address book management system *
* Author: zzr *
* Http://blog.csdn.net/ZhengZhiRen *
* Functions of this program: 1. Interface *
* 2. Add records (keyboard input or File Import )*
* 3. Search for records *
* 4. delete a record *
* 5. Use a file (db. dat) to save data *
**
* The program is debugged under WindowsXP. sp2 + vc6.0 *
* The program also has multiple bugs *
**************************************** ****************************/

# Include "stdio. H"
# Include "stdlib. H"
# Include "string. H"
# Define back () show_menu ()

Struct record
{
Char name [20];/* name */
Char sex;/* Gender */
Long birthday;/* birthday */
Char email [20];/* E-mail */
Char Tel [15];/* phone ***** Note: Use character Arrays for storage ****/
};

Void table_head ()
/* This function creates the record table header */
{
Printf ("============================== begin ====== ===========================================/N ");
Printf ("%-20 S %-4 S %-8 S %-20 S %-15 s/n", "name", "gender", "Birthday ", "email", "phone ");
}

Void table_end ()
/* This function creates the end of the record table */
{
Printf ("================================== end ====== =======================================/N ");
}

Int readtoarray (char filename [], struct record rec [])
/* This function reads records in filename into an array and returns the number of records read */
{
Struct record * P;
Int COUNT = 0;
File * FP;

P = REC;
Fp = fopen (filename, "R ");
While (! Feof (FP ))
{
Fscanf (FP, "% S % 1 S % LD % S % s", p-> name, & P-> sex, & P-> birthday, p-> email, p-> Tel );
P ++;
Count ++;
}
Fclose (FP );

Return (count );
}

Int add ()
/* Add record. 1 is returned successfully */
{
Struct record new_record;
Struct record * P;
Char C;
File * FP;
P = & new_record;

Printf ("enter" Name and gender (male M, female f) birthday e-mail phone number ", separated by spaces:/N ");
Scanf ("% S % 1 S % LD % S % s", p-> name, & P-> sex, & P-> birthday, p-> email, p-> Tel);/* Note the input of p-> sex */
C = getchar ();/* Receives carriage return characters */
/* Add the record to DB. dat */
Fp = fopen ("DB. dat", "");
Fprintf (FP, "% S % C % LD % S % s/n", p-> name, p-> sex, p-> birthday, p-> email, p-> Tel );
Fclose (FP );
Table_head ();
Printf ("%-20 S %-4c %-8ld %-20 S %-15 s/n", p-> name, p-> sex, p-> birthday, p-> email, p-> Tel );
Table_end ();
Printf ("Records added successfully/N ");

Return (1 );
}

Int add_fromfile ()
/* Add a record from the file. 1 is returned successfully */
{
Char filename [30];/* file name */
Struct record new_record [100];/* store the added record */
Struct record * P;
Int I;
Int N;
File * FP;

Printf ("Enter the file name:/N ");
Gets (filename );
/* Read records from files */
N = readtoarray (filename, new_record );
/* Add the record to DB. dat */
P = new_record;
Fp = fopen ("DB. dat", "");
For (I = 0; I <n; I ++, P ++)
Fprintf (FP, "% S % C % LD % S % s/n", p-> name, p-> sex, p-> birthday, p-> email, p-> Tel );
Fclose (FP );
/* Display output */
P = new_record;
Table_head ();
For (I = 0; I <n; I ++, P ++)
Printf ("%-20 S %-4c %-8ld %-20 S %-15 s/n", p-> name, p-> sex, p-> birthday, p-> email, p-> Tel );
Table_end ();
Printf ("% d Records added/N", N );

Return (1 );
}

Void listall ()
/* List all records in the file dB. dat */
{
Struct record REC;
Int COUNT = 0;
File * FP;

Table_head ();
Fp = fopen ("DB. dat", "R ");
While (! Feof (FP ))
{
Fscanf (FP, "% S % 1 S % LD % S % s/n", Rec. name, & rec. sex, & rec. birthday, Rec. email, Rec. tel );
Printf ("%-20 S %-4c %-8ld %-20 S %-15 s/n", Rec. name, Rec. sex, Rec. birthday, Rec. email, Rec. tel );
Count ++;
}
Fclose (FP );
Table_end ();
Printf ("% d records/N", count );
}

Void find_record (char s [20], char C)
/* This function searches for matched records in db. dat. parameter S is the content to be searched, and parameter C is the search method */
{
Struct record rec [200];
Int N;/* Number of records */
Int I;
Int COUNT = 0;

/* Read the records in the DB. dat file into the array */
N = readtoarray ("DB. dat", REC );
/* Search for matched records and display them */
Table_head ();
Switch (c)
{
Case 'N':/* search by name */
For (I = 0; I <n; I ++)
If (strcmp (REC [I]. Name, S) = 0)
{
Printf ("%-20 S %-4c %-8ld %-20 S %-15 s/n", REC [I]. name, REC [I]. sex, REC [I]. birthday, REC [I]. email, REC [I]. tel );
Count ++;
}
Break;
Case's ':/* search by Gender */
For (I = 0; I <n; I ++)
If (REC [I]. Sex = s [0])
{
Printf ("%-20 S %-4c %-8ld %-20 S %-15 s/n", REC [I]. name, REC [I]. sex, REC [I]. birthday, REC [I]. email, REC [I]. tel );
Count ++;
}
Break;
Case 'B':/* search by birthday */
For (I = 0; I <n; I ++)
If (REC [I]. Birthday = atol (s ))
{
Printf ("%-20 S %-4c %-8ld %-20 S %-15 s/n", REC [I]. name, REC [I]. sex, REC [I]. birthday, REC [I]. email, REC [I]. tel );
Count ++;
}
Break;
Case 'E':/* search by email */
For (I = 0; I <n; I ++)
If (strcmp (REC [I]. Email, S) = 0)
{
Printf ("%-20 S %-4c %-8ld %-20 S %-15 s/n", REC [I]. name, REC [I]. sex, REC [I]. birthday, REC [I]. email, REC [I]. tel );
Count ++;
}
Break;
Case 'T':/* search by phone */
For (I = 0; I <n; I ++)
If (strcmp (REC [I]. Tel, S) = 0)
{
Printf ("%-20 S %-4c %-8ld %-20 S %-15 s/n", REC [I]. name, REC [I]. sex, REC [I]. birthday, REC [I]. email, REC [I]. tel );
Count ++;
}
Break;
}
Table_end ();
Printf ("% d matched records/N", count );
}

Void show_menu ()
/* This function displays the first menu */
{
Void menu_add ();
Void menu_find ();
Void menu_delete ();
Char C [2];
Printf ("Please select:/N1 add record/N2 search record/N3 Delete record/N4 list all records/N5 exit/N ");
Gets (C );
Switch (C [0])
{
Case '1 ':
Menu_add ();
Break;
Case '2 ':
Menu_find ();
Break;
Case '3 ':
Menu_delete ();
Break;
Case '4 ':
Listall ();
Break;
Case '5 ':
Exit (0);/* exit the Program */
Break;
}
Back ();/* return to the main menu */
}

Void menu_add ()
/* This function displays the "add record" menu */
{
Char C [2];
Printf ("select:/N1 input/N2 Add/N3 from file return/N ");
Gets (C );
Switch (C [0])
{
Case '1 ':
Add ();
Break;
Case '2 ':
Add_fromfile ();
Break;
Case '3 ':
Break;
}
}

Void menu_find ()
/* This function displays the "search records" menu */
{
Char C [2];
Char s [25];
Printf ("select: /N1 search by name/N2 search by gender/N3 search by birthday/N4 search by email/N5 search by phone/N6 return/N ");
Gets (C );
Switch (C [0])
{
Case '1 ':
Printf ("Enter name:/N ");
Gets (s );
Find_record (S, 'n ');
Break;
Case '2 ':
Printf ("Enter Gender:/N ");
Gets (s );
Find_record (S,'s ');
Break;
Case '3 ':
Printf ("Enter your birthday:/N ");
Gets (s );
Find_record (S, 'B ');
Break;
Case '4 ':
Printf ("enter email:/N ");
Gets (s );
Find_record (S, 'E ');
Break;
Case '5 ':
Printf ("Enter the phone number:/N ");
Gets (s );
Find_record (S, 'T ');
Break;
Case '6 ':
Break;
}
}

Void menu_delete ()
/* This function displays the "delete record" menu */
{
Struct record REC;
Struct record records [200];
Int I;
Int N;
Int flag = 0;
File * FP;

Printf ("enter" Name and gender (male M, female f) birthday e-mail phone number ", separated by spaces:/N ");
Scanf ("% S % 1 S % LD % S % * C", Rec. name, & rec. sex, & rec. birthday, Rec. email, Rec. tel );
/* Read records into the array */
N = readtoarray ("DB. dat", records );
N --;/* Minus 1. readtoarray reads '/N' at the end of the file as the last record, which needs to be improved */
/* Delete matching records */
Fp = fopen ("DB. dat", "W ");
For (I = 0; I <n; I ++)
{
If (strcmp (records [I]. Name, Rec. Name) = 0 &&
Records [I]. Sex = Rec. Sex &&
Records [I]. Birthday = Rec. Birthday &&
Strcmp (records [I]. Email, Rec. Email) = 0 &&
Strcmp (records [I]. Tel, Rec. Tel) = 0
)
{
Flag ++;
Continue;/* Skip matching records */
}
Fprintf (FP, "% S % C % LD % S % s/n ",
Records [I]. Name, records [I]. Sex, records [I]. Birthday, records [I]. Email, records [I]. Tel );
}
Fclose (FP );
Printf ("% d records deleted/N", flag );
}

Void main ()
{
File * FP;
/* If there is no file dB. dat, create */
Fp = fopen ("DB. dat", "");
Fclose (FP );
/* Display menu */
Show_menu ();
}

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.