mhdew

CSE Project

Dec 6th, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<ctype.h>
  5.  
  6. #define MAX_SIZE 100
  7.  
  8. typedef struct
  9. {
  10.  
  11. char firstname[40];
  12. char lastname[40];
  13. char mobileNumber[20];
  14.  
  15.  
  16.  
  17. } contact;
  18.  
  19. typedef enum {FIRST_NAME,LAST_NAME,MOBILE_NUMBER} search_t;
  20.  
  21. contact phonebook[MAX_SIZE];
  22.  
  23. int currentSize=0;
  24.  
  25. /*Function prototype*/
  26.  
  27. void addEntry();
  28. void display (int index);
  29. void search(char *key,search_t type);
  30. void saveToFile();
  31. void readFromFile();
  32. void displaAll();
  33. void removeEntry(char *mobileNumber);
  34. void strtolower(char *str);
  35.  
  36.  
  37. void clear();
  38.  
  39. int main()
  40. {
  41.  
  42.  
  43. system("Color A");
  44. readFromFile();
  45. int i;
  46. char selection[41];
  47. int choice;
  48.  
  49.  
  50. while(1)
  51. {
  52. puts(" Phonebook Application main menu");
  53. puts(" 1.Insert a new Entry");
  54. puts(" 2.Display All Entry");
  55. puts(" 3.Search by First Name");
  56. puts(" 4..Search by Last name");
  57. puts(" 5.Search by mobile Number");
  58. puts(" 6.Delete a contact(by Mobile number) ");
  59. puts(" 7.Save Database");
  60. puts(" 8.Exit");
  61.  
  62.  
  63. do
  64. {
  65. printf("Enter your choice:");
  66. gets(selection);
  67. choice=atoi(selection);
  68.  
  69. }
  70.  
  71. while(choice<1 || choice>8);
  72.  
  73. switch(choice)
  74.  
  75. {
  76. case 1:
  77. addEntry();
  78. clear();
  79. break;
  80.  
  81. case 2:
  82. displayAll();
  83. clear();
  84. break;
  85. case 3:
  86. printf("Enter first name to search:");
  87. gets(selection);
  88. search(selection,FIRST_NAME);
  89. clear();
  90. break;
  91.  
  92. case 4:
  93. printf("Enter last name to search:");
  94. gets(selection);
  95. search(selection,LAST_NAME);
  96. clear();
  97. break;
  98.  
  99. case 5:
  100. printf("enter mobile number to search:");
  101. gets(selection);
  102. search(selection,MOBILE_NUMBER);
  103. clear();
  104. break;
  105.  
  106. case 6:
  107. printf("enter mobile number to delete :");
  108. gets(selection);
  109. removeEntry(selection);
  110. clear();
  111. break;
  112.  
  113. case 7:
  114. saveToFile();
  115. break;
  116. case 8:
  117. exit(0);
  118.  
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. return 0;
  125. }
  126. void clear()
  127. {
  128. system("PAUSE");
  129. system("CLS");
  130.  
  131.  
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. void addEntry()
  142. {
  143. if(currentSize==MAX_SIZE)
  144. {
  145. puts("Error:phonebook is full!");
  146. return ;
  147. }
  148. contact c;
  149. printf("Enter first name:");
  150. gets(c.firstname);
  151. printf("Enter Last name:");
  152. gets(c.lastname);
  153. printf("Enter mobile number:");
  154. gets(c.mobileNumber);
  155. phonebook[currentSize]=c;
  156. ++currentSize;
  157. printf("Contact Added.\n\n");
  158.  
  159.  
  160.  
  161.  
  162. }
  163.  
  164. void display(int index)
  165. {
  166. if(index < 0 ||index>=currentSize)
  167. {
  168. puts("Error:invalid index!");
  169. return ;
  170. }
  171.  
  172. contact c=phonebook[index];
  173.  
  174. printf("First Name: %s\n",c.firstname);
  175. printf("Last Name: %s\n",c.lastname);
  176. printf("Mobile no: %s\n",c.mobileNumber);
  177.  
  178.  
  179. }
  180.  
  181. void displayAll()
  182.  
  183.  
  184. {
  185.  
  186. if(currentSize==0)
  187. {
  188. puts("Phonebook is empty!");
  189. return ;
  190.  
  191. }
  192. int i;
  193. for(i=0; i<currentSize; i++)
  194. {
  195. display(i);
  196. puts(" ");
  197. }
  198. }
  199.  
  200.  
  201.  
  202. void strtolower(char *str)
  203.  
  204. {
  205. int len=strlen(str);
  206. int i;
  207. for(i=0; i<len; i++)
  208. {
  209. str[i]=tolower(str[i]);
  210. }
  211. }
  212. void search(char *key,search_t type)
  213. {
  214.  
  215. int found =0;
  216. int i;
  217. strtolower(key);
  218. char content[41];
  219.  
  220.  
  221. if(type==FIRST_NAME)//search by first name
  222. {
  223. for(i=0; i<currentSize; i++)
  224. {
  225. strcpy(content,phonebook[i].firstname);
  226. strtolower(content);
  227.  
  228. if(strcasecmp(content,key)==0)
  229. {
  230. display(i);
  231. found=1;
  232.  
  233. }
  234. }
  235. }
  236. else if(type==LAST_NAME)
  237. {
  238.  
  239. for(i=0; i<currentSize; i++)
  240. {
  241.  
  242. strcpy(content,phonebook[i].lastname);
  243. strtolower(content);
  244. if(strcasecmp(content,key)==0)
  245. {
  246. display(i);
  247. found=1;
  248.  
  249. }
  250. }
  251.  
  252. }
  253. else if (type==MOBILE_NUMBER)
  254. {
  255. for(i=0; i<currentSize; i++)
  256. {
  257.  
  258. strcpy(content,phonebook[i].mobileNumber);
  259. strtolower(content);
  260. if(strcasecmp(content,key)==0)
  261. {
  262. display(i);
  263. found=1;
  264.  
  265. }
  266. }
  267.  
  268. }
  269.  
  270. else
  271. {
  272.  
  273. puts("Error! Invalid search type!");
  274. return;
  275.  
  276.  
  277. }
  278.  
  279. if(!found)
  280. {
  281. puts("Not found in the phonebook");
  282. }
  283.  
  284.  
  285. }
  286.  
  287. void readFromFile()
  288.  
  289. {
  290. FILE *fp;
  291.  
  292. if((fp=fopen("phonebook.db", "rb"))==NULL)
  293. {
  294. puts("Error:can't load the database file");
  295. return;
  296. }
  297.  
  298. if(fread(&currentSize,sizeof(currentSize),1,fp)!=1)
  299. {
  300. puts("Error:can't read data!");
  301. return ;
  302. }
  303. if(fread(phonebook,sizeof(phonebook),1,fp)!=1)
  304. {
  305. puts("Error:can't read data!");
  306. return ;
  307. }
  308.  
  309. }
  310.  
  311.  
  312. void removeEntry(char *mobileNumber)
  313.  
  314. {
  315. if(currentSize==0)
  316. {
  317. puts("Phnebook is empty ! Nothing to delete");
  318. return ;
  319.  
  320. }
  321.  
  322. int i,j;
  323. int count=0;
  324. for(i=0; i<currentSize; i++)
  325.  
  326.  
  327. {
  328. if(strcmp(phonebook[i].mobileNumber,mobileNumber)==0)
  329. {
  330. for(j=0; j<currentSize-1; j++)
  331. {
  332. phonebook[j]=phonebook[j+1];
  333. }
  334.  
  335. strcpy(phonebook[j].firstname,"");
  336. strcpy(phonebook[j].lastname,"");
  337.  
  338. strcpy(phonebook[j].mobileNumber,"");
  339.  
  340.  
  341. currentSize-=1;
  342. ++count;
  343. }
  344.  
  345. }
  346. if(count==0)
  347. {
  348. puts("No entry deleted");
  349. }
  350. else
  351. {
  352. printf("%d entries deleted\n ",count);
  353. }
  354.  
  355.  
  356. }
  357. void saveToFile()
  358.  
  359. {
  360. FILE *fp;
  361. if( (fp=fopen("phonebook.db","wb"))==NULL)
  362. {
  363. puts("Error:can't create a database file!");
  364. return ;
  365. }
  366.  
  367. if( fwrite(&currentSize,sizeof(currentSize),1,fp)!=1)
  368. {
  369. puts("Error:can't save data!");
  370. return ;
  371. }
  372. if(fwrite(phonebook,sizeof(phonebook),1,fp)!=1)
  373. {
  374. puts("Error:can't save data!");
  375. return ;
  376. }
  377.  
  378. puts("Phonebook save to file successfully!\n\n");
  379.  
  380. }
Advertisement
Add Comment
Please, Sign In to add comment