如何编写一个将二进制数转换为十进制数的递归函数?

发布于 2024-10-27 04:11:20 字数 1324 浏览 0 评论 0原文

我正在尝试编写将二进制数转换为十进制系统的递归函数。我找不到错误。 任何帮助将不胜感激。

这是我的第一次尝试:

#include <iostream>

int sum = 0;
using namespace std;
int powerofO(int taban, int us)
{
    int result = 1;
    for (int i = 1; i <= us; i++)
            result *= taban;
}

int func(int array[], int length, int weight);

int main()
{
    int *size;
    int digits;
    int binary_array[*size];
    cout << "how many digits ? \n";
    cin >> digits;
    *size = digits;
    cout << "Enter your " << digits <<
        " digits binary number using with a space between"
        " each number \n";
    for (int i = 0; i < digits; i++)
            cin >> binary_array[i];
    cout << func(binary_array, digits, -1) << endl;
    return 0;
}

int func(int array[], int length, int weight)
{
    if (length == 0) {
            sum = sum + array[0];
            return sum;
    }
    func(array, length - 1, weight + 1);
    sum = sum + array[length] * powerofO(2, weight);
    return sum;
}

我尝试将我的功能重新排列为以下内容,但它仍然无法正常工作。

int func( int array[],int length ,int weight){
length--;
weight++;
if(length == 0){
sum = sum + array[0] * powerofO(2,weight);
return sum;}   
sum = sum + array[length] * powerofO(2,weight);
func(array,length,weight);}

I am trying to write recursive function which converts a binary numbers into decimal system. I couldn't find the bug.
Any help will be appreciated deeply.

Here is my first attempt:

#include <iostream>

int sum = 0;
using namespace std;
int powerofO(int taban, int us)
{
    int result = 1;
    for (int i = 1; i <= us; i++)
            result *= taban;
}

int func(int array[], int length, int weight);

int main()
{
    int *size;
    int digits;
    int binary_array[*size];
    cout << "how many digits ? \n";
    cin >> digits;
    *size = digits;
    cout << "Enter your " << digits <<
        " digits binary number using with a space between"
        " each number \n";
    for (int i = 0; i < digits; i++)
            cin >> binary_array[i];
    cout << func(binary_array, digits, -1) << endl;
    return 0;
}

int func(int array[], int length, int weight)
{
    if (length == 0) {
            sum = sum + array[0];
            return sum;
    }
    func(array, length - 1, weight + 1);
    sum = sum + array[length] * powerofO(2, weight);
    return sum;
}

And I tried to rearrange my function to the following, but it is still not working.

int func( int array[],int length ,int weight){
length--;
weight++;
if(length == 0){
sum = sum + array[0] * powerofO(2,weight);
return sum;}   
sum = sum + array[length] * powerofO(2,weight);
func(array,length,weight);}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜空下最亮的亮点 2024-11-03 04:11:20

立即分段错误的原因非常接近 main() 函数的顶部:

int main()
{
    int *size;
    int digits;
    int binary_array[*size];

int *size 仅声明了一个指向 int 的指针 ,它实际上并不为该 int 分配内存。稍后在尝试分配数组时使用了 *size,整个事情就崩溃了。

因此,稍微重新安排一下,程序就可以运行了。我认为输出还不正确:)但我不想剥夺你所有的学习机会。

#include <iostream>

int sum = 0;
using namespace std;
int powerofO(int taban, int us)
{
    int result = 1;
    for (int i = 1; i <= us; i++)
            result *= taban;
}

int func(int array[], int length, int weight);

int main()
{
    int digits;
    cout << "how many digits ? \n";
    cin >> digits;
    int binary_array[digits];
    cout << "Enter your " << digits <<
        " digits binary number using with a space between"
        " each number \n";
    for (int i = 0; i < digits; i++)
            cin >> binary_array[i];
    cout << func(binary_array, digits, -1) << endl;
    return 0;
}

int func(int array[], int length, int weight)
{
    if (length == 0) {
            sum = sum + array[0];
            return sum;
    }
    func(array, length - 1, weight + 1);
    sum = sum + array[length] * powerofO(2, weight);
    return sum;
}

The cause of the immediate segmentation fault is very near the top of the main() function:

int main()
{
    int *size;
    int digits;
    int binary_array[*size];

int *size only declares a pointer to an int, it does not actually allocate memory for that int. *size is used a little later when trying to allocate the array, and the whole thing blows up.

So, a little re-arrangement and the program runs. I don't think the output is correct yet :) but I don't want to take away all your learning opportunities.

#include <iostream>

int sum = 0;
using namespace std;
int powerofO(int taban, int us)
{
    int result = 1;
    for (int i = 1; i <= us; i++)
            result *= taban;
}

int func(int array[], int length, int weight);

int main()
{
    int digits;
    cout << "how many digits ? \n";
    cin >> digits;
    int binary_array[digits];
    cout << "Enter your " << digits <<
        " digits binary number using with a space between"
        " each number \n";
    for (int i = 0; i < digits; i++)
            cin >> binary_array[i];
    cout << func(binary_array, digits, -1) << endl;
    return 0;
}

int func(int array[], int length, int weight)
{
    if (length == 0) {
            sum = sum + array[0];
            return sum;
    }
    func(array, length - 1, weight + 1);
    sum = sum + array[length] * powerofO(2, weight);
    return sum;
}
凉墨 2024-11-03 04:11:20

在 func() 中,您在求和之前再次调用 func() 。所以当 length 最终达到 0 时,sum 只包含 array[] 中的最后一位数字。

In func() you are calling func() again before doing the sum. So then when length finally gets to 0, sum only contains the final digit in array[].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文