Guest User

Untitled

a guest
Jan 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.43 KB | None | 0 0
  1. /*
  2. Brian St.Amour
  3. COP 3502
  4. Program 5
  5. 4/1/11
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11.  
  12.  
  13. typedef struct buddyList{
  14. int buddy;
  15. struct buddyList *next;
  16. } buddies;
  17.  
  18.  
  19. typedef struct tree_node{
  20. int ID;
  21. char firstName[21];
  22. char lastName[21];
  23. int age;
  24. buddies *myBuddies;
  25. struct tree_node *left;
  26. struct tree_node *right;
  27. } BSTnode;
  28.  
  29.  
  30. BSTnode* create_node(int val, char first[], char last[], int age);
  31. BSTnode* insert(BSTnode *root, BSTnode *element);
  32. BSTnode* findID(BSTnode *ptr, int val);
  33. BSTnode* findNAME(BSTnode *ptr, char first[], char last[]);
  34. BSTnode* parent(BSTnode *root, BSTnode *node);
  35. BSTnode* maxVal(BSTnode *root);
  36. buddies* insertBuddyNode(buddies *ptr, int value);
  37. buddies* deleteBuddyNode(buddies *ptr, int value);
  38. void addBuddies(BSTnode *ptr, char last[], char first[], char last2[], char first2[]);
  39. void unbuddy(BSTnode *ptr, char last[], char first[], char last2[], char first2[]);
  40. void printNodeID(BSTnode *ptr, int ID, int num);
  41. void printNodeName(BSTnode *ptr, char first[], char last[], int num);
  42. void printBuddies(buddies *ptr);
  43. void inorder(BSTnode *ptr, FILE*);
  44. int compare(int ID, buddies *ptr);
  45. int isLeaf(BSTnode *node);
  46. int hasOnlyLeftChild(BSTnode *node);
  47. int hasOnlyRightChild(BSTnode *node);
  48. int count(buddies *ptr);
  49.  
  50. FILE *ifp, *ofp;
  51. BSTnode* root = NULL;
  52.  
  53.  
  54. /**********************************************************************
  55. * main *
  56. * *
  57. * *
  58. * *
  59. **********************************************************************/
  60.  
  61. int main(void)
  62. {
  63.  
  64. int i, j, k, ID, age, num;
  65. char command[10], fName[21], lName[21], fName2[21], lName2[21];
  66. BSTnode* temp_node;
  67. BSTnode* temp_node1;
  68. BSTnode* parent_node;
  69. BSTnode* maxNode;
  70.  
  71. ifp = fopen("buddyBook.in", "r");
  72. ofp = fopen("buddyBook.out", "w");
  73. if (ifp == NULL) {
  74. printf("Error reading file.\n");
  75. system("pause");
  76. return 1;
  77. }
  78.  
  79. if (ofp == NULL) {
  80. printf("File does not exist.\n");
  81. system("pause");
  82. return 1;
  83. }
  84.  
  85. fscanf(ifp, "%d", &k);
  86.  
  87. for(i = 0; i < k; i++)
  88. {
  89. fscanf(ifp , "%s", command);
  90.  
  91. if(strcmp(command, "ADD") == 0)
  92. {
  93. fscanf(ifp, "%d%s%s%d", &ID, fName, lName, &age);
  94. if(age < 13)
  95. {
  96. fprintf(ofp, "%s %s rejected - You must be 13 or over to create a profile.", fName, lName);
  97. }
  98.  
  99. else
  100. {
  101. temp_node = (BSTnode*) create_node(ID, fName, lName, age);
  102. root = insert(root, temp_node);
  103. fprintf(ofp, "Added %s %s, age %d", temp_node->firstName, temp_node->lastName, temp_node->age);
  104. }
  105. }
  106.  
  107.  
  108. else if (strcmp(command, "FINDNAME") == 0)
  109. {
  110. fscanf(ifp, "%s%s", fName, lName);
  111. temp_node = findNAME(root, fName, lName);
  112. printNodeName(temp_node, fName, lName, count(temp_node->myBuddies));
  113. }
  114.  
  115.  
  116. else if(strcmp(command, "FINDID") == 0)
  117. {
  118. fscanf(ifp, "%d", &ID);
  119. temp_node = findID(root, ID);
  120. printNodeID(temp_node, ID, count(temp_node->myBuddies));
  121. }
  122.  
  123.  
  124. else if(strcmp(command, "BUDDY") == 0)
  125. {
  126. fscanf(ifp, "%s%s%s%s", fName, lName, fName2, lName2);
  127. temp_node = (BSTnode*)findNAME(root, fName, lName);
  128. temp_node1 = (BSTnode*)findNAME(root, fName, lName);
  129.  
  130. if(compare(temp_node->ID, temp_node1->myBuddies) == 1)
  131. {
  132. fprintf(ofp, "Cannot Perform BUDDY Command:\n\t %s %s %s %s are already buddies.", fName, lName, fName2, lName2);
  133. }
  134.  
  135. else if(findNAME(root, fName, lName) == NULL && findNAME(root, fName2, lName2) == NULL)
  136. {
  137. fprintf(ofp, "Cannot Perform BUDDY Command:\n\t %s %s - This profile does not exist.\n\t %s %s - This profile does not exist.", fName, lName, fName2, lName2);
  138. }
  139.  
  140. else if(findNAME(root, fName, lName) == NULL)
  141. {
  142. fprintf(ofp, "Cannot Perform BUDDY Command:\n\t %s %s - This profile does not exist.", fName, lName);
  143. }
  144. else if(findNAME(root, fName2, lName2) == NULL)
  145. {
  146. fprintf(ofp, "Cannot Perform BUDDY Command:\n\t %s %s - This profile does not exist.", fName2, lName2);
  147. }
  148.  
  149. else
  150. {
  151. addBuddies(root, lName, fName, lName2, fName2);
  152. fprintf(ofp, "%s %s and %s %s are now Buddies.\n", fName, lName, fName2, lName2);
  153. }
  154. }
  155.  
  156.  
  157. else if(strcmp(command, "UNBUDDY") == 0)
  158. {
  159. fscanf(ifp, "%s%s%s%s", fName, lName, fName2, lName2);
  160. temp_node = (BSTnode*)findNAME(root, fName, lName);
  161. temp_node1 = (BSTnode*)findNAME(root, fName, lName);
  162.  
  163. if(compare(temp_node->ID, temp_node1->myBuddies) != 1)
  164. {
  165. fprintf(ofp, "Cannot Perform UNBUDDY Command:\n\t %s %s and %s %s are not Buddies.", fName, lName, fName2, lName2);
  166. }
  167.  
  168. else if(findNAME(root, fName, lName) == NULL && findNAME(root, fName, lName) == NULL)
  169. {
  170. fprintf(ofp, "Cannot Perform UNBUDDY Command:\n\t %s %s - This profile does not exist.\n\t %s %s - This profile does not exist.", fName, lName, fName2, lName2);
  171. }
  172.  
  173. else if(findNAME(root, fName, lName) == NULL)
  174. {
  175. fprintf(ofp, "Cannot Perform UNBUDDY command:\n\t %s %s - This profile does not exist.", fName, lName);
  176. }
  177.  
  178. else if(findNAME(root, fName2, lName2) == NULL)
  179. {
  180. fprintf(ofp, "Cannot Perform UNBUDDY command:\n\t %s %s - This profile does not exist.", fName2, lName2);
  181. }
  182.  
  183. else
  184. {
  185. unbuddy(root, lName, fName, lName2, fName2);
  186. fprintf(ofp, "%s %s and %s %s are no longer Buddies.\n", fName, lName, fName2, lName2);
  187. }
  188.  
  189. }
  190.  
  191.  
  192. else if(strcmp(command,"DELETE") == 0)
  193. {
  194. fscanf(ofp, "%s%s", fName, lName);
  195. temp_node = findNAME(root, fName, lName);
  196. if(temp_node == NULL) fprintf(ofp, "Cannot delete %s %s.\n", fName, lName);
  197. else
  198. {
  199. if(isLeaf(temp_node))
  200. {
  201. parent_node = (BSTnode*)malloc(sizeof(BSTnode));
  202. parent_node = (BSTnode*)parent(root, temp_node);
  203. if(temp_node->ID > parent_node->ID)
  204. {
  205. free(parent_node->right);
  206. parent_node->right = NULL;
  207. }
  208.  
  209. else
  210. {
  211. free(parent_node->left);
  212. parent_node->left = NULL;
  213. }
  214.  
  215. }
  216. else if(hasOnlyLeftChild(temp_node))
  217. {
  218. parent_node = (BSTnode*)malloc(sizeof(BSTnode));
  219. parent_node = (BSTnode*)parent(root, temp_node);
  220.  
  221. if(temp_node->ID > parent_node->ID)
  222. {
  223. parent_node->right = parent_node->right->left;
  224. free(temp_node);
  225. }
  226.  
  227. else
  228. {
  229. parent_node->left = parent_node->left->right;
  230. free(temp_node);
  231. }
  232. }
  233.  
  234. else if(hasOnlyRightChild(temp_node))
  235. {
  236. parent_node = (BSTnode*)malloc(sizeof(BSTnode));
  237. parent_node = (BSTnode*)parent(root, temp_node);
  238.  
  239. if(temp_node->ID > parent_node->ID)
  240. {
  241.  
  242. parent_node->right = parent_node->right->right;
  243. free(temp_node);
  244. }
  245.  
  246. else
  247. {
  248. parent_node->left = parent_node->left->right;
  249. free(temp_node);
  250. }
  251. }
  252.  
  253. else if(hasOnlyRightChild(temp_node) && hasOnlyLeftChild(temp_node))
  254. {
  255. parent_node = (BSTnode*)malloc(sizeof(BSTnode));
  256. maxNode = (BSTnode*)malloc(sizeof(BSTnode));
  257. parent_node = (BSTnode*)parent(root, temp_node);
  258. maxNode = (BSTnode*)maxVal(temp_node);
  259.  
  260. if(temp_node->ID < parent_node->ID);
  261. {
  262. maxNode->right = maxNode->left;
  263. maxNode->left = temp_node->left;
  264. parent_node->left = parent_node->left->right;
  265. free(temp_node);
  266. }
  267.  
  268. }
  269. }
  270.  
  271. }
  272.  
  273.  
  274. else if(strcmp(command,"PRINTBUDDIES") == 0)
  275. {
  276. fscanf(ifp, "%s%s", fName, lName);
  277. temp_node = findNAME(root, fName, lName);
  278. num = count(temp_node->myBuddies);
  279.  
  280. if(num == 0) fprintf(ofp, "%s %s has no Buddies;\n", fName, lName);
  281. if(num != 1)
  282. {
  283. fprintf(ofp, "%s %s has %d Buddies:\n", fName, lName, num);
  284. printBuddies(temp_node->myBuddies);
  285. }
  286. else
  287. {
  288. fprintf(ofp, "%s %s has 1 Buddy:\n", fName, lName);
  289. printBuddies(temp_node->myBuddies);
  290. }
  291.  
  292.  
  293. }
  294.  
  295.  
  296. else if(strcmp(command, "PRINTTREE") == 0)
  297. {
  298. if(root != NULL){
  299. fprintf(ofp, "Members of Buddy Book:\n");
  300. inorder(root, ofp);
  301. }
  302. }
  303.  
  304.  
  305.  
  306. }
  307.  
  308. return 1;
  309. }
  310.  
  311.  
  312.  
  313. BSTnode* create_node(int val, char first[], char last[], int age)
  314. {
  315. BSTnode* temp;
  316. temp = (BSTnode*)malloc(sizeof(BSTnode));
  317.  
  318. temp->ID = val;
  319. strcpy(temp->firstName, first);
  320. strcpy(temp->lastName, last);
  321. temp->left = NULL;
  322. temp->age = age;
  323. temp->right = NULL;
  324.  
  325. return temp;
  326. }
  327.  
  328.  
  329.  
  330. BSTnode* insert(BSTnode *root, BSTnode *element)
  331. {
  332. if (root == NULL)
  333. return element;
  334. else
  335. {
  336. if (element->ID > root->ID)
  337. root->right = insert(root->right, element);
  338. else
  339. root->left = insert(root->left, element);
  340. return root;
  341. }
  342. }
  343.  
  344.  
  345.  
  346. BSTnode* findID(struct tree_node *ptr, int val)
  347. {
  348. if (ptr != NULL)
  349. {
  350. if (ptr->ID == val) return ptr;
  351. if (val < ptr->ID) return findID(ptr->left, val);
  352. else return findID(ptr->right, val);
  353. }
  354. else
  355.  
  356. return NULL;
  357. }
  358.  
  359.  
  360.  
  361. BSTnode* findNAME(BSTnode *ptr, char first[], char last[])
  362. {
  363. if (ptr != NULL)
  364. {
  365. if (strcmp(ptr->lastName, last))
  366. {
  367. if(strcmp(ptr->firstName, first)) return ptr;
  368. else return NULL;
  369. }
  370.  
  371. if(strcmp(ptr->lastName, last) > 0) return findNAME(ptr->left, first, last);
  372. else return findNAME(ptr->right, first, last);
  373. }
  374. else
  375. return NULL;
  376. }
  377.  
  378.  
  379.  
  380. void addBuddies(BSTnode *ptr, char last[], char first[], char last2[], char first2[])
  381. {
  382. BSTnode *temp, *temp1;
  383. if (ptr != NULL)
  384. {
  385. temp = findNAME(ptr, first, last);
  386. temp1 = findNAME(ptr, first2, last2);
  387. insertBuddyNode(temp->myBuddies, temp1->ID);
  388. insertBuddyNode(temp1->myBuddies, temp->ID);
  389. }
  390.  
  391. }
  392.  
  393.  
  394.  
  395. void unbuddy(BSTnode *ptr, char last[], char first[], char last2[], char first2[])
  396. {
  397. BSTnode *temp, *temp1;
  398. if(ptr != NULL)
  399. {
  400. temp = findNAME(ptr, first, last);
  401. temp1 = findNAME(ptr, first2, last2);
  402. deleteBuddyNode(temp->myBuddies, temp1->ID);
  403. deleteBuddyNode(temp1->myBuddies, temp->ID);
  404. }
  405. }
  406.  
  407.  
  408.  
  409. buddies* insertBuddyNode(buddies *ptr, int value)
  410. {
  411. buddies *help_ptr = ptr;
  412. buddies *pNew = (buddies*)malloc(sizeof(buddies));
  413. pNew->buddy = value;
  414. pNew->next = NULL;
  415.  
  416. if (ptr == NULL || ptr->buddy > value)
  417. {
  418. pNew->next = ptr;
  419. ptr = pNew;
  420. return ptr;
  421. }
  422.  
  423. while(help_ptr->next != NULL && help_ptr->next->buddy < value)
  424. {
  425. help_ptr = help_ptr->next;
  426. pNew->next = help_ptr->next;
  427. help_ptr->next = pNew;
  428. return ptr;
  429. }
  430.  
  431. }
  432.  
  433.  
  434.  
  435. buddies* deleteBuddyNode(buddies *ptr, int value)
  436. {
  437. buddies *help_ptr = ptr;
  438. buddies *node2delete;
  439.  
  440. if (help_ptr != NULL)
  441. {
  442. if(help_ptr->buddy == value)
  443. {
  444. ptr = help_ptr->next;
  445. free(help_ptr);
  446. return ptr;
  447. }
  448.  
  449. while (help_ptr->next != NULL)
  450. {
  451. if(help_ptr->next->buddy == value)
  452. {
  453. node2delete = help_ptr->next;
  454. help_ptr->next = help_ptr->next->next;
  455. free(node2delete);
  456. return ptr;
  457. }
  458. help_ptr = help_ptr->next;
  459. }
  460. }
  461. return ptr;
  462. }
  463.  
  464.  
  465.  
  466. void printNodeID(BSTnode *ptr, int ID, int num)
  467. {
  468. if(ptr != NULL)
  469. {
  470. if(num != 1)
  471. fprintf(ofp, "Found: %s %s, age %d, %d Buddies\n", ptr->firstName, ptr->lastName, ptr->age, num);
  472.  
  473. else
  474. fprintf(ofp, "Found: %s %s, age %d, 1 Buddy\n", ptr->firstName, ptr->lastName, ptr->age);
  475. }
  476.  
  477. else fprintf(ofp, "ID %d not found.\n", ID);
  478. }
  479.  
  480.  
  481.  
  482. void printNodeName(BSTnode *ptr, char first[], char last[], int num)
  483. {
  484. if(ptr != NULL)
  485. {
  486. if(num != 1)
  487. fprintf(ofp, "Found: %s %s, age %d, %d Buddies\n", ptr->firstName, ptr->lastName, ptr->age, num);
  488.  
  489. else
  490. fprintf(ofp, "Found: %s %s, age %d, 1 Buddy\n", ptr->firstName, ptr->lastName, ptr->age);
  491. }
  492.  
  493. else fprintf(ofp, "%s %s not found.\n", first, last);
  494. }
  495.  
  496.  
  497.  
  498. void printBuddies(buddies *ptr)
  499. {
  500. buddies *help_ptr;
  501. BSTnode* temp;
  502. temp = (BSTnode*)malloc(sizeof(BSTnode));
  503. help_ptr = ptr;
  504.  
  505. while(help_ptr != NULL)
  506. {
  507. temp = findID(root, help_ptr->buddy);
  508. fprintf(ofp, "\t%s %s", temp->firstName, temp->lastName);
  509. help_ptr = help_ptr->next;
  510. }
  511. }
  512.  
  513.  
  514.  
  515. void inorder(BSTnode *ptr, FILE *ofp)
  516. {
  517.  
  518. if (ptr != NULL) {
  519. inorder(ptr->left, ofp);
  520. fprintf(ofp, "\t%s %s, age %d, %d Buddies\n", ptr->firstName, ptr->lastName, ptr->age, count(ptr->myBuddies));
  521. inorder(ptr->right, ofp);
  522. }
  523. }
  524.  
  525.  
  526.  
  527. int compare(int ID, buddies *ptr)
  528. {
  529. buddies *help_ptr;
  530. help_ptr = ptr;
  531.  
  532. while(help_ptr != NULL)
  533. {
  534. if (help_ptr->buddy == ID) return 1;
  535. help_ptr = help_ptr->next;
  536. }
  537. return 0;
  538. }
  539.  
  540.  
  541.  
  542. BSTnode* parent(BSTnode *root, BSTnode *node)
  543. {
  544. if (root == NULL || root == node) return NULL;
  545. if (root->left == node || root->right == node) return root;
  546. if (node->ID < root->ID) return parent (root->left, node);
  547. else if (node->ID > root->ID) return parent(root->right, node);
  548.  
  549. return NULL;
  550. }
  551.  
  552.  
  553.  
  554. int isLeaf(BSTnode *node)
  555. {
  556. return (node->left == NULL && node->right == NULL);
  557. }
  558.  
  559.  
  560.  
  561. int hasOnlyLeftChild(BSTnode *node)
  562. {
  563. return (node->left != NULL && node->right == NULL);
  564. }
  565.  
  566.  
  567.  
  568. int hasOnlyRightChild(BSTnode *node)
  569. {
  570. return (node->left == NULL && node->right != NULL);
  571. }
  572.  
  573.  
  574.  
  575. BSTnode* maxVal(BSTnode *root)
  576. {
  577. if (root->right == NULL) return root;
  578. else return maxVal(root->right);
  579. }
  580.  
  581.  
  582. int count(buddies *ptr)
  583. {
  584. int i;
  585.  
  586. for(i = 0; ptr != NULL; i++)
  587. {
  588. ptr = ptr->next;
  589. }
  590. return i;
  591. }
Add Comment
Please, Sign In to add comment