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.  
  82.  
  83. /* Modify from here */
  84.  
  85. void InitHeap(Heap *pheap) {
  86. //set number of nodes to 0
  87. pheap->num = 0;
  88. };
  89.  
  90. bool IsEmpty(Heap *pheap) {
  91. //check if the number of nodes is 0
  92. return pheap->num == 0;
  93. };
  94.  
  95. bool IsFull(Heap *pheap) {
  96. //check if the number of nodes reached the maximum possible number of heap
  97. return pheap->num == MAX_HEAP;
  98. };
  99.  
  100. void Insert(Heap *pheap, Hex_num data) {
  101. //set index to last position
  102. int index = pheap->num + 1;
  103.  
  104. //while we reach the root
  105. while (index>1) {
  106. //get the parent of index
  107. int parent = index / 2;
  108.  
  109. //compare the new node with its parent
  110. if (compareHex(data, pheap->items[parent].data)<0) { //if new node is smaller
  111. //swap the two nodes
  112. pheap->items[index] = pheap->items[parent];
  113. index = parent;
  114. }
  115. else break; //break from loop if new node is larger than its parent
  116. }
  117.  
  118. //insert the new node to the final index we found from the loop above
  119. HNode newNode;
  120. newNode.data = data;
  121. pheap->items[index] = newNode;
  122.  
  123. //increment the number of nodes in the heap
  124. pheap->num++;
  125.  
  126. };
  127.  
  128. Hex_num Delete(Heap *pheap) {
  129. //store minimum (item to be deleted)
  130. Hex_num min = pheap->items[1].data;
  131.  
  132. //get the last node in the heap
  133. HNode last = pheap->items[pheap->num];
  134. int index = 1, child;
  135.  
  136. //compare the parent (starting from root) with all of its children
  137. while (child = GetHighPrioityChild(pheap, index)) {
  138. //if the child's data is smaller than the last node
  139. if (compareHex(pheap->items[child].data, last.data)<0) {
  140. //swap the parent and child
  141. pheap->items[index] = pheap->items[child];
  142. index = child;
  143. }
  144. else break; //break from the loop if child's data is larger than last node
  145. }
  146.  
  147. //set last node to the index we found from the loop above
  148. pheap->items[index] = last;
  149.  
  150. //decrement the number of nodes in the heap
  151. pheap->num--;
  152.  
  153. //return the value that is deleted from the heap
  154. return min;
  155. };
  156.  
  157. int compareHex(Hex_num num1, Hex_num num2) {
  158. int firstNum[3], secondNum[3]; //array to store hex_num
  159.  
  160. //convert hex characters to integer and save it to integer array
  161. firstNum[0] = convertToInt(num1.large);
  162. firstNum[1] = convertToInt(num1.middle);
  163. firstNum[2] = convertToInt(num1.small);
  164. secondNum[0] = convertToInt(num2.large);
  165. secondNum[1] = convertToInt(num2.middle);
  166. secondNum[2] = convertToInt(num2.small);
  167.  
  168. //convert hex numbers to decimal
  169. int temp = firstNum[0] * 16 * 16 + firstNum[1] * 16 + firstNum[2];
  170. int temp2 = secondNum[0] * 16 * 16 + secondNum[1] * 16 + secondNum[2];
  171.  
  172. //return the difference between the first hex num and the second hex num
  173. return temp - temp2;
  174.  
  175. }
  176.  
  177. int GetHighPrioityChild(Heap *pheap, int idx) {
  178. if (idx * 2 > pheap->num) return 0; //case where there is no child node
  179.  
  180. else if (idx * 2 == pheap->num) return idx * 2; //case where there is only a left child.
  181. else {
  182. //get the index of left and right child
  183. int left = idx * 2, right = idx * 2 + 1;
  184.  
  185. //compare left and right child and return the node with smaller data
  186. if (compareHex(pheap->items[left].data, pheap->items[right].data) < 0) {
  187. return left;
  188. }
  189. else {
  190. return right;
  191. }
  192. }
  193. }
  194.  
  195. //function to convert hex character to integer value
  196. int convertToInt(char c) {
  197. switch (c) {
  198. case '0': return 0; break;
  199. case '1': return 1; break;
  200. case '2': return 2; break;
  201. case '3': return 3; break;
  202. case '4': return 4; break;
  203. case '5': return 5; break;
  204. case '6': return 6; break;
  205. case '7': return 7; break;
  206. case '8': return 8; break;
  207. case '9': return 9; break;
  208. case 'A': return 10; break;
  209. case 'B': return 11; break;
  210. case 'C': return 12; break;
  211. case 'D': return 13; break;
  212. case 'E': return 14; break;
  213. case 'F': return 15; break;
  214. default: return 0; break;
  215. }
  216. }
  217.  
  218.  
  219. /* 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:110: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:137:17: warning: implicit declaration of function ‘GetHighPrioityChild’ [-Wimplicit-function-declaration]
  while (child = GetHighPrioityChild(pheap, index)) {
                 ^~~~~~~~~~~~~~~~~~~
prog.c:137:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  while (child = GetHighPrioityChild(pheap, index)) {
  ^~~~~
prog.c: In function ‘compareHex’:
prog.c:161:16: warning: implicit declaration of function ‘convertToInt’ [-Wimplicit-function-declaration]
  firstNum[0] = convertToInt(num1.large);
                ^~~~~~~~~~~~
prog.c: At top level:
prog.c:196:5: error: conflicting types for ‘convertToInt’
 int convertToInt(char c) {
     ^~~~~~~~~~~~
prog.c:196: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:161:16: note: previous implicit declaration of ‘convertToInt’ was here
  firstNum[0] = convertToInt(num1.large);
                ^~~~~~~~~~~~
stdout
Standard output is empty