Created
November 8, 2016 01:07
-
-
Save lkrych/b394a30243ff90c965c88b396086e6ee to your computer and use it in GitHub Desktop.
c implementation of credit card problem from HW1 in CS50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <cs50.h> | |
| #include <math.h> | |
| int countDigits(long long num) { // helper method that counts the number of digits in a number | |
| int count = 0; | |
| while (num != 0) { | |
| num /= 10; | |
| ++count; | |
| } | |
| return count; | |
| } | |
| int getFirst(long long card_number) { // helper method that returns the first digit in a number | |
| return card_number / pow(10, countDigits(card_number) - 1); | |
| } | |
| int * getDigits(long long card_number, int card_number_length) { // helper method that returns an array of digits from a number | |
| int *digits = malloc(card_number_length * sizeof(int)); | |
| for (int i = 0; i < card_number_length; i++) { | |
| digits[i] = card_number % 10; | |
| card_number = card_number / 10; | |
| } | |
| return digits; | |
| } | |
| int sumDigits(int *odd, int array_length){ //helper method that adds together the digits of numbers in an array | |
| int sum = 0; | |
| for (int i = 0; i < array_length; i++){ | |
| int mod = odd[i]; | |
| if (mod > 9){ | |
| sum += mod % 10; | |
| sum += 1; | |
| } | |
| else{ | |
| sum += mod; | |
| } | |
| } | |
| return sum; | |
| } | |
| bool checkCredit(long long card_number, int card_number_length) { //helper method that splits a credit card number into the components needed to verify whether it is a valid code | |
| int *digits = getDigits(card_number,card_number_length); | |
| int evenSum = 0; | |
| int odd [(card_number_length + 1)/2]; | |
| int odd_count = 0; | |
| for (int i = 0; i < card_number_length; i++) { //separate digits | |
| if (i == 0){ | |
| evenSum += digits[i]; | |
| } | |
| else if (i % 2 != 0){ | |
| odd[odd_count] = digits[i] * 2; | |
| odd_count += 1; | |
| } | |
| else{ | |
| evenSum += digits[i]; | |
| } | |
| } | |
| int oddDigits = sizeof(odd)/sizeof(int); | |
| int oddSum = sumDigits(odd,oddDigits); | |
| if ((oddSum + evenSum) % 10 == 0) { | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| int main(void) { // main method! | |
| long long card_number = 0; | |
| do { //get card number | |
| printf("Number:"); | |
| card_number = get_long_long(); | |
| } | |
| while(countDigits(card_number) < 13 || countDigits(card_number) > 16 ); // check if number is of valid length | |
| int card_number_length = countDigits(card_number); | |
| bool cardValid = checkCredit(card_number, card_number_length); //check if card is valid | |
| if (cardValid == false) { | |
| printf("INVALID\n"); | |
| } | |
| else if (getFirst(card_number) == 5) { | |
| printf("MASTERCARD\n"); | |
| } | |
| else if (getFirst(card_number) == 4) { | |
| printf("VISA\n"); | |
| } | |
| else if (getFirst(card_number) == 3) { | |
| printf("AMEX\n"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment