Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. // Laborübung19.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
  2. //
  3.  
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #pragma warning (disable:4996)
  9.  
  10.  
  11. typedef int dataT;
  12.  
  13. typedef struct node {
  14. struct node* left;
  15. struct node* right;
  16. dataT daten;
  17. }node_t;
  18.  
  19. void init(node_t* root);
  20. void append(node_t* root, unsigned int LR, dataT data);
  21. void balance(node_t* root);
  22. void stringParsen(char *a, char**ptr[10]);
  23.  
  24. int state[4][10] = {
  25. {2,1,1,1,2,1,1,1,2,3},
  26. {1,1,1,1,1,1,1,1,1,1},
  27. {2,1,1,1,2,1,1,1,2,3},
  28. {2,1,1,1,2,1,1,1,2,1}};
  29. char *cmd[10] = { "ST", "SD" ,"SG" ,"GT", "GD","GG","St","Sd","Sg","SF" };
  30.  
  31. int main()
  32. {
  33. //NR1
  34. /*
  35. node_t tree;
  36. init(&tree);
  37. tree.daten = 5;
  38. append(&tree, 1, 8);
  39. append(&tree, 0, 10);
  40. append(tree.left, 1, 7);
  41. append(tree.left, 0, 3);
  42. append(tree.left->left, 0, 1);
  43. balance(&tree);
  44. */
  45.  
  46. //NR2
  47. /*
  48. char a[2];
  49. int s = 0;
  50. int control = 0;
  51.  
  52. while (1) {
  53.  
  54. control = 0;
  55. printf("Zustand: S%d\n", s);
  56. scanf(" %s", a);
  57.  
  58.  
  59. for (int i = 0; i < 10; i++) {
  60. if (!strcmp(a, cmd[i])) {
  61. s = state[s][i];
  62. control = 1;
  63. break;
  64. }
  65. }
  66.  
  67. if (!strcmp(a, "BB"))
  68. break;
  69. if (!strcmp(a, "RE"))
  70. s=0;
  71.  
  72. if (control == 0 && strcmp(a,"RE"))
  73. printf("falsche Eingabe!!\n");
  74.  
  75.  
  76. }
  77. */
  78.  
  79. //NR3
  80. printf("Ausgaben oder Eingabe\n");
  81. char a[100];
  82. scanf(" %s", a);
  83. char *token[10];
  84. strtok("HELLO WORLD", " ");
  85. stringParsen("HALLO WELT", &token[10]);
  86. system("pause");
  87. return 0;
  88. }
  89.  
  90. void stringParsen(char *a, char**ptr[10]) {
  91. char *token = (char *)malloc(sizeof(char*));
  92. token = strtok(a, " ");
  93. *ptr[0] = token;
  94. while (token) {
  95. token = strtok(NULL," ");
  96. *ptr[0] = token;
  97. }
  98. }
  99.  
  100. void init(node_t* root) {
  101. root->left = NULL;
  102. root->right = NULL;
  103. root->daten = 42;
  104. }
  105.  
  106. //LR 0 für links 1 für rechts
  107. void append(node_t* root, unsigned int LR, dataT data) {
  108. if (LR > 1)
  109. return EXIT_FAILURE;
  110.  
  111. //links anhängen
  112. if (LR == 0) {
  113. root->left = (node_t *)malloc(sizeof(node_t));
  114. root->left->left = NULL;
  115. root->left->right = NULL;
  116. root->left->daten = data;
  117. }
  118.  
  119. //rechts anhängen
  120. else {
  121. root->right = (node_t *)malloc(sizeof(node_t));
  122. root->right->right = NULL;
  123. root->right->left = NULL;
  124. root->right->daten = data;
  125. }
  126. }
  127.  
  128. void balance(node_t* root) {
  129. //abruchbedingung
  130. printf("%p\n", root);
  131. if (root == NULL || (root->left == NULL && root->right == NULL))
  132. return;
  133.  
  134. //baum rekursiv durchegehen (preorder)
  135.  
  136. if (root->left != NULL && root->daten < root->left->daten) {
  137. dataT buffer = root->left->daten;
  138. root->left->daten = root->daten;
  139. root->daten = buffer;
  140. }
  141.  
  142. if (root->right != NULL && root->daten > root->right->daten) {
  143. dataT buffer = root->right->daten;
  144. root->right->daten = root->daten;
  145. root->daten = buffer;
  146. }
  147.  
  148.  
  149. balance(root->left);
  150. balance(root->right);
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement