Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
分隔整数的位数(C++)_C++ - Fatal编程技术网

分隔整数的位数(C++)

分隔整数的位数(C++),c++,C++,我需要写一个程序,把一个整数作为输入,并输出它的数字间隔。该算法工作正常,但按相反顺序打印数字。为了获得原始顺序,我使用了for循环。问题是它什么也不打印 #include <iostream> using namespace std; int countDigit(int n) { int count = 0; while (n != 0) { n = n / 10; ++count; } return count;

我需要写一个程序,把一个整数作为输入,并输出它的数字间隔。该算法工作正常,但按相反顺序打印数字。为了获得原始顺序,我使用了for循环。问题是它什么也不打印

#include <iostream>
using namespace std;
int countDigit(int n)
{
    int count = 0;
    while (n != 0) {
        n = n / 10;
        ++count;
    }
    return count;
}
int main()
{
    int Num ;
    int sum = 0;
    int arr[100];

    cout <<"Enter an integer: " ;
    cin >> Num ;
    int c = countDigit(Num) ;
    for (int i = c; i > 0; i--)
    {   
        arr[i] = (Num % 10);
        Num = (Num / 10);
        sum += arr[i];
    }

    cout << " Your digits:";
 for (int x = 1 ; x <= c; x++)
    {
        cout << arr[x] << " " ;
        cout << sum ;
    }

    return 0;
}
我的问题在这里

for (int x = 1 ; x <= c; x++)
    {
        cout << arr[x] << " " ;
        cout << sum ;
    }
它什么也不印。有什么问题吗

使用的输入是1234 预期输出为1 2 3 4

对于启动器,功能countDigit错误

int countDigit(int n)
{
    int count = 0;
    while (n != 0) {
        n = n / 10;
        ++count;
    }
    return count;
}
对于具有一个数字的有效数字0,它返回0

该函数可以按以下方式显示

size_t countDigit( int n, int base = 10 )
{
    size_t count = 0;

    do
    {
        ++count;
    } while ( n /= base );

    return count;
}
在这个循环中

for (int i = countDigit(Num); i > 0; i--)
{   
    arr[i] = (Num % 10);
    Num = (Num / 10);
    sum += arr[i];
}
变量Num已更改。所以在下一个循环中

for (int x = 1 ; x <= countDigit(Num) ; x++)
{
    cout << arr[x] << " " ;
    cout << sum ;
}
它的输出可能看起来像

Enter an integer: -123456789
Your digits: 1 2 3 4 5 6 7 8 9 
The sum of digits is 45
Enter an integer: -123456789
1 2 3 4 5 6 7 8 9 
The sum of digits is 45
另一种方法是编写一个递归函数,输出数字并返回数字之和

#include <iostream>

unsigned int recursive_sum( long long int n, long long int base = 10, std::ostream &os = std::cout )
{
    long long int digit = n % base;

    if ( digit < 0 ) digit = -digit;

    unsigned int sum = digit + ( ( n /= base ) == 0 ? 0 : recursive_sum( n ) );

    os << digit << ' ';

    return sum;
}

int main()
{
    int num;

    std::cout << "Enter an integer: ";
    std::cin >> num;

    unsigned int sum = recursive_sum( num );
    std::cout << '\n';

    std::cout << "The sum of digits is " << sum << '\n';

    return 0;
}

这就给出了预期的输出。我将countDigitNum分配给c,并将总和写入循环外部

int main()
{
    int Num ;
    int sum = 0;
    int arr[100];

    cout <<"Enter an integer: " ;
    cin >> Num ;
    int c = countDigit(Num); //edit 1

    // extracts the digits
    for (int i = c; i > 0; i--)
    {   
        arr[i] = (Num % 10);
        Num = (Num / 10);
        sum += arr[i];
    }

    cout << "Your digits with spaces: ";
    // reverses the order
    for (int x = 1 ; x <= c ; x++)
    {
        cout << arr[x] << " " ;

    }
    cout << endl << "The sum of the digits is: " << sum ; // edit 2
    return 0;
}

以下是一个简短的版本:

#include <concepts>
#include <cstdio>
#include <string>

template <std::integral T>
void printDigits(T const i) {
  for (auto const ch : std::to_string(i)) {
    std::printf("%c ", ch);
  }
  std::fflush(stdout);
}

这里有一个使用字符串的例子

包括 包括 int main { std::字符串输入; 标准:尺寸和位置;
STD::CUT请提供输入,以及预期的输出。C++是基于0的。请考虑你的代码。然后考虑一下Num在前一个循环之后的值。你真的想每次循环循环调用CurtDigy吗?为什么不把结果保存到变量中,而使用那个变量?最好是在第一个循环之前。考虑到它的作用。嗯,请不要修复代码中的错误。修复的代码应该转到“答案”中。修改后的代码是否仍具有相同的行为?我的看法是,如果将输入作为字符串,并验证整个字符串是数字,这将容易得多。提示:这是一个STL函数调用,如果选中。@down投票人:有什么建议如何改进答案吗?@Stephanchner在发布此答案时,它指的是问题中不再出现的cdoe。我猜dv是由于这个原因,可能从1开始计数更简单,因为不可能有一个0位数的数字:
int main()
{
    int Num ;
    int sum = 0;
    int arr[100];

    cout <<"Enter an integer: " ;
    cin >> Num ;
    int c = countDigit(Num); //edit 1

    // extracts the digits
    for (int i = c; i > 0; i--)
    {   
        arr[i] = (Num % 10);
        Num = (Num / 10);
        sum += arr[i];
    }

    cout << "Your digits with spaces: ";
    // reverses the order
    for (int x = 1 ; x <= c ; x++)
    {
        cout << arr[x] << " " ;

    }
    cout << endl << "The sum of the digits is: " << sum ; // edit 2
    return 0;
}
#include <concepts>
#include <cstdio>
#include <string>

template <std::integral T>
void printDigits(T const i) {
  for (auto const ch : std::to_string(i)) {
    std::printf("%c ", ch);
  }
  std::fflush(stdout);
}