Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- #include "big40.h"
- // Function to initialize Integer40 struct and create an array to hold a big integer
- Integer40 *createIntArray(int length)
- {
- // Create a variable of type Integer40
- Integer40 *array = malloc(sizeof(Integer40));
- // return NULL if call to malloc fails
- if(array == NULL)
- return NULL;
- // Access digits variable inside struct and malloc space for it as well
- array->digits = malloc(sizeof(int) * length);
- // If malloc call to create digits array fails, return NULL
- if (array->digits == NULL)
- {
- // Avoid memory leaks by freeing the array pointer
- free(array);
- return NULL;
- }
- return array;
- }
- // Function to add two big integers
- Integer40 *big40Add(Integer40 *p, Integer40 *q)
- {
- // Variable declarations
- Integer40 *result;
- int i, int1, int2, sum, carry = 0;
- // If any NULL pointers are passed in, return NULL
- if (p == NULL || q == NULL)
- return NULL;
- // Create the array to hold a big integer by calling our create function
- result = createIntArray(MAX40);
- // Take care of the cases when create function returns NULL
- if(result == NULL)
- return NULL;
- // Add both integers and store it in the digits array
- for(i = 0; i < MAX40; i++)
- {
- // If i is less than 40, make int1/int2 equal to the ith digit of the array, else make it 0
- int1 = (i < MAX40) ? p->digits[i] : 0;
- int2 = (i < MAX40) ? q->digits[i] : 0;
- // Add both integer arrays, making sure we carry if the addition results in a double-digit number
- sum = int1 + int2 + carry;
- carry = sum / 10;
- // Store the added integers in the digits array, ignoring the last carry
- result->digits[i] = sum % 10;
- }
- return result;
- }
- // Function to free all memory / destroy all structs created in this program
- Integer40 *big40Destroyer(Integer40 *p)
- {
- // Make sure that the passed-in struct is not NULL
- if(p == NULL)
- return NULL;
- free(p->digits);
- free(p);
- return NULL;
- }
- // Convert a number from string format to Integer40 format.
- // The string passed in will only contain ASCII digits '0' to '9' and letters 'A' thru 'F'
- Integer40 *parseString(char *str)
- {
- // Variable declaration
- Integer40 *array;
- int i, length;
- // Create our array
- array = createIntArray(MAX40 + 1);
- if(array == NULL)
- return NULL;
- // Make sure to take care of special cases (NULL and empty string)
- if(str == NULL)
- return NULL;
- else if(str[0] == '\0')
- {
- fprintf(STDERR, "The string is empty.\n");
- // Make the entire array 0
- for(i = 0; i < 40; i++)
- array->digits[i] = 0;
- return array;
- }
- // Figure out how long the passed-in string is, cast it as an integer
- length = (int)strlen(str);
- // Adjust passed in string: if less than 40 chars, fill with leading zeroes
- // Else concatenate the string to just 40 chars
- if(length < MAX40)
- {
- fprintf(STDERR, "This number has less than 40 digits.\n");
- // Populate the rest of the array with 0's
- // Remember, the digits array is length 40
- for(i = length - 1; i < MAX40; i++)
- array->digits[i] = 0;
- }
- else if(length > MAX40)
- {
- length = 40;
- //array->digits[40] = '\0';
- }
- // Converts ASCII digit characters to integers
- for(i = 0; i < MAX40; i++)
- {
- // 40 - 1 - i
- array->digits[MAX40 - 1 - i] = str[i] - '0';
- }
- return array;
- }
- // Returns a pointer to an Integer40 array of 40 digits.
- // If the input variable seed is set, the rand. num. generator will be seeded, otherwise not.
- // Regardless, the 40 digits will be initialized in 8 unique groups of 4 random digits.
- // Returns NULL if there is an error during creation or initialization of the hwConfigVariable.
- Integer40 *loadHWConfigVariable(int seed)
- {
- // Variable declarations
- Integer40 *array;
- int i, j, k;
- int group[5]; // Make 8 groups of 5 numbers that repeat
- // This is a char array (a string), thus need to make space for the null terminator
- array = createIntArray(MAX40 + 1);
- k = 0;
- // If seed = 0, rand will be seeded with 0
- if(seed == 0)
- {
- // Populate group array with random numbers, up to hexadecimal F, backwards
- for(i = 4; i >= 0; i--)
- group[i] = rand() % 16;
- // Populate digits array with the random numbers stored in group array, and repeat them
- for(j = 39; j >= 0; j--)
- {
- array->digits[j] = group[k];
- k++;
- if(k > 4)
- k = 0;
- }
- return array;
- }
- // Produce more random numbers by seeing rand with time(NULL)
- // This produces different values each time the program is ran
- else
- {
- srand(time(NULL));
- for(i = 39; i >= 0; i--)
- array->digits[i] = rand() % 16;
- return array;
- }
- }
- // Reads a string from a file and converts it to Integer40 format
- Integer40 *loadCryptoVariable(char *cryptoVariableFilename)
- {
- // Variable declarations
- FILE *ifp;
- char *str;
- Integer40 *array;
- array = createIntArray(MAX40 + 1);
- str = malloc(sizeof(char) * 41);
- // Open file for reading
- ifp = fopen(cryptoVariableFilename, "r");
- if(ifp == NULL)
- {
- fprintf(STDERR, "Error: File \"%s\" not found.\n", cryptoVariableFilename);
- return NULL;
- }
- // Read file
- while((fgets(str, 50, ifp)) != NULL)
- {
- array = parseString(str);
- }
- free(str);
- fclose(ifp);
- return array;
- }
- //
- Integer40 *fibBig40(int n, Integer40 *first, Integer40 *second)
- {
- // Variable declarations
- Integer40 *result;
- int i;
- result = createIntArray(MAX40);
- // Base cases
- if(n == 1)
- return first;
- if(n == 2)
- return second;
- for(i = 2; i <= n; i++)
- {
- result = big40Add(first, second);
- first = second;
- second = result;
- }
- return result;
- }
- // Difficulty rating
- void big40Rating(void)
- {
- int NID = 3674869;
- float diff = 3.5;
- int duration = 36;
- fprintf(STDERR, "NID: %d; Difficulty Rating: %f; Duration: %d", NID, diff, duration);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement