Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File : header.h
- * Custom Header Definition
- */
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<ctype.h>
- #ifdef __linux__
- #define CLEAR_SCREEN system("clear")
- #elif _WIN32
- #define CLEAR_SCREEN system("cls")
- #endif
- #define MAX_NAME 26
- #define MAX_NO 11
- typedef struct phonebook {
- char * name ;
- char * no ;
- struct phonebook * next ;
- } phonebook ;
- /* Root Node */
- extern phonebook * head ;
- /* Temporary Storage */
- extern char temp_name[] ;
- extern char temp_no[] ;
- /* Serial no while printing */
- extern int START ;
- /* Gets Input values From User , Returns 1 if it Fails */
- extern int get_details() ;
- /* Flushing stdin */
- extern void input_flush() ;
- /* basic verbs */
- extern void add_contact ( ) ;
- extern void print_book () ;
- extern void search_contact ( ) ;
- extern void delete_contact ( ) ;
- extern void format_book ( struct phonebook ** ) ;
- /* File Operations */
- extern void file_read() ;
- extern void file_write() ;
- #include "header.h"
- char temp_name [ MAX_NAME ] ;
- char temp_no [ MAX_NO ] ;
- int START = 1 ;
- phonebook * head = NULL ;
- int run ()
- {
- char ch ;
- char c ; /* flush */
- int m =0 ;
- CLEAR_SCREEN ;
- printf ("nnt PHONEBOOK MANAGER n") ;
- printf ("nnn 1 . %-12s " , "Add Contact") ;
- printf ("nn 2 . %-12s " , "Multiple") ;
- printf ("nn 3 . %-12s " , "Search") ;
- printf ("nn 4 . %-12s " , "Delete") ;
- printf ("nn 5 . %-12s " , "Format") ;
- printf ("nn 6 . %-12s " , "Print All") ;
- printf ("{ Choice : }bbb") ;
- ch = getchar() ;
- input_flush() ;
- CLEAR_SCREEN ;
- switch ( ch )
- {
- case '1':
- if ( ! get_details() ) add_contact();
- break ;
- case '2':
- printf (" n How many Contacts ? : ") ;
- if ( scanf ("%d" , &m ) !=1 )
- break ;
- else
- input_flush() ;
- while ( m>0 )
- {
- if ( ! get_details() ) add_contact() ;
- m-- ;
- }
- break ;
- case '3':
- printf (" n Enter Part/Full Contact Name/Number : ") ;
- gets(temp_name ) ;
- search_contact() ;
- break ;
- case '4':
- printf (" n Enter Full Contact Name : ") ;
- gets(temp_name ) ;
- delete_contact() ;
- break ;
- case '5':
- printf (" n Entire Data Will Be Lost ! Continue ?? { Y/other } : ") ;
- ch = getchar () ; getchar() ;
- if ( ch == 'Y' || ch == 'y' )
- {
- format_book( &head ) ;
- printf ("n Successful ! ") ;
- }
- else
- printf ("n Aborted ! ") ;
- break ;
- case '6':
- print_book() ; break ;
- default :
- printf ("nnnThank You ! n") ;
- return 0;
- }
- input_flush() ;
- return 1 ;
- }
- int main ()
- {
- file_read() ;
- while ( run () ) ;
- file_write() ;
- return 0 ;
- }
- #include"header.h"
- /* helping subroutines */
- void new_node ( phonebook ** ) ;
- void fill_details () ;
- void print_record ( phonebook * ) ;
- void header () ;
- void footer () ;
- int duplicate_check() ;
- int error_check () ;
- void split (char * ) ;
- void lowercase (char * ) ;
- void input_flush() ;
- void add_contact ( )
- {
- /* Traversing */
- phonebook *temp = head ;
- /* Storing Temporary Address */
- phonebook * address = NULL ;
- /* Adding First Contact */
- if ( temp == NULL )
- {
- new_node ( &head ) ;
- fill_details ( head ) ;
- return ;
- }
- /* Modifying Root Node */
- if ( strcmp ( temp->name, temp_name ) >0 )
- {
- new_node ( &head ) ;
- fill_details ( head ) ;
- head->next = temp ;
- return ;
- }
- /* Adding Upcoming */
- while ( temp->next !=NULL )
- {
- if ( strcmp( temp->next->name , temp_name ) < 0 )
- temp=temp->next ;
- else
- break ;
- }
- /* Contact to add in the middle of two nodes */
- if ( temp->next != NULL )
- {
- address = temp->next ;
- new_node ( &temp->next ) ;
- fill_details ( temp->next ) ;
- temp->next->next = address ;
- return ;
- }
- /* Adding contact at the end ( appending node at the end ) */
- else
- {
- new_node ( &temp->next ) ;
- fill_details ( temp->next ) ;
- temp->next->next = NULL ;
- return ;
- }
- }
- void search_contact ()
- {
- phonebook * temp = head ;
- /* How many contacts matched */
- int cnt =0 ;
- header () ;
- START = 1 ;
- if ( head ==NULL)
- {
- footer () ;
- printf("nn Phone Book is Empty ..! ") ;
- return ;
- }
- /* String to be searched is read to ' temp_name' */
- while ( temp!= NULL )
- {
- if ( strstr (temp->name , temp_name ) !=NULL || strstr(temp->no , temp_name )!=NULL )
- {
- print_record( temp ) ; /* Detail Matched */
- cnt++ ;
- }
- temp=temp->next ;
- }
- footer () ;
- printf ("nn %d Contact%s have been Matched ! n " , cnt , cnt==1?"":"s" ) ;
- }
- void delete_contact( )
- {
- /* Traversing */
- phonebook * temp = head ;
- /* Storing Temporary Address */
- phonebook * address = NULL ;
- if ( head ==NULL)
- {
- printf("nn Phone Book is Empty ..! ") ;
- return ;
- }
- if ( strcmp( head->name , temp_name ) == 0 )
- {
- address = head ->next ;
- free( head ) ;
- head = address ;
- printf("nn Contact deleted successfully...!! ") ;
- return ;
- }
- while ( temp->next !=NULL )
- {
- if ( strcmp (temp->next->name , temp_name ) == 0 )
- {
- address = temp ->next->next ;
- free ( temp->next ) ;
- printf("nn Contact deleted successfully...!! ") ;
- temp->next = address ;
- return ;
- }
- else
- temp=temp->next ;
- }
- printf ("nn Contact Does not Exist ! n ") ;
- }
- void print_book ( )
- {
- phonebook *temp = head ;
- /* Serial no is reset to 1 */
- START = 1 ;
- printf ("n Complete List n") ;
- header () ;
- if ( head ==NULL)
- {
- footer () ;
- printf("nn Phone Book is Empty ..! ") ;
- return ;
- }
- while ( temp!= NULL)
- {
- print_record( temp ) ;
- temp = temp->next ;
- }
- footer () ;
- }
- void file_read ()
- {
- char data[44] ;
- int i=-1 ;
- char ch ;
- FILE * fp1 ;
- fp1 = fopen ("contacts.txt" , "r") ;
- while ( ( ch = getc (fp1) ) != EOF )
- {
- if ( ch == 'n')
- {
- data[++i] ='