Advertisement
Guest User

Untitled

a guest
May 16th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. #define NAME 1024
  7. #define EMAIL 512
  8. #define PHONE 64
  9. #define FALSE 0
  10. #define TRUE 1
  11.  
  12.  
  13. typedef struct node {
  14. char *name;
  15. char *email;
  16. char *domain;
  17. char *phone;
  18. struct node *next;
  19. struct node *prev;
  20. } node;
  21.  
  22.  
  23. typedef struct tree {
  24. node *contact;
  25. struct tree *left;
  26. struct tree *right;
  27. } tree;
  28.  
  29.  
  30. // a Alan alan@gmail.com 1-2-3
  31. // a asff afsaf@gmail.com 6-5-7
  32. // a mmmeo didn1134@gmail.com 55-44-3
  33.  
  34.  
  35.  
  36. /* ================================================================================= //
  37. // "a" - ADD A NEW CONTACT //
  38. // ================================================================================= */
  39.  
  40.  
  41. /* Receives a node and an email and adds the email domain to the node */
  42. void add_domain(node *contact, char email[]) {
  43. char aux[NAME];
  44. char *p = strtok(email, "@");
  45. strcpy(aux, strtok(NULL, ""));
  46. contact->domain = (char*)malloc(sizeof(aux));
  47. strcpy(contact->domain, aux);
  48. }
  49.  
  50.  
  51. /* Receives user input and stores it in the right parameters of the node */
  52. void create_contact(char input[], node *contact) {
  53. char aux[NAME];
  54.  
  55. strcpy(aux, strtok(input, " "));
  56. contact->name = (char*)malloc(sizeof(aux));
  57. strcpy(contact->name, aux);
  58.  
  59. strcpy(aux, strtok(NULL, " "));
  60. contact->email = (char*)malloc(sizeof(aux));
  61. strcpy(contact->email, aux);
  62.  
  63. strcpy(aux, strtok(NULL, " "));
  64. contact->phone = (char*)malloc(sizeof(aux));
  65. strcpy(contact->phone, aux);
  66.  
  67. strcpy(aux, contact->email);
  68. add_domain(contact, aux);
  69. }
  70.  
  71.  
  72. int existing_name(tree *root, char name[], node **searched) {
  73. int comp;
  74.  
  75. while (root != NULL) {
  76. switch (comp = strcmp(root->contact->name, name)) {
  77.  
  78. case 0:
  79. *searched = root->contact;
  80. return TRUE;
  81. break;
  82.  
  83. case -1:
  84. return existing_name(root->right, name, searched);
  85. break;
  86.  
  87. case 1:
  88. return existing_name(root->left, name, searched);
  89. break;
  90.  
  91. default:
  92.  
  93. break;
  94. }
  95. }
  96. return FALSE;
  97. }
  98.  
  99.  
  100. tree *insert(tree *root, node *contact) {
  101. tree *new_root;
  102.  
  103. if (root == NULL) {
  104. new_root = (tree*)malloc(sizeof(tree));
  105. new_root->contact = contact;
  106. new_root->left = NULL;
  107. new_root->right = NULL;
  108. root = new_root;
  109. }
  110. else if (strcmp(root->contact->name, contact->name) > 0)
  111. root->left = insert(root->left, contact);
  112. else
  113. root->right = insert(root->right, contact);
  114.  
  115. return root;
  116. }
  117.  
  118.  
  119. /* Creates a new node, stores the contact information inside of it, links
  120. it to the rest of the list and returns the address of the new list */
  121. void add_contact(node **head, node **tail, tree **root, char input[]) {
  122. node *new_contact = (node*)malloc(sizeof(node));
  123. node *temp_search;
  124. create_contact(input, new_contact);
  125.  
  126. if (!existing_name(*root, new_contact->name, &temp_search)) {
  127. if (*head == NULL)
  128. {
  129. new_contact->next = NULL;
  130. new_contact->prev = NULL;
  131. *head = new_contact;
  132. *tail = new_contact;
  133. *root = insert(*root, new_contact);
  134.  
  135. }
  136. else {
  137. new_contact->next = (*tail)->next;
  138. new_contact->prev = *tail;
  139. (*tail)->next = new_contact;
  140. *tail = new_contact;
  141. *root = insert(*root, new_contact);
  142. }
  143. }
  144. else
  145. printf("Nome existente.\n");
  146. }
  147.  
  148.  
  149. /* ================================================================================= //
  150. // "l" - PRINT CONTACTS CHRONOLOGICALLY //
  151. // ================================================================================= */
  152.  
  153.  
  154. void print_contact(node *contact) {
  155. printf("%s %s %s\n", contact->name, contact->email, contact->phone);
  156. }
  157.  
  158.  
  159. void print_contacts(node *head) {
  160. node *contact = head;
  161.  
  162. for (; contact != NULL; contact = contact->next)
  163. print_contact(contact);
  164. }
  165.  
  166.  
  167. /* ================================================================================= //
  168. // "p" - SEARCH CONTACTS BY NAME //
  169. // ================================================================================= */
  170.  
  171. void search_contact(tree *root, char name[]) {
  172. tree *root_copy = root;
  173. node *n_contact;
  174.  
  175. if (!existing_name(root_copy, name, &n_contact)) {
  176. printf("Nome inexistente.\n");
  177. return;
  178. }
  179. print_contact(n_contact);
  180. }
  181.  
  182.  
  183. /* ================================================================================= //
  184. // "r" - REMOVE CONTACT //
  185. // ================================================================================= */
  186.  
  187. tree *parent(tree *root) {
  188. if (root == NULL || root->right == NULL)
  189. return root;
  190. else
  191. return parent(root->right);
  192. }
  193.  
  194.  
  195. tree *tree_remove(tree *root, char name[]) {
  196. tree *new_parent;
  197. node *removed;
  198.  
  199. if (root == NULL)
  200. return root;
  201.  
  202. else if (strcmp(root->contact->name, name) < 0)
  203. tree_remove(root->right, name);
  204.  
  205. else if (strcmp(root->contact->name, name) > 0)
  206. tree_remove(root->left, name);
  207.  
  208. else {
  209. if ((root->left != NULL) && (root->right != NULL)) {
  210. new_parent = parent(root->left);
  211. removed = root->contact;
  212. root->contact = new_parent->contact;
  213. new_parent->contact = removed;
  214. root->left = tree_remove(root->left, new_parent->contact->name);
  215. }
  216. else {
  217. if ((root->left == NULL) && (root->right == NULL))
  218. root = NULL;
  219.  
  220. else if (root->left == NULL)
  221. root = root->right;
  222.  
  223. else
  224. root = root->left;
  225. }
  226. }
  227. return root;
  228. }
  229.  
  230.  
  231. void remove_contact(node **head, tree **root, char name[]) {
  232. node *temp_prev, *temp_next, *removed;
  233.  
  234. if (!existing_name(*root, name, &removed)) {
  235. printf("Nome inexistente.\n");
  236. return;
  237. }
  238.  
  239. *root = tree_remove(*root, name);
  240. if (removed->prev == NULL) {
  241. temp_prev = *head;
  242. *head = (*head)->next;
  243. (*head)->prev = NULL;
  244. free(temp_prev);
  245. }
  246. else {
  247. temp_prev = removed->prev;
  248. temp_next = removed->next;
  249. temp_prev->next = temp_next;
  250. free(removed);
  251. }
  252. }
  253.  
  254.  
  255. /* ================================================================================= //
  256. // "e" - CHANGE THE EMAIL ADDRESS //
  257. // ================================================================================= */
  258.  
  259. void change_email(node *head, tree *root, char name[], char new_email[]) {
  260. node *temp;
  261. node *contact = head;
  262.  
  263. if (!existing_name(root, name, &temp)) {
  264. printf("Nome inexistente.\n");
  265. return;
  266. }
  267.  
  268. for (; contact != NULL; contact = contact->next) {
  269. if (!strcmp(contact->name, name)) {
  270. strcpy(contact->email, new_email);
  271. add_domain(contact, new_email);
  272. }
  273. }
  274. }
  275.  
  276. /* ================================================================================= //
  277. // "c" - COUNT EMAIL DOMAIN INSTANCES //
  278. // ================================================================================= */
  279.  
  280. void count_domain(node *head, char domain[]) {
  281. node *contact = head;
  282. int count = 0;
  283.  
  284. for (; contact != NULL; contact = contact->next) {
  285. if (!strcmp(contact->domain, domain))
  286. ++count;
  287. }
  288. printf("%s:%d\n", domain, count);
  289. }
  290.  
  291.  
  292.  
  293. int main() {
  294. char c, input[NAME + EMAIL + PHONE], name[NAME], email[EMAIL];
  295. node *head = NULL;
  296. node *tail = NULL;
  297. tree *root = NULL;
  298.  
  299.  
  300. while (1) {
  301. switch (c = getchar()) {
  302.  
  303. case 'a':
  304.  
  305. scanf("%[0-9a-zA-Z:-+@ ]s", input);
  306. add_contact(&head, &tail, &root, input);
  307. break;
  308.  
  309. case 'l':
  310.  
  311. print_contacts(head);
  312. break;
  313.  
  314. case 'p':
  315.  
  316. scanf("%s", name);
  317. search_contact(root, name);
  318. break;
  319.  
  320. case 'r':
  321.  
  322. scanf("%s", name);
  323. remove_contact(&head, &root, name);
  324. break;
  325.  
  326. case 'e':
  327.  
  328. scanf("%s %s", name, email);
  329. //head = change_email(head, name, email);
  330. break;
  331.  
  332. case 'c':
  333.  
  334. scanf("%s", email);
  335. count_domain(head, email);
  336. break;
  337.  
  338. case 'x':
  339.  
  340. free(head);
  341. free(root);
  342. return 0;
  343.  
  344. default:
  345.  
  346. break;
  347.  
  348. }
  349. }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement