Guest User

Untitled

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