简体   繁体   English

程序逐步执行失败

[英]Program failed in executing step by step

#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();
}

I was working with the program to perform division between binary numbers. 我正在使用该程序来执行二进制数之间的划分。 But the program execution stops after 6th statement. 但程序执行在第6次声明后停止。 I tried to change the variable types but still having problem. 我试图改变变量类型但仍有问题。

Output of the program execution is here. 程序执行的输出在这里。

Output 产量

You have an infinite loop problem in your binaryToDecimal function. 您的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;
}

Where's the part that changes binarynum variable? 改变binarynum变量的部分在哪里? See, it never changes in the loop so it can't ever be 0. Your loop runs forever. 看,它永远不会在循环中改变,所以它永远不会是0.你的循环永远运行。

Your binaryToDecimal function runs in infinite loop. 您的binaryToDecimal函数在无限循环中运行。 Try the function below instead. 请尝试以下功能。

int binaryToDecimal(long n)
{
    int decimalNumber = 0, i = 0, remainder;
    while (n!=0)
    {
        remainder = n%10;
        n /= 10;
        decimalNumber += remainder*pow(2,i);
        ++i;
    }
    return decimalNumber;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM