Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5.  
  6. // declare our global vars that will be shared through out the program's functions
  7. int *digits;
  8. long cardNum;
  9. long firstDigit; // yeah this should be an int, but im lazy to convert, TODO adress later
  10. int secondDigit;
  11. int numberOfDigits;
  12. int *evenIndexDigits;
  13. int *oddIndexDigits;
  14.  
  15. int evenIndexSum = 0;
  16. int oddIndexSum = 0;
  17.  
  18. void reverseArray(int arrStart, int arrEnd)
  19. {
  20.     int moverStorage;
  21.     while (arrStart < arrEnd)
  22.     {
  23.         moverStorage = digits[arrStart];
  24.         digits[arrStart] = digits[arrEnd];
  25.         digits[arrEnd] = moverStorage;
  26.         arrStart++;
  27.         arrEnd--;
  28.     }
  29.    
  30. }
  31.  
  32. // gets input
  33. void getInput(void)
  34. {
  35.     // checks input and passes it through a validator, if is valid, return value
  36.     cardNum = get_long("Enter a credit card number: "); // this function is a myth, pretend its scanf
  37.     numberOfDigits = floor(log10(cardNum));
  38.     printf("%d\n", numberOfDigits);
  39.     firstDigit = cardNum;
  40.     while (firstDigit >= 10)
  41.     {
  42.         firstDigit /= 10;
  43.     }
  44.      if (numberOfDigits < 13 || numberOfDigits > 16)
  45.     {
  46.         printf("INVALID check number size!\n");
  47.         exit(0);
  48.     }
  49.     digits = calloc(numberOfDigits, sizeof(int));
  50.     for (int i = 0; i <= numberOfDigits; i++)
  51.     {
  52.         digits[i] = cardNum % 10;
  53.         cardNum /= 10;
  54.     }
  55.    
  56.     reverseArray(0, numberOfDigits);
  57.     secondDigit = digits[1];
  58. }
  59.  
  60. void parse(void)
  61. {
  62.     // the big gay region
  63.     numberOfDigits += 1;
  64.     if (numberOfDigits % 2 == 0)
  65.     {
  66.         evenIndexDigits = calloc(8, sizeof(int));
  67.         oddIndexDigits = calloc(8, sizeof(int));
  68.  
  69.         int evenCounter = 0;
  70.         int oddCounter = 0;
  71.         printf("Card is 16 digits long starting from 1\n");
  72.         for (int i = 0; i < numberOfDigits; i++)
  73.         {
  74.             if (i % 2 == 0)
  75.             {
  76.                 evenIndexDigits[evenCounter] = digits[i] * 2;
  77.                 evenCounter++;
  78.                 //printf("%d\n", digits[i] * 2);
  79.             }
  80.             else
  81.             {
  82.                 oddIndexDigits[oddCounter] = digits[i];
  83.                 oddCounter++;
  84.             }
  85.         }
  86.     }
  87.     else
  88.     {
  89.         // ignore this, pretend its like whats above in the else block
  90.         printf("Card is 15 digits long starting from 1\n");
  91.         for (int i = 0; i < numberOfDigits; i++)
  92.         {
  93.             if (i % 2 != 0)
  94.             {
  95.                 evenIndexSum += digits[i] * 2;
  96.               //  printf("%d\n", digits[i] * 2);
  97.             }
  98.             else
  99.             {
  100.                 oddIndexSum += digits[i];
  101.                 printf("%d\n", digits[i]);
  102.             }
  103.         }
  104.     }
  105. }
  106.  
  107. // this function is a myth
  108. string checkValidity(void)
  109. {
  110.     return "i dont exist";
  111. }
  112.  
  113.  
  114. int main(void)
  115. {
  116.     getInput();
  117.     //string res = checkValidity();
  118.     parse();
  119.     printf("evenIndex\n");
  120.     for (int i = 0; i < sizeof(evenIndexDigits); i++)
  121.     {
  122.         printf("%d\n", evenIndexDigits[i]);
  123.     }
  124.  
  125.     printf("oddIndex\n");
  126.     for (int i = 0; i < sizeof(oddIndexDigits); i++)
  127.     {
  128.         printf("%d\n", oddIndexDigits[i]);
  129.     }
  130.     printf("\nEvenIndexSum: %d  OddIndexedSum: %d\n", evenIndexSum, oddIndexSum);
  131.  
  132.     free(digits);
  133.     free(evenIndexDigits);
  134.     free(oddIndexDigits);
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement