c - 为什么我的程序不能正确地将十进制转换为二进制打印?

标签 c eclipse

我有一个作业要求我将十进制数转换为存储在 char 数组中的 16 位 2 的补码二进制表示形式。我获得了启动代码,其中包括一个完整的 main 方法和一个 addOne 方法。我只需要完成 flipBitsmagnitudeToBinary 方法。

我已经完成了这些方法,它们应该可以工作,但我得到的是字符串输出。一个例子是:

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;
}

我只想看到正确的二进制输出。现在我不确定这是否是我的错误、我的教授的错误,或者 Eclipse 是否正在运行。感谢任何帮助,谢谢!

最佳答案

尝试将 magnitudeToBinary() 中的行从:

    arr[i] = n % 2;

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

您想将“1”或“0”的 ASCII 值写入数组 - 而不是 n % 2 的结果。

关于c - 为什么我的程序不能正确地将十进制转换为二进制打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58030728/

相关文章:

c - 动态增加 Struct "C"中的数组大小

java - 如何使用 eclipse 构建 PingFederate 插件

java - Eclipse 和 JAR 中的不同 AES 加密

android - 使用 Ant/Ivy 构建 Android 项目

c - 如何在 C 中的给定地址动态分配内存?

c - 如何使用 fwrite() 将结构写入文件?

c# - 在应用程序 Xcode 中编写代码

java - Eclipse 调试器在终止前停止

java - 给定经纬度坐标,如何打印两个城市之间的距离?

Xcode cs50.h 中的 C 问题