Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- // declare our global vars that will be shared through out the program's functions
- int *digits;
- long cardNum;
- long firstDigit; // yeah this should be an int, but im lazy to convert, TODO adress later
- int secondDigit;
- int numberOfDigits;
- int *evenIndexDigits;
- int *oddIndexDigits;
- int evenIndexSum = 0;
- int oddIndexSum = 0;
- void reverseArray(int arrStart, int arrEnd)
- {
- int moverStorage;
- while (arrStart < arrEnd)
- {
- moverStorage = digits[arrStart];
- digits[arrStart] = digits[arrEnd];
- digits[arrEnd] = moverStorage;
- arrStart++;
- arrEnd--;
- }
- }
- // gets input
- void getInput(void)
- {
- // checks input and passes it through a validator, if is valid, return value
- cardNum = get_long("Enter a credit card number: "); // this function is a myth, pretend its scanf
- numberOfDigits = floor(log10(cardNum));
- printf("%d\n", numberOfDigits);
- firstDigit = cardNum;
- while (firstDigit >= 10)
- {
- firstDigit /= 10;
- }
- if (numberOfDigits < 13 || numberOfDigits > 16)
- {
- printf("INVALID check number size!\n");
- exit(0);
- }
- digits = calloc(numberOfDigits, sizeof(int));
- for (int i = 0; i <= numberOfDigits; i++)
- {
- digits[i] = cardNum % 10;
- cardNum /= 10;
- }
- reverseArray(0, numberOfDigits);
- secondDigit = digits[1];
- }
- void parse(void)
- {
- // the big gay region
- numberOfDigits += 1;
- if (numberOfDigits % 2 == 0)
- {
- evenIndexDigits = calloc(8, sizeof(int));
- oddIndexDigits = calloc(8, sizeof(int));
- int evenCounter = 0;
- int oddCounter = 0;
- printf("Card is 16 digits long starting from 1\n");
- for (int i = 0; i < numberOfDigits; i++)
- {
- if (i % 2 == 0)
- {
- evenIndexDigits[evenCounter] = digits[i] * 2;
- evenCounter++;
- //printf("%d\n", digits[i] * 2);
- }
- else
- {
- oddIndexDigits[oddCounter] = digits[i];
- oddCounter++;
- }
- }
- }
- else
- {
- // ignore this, pretend its like whats above in the else block
- printf("Card is 15 digits long starting from 1\n");
- for (int i = 0; i < numberOfDigits; i++)
- {
- if (i % 2 != 0)
- {
- evenIndexSum += digits[i] * 2;
- // printf("%d\n", digits[i] * 2);
- }
- else
- {
- oddIndexSum += digits[i];
- printf("%d\n", digits[i]);
- }
- }
- }
- }
- // this function is a myth
- string checkValidity(void)
- {
- return "i dont exist";
- }
- int main(void)
- {
- getInput();
- //string res = checkValidity();
- parse();
- printf("evenIndex\n");
- for (int i = 0; i < sizeof(evenIndexDigits); i++)
- {
- printf("%d\n", evenIndexDigits[i]);
- }
- printf("oddIndex\n");
- for (int i = 0; i < sizeof(oddIndexDigits); i++)
- {
- printf("%d\n", oddIndexDigits[i]);
- }
- printf("\nEvenIndexSum: %d OddIndexedSum: %d\n", evenIndexSum, oddIndexSum);
- free(digits);
- free(evenIndexDigits);
- free(oddIndexDigits);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement