如何编写一个将二进制数转换为十进制数的递归函数?
我正在尝试编写将二进制数转换为十进制系统的递归函数。我找不到错误。 任何帮助将不胜感激。
这是我的第一次尝试:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
立即分段错误的原因非常接近
main()
函数的顶部:int *size
仅声明了一个指向 int 的指针 ,它实际上并不为该 int 分配内存。稍后在尝试分配数组时使用了 *size,整个事情就崩溃了。因此,稍微重新安排一下,程序就可以运行了。我认为输出还不正确:)但我不想剥夺你所有的学习机会。
The cause of the immediate segmentation fault is very near the top of the
main()
function: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.
在 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[].