gpt4 book ai didi

c - 程序一步步执行失败

转载 作者:太空宇宙 更新时间:2023-11-04 01:48:28 27 4
gpt4 key购买 nike

#include<math.h>
#include<stdio.h>
#include<conio.h>
int binaryToDecimal(long binarynum){
int decimalnum = 0, temp = 0, remainder;
while(binarynum!=0){
remainder = binarynum % 10;
remainder = binarynum / 10;
decimalnum = decimalnum+remainder*pow(2,temp);
temp++;
}
return decimalnum;
}
long decimalToBinary(int n){
int remainder;
long binary = 0,i = 1;
while(n != 0){
remainder = n % 2;
n = n / 2;
binary = binary + (remainder*i);
i = i*10;
}
return binary;
}
int main(){
int quotient, rem,decimalnum1,decimalnum2;
long binarynum1, binarynum2;

printf("Enter dividend binary number::");
scanf("%ld",&binarynum1);

printf("Enter divisor binary number::");
scanf("%ld",&binarynum2);
/// execution stops from here.
decimalnum1 = binaryToDecimal(binarynum1);
decimalnum2 = binaryToDecimal(binarynum2);
quotient = decimalnum1/decimalnum2;
rem = decimalnum1%decimalnum2;
printf("Quotient is: %ld",decimalToBinary(quotient));
printf("Remainder is: %ld",decimalToBinary(rem));
getch();
}

我正在使用程序来执行二进制数之间的除法。但程序执行在第 6 条语句后停止。我试图更改变量类型但仍然有问题。

程序执行的输出在这里。

Output

最佳答案

binaryToDecimal 函数中存在无限循环问题。

int binaryToDecimal(long binarynum){
int decimalnum = 0, temp = 0, remainder;
while(binarynum!=0){
remainder = binarynum % 10;
remainder = binarynum / 10;
decimalnum = decimalnum+remainder*pow(2,temp);
temp++;
}
return decimalnum;
}

改变 binarynum 变量的部分在哪里?看,它在循环中永远不会改变,所以它永远不会为 0。你的循环永远运行。

关于c - 程序一步步执行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48597692/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com