Advertisement
ahmad_zizo

FINAL PROJECT

Dec 25th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. FILE *pb;
  6. int counter=0;
  7. typedef struct
  8. {
  9. char fname[20];
  10. char lname[20];
  11. char address[50];
  12. char city[20];
  13. char number[16];
  14. } phonebook;
  15. phonebook person[100];
  16. void Load();
  17. void add();
  18. void delete_();
  19. void modify();
  20. void save();
  21. void print();
  22.  
  23. void main()
  24. {
  25. char x[20];
  26. int op,p;
  27. printf("|-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-|\n\n");
  28. printf("choose:\n");
  29. printf("1)Load contacts.\n2)show all contacts\n3)Add contact.\n4)Modify.\n5)delete contact.\n6)Search.\n7)save\n8)save & exit.\n9)exit without saving.\n\n");
  30. scanf(" %s",&x);
  31. op=atoi(x);
  32. while(op!=9)
  33. {
  34. system("cls");
  35. if(op<1||op>9)
  36. printf("enter a vaild number.\n\n");
  37. else
  38. {
  39. switch (op)
  40. {
  41. case 1:
  42. Load();
  43. break;
  44. case 2:
  45. print();
  46. break;
  47. case 3:
  48. add();
  49. break;
  50. case 4:
  51. modify();
  52. break;
  53. case 5:
  54. delete_();
  55. break;
  56. case 6:
  57. printf("enter the last name of contatct you search for:\n");
  58. search_(p);
  59. break;
  60. case 7:
  61. save1();
  62. break;
  63. case 8:
  64. save();
  65. break;
  66. }
  67. }
  68. printf("\n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
  69. printf("choose:\n");
  70. printf("1)Load contacts.\n2)show all contacts\n3)Add contact.\n4)Modify.\n5)delete contact.\n6)Search.\n7)save\n8)save & exit.\n9)exit without saving.\n\n");
  71. scanf(" %s",&x);
  72. op=atoi(x);
  73. }
  74. }
  75.  
  76. void Load()
  77. {
  78. int i=0;
  79. pb = fopen("phonebook.txt","r");
  80. if(pb==NULL)
  81. printf("no such file");
  82. else
  83. {
  84. while(!feof(pb))
  85. {
  86. fscanf(pb,"%[^,],%[^,],%[^,],%[^,],%s\n",person[counter].fname,person[counter].lname,person[counter].address,person[counter].city,person[counter].number);
  87. counter++;
  88. }
  89. }
  90. fclose(pb);
  91. printf("contacts have been loaded");
  92. }
  93. void add()
  94. {
  95. int i;
  96. if(counter ==99)
  97. {
  98. system("cls");
  99. printf("the memory is full, you cant add another contact.\n\n");
  100. }
  101. else
  102. {
  103. printf("first name: ");
  104. getchar();
  105. scanf("%[^\n]s",person[counter].fname);
  106. printf("last name: ");
  107. scanf(" %[^\n]s",person[counter].lname);
  108. printf("address: ");
  109. scanf(" %[^\n]s",person[counter].address);
  110. printf("city: ");
  111. scanf(" %[^\n]s",person[counter].city);
  112. printf("Number: ");
  113. scanf(" %[^\n]s",person[counter].number);
  114. counter++;
  115. printf("\n\nA new contact has been added.");
  116. }
  117. }
  118. void delete_()
  119. {
  120. int i,n,p,x;
  121. char z[10];
  122. printf("enter the last name of the contact you want to delete:\n");
  123. x=search_(p);
  124. if(x==0);
  125. else{
  126. printf("enter number of the contact you want to modify:\n");
  127. scanf("%s",z);
  128. n=atoi(z);
  129. if(n<1||n>counter)
  130. {
  131. system("cls");
  132. printf("not a valid choise");
  133. }
  134. else
  135. {
  136. n--;
  137. for(i=n; i<=counter; i++)
  138. {
  139. strcpy(person[i].fname,person[i+1].fname);
  140. strcpy(person[i].lname,person[i+1].lname);
  141. strcpy(person[i].address,person[i+1].address);
  142. strcpy(person[i].city,person[i+1].city);
  143. strcpy(person[i].number,person[i+1].number);
  144. }
  145. counter--;
  146. system("cls");
  147. printf("\nA contact has been deleted.");
  148. }
  149. }
  150. }
  151. void modify()
  152. {
  153. char names[20];
  154. int i,n,p,x;
  155. char m,z[10];
  156. printf("enter the last name of contatct you want to modify:\n");
  157. x=search_(p);
  158. if(x==0);
  159. else{
  160. printf("\nenter number of the contact you want to modify:\n");
  161. scanf(" %s",z);
  162. n=atoi(z);
  163. if(n<1||n>counter)
  164. {
  165. system("cls");
  166. printf("not a valid number");
  167. }
  168. else
  169. {
  170. n--;
  171. system("cls");
  172. printf("%d)\nfirst name: %s\nlast name: %s\naddress: %s\nCity: %s\nNumber: %s\n",n+1,person[n].fname,person[n].lname,person[n].address,person[n].city,person[n].number);
  173. printf("\n\nyou want to modify:\n");
  174. printf("1-first name\n2-last name\n3-number\n4-address\n5-city\n6-Done.\n\n");
  175. scanf(" %c",&m);
  176. while(m!='6')
  177. {
  178. if(m!='1'&&m!='2'&&m!='3'&&m!='4'&&m!='5')
  179. {
  180. printf("enter a valid number.\n\n");
  181. }
  182. else if(m=='1')
  183. {
  184. printf("Enter the new first name:\n");
  185. scanf("%s",person[n].fname);
  186. }
  187. else if(m=='2')
  188. {
  189. printf("Enter the new last name:\n");
  190. scanf("%s",person[n].lname);
  191. }
  192. else if(m=='3')
  193. {
  194. printf("Enter the new number:\n");
  195. scanf("%s",person[n].number);
  196. }
  197. else if(m=='4')
  198. {
  199. printf("Enter the new Address:\n");
  200. scanf(" %[^\n]s",person[n].address);
  201. }
  202. else if(m=='5')
  203. {
  204. printf("Enter the new City:\n");
  205. scanf("%s",person[n].city);
  206. }
  207. printf("\nenter the number of contact you want to modify: ");
  208. printf("\n1-first name\n2-last name\n3-number\n4-address\n5-city\n6-DONE\n\n");
  209. scanf(" %c",&m);
  210. }
  211. if(m=='6')
  212. {
  213. system("cls");
  214. printf("\n\nthe contact after modification:\n");
  215. printf("first name: %s\nlast name: %s\nAddress: %s\nCity: %s\nNumber: %s\n",person[n].fname,person[n].lname,person[n].address,person[n].city,person[n].number);
  216. }
  217. printf("\n\nA contact has been modified.");
  218. }
  219. }
  220. }
  221. void search_(int p)
  222. {
  223. int i;
  224. p=0;
  225. char name[20];
  226. scanf("%s",name);
  227. system("cls");
  228. printf("the results:\n");
  229. for(i=0; i<counter ; i++)
  230. {
  231. if(strcmpi(name,person[i].lname)==0)
  232. {
  233. printf("%d)\nfirst name: %s\nlast name: %s\nAddress: %s\nCity: %s\nNumber: %s\n\n\n",i+1,person[i].fname,person[i].lname,person[i].address,person[i].city,person[i].number);
  234. p++;
  235. }
  236. }
  237. if(p==0)
  238. {
  239. printf("the name doesn't exist.\n");
  240. }
  241. return p;
  242. }
  243. void save1()
  244. {
  245. int i;
  246. pb=fopen("phonebook.txt","w");
  247. for(i=0; i<counter ; i++)
  248. {
  249. fprintf(pb,"%s,%s,%s,%s,%s",person[i].fname,person[i].lname,person[i].address,person[i].city,person[i].number);
  250. if(i!=counter-1)
  251. fprintf(pb,"\n");
  252. }
  253. fclose(pb);
  254. printf("\n\nyour changes saved.\n\n");
  255. }
  256. void save()
  257. {
  258. int i;
  259. pb=fopen("phonebook.txt","w");
  260. for(i=0; i<counter ; i++)
  261. {
  262. fprintf(pb,"%s,%s,%s,%s,%s",person[i].fname,person[i].lname,person[i].address,person[i].city,person[i].number);
  263. if(i!=counter-1)
  264. fprintf(pb,"\n");
  265. }
  266. fclose(pb);
  267. printf("\n\nyour changes saved.\n\n");
  268. exit(0);
  269. }
  270.  
  271. void print()
  272. {
  273. int i,j,k,l;
  274. phonebook swap_;
  275. for (i=0 ; i < counter; i++)
  276. {
  277. for (j=0; j < counter-1; j++)
  278. {
  279. if (strcmpi(person[j].lname,person[j+1].lname)>0)
  280. {
  281. swap_=person[j];
  282. person[j]=person[j+1];
  283. person[j+1]=swap_;
  284. }
  285. else if(strcmpi(person[j].lname,person[j+1].lname)==0)
  286. {
  287. if (strcmpi(person[j].fname,person[j+1].fname)>0)
  288. {
  289. swap_=person[j];
  290. person[j]=person[j+1];
  291. person[j+1]=swap_;
  292. }
  293. else if(strcmpi(person[j].fname,person[j+1].fname)==0)
  294. {
  295. if(strcmp(person[j].number,person[j+1].number)>0)
  296. {
  297. swap_=person[j];
  298. person[j]=person[j+1];
  299. person[j+1]=swap_;
  300. }
  301. }
  302. }
  303. else
  304. continue;
  305. }
  306. }
  307. system("cls");
  308. for (i=0 ; i <counter; i++)
  309. printf("%d)\nfirst name: %s\nlast name: %s\nAddress: %s\nCity: %s\nNumber: %s\n",i+1,person[i].fname,person[i].lname,person[i].address,person[i].city,person[i].number);
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement