Advertisement
erfanul007

Phone Book Project final

Dec 7th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. /*C-Project *Phone Book* (doubly link list)*/
  2.  
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>
  6.  
  7. struct node{
  8. char name[100];
  9. char number[20];
  10. char about[1000];
  11. struct node *next;
  12. struct node *prev;
  13. }*start=NULL;
  14.  
  15.  
  16. void sort(struct node *newnode)
  17. {
  18. struct node *current,*currentprev,*temp;
  19.  
  20. current = start;
  21.  
  22. if(start == NULL)
  23. start = newnode;
  24.  
  25. else if(start->next == NULL){
  26.  
  27. if(strcmp(newnode,current)>0){
  28. current->next = newnode;
  29. newnode -> prev = current;
  30. }
  31.  
  32. else{
  33. newnode->next = current;
  34. current->prev = newnode;
  35. start = newnode;
  36. }
  37. }
  38.  
  39. else{
  40. while(current!=NULL){
  41. if(strcmp(newnode,current)<0)
  42. break;
  43. if(current->next==NULL)
  44. temp=current;
  45. current = current->next;
  46. }
  47.  
  48. if(current == start){
  49. newnode->next = current;
  50. current->prev = newnode;
  51. start = newnode;
  52. }
  53.  
  54. else if(current == NULL){
  55. temp->next = newnode;
  56. newnode->prev = temp;
  57. }
  58.  
  59. else{
  60. currentprev = current->prev;
  61. currentprev->next = newnode;
  62. newnode->prev = currentprev;
  63. newnode->next = current;
  64. current->prev = newnode;
  65. }
  66. }
  67.  
  68. }
  69.  
  70.  
  71. int checkernum(struct node *newnode)
  72. {
  73. int i=0;
  74. struct node *current;
  75. current=start;
  76. while(current!=NULL){
  77. if(strcmp(current->number,newnode->number)==0){
  78. i++;
  79. break;
  80. }
  81. current=current->next;
  82. }
  83. return i;
  84. }
  85.  
  86.  
  87. int checkername(struct node *newnode)
  88. {
  89. int i=0;
  90. struct node *current;
  91. current=start;
  92. while(current!=NULL){
  93. if(strcmp(current->name,newnode->name)==0){
  94. i++;
  95. break;
  96. }
  97. current=current->next;
  98. }
  99. return i;
  100. }
  101.  
  102. void insert()
  103. {
  104. struct node *newnode,*current;
  105. newnode=(struct node*)malloc(sizeof(struct node));
  106. newnode->next=NULL;
  107. newnode->prev=NULL;
  108.  
  109. printf("Enter Name : ");
  110. getchar();
  111. scanf("%[^\n]",newnode->name);
  112.  
  113. printf("Enter Number: ");
  114. getchar();
  115. scanf("%s",newnode->number);
  116.  
  117. printf("Enter About : ");
  118. getchar();
  119. scanf("%[^\n]",newnode->about);
  120.  
  121. int i=checkername(newnode);
  122. int j=checkernum(newnode);
  123. if(i==0 && j==0){
  124. sort(newnode);
  125. system("CLS");
  126. printf("*save successful*\n");
  127. char n;
  128. getchar();
  129. printf("1. save another contact\n");
  130. printf("2. view recent save contact\n");
  131. printf("0. Home\n");
  132. printf("Chose an option: ");
  133. scanf("%c",&n);
  134. if(n=='0'){
  135. system("CLS");
  136. return;
  137. }
  138. else if(n=='1'){
  139. system("CLS");
  140. insert();
  141. }
  142. else if(n=='2'){
  143. system("CLS");
  144. printf("name: %s\n",newnode->name);
  145. printf("number: %s\n",newnode->number);
  146. printf("about: %s\n\n",newnode->about);
  147. }
  148. }
  149. else{
  150. system("CLS");
  151. printf("Contact Exist, can't save it\n");
  152. printf("1. save another contact\n");
  153. printf("0. Home\n");
  154. char n;
  155. getchar();
  156. printf("Chose an option: ");
  157. scanf("%c",&n);
  158. if(n=='0'){
  159. system("CLS");
  160. return;
  161. }
  162. else if(n=='1'){
  163. system("CLS");
  164. insert();
  165. }
  166. }
  167. }
  168.  
  169.  
  170. void del(struct node *current)
  171. {
  172. if(current == start)
  173. start = current->next;
  174.  
  175. else if(current->next == NULL)
  176. (current->prev)->next = NULL;
  177.  
  178. else{
  179. struct node *currentprev, *currentnext;
  180. currentprev = current->prev;
  181. currentnext = current->next;
  182. currentprev->next = currentnext;
  183. currentnext->prev = currentprev;
  184. }
  185. }
  186.  
  187.  
  188. void edit(struct node *current)
  189. {
  190. del(current);
  191.  
  192. char n;
  193.  
  194. printf("1. Change name\n");
  195. printf("2. Skip\n");
  196. printf("\nChose an option : ");
  197. getchar();
  198. scanf("%c",&n);
  199. system("CLS");
  200. if(n == '1'){
  201. printf("edit name : ");
  202. getchar();
  203. scanf("%[^\n]",current->name);
  204. }
  205. system("CLS");
  206. printf("1. Change number\n");
  207. printf("2. Skip\n");
  208. printf("\nChose an option : ");
  209. getchar();
  210. scanf("%c",&n);
  211. system("CLS");
  212. if(n == '1'){
  213. printf("edit number: ");
  214. getchar();
  215. scanf("%s",current->number);
  216. }
  217. system("CLS");
  218. printf("1. Change about\n");
  219. printf("2. Skip\n");
  220. printf("\nChose an option : ");
  221. getchar();
  222. scanf("%c",&n);
  223. system("CLS");
  224. if(n == '1'){
  225. printf("edit about : ");
  226. getchar();
  227. scanf("%[^\n]",current->about);
  228. }
  229. system("CLS");
  230. current->next = NULL;
  231. current->prev = NULL;
  232.  
  233. sort(current);
  234.  
  235. }
  236.  
  237.  
  238. void byname()
  239. {
  240. struct node *current;
  241. if(start==NULL){
  242. printf("\nNo Contact saved for search\n");
  243. return;
  244. }
  245.  
  246. char str[100];
  247. printf("Enter name for search : ");
  248. getchar();
  249. scanf("%[^\n]",str);
  250. current = start;
  251.  
  252. int i=0;
  253. char n;
  254.  
  255. while(current!=NULL){
  256. if(strcmp(current->name,str)==0){
  257. system("CLS");
  258. printf("Name found\n");
  259. printf("Name : %s\n",current->name);
  260. printf("Number : %s\n",current->number);
  261. printf("About : %s\n",current->about);
  262. printf("\n");
  263. i++;
  264. break;
  265. }
  266.  
  267. current = current->next;
  268. }
  269.  
  270. if(i==0){
  271. system("CLS");
  272. printf("Contact Name Not Found\n");
  273. return;
  274. }
  275.  
  276. else{
  277. printf("1. Edit\n");
  278. printf("2. Delete\n");
  279. printf("0. Home\n");
  280. printf("Chose an option : ");
  281.  
  282. getchar();
  283. scanf("%c",&n);
  284.  
  285. if(n == '1'){
  286. system("CLS");
  287. edit(current);
  288. system("CLS");
  289. printf("*edit successful*\n");
  290. }
  291. else if(n == '2'){
  292. system("CLS");
  293. del(current);
  294. system("CLS");
  295. printf("*Delete successful*\n");
  296. }
  297. else if(n == '0'){
  298. system("CLS");
  299. return;
  300. }
  301. }
  302.  
  303. }
  304.  
  305.  
  306. void bynumber()
  307. {
  308. struct node *current;
  309. if(start==NULL){
  310. printf("\nNo Contact saved for search\n");
  311. return;
  312. }
  313.  
  314. char str[100];
  315.  
  316. printf("Enter number for search : ");
  317. getchar();
  318. scanf("%[^\n]",str);
  319.  
  320. current = start;
  321.  
  322. int i=0;
  323. char n;
  324.  
  325. while(current!=NULL){
  326. if(strcmp(current->number,str)==0){
  327. system("CLS");
  328. printf("Number found\n");
  329. printf("Name : %s\n",current->name);
  330. printf("Number : %s\n",current->number);
  331. printf("About : %s\n",current->about);
  332. printf("\n");
  333. i++;
  334. break;
  335. }
  336. current = current->next;
  337. }
  338.  
  339. if(i==0){
  340. system("CLS");
  341. printf("Contact Number Not Found\n");
  342. return;
  343. }
  344.  
  345. else{
  346. printf("1. Edit\n");
  347. printf("2. Delete\n");
  348. printf("0. Home\n");
  349. printf("Chose an option : ");
  350.  
  351. getchar();
  352. scanf("%c",&n);
  353.  
  354. if(n == '1'){
  355. system("CLS");
  356. edit(current);
  357. system("CLS");
  358. printf("*edit successful*\n");
  359. }
  360. else if(n == '2'){
  361. system("CLS");
  362. del(current);
  363. system("CLS");
  364. printf("*Delete successful*\n");
  365. }
  366. else if(n == '0'){
  367. system("CLS");
  368. return;
  369. }
  370. }
  371. }
  372.  
  373.  
  374. void count()
  375. {
  376. struct node *current;
  377. int count = 0;
  378. current = start;
  379.  
  380. while(current!=NULL){
  381. count++;
  382. current = current->next;
  383. }
  384. printf("\nTotal contacts %d\n",count);
  385. }
  386.  
  387.  
  388. void byserial(int i)
  389. {
  390. int j=1;
  391. struct node *current;
  392. current=start;
  393. while(j<i){
  394. current=current->next;
  395. }
  396. printf("\n");
  397. printf("Name : %s\n",current->name);
  398. printf("Number : %s\n",current->number);
  399. printf("About : %s\n",current->about);
  400. printf("\n");
  401.  
  402. char n;
  403. getchar();
  404. printf("1. Edit\n");
  405. printf("2. Delete\n");
  406. printf("0. Home\n");
  407. printf("Chose an option : ");
  408. scanf("%c",&n);
  409. if(n == '1'){
  410. system("CLS");
  411. edit(current);
  412. printf("*edit successful*\n");
  413. }
  414. else if(n == '2'){
  415. system("CLS");
  416. del(current);
  417. printf("*Delete successful*\n");
  418. }
  419. else if(n == '0'){
  420. system("CLS");
  421. return;
  422. }
  423. }
  424.  
  425.  
  426. void displayall()
  427. {
  428. if(start == NULL){
  429. printf("No contact for display\n");
  430. return;
  431. }
  432.  
  433. else{
  434. count();
  435. struct node *current;
  436. current = start;
  437. int i=0;
  438. while(current!=NULL){
  439. i++;
  440.  
  441. printf("\nContact no %d :-\n",i);
  442. printf("name : %s\n",current->name);
  443. printf("Number : %s\n",current->number);
  444. printf("about : %s\n",current->about);
  445. printf("\n");
  446.  
  447. current = current->next;
  448. }
  449. }
  450. char n;
  451. getchar();
  452. printf("Select a contact or press '0' to go home: ");
  453. scanf("%c",&n);
  454. int i=n-48;
  455. if(i==0){
  456. system("CLS");
  457. return;
  458. }
  459. else{
  460. system("CLS");
  461. byserial(i);
  462. }
  463. printf("\n");
  464. }
  465.  
  466.  
  467. void home()
  468. {
  469. char n;
  470. while(1){
  471. printf("***HOME***\n\n");
  472. printf("1. Save a new contact\n");
  473. printf("2. Show all contact\n");
  474. printf("3. Search by name\n");
  475. printf("4. Search by number\n");
  476. printf("0. exit\n");
  477. printf("\nChose an option : ");
  478. getchar();
  479. scanf("%c",&n);
  480. if(n=='0'){
  481. system("CLS");
  482. exit(0);
  483. }
  484. else if(n=='1'){
  485. system("CLS");
  486. insert();
  487. }
  488. else if(n=='2'){
  489. system("CLS");
  490. displayall();
  491. }
  492.  
  493. else if(n=='3'){
  494. system("CLS");
  495. byname();
  496. }
  497. else if(n=='4'){
  498. system("CLS");
  499. bynumber();
  500. }
  501. else{
  502. printf("Invalid input\n");
  503. }
  504. }
  505. return;
  506. }
  507.  
  508.  
  509. int main()
  510. {
  511. char n[100],x[100];
  512. strcpy(x,"password");
  513. printf("\t\t\t\t\t\t***PHONE BOOK***\n\n");
  514. while(1){
  515. printf("\nEnter password: ");
  516. scanf("%s",n);
  517.  
  518. if(strcmp(n,x)!=0){
  519. printf("Wrong password\n");
  520. continue;
  521. }
  522. system("CLS");
  523. printf("Password matched - WELCOME\n");
  524. home();
  525. }
  526. return 0;
  527. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement