簡體   English   中英

為什么我的程序不能正確地將十進制轉換為二進制打印?

[英]Why isn't my program that converts decimal to binary printing correctly?

我有一個任務要求我將十進制數轉換為存儲在 char 數組中的 16 位 2 的補碼二進制表示。 我獲得了包含完整 main 方法和addOne方法的啟動代碼。 我只需要完成flipBitsmagnitudeToBinary方法。

我已經完成了這些方法,它們應該可以工作,但是我得到了字符串 output。 一個例子是:

Enter integers and convert it to 2's complement binary.
Non-numeric input terminates program.
27
Integer: 27, 2's complement binary: 

這是代碼:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void magnitudeToBinary(int n, char arr[], int numOfBits);
void flipBits(char arr[], int numOfBits);
void addOne(char arr[], int numOfBits);


int main(void) {
    setvbuf(stdout, NULL, _IONBF, 0);

    //Declare a char array with size 16 to simulate 16-bit binary
    int numOfBits = 16;
    char arr[numOfBits + 1];
    arr[numOfBits] = '\0'; //set the terminating character
    //Declare integer n to hold the decimal integer
    int n = 0;

    puts("Enter integers and convert it to 2's complement binary.");
    puts("Non-numeric input terminates program.");

    //Continually taking input and convert it to binary
    while (scanf("%d", &n) == 1) {//if successfully read in ONE decimal integer
        //Initialize the char array to all 0s (leave the terminating character unchanged)
        for(int i = 0; i < numOfBits; i ++){
            arr[i] = '0';
        }

        //Convert magnitude (absolute value) to binary
        magnitudeToBinary(abs(n), arr, numOfBits);

        //if the number is negative: flip all bits, then add 1.
        if(n < 0){
            //Flip all bits in char arr
            flipBits(arr, numOfBits);
            //Add 1
            addOne(arr, numOfBits);
        }
        //Output binary:
        printf("Integer: %d, 2's complement binary: %s\n", n, arr);
    }

    return EXIT_SUCCESS;
}
/* addOne: arithmatically add 1 to the binary simulated by character array
 * INPUT:   char arr[]: the character array holding the binary
 *          int numOfBits: the number of bits in the binary
 * */
void addOne(char arr[], int numOfBits){
    /* True table
     * ******************************
     * carry arr[i] |  arr[i] carry
     *  1       0   |   1       0
     *  1       1   |   0       1
     *  0       0   |   0       0
     *  0       1   |   1       0
     * *********************************/
    char carry = '1';
    for(int i = numOfBits - 1; i >= 0; i --){
        if(carry != arr[i]){
            arr[i] = '1';
            carry = '0';
        }
        else if(carry == '1'){
            arr[i] = '0';
        }
        else{
            arr[i] = '0';
        }
    }
    return;
}
/* flipBits: perform 1's complement on the binary, i.e., change 1 to 0, 0 to 1
 * INPUT:   char arr[]: the character array holding the binary
 *          int numOfBits: the number of bits in the binary *
 * */
void flipBits(char arr[], int numOfBits){
    //Implement your solution here
    for(int i = 0; i < numOfBits; i++) {
        if(arr[i] == '1') {
            arr[i] = '0';
        } else if(arr[i] == '0') {
            arr[i] = '1';
        }
    }
    return;
}
/* magnitudeToBinary:   Convert a non-negative decimal integer to binary (stored in a char array)
 *                      using division-remainder algorithm
 * INPUT:   int n: The decimal integer number to be converted
 *          char arr[]: the character array holding the binary
 *          int numOfBits: the number of bits in the binary *
 * */
void magnitudeToBinary(int n, char arr[], int numOfBits){
    //Implement the division-remainder algorithm here
    int i = 0;
    while (n > 0) {
        arr[i] = n % 2;
        n = n / 2;
        i++;
    }
    return;
}

我只想看看正確的二進制 output。 現在我不確定這是否是我的錯誤,我的教授的錯誤,或者 Eclipse 是否在起作用。 任何幫助表示贊賞,謝謝!

嘗試從以下位置更改magnitudeToBinary() 中的行:

    arr[i] = n % 2;

    arr[i] = n % 2 == 1 ? '1' : '0';

您想將 '1' 或 '0' 的 ASCII 值寫入數組 - 而不是 n % 2 的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM