有没有一种方法可以构造我的代码以在多个if语句中相加多个数字? [英] Is there a way to structure my code to add up numbers across multiple if else statements?

查看:61
本文介绍了有没有一种方法可以构造我的代码以在多个if语句中相加多个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,此代码将输入一系列数字的所有乘积并打印出来。我希望能够将这些产品加在一起,但是如果有其他条件的话,我正在努力构建产品。

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);
    }
///////////////////////////////////////////////////////////////////////////////////////////////////



}


推荐答案

这是如何进行操作的提示。

This is a hint on how to proceed.


  1. 排列14个数组 int 存储单个数字

  2. 使用for循环将数字放入数组

  1. Make an array of 14 int to store the individual digits
  2. 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;
} 

请注意,上述算法以相反的顺序给出数字(即 digit [0] 是最低有效位)

Note that the above algorithm gives the digits in reverse order (i.e. digit[0] is the least significant digit)

按照上面的代码将偶数乘以2。按照上述算法添加并打印。

Multiply by two the even digits as per the code above. Add and print as per algorithm above.

您可以使用简单的for循环将所有数字相乘。

You can multiply all digits together using a simple for loop.

这篇关于有没有一种方法可以构造我的代码以在多个if语句中相加多个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆