Cześć,
Rozpocząłem naukę programowania z Cs50. Udało mi się zrobić zadania z pset1 i chciałbym prosić o mały code review. Nie ma dużo kodu, ale już od początku chciałbym wiedzieć gdzie popełniam błędy i je niwelować. Pozdrawiam!
Rysuje drzewko - ala przeszkoda w Mario
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do
{
height = get_int("Height: ");
}
while (height < 1 || height > 8);
for(int i = 0; i < height; i++) // Creates "height" number of verses
{
for(int a = 1; a <= height; a++) //Creates first part of columns
{
if (a >= height - i)
{
printf("#");
}
else
{
printf(" ");
}
}
printf(" "); // Two spaces dividing columns
for(int b = height; b >= 1; b--) //Creates second part of columns
{
if (b >= height - i)
{
printf("#");
}
}
printf("\n");
}
}
Sprawdza poprawność numeru karty kredytowej i wypisuje jej typ:
/* Asks user for a Credit card number, validaes if the number is correct according to
Luhn's algorithm, and prints if this is a American Express card, MasterCard or VISA */
#include <stdio.h>
#include <cs50.h>
int validate(long number);
long get_number(void);
void exist(long number);
int valid = 0;
int main(void)
{
long number = get_number();
exist(number);
}
long get_number(void)
{
// Get credit card number from user
long number = get_long("Number: ");
return number;
}
void exist(long number)
// Checks the type of a card and calls validate function
{
if((number >= 340000000000000 && number < 350000000000000) || (number >= 370000000000000 && number < 380000000000000))
{
string type = "AMEX\n";
if (validate(number))
{
printf("%s",type);
}
else
{
printf("INVALID\n");
}
}
else if (number >= 5100000000000000 && number < 5600000000000000)
{
string type = "MASTERCARD\n";
if (validate(number))
{
printf("%s",type);
}
else
{
printf("INVALID\n");
}
}
else if ((number >= 4000000000000 && number < 5000000000000) || (number >= 4000000000000000 && number < 5000000000000000))
{
string type = "VISA\n";
if (validate(number))
{
printf("%s",type);
}
else
{
printf("INVALID\n");
}
}
else
{
string type = "INVALID\n";
printf("%s",type);
}
}
int validate(long number)
// Validates if the credit card number is correct according to Luhn's algorithm
{
int sum = 0;
int sum_2 =0;
for (long divide = 10; divide <= number * 10; divide *= 100)
// Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.
{
long num_1 = number / divide;
int other = num_1 % 10; // Geting the digit
int other_x2 = other * 2; // Digit * 2
sum = sum + other_x2 / 10 + other_x2 % 10; //Sum of digits in number
}
for (long divide = 1; divide <= number * 10; divide *= 100)
// Add the sum to the sum of the digits that weren’t multiplied by 2.
{
long num_2 = number / divide;
int otherNotDivided = num_2 % 10;
sum_2 += otherNotDivided;
}
int validation_sum = sum + sum_2;
if (validation_sum % 10 == 0)
{
valid = 1;
}
return valid;
}