试图将阶乘部分转换为另一个功能

时间:2021-08-06 16:04:40

I have to begin my thanking you guys for the help. I am trying to turn the factorial part of the code into another function and was wondering if I needed to add everything within the

我必须开始感谢你们的帮助。我试图将代码的阶乘部分转换为另一个函数,并且想知道我是否需要在其中添加所有内容

#include <stdio.h>
#include <stdlib.h>


int main()
{
    int num;
    int indx;
    int arrayIndx;
    int accumulator;
    int fact;
    int individualDigit[50];
    int length;

for(indx = 99999; indx > 0; indx--)
{

    num = indx;
    for (length = 0; num > 0; length++)
    {
        individualDigit[length] = num % 10;
        num /= 10;
        }

        accumulator = 0;
        for (arrayIndx = 0; arrayIndx < length; arrayIndx++)
        {
            fact = 1;
            while(individualDigit[arrayIndx] > 0)
            {
                fact*= individualDigit[arrayIndx];
                individualDigit[arrayIndx]--;
            }
            accumulator += fact;
        }

        if(accumulator == indx)
        {
            printf("%d ", accumulator);
        }


    }
    return 0;
}   

1 个解决方案

#1


2  

You program is badly designed. It is not indented, you are using variable names index, indx and idex which is confusing for the reader and would lead to nightmares for long term maintenance. Also the factorial computation would deserve to be in a function for better modularity.

你的程序设计很糟糕。它不是缩进的,你使用的是变量名index,indx和idex,这对读者来说很困惑,并且会导致长期维护的噩梦。因此,因子计算也应该具有更好的模块性。

But apart from that, your program does what you ask, correctly computes factorials and adds them in the accumulator variable. The only problem is that you never print that accumulator except for the last 2 cases (2 and 1) where n = n!.

但除此之外,您的程序会按照您的要求执行,正确计算阶乘并将它们添加到累加器变量中。唯一的问题是你永远不会打印那个累加器,除了最后2例(2和1),其中n = n!。

Simply replace :

简单地替换:

if (accumulator == indx)
{
printf("\n%d\n", indx);
}

with

printf("\n%d\n", accumulator);

and you will see your results.

你会看到你的结果。

If you want to store the sum of factorials in an array, you just have to declare int sumOfFact[26] = {0}; just before int individualDigit[50]; to define the array and initialize sumOfFact[0] to 1, and then add sumOfFact[indx] = accumulator; just before printing the accumulator.

如果要将阶乘的和存储在数组中,则只需声明int sumOfFact [26] = {0};就在int IndividualDigit之前[50];定义数组并将sumOfFact [0]初始化为1,然后添加sumOfFact [indx] = accumulator;就在打印蓄电池之前。


To put the factorial part in a function, it is quite simple. First declare it above your main:

要将阶乘部分放在函数中,这很简单。首先将它声明在你的主要上方:

int ffact(int n);

the define it anywhere in your code (eventually in another compilation unit - a .c file - if you want)

在代码中的任何地方定义它(最终在另一个编译单元中 - 一个.c文件 - 如果你想要的话)

inf ffact(int n) {
    fact = 1;
    while (n > 1) {
        fact *= n--;
        /* if (fact < 0) { fprintf(stderr, "Overflow in ffact(%d)\n", n); return 0; } */
    }
    return fact
}

I commented out the test for overflow, because I assume you use at least 32 bits int and fact(9) will not overflow (but fact(13) would ...)

我注释掉溢出的测试,因为我假设你使用至少32位int并且fact(9)不会溢出(但事实(13)会...)

The loop computing the sum of factorials becomes:

计算阶乘总和的循环变为:

accumulator = 0;
for (arrayIndx = 0; arrayIndx < length; arrayIndx++)
{
    accumulator += ffact(individualDigit[arrayIndx]);
}
printf("\n%d\n", accumulator);

Advantages for that modularity: it is simpler to separately test the code for ffact. So when things go wrong, you have not to crawl among one simple piece of code of more than 40 lines (not counting the absent but necessaries comments). And the code no longers clutters the individualDigit array.

该模块化的优点:单独测试ffact的代码更简单。因此,当出现问题时,您不必在一条超过40行的简单代码中进行爬行(不包括缺少但必需的注释)。代码没有longers使individualDigit数组混乱。

#1


2  

You program is badly designed. It is not indented, you are using variable names index, indx and idex which is confusing for the reader and would lead to nightmares for long term maintenance. Also the factorial computation would deserve to be in a function for better modularity.

你的程序设计很糟糕。它不是缩进的,你使用的是变量名index,indx和idex,这对读者来说很困惑,并且会导致长期维护的噩梦。因此,因子计算也应该具有更好的模块性。

But apart from that, your program does what you ask, correctly computes factorials and adds them in the accumulator variable. The only problem is that you never print that accumulator except for the last 2 cases (2 and 1) where n = n!.

但除此之外,您的程序会按照您的要求执行,正确计算阶乘并将它们添加到累加器变量中。唯一的问题是你永远不会打印那个累加器,除了最后2例(2和1),其中n = n!。

Simply replace :

简单地替换:

if (accumulator == indx)
{
printf("\n%d\n", indx);
}

with

printf("\n%d\n", accumulator);

and you will see your results.

你会看到你的结果。

If you want to store the sum of factorials in an array, you just have to declare int sumOfFact[26] = {0}; just before int individualDigit[50]; to define the array and initialize sumOfFact[0] to 1, and then add sumOfFact[indx] = accumulator; just before printing the accumulator.

如果要将阶乘的和存储在数组中,则只需声明int sumOfFact [26] = {0};就在int IndividualDigit之前[50];定义数组并将sumOfFact [0]初始化为1,然后添加sumOfFact [indx] = accumulator;就在打印蓄电池之前。


To put the factorial part in a function, it is quite simple. First declare it above your main:

要将阶乘部分放在函数中,这很简单。首先将它声明在你的主要上方:

int ffact(int n);

the define it anywhere in your code (eventually in another compilation unit - a .c file - if you want)

在代码中的任何地方定义它(最终在另一个编译单元中 - 一个.c文件 - 如果你想要的话)

inf ffact(int n) {
    fact = 1;
    while (n > 1) {
        fact *= n--;
        /* if (fact < 0) { fprintf(stderr, "Overflow in ffact(%d)\n", n); return 0; } */
    }
    return fact
}

I commented out the test for overflow, because I assume you use at least 32 bits int and fact(9) will not overflow (but fact(13) would ...)

我注释掉溢出的测试,因为我假设你使用至少32位int并且fact(9)不会溢出(但事实(13)会...)

The loop computing the sum of factorials becomes:

计算阶乘总和的循环变为:

accumulator = 0;
for (arrayIndx = 0; arrayIndx < length; arrayIndx++)
{
    accumulator += ffact(individualDigit[arrayIndx]);
}
printf("\n%d\n", accumulator);

Advantages for that modularity: it is simpler to separately test the code for ffact. So when things go wrong, you have not to crawl among one simple piece of code of more than 40 lines (not counting the absent but necessaries comments). And the code no longers clutters the individualDigit array.

该模块化的优点:单独测试ffact的代码更简单。因此,当出现问题时,您不必在一条超过40行的简单代码中进行爬行(不包括缺少但必需的注释)。代码没有longers使individualDigit数组混乱。