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