问题
Right now this code takes all the products of an inputted series of digits and prints them. I want to be able to add these products together but I am struggling with how to structure it, given the if else conditions.
It needs to be able to output the total products of every 2nd digit starting from the second last and then add those products.
Any advice on how to structure this much appreciated.
// Define libraries
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
// Declare Variables
long int number;
// Defining the function and storing it in a variable
// Creating do while loop to ensure the digit amount is valid
do
{
number = get_long("Credit Card Number: ");
}
while(number < 0);
// Creating variable to store every 2nd digit of credit card number
int second_last = (number % 100)/ 10;
int fourth_last = (number % 10000)/ 1000;
int sixth_last = (number % 1000000)/ 100000;
int eighth_last = (number % 100000000)/ 10000000;
int tenth_last = (number % 10000000000)/ 1000000000;
int twelfth_last = (number % 1000000000000)/ 100000000000;
int fourteenth_last = (number % 100000000000000)/ 10000000000000;
int sixteenth_last = (number % 10000000000000000)/ 1000000000000000;
// Multiplying every 2nd digit by 2 starting from the last digit
// Multiplying every 2nd digit by 2 starting from the last digit
int second_times_two = (second_last * 2);
//finding the product of every 2nd last digit
if (second_times_two >= 10)
{
int product_second_digit = (second_times_two % 10);
int remaining_digits_second = second_times_two - 10;
int prod_second_last = (product_second_digit + 1);
printf("%i\n", prod_second_last);
}
else
{
printf("%i\n", second_times_two);
}
int fourth_times_two = (fourth_last * 2);
//finding the product of every 4th last digit
if (fourth_times_two >= 10)
{
int product_fourth_digit = (fourth_times_two % 10);
int remaining_digits_fourth = fourth_times_two - 10;
int prod_fourth_last = (product_fourth_digit + 1);
printf("%i\n", prod_fourth_last);
}
else
{
printf("%i\n", fourth_times_two);
}
int sixth_times_two = (sixth_last * 2);
//finding the product of every 6th last digit
if (sixth_times_two >= 10)
{
int product_sixth_digit = (sixth_times_two % 10);
int remaining_digits_sixth = sixth_times_two - 10;
int prod_sixth_last = (product_sixth_digit + 1);
printf("%i\n", prod_sixth_last);
}
else
{
printf("%i\n", sixth_times_two);
}
int eighth_times_two = (eighth_last * 2);
//finding the product of every eight last digit
if (eighth_times_two >= 10)
{
int product_eighth_digit = (eighth_times_two % 10);
int remaining_digits_eighth = eighth_times_two - 10;
int prod_eighth_last = (product_eighth_digit + 1);
printf("%i\n", prod_eighth_last);
}
else
{
printf("%i\n", eighth_times_two);
}
int tenth_times_two = (tenth_last * 2);
//finding the product of every tenth last digit
if (tenth_times_two >= 10)
{
int product_tenth_digit = (tenth_times_two % 10);
int remaining_digits_tenth = tenth_times_two - 10;
int prod_tenth_last = (product_tenth_digit + 1);
printf("%i\n", prod_tenth_last);
}
else
{
printf("%i\n", tenth_times_two);
}
int twelfth_times_two = (twelfth_last * 2);
//finding the product of every twelfth last digit
if (twelfth_times_two >= 10)
{
int product_twelfth_digit = (twelfth_times_two % 10);
int remaining_digits_twelfth = twelfth_times_two - 10;
int prod_twelfth_last = (product_twelfth_digit + 1);
printf("%i\n", prod_twelfth_last);
}
else
{
printf("%i\n", twelfth_times_two);
}
int fourteenth_times_two = (fourteenth_last * 2);
//finding the product of every fourtenth last digit
if (fourteenth_times_two >= 10)
{
int product_fourteenth_digit = (fourteenth_times_two % 10);
int remaining_digits_fourteenth = fourteenth_times_two - 10;
int prod_fourteenth_last = (product_fourteenth_digit + 1);
printf("%i\n", prod_fourteenth_last);
}
else
{
printf("%i\n", fourteenth_times_two);
}
int sixteenth_times_two = (sixteenth_last * 2);
//finding the product of every sixteenth last digit
if (sixteenth_times_two >= 10)
{
int product_sixteenth_digit = (sixteenth_times_two % 10);
int remaining_digits_sixteenth = sixteenth_times_two - 10;
int prod_sixteenth_last = (product_sixteenth_digit + 1);
printf("%i\n", prod_sixteenth_last);
}
else
{
printf("%i\n", sixteenth_times_two);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
}
回答1:
This is a hint on how to proceed.
- Make an array of 14
int
to store the individual digits Get the digits into the array using a for loop
long int curr = number; int i =0; while (curr >0) { digits[i++] = curr%10; curr/=10; }
Note that the above algorithm gives the digits in reverse order (i.e.
digit[0]
is the least significant digit)Multiply by two the even digits as per the code above. Add and print as per algorithm above.
You can multiply all digits together using a simple for loop.
来源:https://stackoverflow.com/questions/60810680/is-there-a-way-to-structure-my-code-to-add-up-numbers-across-multiple-if-else-st