/*This is my source code for the hacker edition of pset1 in Harvard's CS50. I'm getting a few errors off of check50 on my mario program, but as far as I can tell it meets the specifications set forth in the assignment, and since it can't be turned in anyway I'm just going to let the problems go.
===================================================================
credit.c source code:
-------------------------------------------------------------------*/
#include <cs50.h>
#include <stdio.h>
#include <math.h>
//used to get the length of the credit card number entered
int CCLength(long long int ccNum)
{
if(ccNum < 999999999999)
return 0;
else if(ccNum <= 9999999999999)
return 13;
else if(ccNum <= 99999999999999)
return 14;
else if(ccNum <= 999999999999999)
return 15;
else if(ccNum <= 9999999999999999)
return 16;
else
return 0;
}
int main(void)
{
printf("Please enter a credit card number.\n");
long long int cNumber = GetDouble();
//printf("You entered %lli\n", cNumber);
//get the length of the credit card number
int cLength = CCLength(cNumber);
//if the number is too short or too long then it is invalid
if(cLength == 0)
{
//printf("This number is not the right length to be a credit card number.\n");
printf("INVALID\n");
return 0;
}
int total1 = 0;
int total2 = 0;
int ccType = 0;
for(int count=0; count < cLength; count++)
{
//seperate the digit we are going to be working with
long long int mod = pow(10, count+1);
long long int divide = pow(10, count);
int digit = (cNumber % mod) / divide;
//if this is an odd digit then add it to the total
if(count%2 == 0)
total1 += digit;
//if this is an even digit then multiply by two and add each digit of the result to the total
else
{
int oddDigits = (digit * 2);
//if the result has 1 digit then add it to the total
if(oddDigits < 10)
total2 += oddDigits;
//if the result has 2 digits then add each digit to the total
else
{
oddDigits = (oddDigits % 10) + 1;
total2 += oddDigits;
}
}
//if we are on the last 2 digits then put them in ccType to identify the kind of card this is.
if(cLength - count == 2)
ccType += digit;
else if(cLength - count == 1)
ccType += (digit * 10);
//printf("Mod = %lli, Divide = %lli, Result = %lli, Total1 = %i, Total2 = %i\n", mod, divide, ((cNumber % mod) / divide), total1, total2 );
}
int finalTotal = total1 + total2;
//printf("The total is %i.\n", finalTotal);
if((finalTotal % 10) == 0)
{
//printf("This is a valid credit card number.\n");
if((ccType == 34) || (ccType == 37))
printf("AMEX\n");
else if((ccType == 51) || (ccType == 52) || (ccType == 53) || (ccType == 54) || (ccType == 55))
printf("MASTERCARD\n");
else if(ccType / 10 == 4) //because we only need the first digit to ID a visa card
printf("VISA\n");
else
printf("INVALID\n");
}
else
printf("INVALID\n");
}
/*===================================================================
Running credit:
-------------------------------------------------------------------
jharvard@appliance (~/Dropbox/CS50/hacker1/credit): ./credit
Please enter a credit card number.
4012888888881881
VISA
===================================================================
Checking credit:
-------------------------------------------------------------------
jharvard@appliance (~/Dropbox/CS50/hacker1/credit): check50 2014/x/hacker1/credit credit.c
:) credit.c exists
:) credit.c compiles
:) identifies 378282246310005 as AMEX
:) identifies 371449635398431 as AMEX
:) identifies 5555555555554444 as MASTERCARD
:) identifies 5105105105105100 as MASTERCARD
:) identifies 4111111111111111 as VISA
:) identifies 4012888888881881 as VISA
:) identifies 1234567890 as INVALID
:) rejects a non-numeric input of "foo"
:) rejects a non-numeric input of ""
https://sandbox.cs50.net/checks/0dd1e381e3254b7c8dc9060f359a401b
===================================================================
mario.c source code:
-------------------------------------------------------------------*/
#include <cs50.h>
#include <stdio.h>
int main(void)
{
printf("Please enter a number between 1 and 23: ");
int height = GetInt();
if((height > 23) || (height < 1))
{
//printf("Invalid input\n");
return 0;
}
int x = 0;
int y = 0;
for(int count = 0; count < height; count++)
{
//makes the left side of the pyramid
while(y < height)
{
int number = height - (count + 1);
if(y >= number)
printf("#");
else
printf(" ");
y++;
}
y = 0;
//makes the spaces in the middle
printf(" ");
//makes the right side of the pyramid
while(x < height)
{
if(x > count)
printf(" ");
else
printf("#");
x++;
}
x = 0;
printf("\n");
}
}
/*===================================================================
Running it:
-------------------------------------------------------------------
jharvard@appliance (~/Dropbox/CS50/hacker1/mario): ./mario
Please enter a number between 1 and 23: 5
# #
## ##
### ###
#### ####
##### #####
===================================================================
Checking it:
-------------------------------------------------------------------
jharvard@appliance (~/Dropbox/CS50/hacker1/mario): check50 2014/x/hacker1/mario mario.c
:) mario.c exists
:) mario.c compiles
:( rejects a height of -1
\ expected output, not an exit code of 0
:) handles a height of 0 correctly
:) handles a height of 1 correctly
:( handles a height of 2 correctly
\ expected output, but not " # # \n## ##\n"
:( handles a height of 23 correctly
\ expected output, but not " # # ..."
:( rejects a height of 24
\ expected output, not an exit code of 0
:) rejects a non-numeric height of "foo"
:) rejects a non-numeric height of ""
https://sandbox.cs50.net/checks/d34c1b1bba124ebcb47d5406850cedc3