/*------------------------------------------------------------------------
Author: AaronBai
Project: TelephoneSystem
State:
Creation Date: 2006-11.-2
Description:
------------------------------------------------------------------------*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
// # Include <conio. h>
# Include <curses. h>
Void addsubs (); // ADD NEW SUBSCRIBE
Void modsubs (); // MODIFY SUBSCRIBE
Void delsubs (); // DELETE SUBSCRIBE
Void demand_client (); // QUERY SUBSCRIBE
Void viewdir (); // VIEW ALL SUBSCRIBE
Void queryname (); // QUERY BY NAME
Void querynum (); // QUERY BY TELNUMER
Struct subscriber
{
Char name [20];
Char address [30];
Long int telno;
};
Int main ()
{
Int choice = 0;
While (choice! = 7)
{
/* Display menu */
Printf ("/nTELEPHONE directory system ");
Printf ("/n/n1. Add new subscriber details ");
Printf ("/n2. Modify existing subscriber details ");
Printf ("/n3. Delete existing subscriber details ");
Printf ("/n4. Display subscriber details based on telephone number ");
Printf ("/n5. Display subscriber details based on subscriber name ");
Printf ("/n6. View Directory ");
Printf ("/n7. Quit ");
Printf ("/n/nEnter choice :");
/* Accept menu choice */
Scanf ("% d", & choice );
/* Invoke appropriate function based on menu choice */
If (choice = 1)
Addsubs ();
Else if (choice = 2)
Modsubs ();
Else if (choice = 3)
Delsubs ();
Else if (choice = 4)
Querynum ();
Else if (choice = 5)
Queryname ();
Else if (choice = 6)
Viewdir ();
Else if (choice = 7)
Exit (0 );
Else
{Printf ("/n/nYou have entered an invalid menu choice. Please reenter/n .");
Getchar ();
}
}
}
// ADD NEW SUBSCRIBE
Void addsubs ()
{
Struct subscriber info;
Char reply = 'y ';
Char save = 'y ';
FILE * fp;
Fp = fopen ("telefon. dat", "a + ");
While (reply = 'y ')
{
System ("cls ");
Printf ("/nEnter Subscriber name :");
Scanf ("% s", info. name );
Printf ("/nEnter Subscriber address :");
Scanf ("% s", info. address );
Printf ("/nEnter Subscriber telephonum :");
Scanf ("% ld", & info. telno );
While (info. telno <4000000 | info. telno> 5000000)
{Printf ("The telephonum must between 4000000 & 4999999! /N ");
Printf ("/nEnter Subscriber telephonum :");
Scanf ("% ld", & info. telno );}
If (fp! = NULL)
{
Fwrite (& info, sizeof (struct subscriber), 1, fp );
}
Else
{
Printf ("/nFile open error,/n ");
Getchar ();
Return;
}
Fclose (fp );
Printf ("/nContinued or not? (Y/n ):");
Scanf ("% c", & reply );
}
Getchar ();
}
// MODIFY SUBSCRIBE
Void modsubs ()
{
Struct subscriber info;
FILE * fp;
Char amend_name [20];
Char reply = 'y ';
Char found = 'y ';
Char save = 'y ';
Int size = sizeof (struct subscriber );
While (reply = 'y ')
{
Found = 'n ';
Fp = fopen ("telefon. dat", "r + w ");
If (fp! = NULL)
{
System ("cls ");
Printf ("/nPlease input the name you want to modify :");
Scanf ("% s", amend_name );
While (fread (& info, size, 1, fp) = 1)
{
If (strcmp (amend_name, info. name) = 0)
{
Found = 'y ';
Break;
}
}
If (found = 'y ')
{
Printf ("/n ====================================== =========/n ");
Printf ("/nname: % s/n", info. name );
Printf ("/naddress: % s/n", info. address );
Printf ("/ntelephonum: % s/n", info. telno );
Printf ("/n ====================================== =========/n ");
Printf ("/nModify the record:/n ");
Printf ("/nname :");
Scanf ("% s", info. name );
Printf ("/n Home Address :");
Scanf ("% s", info. address );
Printf ("/ntelephonum :");
Scanf ("% ld", & info. telno );
Printf ("/nSave or not? (Y/n ):");
Scanf ("% c", & save );
If (save = 'y ')
{
Fseek (fp,-size, 1 );
Fwrite (& info, sizeof (struct subscriber), 1, fp );
}
}
Else
{
Printf ("/nNO such info! /N ");
}
}
Else
{
Printf ("/nFile open error,/n ");
Getchar ();
Return;
}
Fclose (fp );
Printf ("/nContinued or not? (Y/n ):");
Scanf ("% c", & reply );
}
Getchar ();
}
// Delete subscribe
Void delsubs ()
{
Struct subscriber info [100];
Struct subscriber temp_str;
Struct subscriber delete_str;
Int I = 0, j = 0;
Char reply = 'y ';
Char found = 'y ';
Char confirm = 'y ';
Char delete_name [20];
File * FP;
While (reply = 'y ')
{
System ("CLS ");
Fp = fopen ("Telefon. dat", "R ");
If (FP! = NULL)
{
I = 0;
Found = 'n ';
Printf ("/nplease input name :");
Scanf ("% s", delete_name );
While (fread (& temp_str, sizeof (struct subscriber), 1, FP) = 1)
{
If (strcmp (delete_name, temp_str.name) = 0)
{
Found = 'y ';
Delete_str = temp_str;
} // QUERY THE RECORD TO BE DELETED
Else
{
Info [I] = temp_str;
I ++;
} // STORE OTHER RECORD
}
}
Else
{
Printf ("/nFile open error,/n ");
Getchar ();
Return;
}
Fclose (fp );
If (found = 'y ')
{
Printf ("/n ====================================== =========/n ");
Printf ("/nname: % s/n", delete_str.name );
Printf ("/naddress: % s/n", delete_str.address );
Printf ("/ntelephonum: % ld/n", delete_str.telno );
Printf ("/n ====================================== =========/n ");
}
Else
{
Printf ("/nNO such info,/n ");
Getchar ();
Break;
}
Printf ("/ndelete or not? (Y/N ):");
Scanf ("% C", & confirm );
If (confirm = 'y ')
{
Fp = fopen ("Telefon. dat", "W ");
If (FP! = NULL)
{
For (j = 0; j <I; j ++)
{
Fwrite (& info [J], sizeof (struct subscriber), 1, FP );
}
Printf ("/nrecord Delete! /N ");
}
Else
{
Printf ("/nfile open error,/N ");
Getchar ();
Return;
}
Fclose (FP );
}
Printf ("/ncontinued or not? (Y/N ):");
Scanf ("% C", & reply );
}
Getchar ();
}
// Query by name
Void queryname ()
{
Struct subscriber info;
FILE * fp;
Char amend_name [20];
Char reply = 'y ';
Char found = 'y ';
While (reply = 'y ')
{
Found = 'n ';
Fp = fopen ("telefon. dat", "r + w ");
If (fp! = NULL)
{
System ("cls ");
Printf ("/nPlease input name :");
Scanf ("% s", amend_name );
While (fread (& info, sizeof (struct subscriber), 1, fp) = 1)
{
If (strcmp (amend_name, info. name) = 0)
{
Found = 'y ';
Break;
}
}
If (found = 'y ')
{
Printf ("/n ====================================== =========/n ");
Printf ("/nname: % s/n", info. name );
Printf ("/naddress: % s/n", info. address );
Printf ("/ntelephonum: % ld/n", info. telno );
Printf ("/n ====================================== =========/n ");
}
Else
{
Printf ("/nNO such info! /N ");
}
}
Else
{
Printf ("/nFile open error,/n ");
Getchar ();
Return;
}
Fclose (fp );
Printf ("/nContinued or not? (Y/n ):");
Scanf ("% c", & reply );
}
Getchar ();
}
// QUERY BY TELNUMER
Void querynum ()
{
Struct subscriber info;
FILE * fp;
Long int telephone;
Char reply = 'y ';
Char found = 'y ';
While (reply = 'y ')
{
Found = 'n ';
Fp = fopen ("telefon. dat", "r + w ");
If (fp! = NULL)
{
System ("cls ");
Printf ("/nPlease input your telephonum :");
Scanf ("% ld", telephone );
While (fread (& info, sizeof (struct subscriber), 1, fp) = 1)
{
// If (strcmp (telephone, info. telno) = 0)
If (telephone-info.telno = 0)
{
Found = 'y ';
Break;
}
}
If (found = 'y ')
{
Printf ("/n ====================================== =========/n ");
Printf ("/nname: % s/n", info. name );
Printf ("/naddress: % s/n", info. address );
Printf ("/ntelephonum: % ld/n", info. telno );
Printf ("/n ====================================== =========/n ");
}
Else
{
Printf ("/nNO such telenumer record! /N ");
}
}
Else
{
Printf ("/nFile open error,/n ");
Getchar ();
Return;
}
Fclose (fp );
Printf ("/nContinued or not? (Y/n ):");
Scanf ("% c", & reply );
}
Getchar ();
}
// SORR BY SUBSCRIBE NAME
Void sort ()
{
FILE * fp;
Struct subscriber info, temp [100],;
Int I, j, k;
Fp = fopen ("telefon. dat", "r ");
For (I = 0; (fread (& info, sizeof (struct subscriber), 1, fp) = 1; I ++)
{
Temp [I] = info;
}
For (k = 0; k <I-1; k ++)
{
For (j = k + 1; j <I; j ++)
{
If (strcmp (temp [k]. name, temp [j]. name)> 0)
{
A = temp [k];
Temp [k] = temp [j];
Temp [j] =;
}
}
}
Fclose (fp );
Fp = fopen ("telefon. dat", "w ");
If (fp! = NULL)
{
For (j = 0; j <I; j ++)
{
Fwrite (& temp [j], sizeof (struct subscriber), 1, fp );
}
}
Fclose (fp );
Getch ();
}
// VIEW ALL SUBSCRIBE
Void viewdir ()
{
Struct subscriber info;
FILE * fp;
Fp = fopen ("telefon. dat", "r ");
Sort ();
If (fp! = NULL)
{
System ("cls ");
Printf ("/nname/t/taddress/t/ttelephonum/n ");
While (fread (& info, sizeof (struct subscriber), 1, fp) = 1)
{
Printf ("/n %-24 s", info. name );
Printf ("%-25 s", info. address );
Printf ("%-12ld/n", info. telno );
}
}
Else
{
Printf ("/nFile open error,/n ");
Getchar ();
Return;
}
Fclose (fp );
Getch ();
}