Advertisement
yoyo106

fuck tarek

Dec 24th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. FILE *fr,*fw;
  5. int position = 0;
  6. int arr[10];
  7. char c;
  8. int n;
  9. int i=0,count=0;
  10. typedef struct birth //defining birthday date
  11. {
  12. int day;
  13. int month;
  14. int year;
  15. }birth;
  16. birth readbirthday() //function for reading birthday from file (used in readcontact() function)
  17. {
  18. birth birthday;
  19. fscanf(fr,",%d",&birthday.day);
  20. fscanf(fr,"/%d",&birthday.month);
  21. fscanf(fr,"/%d",&birthday.year);
  22. return birthday;
  23. }
  24.  
  25. typedef struct contact //defining contacts and its fields
  26. {
  27. char firstname[20];
  28. char lastname[20];
  29. birth birthday;
  30. char address[50];
  31. char email[50];
  32. int phonenumber;
  33. }contact;
  34. contact contacts[100];
  35. void loadcontacts()
  36. {
  37. FILE * fr = fopen("book.txt","r");
  38. fseek(fr,position,0);
  39. fscanf(fr,"%[^,]",contacts[i].firstname);
  40. fscanf(fr,",%[^,]",contacts[i].lastname);
  41. fscanf(fr,",%d",&contacts[i].birthday.day);
  42. fscanf(fr,"/%d",&contacts[i].birthday.month);
  43. fscanf(fr,"/%d",&contacts[i].birthday.year);
  44. fscanf(fr,",%[^,]",contacts[i].address);
  45. fscanf(fr,",%[^,]",contacts[i].email);
  46. fscanf(fr,",%d\n",&contacts[i].phonenumber);
  47. position = ftell(fr);
  48. fclose(fr);
  49. i++;
  50.  
  51. }
  52. int count_contacts()
  53. {
  54. int x = 0;//local counter in this function
  55. fr = fopen("book.txt","r");
  56. while(!feof(fr))
  57. {
  58. if((c=fgetc(fr))=='\n')
  59. x++;
  60. }
  61. fclose(fr);
  62. return x;
  63. }
  64. void printcontact(contact contacts)
  65. {
  66. printf("\nFirst name is: %s\n",contacts.firstname);
  67. printf("Last name is: %s\n",contacts.lastname);
  68. printf("Birthday: %d/%d/%d\n",contacts.birthday.day,contacts.birthday.month,contacts.birthday.year);
  69. printf("Address: %s\n", contacts.address);
  70. printf("E-mail: %s\n",contacts.email);
  71. printf("Phone number: 0%d\n",contacts.phonenumber);
  72.  
  73. }
  74. void printsorted()
  75. {
  76. int x;
  77. sort();
  78. for(x=0;x<count;x++)
  79. {
  80. printcontact(contacts[x]);
  81. }
  82. }
  83. void add_contact()
  84. {
  85. printf("Enter the contact's first name\t:");
  86. scanf("%s",contacts[count].firstname);
  87. printf("Enter the contact's last name\t:");
  88. scanf("%s",contacts[count].lastname);
  89. printf("Enter the birth date\n");
  90. printf("enter the day:\t");
  91. scanf("%d",&contacts[count].birthday.day);
  92. printf("enter the month\t:");
  93. scanf("%d",&contacts[count].birthday.month);
  94. printf("enter the year\t:");
  95. scanf("%d",&contacts[count].birthday.year);
  96. printf("Enter the address: ");
  97. scanf(" %199[^\n]",contacts[count].address);
  98. printf("Enter the E-mail: ");
  99. scanf("%s",contacts[count].email);
  100. printf("Enter phone number\t:");
  101. scanf("%d",&contacts[count].phonenumber);
  102. count++;
  103. display_menu();
  104. switch_function();
  105. }
  106. int* searchlastname()
  107. {
  108. int x,j=0;
  109. char key[30],confirm[5];
  110. int *p=arr;
  111.  
  112. printf("Enter the search key: ");
  113. scanf("%s",key);
  114. for(x=0;x<count;x++)
  115. {
  116. if(substring(contacts[x].lastname,key)==1)
  117. {
  118. *(p+j)=x;
  119. j++;
  120. n=j;
  121. }
  122. }
  123. if(j==0)
  124. {printf("Search not found\n");
  125. printf("Do you want to search again? (yes/no)");
  126. scanf("%s",confirm);
  127. if(confirm=="yes" || confirm=="YES")
  128. searchlastname();
  129. else display_menu();
  130.  
  131. }
  132. if(j!=0)
  133. {//for(j=0;j<n;j++)
  134. //printcontact(contacts[arr[j]]);
  135. return p;
  136. }
  137. }
  138. void searchmenulastname()
  139. {
  140. int x,j=0;
  141. char key[30],confirm[5];
  142. int *p=arr;
  143.  
  144. printf("Enter the search key: ");
  145. scanf("%s",key);
  146. for(x=0;x<count;x++)
  147. {
  148. if(substring(contacts[x].lastname,key)==1)
  149. {
  150. *(p+j)=x;
  151. j++;
  152. n=j;
  153. }
  154. }
  155. if(j==0)
  156. {printf("Search not found\n");
  157. printf("Do you want to search again? (yes/no)");
  158. scanf("%s",confirm);
  159. if(confirm=="yes" || confirm=="YES")
  160. searchlastname();
  161. else display_menu();
  162.  
  163. }
  164. if(j!=0)
  165. { for(j=0;j<n;j++)
  166. printcontact(contacts[arr[j]]);
  167. }
  168. }
  169. void modify()
  170. {
  171. int x,y;
  172. int*p = searchlastname();
  173. for(x=0;x<n;x++)
  174. {
  175. printf("Contact number %d:\n",x+1);
  176. printcontact(contacts[*(p+x)]);
  177. }
  178. printf("Choose the contact's number to modify:");
  179. scanf("%d",&y);
  180. printcontact(contacts[*(p+y-1)]);
  181. printf("Enter the contact's first name\t:");
  182. scanf("%s",contacts[*(p+y-1)].firstname);
  183. printf("Enter the contact's last name\t:");
  184. scanf("%s",contacts[*(p+y-1)].lastname);
  185. printf("Enter the birth date\n");
  186. printf("enter the day:\t");
  187. scanf("%d",&contacts[*(p+y-1)].birthday.day);
  188. printf("enter the month\t:");
  189. scanf("%d",&contacts[*(p+y-1)].birthday.month);
  190. printf("enter the year\t:");
  191. scanf("%d",&contacts[*(p+y-1)].birthday.year);
  192. printf("Enter the address: ");
  193. scanf(" %199[^\n]",contacts[*(p+y-1)].address);
  194. printf("Enter the E-mail: ");
  195. scanf("%s",contacts[*(p+y-1)].email);
  196. printf("Enter phone number\t:");
  197. scanf("%d",&contacts[*(p+y-1)].phonenumber);
  198. printf("Contact is modified.\n");
  199. display_menu();
  200. switch_function();
  201.  
  202. }
  203. void deletecontact()
  204. {
  205. int x,y,flag=0;
  206. char firstname[20],lastname[20],key[5];
  207. printf("Enter first name: ");
  208. scanf("%s",firstname);
  209. printf("Enter last name: ");
  210. scanf("%s",lastname);
  211. for(x=0;x<count;x++)
  212. {
  213. if(strcmp(contacts[x].firstname,firstname)==0&&strcmp(contacts[x].lastname,lastname)==0)
  214. {
  215. flag =1;
  216. printcontact(contacts[x]);
  217. printf("Are you sure? (yes/no)");
  218. scanf("%s",key);
  219. if (strcmp(key,"yes")==0||strcmp(key,"Yes")==0)
  220. {
  221. for(y=x;y<count-1;y++)
  222. contacts[y]=contacts[y+1];
  223. count--;
  224. display_menu();
  225. switch_function();
  226. }
  227. else
  228. {
  229. display_menu();
  230. switch_function();
  231. }
  232.  
  233. }
  234.  
  235.  
  236. }
  237. if(flag==0)
  238. {
  239. printf("Search not found\nDo you want to search again?(yes/no)");
  240. scanf("%s",key);
  241. if (strcmp(key,"yes")==0||strcmp(key,"Yes")==0)
  242. deletecontact();
  243. else{
  244. display_menu();
  245. switch_function();
  246. }
  247. }
  248.  
  249. }
  250. void save()
  251. {
  252.  
  253. fw = fopen("book.txt","w");
  254. int x;
  255. for(x=0;x<count;x++)
  256. {
  257. fprintf(fw,"%s,",contacts[x].firstname);
  258. fprintf(fw,"%s,",contacts[x].lastname);
  259. fprintf(fw,"%d/",contacts[x].birthday.day);
  260. fprintf(fw,"%d/",contacts[x].birthday.month);
  261. fprintf(fw,"%d,",contacts[x].birthday.year);
  262. fprintf(fw,"%s,",contacts[x].address);
  263. fprintf(fw,"%s,",contacts[x].email);
  264. fprintf(fw,"%d\n",contacts[x].phonenumber);
  265. }
  266. fclose(fw);
  267. }
  268. void searchmenufirstname()
  269. {
  270. int x,j=0;
  271. char key[30],confirm[5];
  272. int *p=arr;
  273.  
  274. printf("Enter the search key: ");
  275. scanf("%s",key);
  276. for(x=0;x<count;x++)
  277. {
  278. if(substring(contacts[x].firstname,key)==1)
  279. {
  280. *(p+j)=x;
  281. j++;
  282. n=j;
  283. }
  284. }
  285. if(j==0)
  286. {printf("Search not found\n");
  287. printf("Do you want to search again? (yes/no)");
  288. scanf("%s",confirm);
  289. if(confirm=="yes" || confirm=="YES")
  290. searchlastname();
  291. else display_menu();
  292. }
  293. if(j!=0)
  294. { for(j=0;j<n;j++)
  295. printcontact(contacts[arr[j]]);
  296. }
  297. }
  298. int* searchfirstname()
  299. {
  300. int x,j=0;
  301. char key[30],confirm[5];
  302. int *p=arr;
  303.  
  304. printf("Enter the search key: ");
  305. scanf("%s",key);
  306. for(x=0;x<count;x++)
  307. {
  308. if(substring(contacts[x].firstname,key)==1)
  309. {
  310. *(p+j)=x;
  311. j++;
  312. n=j;
  313. }
  314. }
  315. if(j==0)
  316. {printf("Search not found\n");
  317. printf("Do you want to search again? (yes/no)");
  318. scanf("%s",confirm);
  319. if(confirm=="yes" || confirm=="YES")
  320. searchlastname();
  321. else display_menu();
  322. }
  323. if(j!=0)
  324. { //for(j=0;j<n;j++)
  325. //printcontact(contacts[arr[j]]);
  326. return p;
  327. }
  328. }
  329. int* searchphonenumber()//needs to be rechecked
  330. {
  331. int x,j=0;
  332. char key[30],confirm[5];
  333. int *p=arr;
  334.  
  335. printf("Enter the search key: ");
  336. scanf("%s",key);
  337. for(x=0;x<count;x++)
  338. {
  339. if(substring((char)contacts[x].phonenumber,key)==1)
  340. {
  341. *(p+j)=x;
  342. j++;
  343. n=j;
  344. }
  345. }
  346. if(j==0)
  347. {printf("Search not found\n");
  348. printf("Do you want to search again? (yes/no)");
  349. scanf("%s",confirm);
  350. if(confirm=="yes" || confirm=="YES")
  351. searchlastname();
  352. else display_menu();
  353.  
  354. }
  355. if(j!=0)
  356. { //for(j=0;j<n;j++)
  357. //printcontact(contacts[arr[j]]);
  358. return p;
  359. }
  360. }
  361. void searchmenuphonenumber()//needs to be rechecked
  362. {
  363. int x,j=0;
  364. char key[30],confirm[5];
  365. int *p=arr;
  366.  
  367. printf("Enter the search key: ");
  368. scanf("%s",key);
  369. for(x=0;x<count;x++)
  370. {
  371. if(substring((char)contacts[x].phonenumber,key)==1)
  372. {
  373. *(p+j)=x;
  374. j++;
  375. n=j;
  376. }
  377. }
  378. if(j==0)
  379. {printf("Search not found\n");
  380. printf("Do you want to search again? (yes/no)");
  381. scanf("%s",confirm);
  382. if(confirm=="yes" || confirm=="YES")
  383. searchlastname();
  384. else display_menu();
  385.  
  386. }
  387. if(j!=0)
  388. { for(j=0;j<n;j++)
  389. printcontact(contacts[arr[j]]);
  390. }
  391. }
  392. int substring(char name[],char key[])
  393. {
  394. int x,j,flag;
  395. if(strlen(key)>strlen(name)){
  396. flag = 0;
  397. return flag;
  398. }
  399. for(x=0;x<=strlen(name)-strlen(key);x++)
  400. {
  401.  
  402. for(j=0;j<strlen(key);j++)
  403. {
  404.  
  405. flag = 1;
  406. if(key[j] != name[j+x])
  407. {
  408. flag = 0;
  409. break;
  410. }
  411. }
  412. if (flag ==1)
  413. break;
  414. }
  415. return flag;
  416. }
  417. void search_menu()
  418. {
  419. int y;
  420. printf("Search by:\n1-First name\n2-Last name\n3-Phone number\n");
  421. scanf("%d",&y);
  422. switch(y)
  423. {
  424. case(1): searchmenufirstname();break;
  425. case(2): searchmenulastname();break;
  426. case(3): searchmenuphonenumber();break;
  427. default: printf("Invalid input\n"); search_menu();//break;
  428. }
  429. }
  430. void quit()
  431. {
  432. char confirm[5];
  433. printf("Are you sure you want to exit? (yes/no)\n");
  434. scanf("%s",&confirm);
  435. if (strcmp(confirm,"yes")==0||strcmp(confirm,"Yes")==0)
  436. exit(0);
  437. else{
  438. display_menu();
  439. switch_function();
  440. }
  441.  
  442. }
  443. void sort()
  444. {
  445. int pass,x;
  446. contact temp;
  447. for (pass=1; pass<count; pass++)
  448. {
  449. for(x=0; x<count-1; x++)
  450. {
  451. if (strcmp(contacts[x].lastname,contacts[x+1].lastname)>0)
  452. {
  453. temp=contacts[x];
  454. contacts[x]=contacts[x+1];
  455. contacts[x+1]=temp;
  456.  
  457. }
  458. else if(strcmp(contacts[x].lastname,contacts[x+1].lastname)==0)
  459. {
  460. if (strcmp(contacts[x].firstname,contacts[x+1].firstname)>0)
  461. {
  462. temp=contacts[x];
  463. contacts[x]=contacts[x+1];
  464. contacts[x+1]=temp;
  465. }
  466. }
  467. }
  468. }
  469. }
  470. void display_menu()
  471. {
  472. printf("\nChoose an option:\n1-Add contact\n2-Delete\n3-Modify\n4-Search\n5-Print\n6-Save\n7-Quit\n");
  473.  
  474. }
  475. void switch_function()
  476. {
  477. printf("Please enter a number: ");
  478. int x;
  479. scanf("%d",&x);
  480. switch(x)
  481. {
  482. case(1): add_contact();break;
  483. case(2): deletecontact();break;
  484. case(3): modify();break;
  485. case(4): search_menu();break;
  486. case(5): printsorted();break;
  487. case(6): save();break;
  488. case(7): quit();break;
  489. default: printf("Invalid input\n"); switch_function();//break;
  490.  
  491. }
  492. // if (x>7||x<1)
  493. // {
  494. // switch_function();
  495. // }
  496. }
  497.  
  498. int main()
  499. {
  500. printf("***********\n*Phonebook*\n***********\n");
  501. count = count_contacts();
  502. int x;
  503. for(x=0;x<count;x++)
  504. loadcontacts();
  505. display_menu();
  506. switch_function();
  507.  
  508. return 0;
  509. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement