fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define MAX_HEAP 100
  6.  
  7. typedef enum { false, true } bool;
  8.  
  9. typedef struct {
  10. char small;
  11. char middle;
  12. char large;
  13. } Hex_num;
  14.  
  15. typedef struct {
  16. Hex_num data; // This is a priority as well as data
  17. } HNode;
  18.  
  19. typedef struct {
  20. HNode items[MAX_HEAP + 1];
  21. int num;
  22. } Heap;
  23.  
  24. void InitHeap(Heap *pheap);
  25.  
  26. bool IsEmpty(Heap *pheap);
  27.  
  28. bool IsFull(Heap *pheap);
  29.  
  30. void Insert(Heap *pheap, Hex_num data);
  31.  
  32. Hex_num Delete(Heap *pheap);
  33.  
  34. void HeapSort(Hex_num a[], int n);
  35.  
  36. Hex_num *CreateHexNum(char str[]);
  37.  
  38. void GetInput();
  39.  
  40. int main() {
  41.  
  42. Hex_num a[5] = { { '0','F','0' },{ '1','2','3' },{ 'F','F','F' },{ 'C','C','C' },{ '0','D','3' } }; // 0F0321FFFCCC3D0
  43. HeapSort(a, 5);
  44.  
  45. // GetInput();
  46. /*
  47. 5
  48. 0F0321FFFCCC3D0
  49. */
  50.  
  51. return 0;
  52. }
  53.  
  54. void HeapSort(Hex_num a[], int n) {
  55. Heap heap;
  56. InitHeap(&heap);
  57.  
  58. // Insert all elements to the heap.
  59. for (int i = 0; i < n; i++)
  60. Insert(&heap, a[i]);
  61.  
  62. // Remove all elements from the heap.
  63. for (int i = n - 1; i >= 0; i--)
  64. a[i] = Delete(&heap);
  65.  
  66. for (int i = 0; i < n; i++)
  67. printf("%c%c%c", a[i].large, a[i].middle, a[i].small);
  68. }
  69.  
  70. Hex_num *CreateHexNum(char str[]) {
  71. int n = strlen(str) / 3;
  72. Hex_num *res = (Hex_num*)malloc(sizeof(Hex_num)*(n));
  73. for (int i = 0; i < n; i++) {
  74. res[i].large = str[i * 3];
  75. res[i].middle = str[i * 3 + 1];
  76. res[i].small = str[i * 3 + 2];
  77. }
  78. return res;
  79. }
  80.  
  81. void GetInput() {
  82. int n;
  83. char *a;
  84. Hex_num *data;
  85.  
  86. scanf("%d", &n);
  87. a = malloc(sizeof(char)*(3 * n + 1));
  88. scanf("%s", a);
  89. data = CreateHexNum(a);
  90. HeapSort(data, n);
  91. }
  92.  
  93. /* Modify from here */
  94.  
  95. void InitHeap(Heap *pheap) {
  96. //set number of nodes to 0
  97. pheap->num = 0;
  98. };
  99.  
  100. bool IsEmpty(Heap *pheap) {
  101. //check if the number of nodes is 0
  102. return pheap->num == 0;
  103. };
  104.  
  105. bool IsFull(Heap *pheap) {
  106. //check if the number of nodes reached the maximum possible number of heap
  107. return pheap->num == MAX_HEAP;
  108. };
  109.  
  110. void Insert(Heap *pheap, Hex_num data) {
  111. //set index to last position
  112. int index = pheap->num + 1;
  113.  
  114. //while we reach the root
  115. while (index>1) {
  116. //get the parent of index
  117. int parent = index / 2;
  118.  
  119. //compare the new node with its parent
  120. if (compareHex(data, pheap->items[parent].data)<0) { //if new node is smaller
  121. //swap the two nodes
  122. pheap->items[index] = pheap->items[parent];
  123. index = parent;
  124. }
  125. else break; //break from loop if new node is larger than its parent
  126. }
  127.  
  128. //insert the new node to the final index we found from the loop above
  129. HNode newNode;
  130. newNode.data = data;
  131. pheap->items[index] = newNode;
  132.  
  133. //increment the number of nodes in the heap
  134. pheap->num++;
  135.  
  136. };
  137.  
  138. Hex_num Delete(Heap *pheap) {
  139. //store minimum (item to be deleted)
  140. Hex_num min = pheap->items[1].data;
  141.  
  142. //get the last node in the heap
  143. HNode last = pheap->items[pheap->num];
  144. int index = 1, child;
  145.  
  146. //compare the parent (starting from root) with all of its children
  147. while (child = GetHighPrioityChild(pheap, index)) {
  148. //if the child's data is smaller than the last node
  149. if (compareHex(pheap->items[child].data, last.data)<0) {
  150. //swap the parent and child
  151. pheap->items[index] = pheap->items[child];
  152. index = child;
  153. }
  154. else break; //break from the loop if child's data is larger than last node
  155. }
  156.  
  157. //set last node to the index we found from the loop above
  158. pheap->items[index] = last;
  159.  
  160. //decrement the number of nodes in the heap
  161. pheap->num--;
  162.  
  163. //return the value that is deleted from the heap
  164. return min;
  165. };
  166.  
  167. int compareHex(Hex_num num1, Hex_num num2) {
  168. int firstNum[3], secondNum[3]; //array to store hex_num
  169.  
  170. //convert hex characters to integer and save it to integer array
  171. firstNum[0] = convertToInt(num1.large);
  172. firstNum[1] = convertToInt(num1.middle);
  173. firstNum[2] = convertToInt(num1.small);
  174. secondNum[0] = convertToInt(num2.large);
  175. secondNum[1] = convertToInt(num2.middle);
  176. secondNum[2] = convertToInt(num2.small);
  177.  
  178. //convert hex numbers to decimal
  179. int temp = firstNum[0] * 16 * 16 + firstNum[1] * 16 + firstNum[2];
  180. int temp2 = secondNum[0] * 16 * 16 + secondNum[1] * 16 + secondNum[2];
  181.  
  182. //return the difference between the first hex num and the second hex num
  183. return temp - temp2;
  184.  
  185. }
  186.  
  187. int GetHighPrioityChild(Heap *pheap, int idx) {
  188. if (idx * 2 > pheap->num) return 0; //case where there is no child node
  189.  
  190. else if (idx * 2 == pheap->num) return idx * 2; //case where there is only a left child.
  191. else {
  192. //get the index of left and right child
  193. int left = idx * 2, right = idx * 2 + 1;
  194.  
  195. //compare left and right child and return the node with smaller data
  196. if (compareHex(pheap->items[left].data, pheap->items[right].data) < 0) {
  197. return left;
  198. }
  199. else {
  200. return right;
  201. }
  202. }
  203. }
  204.  
  205. //function to convert hex character to integer value
  206. int convertToInt(char c) {
  207. switch (c) {
  208. case '0': return 0; break;
  209. case '1': return 1; break;
  210. case '2': return 2; break;
  211. case '3': return 3; break;
  212. case '4': return 4; break;
  213. case '5': return 5; break;
  214. case '6': return 6; break;
  215. case '7': return 7; break;
  216. case '8': return 8; break;
  217. case '9': return 9; break;
  218. case 'A': return 10; break;
  219. case 'B': return 11; break;
  220. case 'C': return 12; break;
  221. case 'D': return 13; break;
  222. case 'E': return 14; break;
  223. case 'F': return 15; break;
  224. default: return 0; break;
  225. }
  226. }
  227.  
  228.  
  229. /* Modify to here */
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘Insert’:
prog.c:120:7: warning: implicit declaration of function ‘compareHex’ [-Wimplicit-function-declaration]
   if (compareHex(data, pheap->items[parent].data)<0) { //if new node is smaller
       ^~~~~~~~~~
prog.c: In function ‘Delete’:
prog.c:147:17: warning: implicit declaration of function ‘GetHighPrioityChild’ [-Wimplicit-function-declaration]
  while (child = GetHighPrioityChild(pheap, index)) {
                 ^~~~~~~~~~~~~~~~~~~
prog.c:147:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  while (child = GetHighPrioityChild(pheap, index)) {
  ^~~~~
prog.c: In function ‘compareHex’:
prog.c:171:16: warning: implicit declaration of function ‘convertToInt’ [-Wimplicit-function-declaration]
  firstNum[0] = convertToInt(num1.large);
                ^~~~~~~~~~~~
prog.c: At top level:
prog.c:206:5: error: conflicting types for ‘convertToInt’
 int convertToInt(char c) {
     ^~~~~~~~~~~~
prog.c:206:1: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
 int convertToInt(char c) {
 ^~~
prog.c:171:16: note: previous implicit declaration of ‘convertToInt’ was here
  firstNum[0] = convertToInt(num1.large);
                ^~~~~~~~~~~~
stdout
Standard output is empty