Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <ctype.h>
  6. #include "big40.h"
  7.  
  8. // Function to initialize Integer40 struct and create an array to hold a big integer
  9. Integer40 *createIntArray(int length)
  10. {
  11. // Create a variable of type Integer40
  12. Integer40 *array = malloc(sizeof(Integer40));
  13.  
  14. // return NULL if call to malloc fails
  15. if(array == NULL)
  16. return NULL;
  17.  
  18. // Access digits variable inside struct and malloc space for it as well
  19. array->digits = malloc(sizeof(int) * length);
  20.  
  21. // If malloc call to create digits array fails, return NULL
  22. if (array->digits == NULL)
  23. {
  24. // Avoid memory leaks by freeing the array pointer
  25. free(array);
  26. return NULL;
  27. }
  28.  
  29. return array;
  30. }
  31.  
  32. // Function to add two big integers
  33. Integer40 *big40Add(Integer40 *p, Integer40 *q)
  34. {
  35. // Variable declarations
  36. Integer40 *result;
  37. int i, int1, int2, sum, carry = 0;
  38.  
  39. // If any NULL pointers are passed in, return NULL
  40. if (p == NULL || q == NULL)
  41. return NULL;
  42.  
  43. // Create the array to hold a big integer by calling our create function
  44. result = createIntArray(MAX40);
  45.  
  46. // Take care of the cases when create function returns NULL
  47. if(result == NULL)
  48. return NULL;
  49.  
  50. // Add both integers and store it in the digits array
  51. for(i = 0; i < MAX40; i++)
  52. {
  53. // If i is less than 40, make int1/int2 equal to the ith digit of the array, else make it 0
  54. int1 = (i < MAX40) ? p->digits[i] : 0;
  55. int2 = (i < MAX40) ? q->digits[i] : 0;
  56.  
  57. // Add both integer arrays, making sure we carry if the addition results in a double-digit number
  58. sum = int1 + int2 + carry;
  59. carry = sum / 10;
  60.  
  61. // Store the added integers in the digits array, ignoring the last carry
  62. result->digits[i] = sum % 10;
  63. }
  64.  
  65. return result;
  66. }
  67.  
  68. // Function to free all memory / destroy all structs created in this program
  69. Integer40 *big40Destroyer(Integer40 *p)
  70. {
  71. // Make sure that the passed-in struct is not NULL
  72. if(p == NULL)
  73. return NULL;
  74.  
  75. free(p->digits);
  76. free(p);
  77.  
  78. return NULL;
  79. }
  80.  
  81. // Convert a number from string format to Integer40 format.
  82. // The string passed in will only contain ASCII digits '0' to '9' and letters 'A' thru 'F'
  83. Integer40 *parseString(char *str)
  84. {
  85. // Variable declaration
  86. Integer40 *array;
  87. int i, length;
  88.  
  89. // Create our array
  90. array = createIntArray(MAX40 + 1);
  91. if(array == NULL)
  92. return NULL;
  93.  
  94. // Make sure to take care of special cases (NULL and empty string)
  95. if(str == NULL)
  96. return NULL;
  97. else if(str[0] == '\0')
  98. {
  99. fprintf(STDERR, "The string is empty.\n");
  100.  
  101. // Make the entire array 0
  102. for(i = 0; i < 40; i++)
  103. array->digits[i] = 0;
  104.  
  105. return array;
  106. }
  107.  
  108. // Figure out how long the passed-in string is, cast it as an integer
  109. length = (int)strlen(str);
  110.  
  111. // Adjust passed in string: if less than 40 chars, fill with leading zeroes
  112. // Else concatenate the string to just 40 chars
  113. if(length < MAX40)
  114. {
  115. fprintf(STDERR, "This number has less than 40 digits.\n");
  116.  
  117. // Populate the rest of the array with 0's
  118. // Remember, the digits array is length 40
  119. for(i = length - 1; i < MAX40; i++)
  120. array->digits[i] = 0;
  121. }
  122. else if(length > MAX40)
  123. {
  124. length = 40;
  125.  
  126. //array->digits[40] = '\0';
  127. }
  128.  
  129. // Converts ASCII digit characters to integers
  130. for(i = 0; i < MAX40; i++)
  131. {
  132. // 40 - 1 - i
  133. array->digits[MAX40 - 1 - i] = str[i] - '0';
  134. }
  135.  
  136. return array;
  137. }
  138.  
  139. // Returns a pointer to an Integer40 array of 40 digits.
  140. // If the input variable seed is set, the rand. num. generator will be seeded, otherwise not.
  141. // Regardless, the 40 digits will be initialized in 8 unique groups of 4 random digits.
  142. // Returns NULL if there is an error during creation or initialization of the hwConfigVariable.
  143. Integer40 *loadHWConfigVariable(int seed)
  144. {
  145. // Variable declarations
  146. Integer40 *array;
  147. int i, j, k;
  148. int group[5]; // Make 8 groups of 5 numbers that repeat
  149.  
  150. // This is a char array (a string), thus need to make space for the null terminator
  151. array = createIntArray(MAX40 + 1);
  152.  
  153. k = 0;
  154.  
  155. // If seed = 0, rand will be seeded with 0
  156. if(seed == 0)
  157. {
  158. // Populate group array with random numbers, up to hexadecimal F, backwards
  159. for(i = 4; i >= 0; i--)
  160. group[i] = rand() % 16;
  161.  
  162. // Populate digits array with the random numbers stored in group array, and repeat them
  163. for(j = 39; j >= 0; j--)
  164. {
  165. array->digits[j] = group[k];
  166. k++;
  167.  
  168. if(k > 4)
  169. k = 0;
  170. }
  171.  
  172. return array;
  173. }
  174.  
  175. // Produce more random numbers by seeing rand with time(NULL)
  176. // This produces different values each time the program is ran
  177. else
  178. {
  179. srand(time(NULL));
  180.  
  181. for(i = 39; i >= 0; i--)
  182. array->digits[i] = rand() % 16;
  183.  
  184. return array;
  185. }
  186. }
  187.  
  188. // Reads a string from a file and converts it to Integer40 format
  189. Integer40 *loadCryptoVariable(char *cryptoVariableFilename)
  190. {
  191. // Variable declarations
  192. FILE *ifp;
  193. char *str;
  194. Integer40 *array;
  195.  
  196. array = createIntArray(MAX40 + 1);
  197. str = malloc(sizeof(char) * 41);
  198.  
  199. // Open file for reading
  200. ifp = fopen(cryptoVariableFilename, "r");
  201.  
  202. if(ifp == NULL)
  203. {
  204. fprintf(STDERR, "Error: File \"%s\" not found.\n", cryptoVariableFilename);
  205.  
  206. return NULL;
  207. }
  208.  
  209. // Read file
  210. while((fgets(str, 50, ifp)) != NULL)
  211. {
  212. array = parseString(str);
  213. }
  214.  
  215. free(str);
  216.  
  217. fclose(ifp);
  218.  
  219. return array;
  220. }
  221.  
  222. //
  223. Integer40 *fibBig40(int n, Integer40 *first, Integer40 *second)
  224. {
  225. // Variable declarations
  226. Integer40 *result;
  227. int i;
  228.  
  229. result = createIntArray(MAX40);
  230.  
  231. // Base cases
  232. if(n == 1)
  233. return first;
  234.  
  235. if(n == 2)
  236. return second;
  237.  
  238. for(i = 2; i <= n; i++)
  239. {
  240. result = big40Add(first, second);
  241. first = second;
  242. second = result;
  243. }
  244.  
  245. return result;
  246.  
  247. }
  248.  
  249. // Difficulty rating
  250. void big40Rating(void)
  251. {
  252. int NID = 3674869;
  253. float diff = 3.5;
  254. int duration = 36;
  255.  
  256. fprintf(STDERR, "NID: %d; Difficulty Rating: %f; Duration: %d", NID, diff, duration);
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement