Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.71 KB | None | 0 0
  1. /*
  2. * File : header.h
  3. * Custom Header Definition
  4. */
  5.  
  6. #include<stdio.h>
  7. #include<string.h>
  8. #include<stdlib.h>
  9. #include<ctype.h>
  10.  
  11. #ifdef __linux__
  12. #define CLEAR_SCREEN system("clear")
  13. #elif _WIN32
  14. #define CLEAR_SCREEN system("cls")
  15. #endif
  16.  
  17. #define MAX_NAME 26
  18. #define MAX_NO 11
  19.  
  20. typedef struct phonebook {
  21. char * name ;
  22. char * no ;
  23. struct phonebook * next ;
  24.  
  25. } phonebook ;
  26.  
  27.  
  28. /* Root Node */
  29. extern phonebook * head ;
  30.  
  31. /* Temporary Storage */
  32. extern char temp_name[] ;
  33. extern char temp_no[] ;
  34.  
  35. /* Serial no while printing */
  36. extern int START ;
  37.  
  38.  
  39. /* Gets Input values From User , Returns 1 if it Fails */
  40. extern int get_details() ;
  41.  
  42. /* Flushing stdin */
  43. extern void input_flush() ;
  44.  
  45. /* basic verbs */
  46.  
  47. extern void add_contact ( ) ;
  48. extern void print_book () ;
  49. extern void search_contact ( ) ;
  50. extern void delete_contact ( ) ;
  51. extern void format_book ( struct phonebook ** ) ;
  52.  
  53. /* File Operations */
  54.  
  55. extern void file_read() ;
  56. extern void file_write() ;
  57.  
  58. #include "header.h"
  59.  
  60. char temp_name [ MAX_NAME ] ;
  61. char temp_no [ MAX_NO ] ;
  62. int START = 1 ;
  63.  
  64. phonebook * head = NULL ;
  65.  
  66. int run ()
  67. {
  68. char ch ;
  69. char c ; /* flush */
  70. int m =0 ;
  71. CLEAR_SCREEN ;
  72.  
  73. printf ("nnt PHONEBOOK MANAGER n") ;
  74. printf ("nnn 1 . %-12s " , "Add Contact") ;
  75. printf ("nn 2 . %-12s " , "Multiple") ;
  76. printf ("nn 3 . %-12s " , "Search") ;
  77. printf ("nn 4 . %-12s " , "Delete") ;
  78. printf ("nn 5 . %-12s " , "Format") ;
  79. printf ("nn 6 . %-12s " , "Print All") ;
  80. printf ("{ Choice : }bbb") ;
  81.  
  82. ch = getchar() ;
  83.  
  84. input_flush() ;
  85. CLEAR_SCREEN ;
  86.  
  87. switch ( ch )
  88. {
  89. case '1':
  90. if ( ! get_details() ) add_contact();
  91. break ;
  92.  
  93. case '2':
  94. printf (" n How many Contacts ? : ") ;
  95. if ( scanf ("%d" , &m ) !=1 )
  96. break ;
  97. else
  98. input_flush() ;
  99. while ( m>0 )
  100. {
  101. if ( ! get_details() ) add_contact() ;
  102. m-- ;
  103. }
  104. break ;
  105.  
  106. case '3':
  107. printf (" n Enter Part/Full Contact Name/Number : ") ;
  108. gets(temp_name ) ;
  109. search_contact() ;
  110. break ;
  111.  
  112. case '4':
  113. printf (" n Enter Full Contact Name : ") ;
  114. gets(temp_name ) ;
  115. delete_contact() ;
  116. break ;
  117.  
  118. case '5':
  119. printf (" n Entire Data Will Be Lost ! Continue ?? { Y/other } : ") ;
  120. ch = getchar () ; getchar() ;
  121. if ( ch == 'Y' || ch == 'y' )
  122. {
  123. format_book( &head ) ;
  124. printf ("n Successful ! ") ;
  125. }
  126. else
  127. printf ("n Aborted ! ") ;
  128. break ;
  129.  
  130. case '6':
  131. print_book() ; break ;
  132.  
  133. default :
  134. printf ("nnnThank You ! n") ;
  135. return 0;
  136. }
  137.  
  138. input_flush() ;
  139.  
  140. return 1 ;
  141. }
  142.  
  143. int main ()
  144. {
  145. file_read() ;
  146. while ( run () ) ;
  147. file_write() ;
  148. return 0 ;
  149. }
  150.  
  151. #include"header.h"
  152.  
  153. /* helping subroutines */
  154.  
  155. void new_node ( phonebook ** ) ;
  156. void fill_details () ;
  157.  
  158. void print_record ( phonebook * ) ;
  159. void header () ;
  160. void footer () ;
  161.  
  162. int duplicate_check() ;
  163. int error_check () ;
  164. void split (char * ) ;
  165.  
  166. void lowercase (char * ) ;
  167. void input_flush() ;
  168.  
  169.  
  170. void add_contact ( )
  171. {
  172. /* Traversing */
  173. phonebook *temp = head ;
  174.  
  175. /* Storing Temporary Address */
  176. phonebook * address = NULL ;
  177.  
  178. /* Adding First Contact */
  179.  
  180. if ( temp == NULL )
  181. {
  182. new_node ( &head ) ;
  183. fill_details ( head ) ;
  184. return ;
  185. }
  186.  
  187. /* Modifying Root Node */
  188.  
  189. if ( strcmp ( temp->name, temp_name ) >0 )
  190. {
  191. new_node ( &head ) ;
  192. fill_details ( head ) ;
  193. head->next = temp ;
  194. return ;
  195. }
  196.  
  197. /* Adding Upcoming */
  198.  
  199. while ( temp->next !=NULL )
  200. {
  201. if ( strcmp( temp->next->name , temp_name ) < 0 )
  202. temp=temp->next ;
  203. else
  204. break ;
  205. }
  206.  
  207. /* Contact to add in the middle of two nodes */
  208.  
  209. if ( temp->next != NULL )
  210. {
  211. address = temp->next ;
  212. new_node ( &temp->next ) ;
  213. fill_details ( temp->next ) ;
  214. temp->next->next = address ;
  215. return ;
  216. }
  217. /* Adding contact at the end ( appending node at the end ) */
  218. else
  219. {
  220. new_node ( &temp->next ) ;
  221. fill_details ( temp->next ) ;
  222. temp->next->next = NULL ;
  223. return ;
  224. }
  225.  
  226. }
  227.  
  228. void search_contact ()
  229. {
  230. phonebook * temp = head ;
  231.  
  232. /* How many contacts matched */
  233. int cnt =0 ;
  234.  
  235. header () ;
  236. START = 1 ;
  237.  
  238. if ( head ==NULL)
  239. {
  240. footer () ;
  241. printf("nn Phone Book is Empty ..! ") ;
  242. return ;
  243. }
  244. /* String to be searched is read to ' temp_name' */
  245.  
  246. while ( temp!= NULL )
  247. {
  248. if ( strstr (temp->name , temp_name ) !=NULL || strstr(temp->no , temp_name )!=NULL )
  249. {
  250. print_record( temp ) ; /* Detail Matched */
  251. cnt++ ;
  252. }
  253. temp=temp->next ;
  254. }
  255.  
  256. footer () ;
  257. printf ("nn %d Contact%s have been Matched ! n " , cnt , cnt==1?"":"s" ) ;
  258. }
  259.  
  260. void delete_contact( )
  261. {
  262. /* Traversing */
  263. phonebook * temp = head ;
  264.  
  265. /* Storing Temporary Address */
  266. phonebook * address = NULL ;
  267.  
  268. if ( head ==NULL)
  269. {
  270. printf("nn Phone Book is Empty ..! ") ;
  271. return ;
  272. }
  273. if ( strcmp( head->name , temp_name ) == 0 )
  274. {
  275. address = head ->next ;
  276. free( head ) ;
  277. head = address ;
  278. printf("nn Contact deleted successfully...!! ") ;
  279. return ;
  280. }
  281. while ( temp->next !=NULL )
  282. {
  283. if ( strcmp (temp->next->name , temp_name ) == 0 )
  284. {
  285. address = temp ->next->next ;
  286. free ( temp->next ) ;
  287. printf("nn Contact deleted successfully...!! ") ;
  288. temp->next = address ;
  289. return ;
  290. }
  291. else
  292. temp=temp->next ;
  293. }
  294. printf ("nn Contact Does not Exist ! n ") ;
  295. }
  296.  
  297. void print_book ( )
  298. {
  299.  
  300. phonebook *temp = head ;
  301.  
  302. /* Serial no is reset to 1 */
  303. START = 1 ;
  304. printf ("n Complete List n") ;
  305. header () ;
  306.  
  307. if ( head ==NULL)
  308. {
  309. footer () ;
  310. printf("nn Phone Book is Empty ..! ") ;
  311. return ;
  312. }
  313. while ( temp!= NULL)
  314. {
  315. print_record( temp ) ;
  316. temp = temp->next ;
  317. }
  318. footer () ;
  319. }
  320.  
  321. void file_read ()
  322. {
  323.  
  324. char data[44] ;
  325. int i=-1 ;
  326. char ch ;
  327. FILE * fp1 ;
  328. fp1 = fopen ("contacts.txt" , "r") ;
  329.  
  330. while ( ( ch = getc (fp1) ) != EOF )
  331. {
  332. if ( ch == 'n')
  333. {
  334. data[++i] ='', split (data) ;
  335. add_contact() ;
  336. i = -1 ;
  337. }
  338. else
  339. {
  340. data[++i] = ch ;
  341. }
  342. }
  343. fclose( fp1 ) ;
  344. remove ( "contacts.txt") ;
  345. }
  346.  
  347.  
  348. /* Separate Name and No. from "Data" store them in temp_name , temp_no */
  349.  
  350. void split (char * str )
  351. {
  352.  
  353. int i=-1 ;
  354. while ( *str!='=' )
  355. {
  356. temp_name[++i] = *str ;
  357. str++ ;
  358. }
  359. temp_name[++i] = '' ;
  360.  
  361. str++ ;
  362. i=-1 ;
  363.  
  364. while ( temp_no[++i] = *str )
  365. str++ ;
  366. }
  367.  
  368. void file_write ()
  369. {
  370. phonebook * temp = head ;
  371. char data[40] ;
  372. FILE * fp1 ;
  373. fp1 = fopen ("contacts.txt" , "w") ;
  374.  
  375. while ( temp != NULL )
  376. {
  377. strcpy ( data , temp->name ) ;
  378. strcat ( data , "=") ;
  379. strcat ( data , temp->no ) ;
  380. fprintf ( fp1 , "%sn" , data) ;
  381. temp=temp->next ;
  382. }
  383. fclose ( fp1 ) ;
  384. }
  385.  
  386.  
  387. void format_book ( phonebook ** temp )
  388. {
  389. if ( *temp == NULL )
  390. return ;
  391. format_book ( &( (*temp)->next ) ) ;
  392. free ( *temp ) ;
  393. *temp=NULL ;
  394. }
  395.  
  396. int get_details ()
  397. {
  398. int response = 0 ;
  399. printf("nn Enter Contact Name : " ) ;
  400. gets(temp_name) ;
  401. printf("n Enter Mobile Number : " ) ;
  402. gets(temp_no) ;
  403.  
  404. return error_check() ;
  405. }
  406.  
  407. void new_node ( phonebook ** temp )
  408. {
  409. *temp = ( phonebook * ) malloc ( sizeof ( head ) ) ;
  410. (*temp)->name = NULL ;
  411. (*temp)->no = NULL ;
  412. (*temp)->next = NULL ;
  413. return ;
  414. }
  415.  
  416. void fill_details ( phonebook * temp )
  417. {
  418. temp->name = ( char* ) malloc ( sizeof (char) * MAX_NAME ) ;
  419. temp->no = ( char* ) malloc ( sizeof (char) * MAX_NO ) ;
  420. strcpy ( temp->name , temp_name ) ;
  421. strcpy ( temp->no , temp_no ) ;
  422. return ;
  423. }
  424.  
  425.  
  426. void input_flush()
  427. {
  428. int c ;
  429. while((c = getchar()) != 'n' && c != EOF) ;
  430. }
  431.  
  432.  
  433. void header ()
  434. {
  435. printf("n %-50s" , "------------------------------------------------------- " ) ;
  436. printf("n %-8s %-29s %-11s" , "SL.NO" ,"CONTACT NAME " , "MOBILE NUMBER " ) ;
  437. printf("n %-50s" , "------------------------------------------------------- " ) ;
  438.  
  439. }
  440.  
  441.  
  442. void footer ()
  443. {
  444. printf("n %-50s" , "------------------------------------------------------- " ) ;
  445. }
  446.  
  447.  
  448. void print_record ( phonebook * temp )
  449. {
  450. printf ("n %2d %-29s %-11s" , START , temp->name , temp->no ) ;
  451. START++ ;
  452. }
  453.  
  454.  
  455. /* Arbitrary for strlwr */
  456. void lowercase ( char * temp )
  457. {
  458. while ( *temp )
  459. {
  460. if (*temp>='A' && *temp<='Z' )
  461. *temp= *temp+32 ;
  462. temp++ ;
  463. }
  464. }
  465.  
  466. int error_check ( )
  467. {
  468. char * name = temp_name ;
  469. char * no = temp_no ;
  470.  
  471. if ( strlen ( temp_name ) > MAX_NAME || strlen( temp_name ) <1 )
  472. {
  473. printf("nn Failed !n Length exceeded/ No input for name { max 25 } n") ;
  474. return 1;
  475. }
  476. for ( ; *name ; name ++ )
  477.  
  478. if ( !isalpha (*name ) && *name != ' ' )
  479. {
  480. printf( "nn Failed !n Invalid characters used for name { only alphabet , space allowed } n" ) ;
  481. return 1;
  482. }
  483. if ( strlen ( temp_no)!=10 )
  484. {
  485. printf("nn Failed !n Invalid ten digit number { Ten Digits Please }.n") ;
  486. return 1;
  487. }
  488.  
  489. if ( *temp_no <'7' )
  490. {
  491. printf("nn Failed !n Currently No Numbers Exist on '%c' Series .n" , *temp_no ) ;
  492. return 1;
  493. }
  494. for ( ; *no ; no++ )
  495.  
  496. if ( ! isdigit (*no ) )
  497. {
  498. printf( "nn Failed !n Invalid characters used for number { only digits allowed } n" ) ;
  499. return 1;
  500. }
  501.  
  502. lowercase ( temp_name ) ;
  503.  
  504. if ( !duplicate_check() )
  505. {
  506. printf ("nn Successful ! n ") ;
  507. return 0;
  508. }
  509.  
  510. }
  511.  
  512. int duplicate_check( )
  513. {
  514. phonebook * temp = head ;
  515.  
  516. while (temp!= NULL )
  517. {
  518. if ( strcmp( temp->name , temp_name )==0 )
  519. {
  520. printf ("nn Failedn Contact Exists with Same Name ! ") ;
  521. return 1 ;
  522. }
  523. if ( strcmp( temp->no , temp_no )==0 )
  524. {
  525. printf ("nn Failedn Number Associated with " %s ". " , temp->name ) ;
  526. return 1 ;
  527. }
  528. temp=temp->next ;
  529. }
  530. return 0 ;
  531. }
  532.  
  533. while ( temp_no[++i] = *str )
  534. str++ ;
  535.  
  536. void print_record(const phonebook *);
  537.  
  538. void cls()
  539. {
  540. printf("x1b[2J");
  541. }
  542.  
  543. void ClearScreen()
  544. {
  545. #ifdef __linux__
  546. system("clear")
  547. #elif _WIN32
  548. system("cls")
  549. #else
  550. #error "Platform not defined"
  551. #endif
  552. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement