【问题标题】:Convert number in binary and print out as matrix (C++)将数字转换为二进制并打印为矩阵(C++)
【发布时间】:2021-07-03 22:07:06
【问题描述】:

在您提前阅读或尝试提供帮助之前,这个问题与我的作业有关,因此对这个问题的要求将非常具体。

我正在编写一个代码,它接受 0 到 511 之间的用户输入并将其转换为二进制数。然后程序会将二进制数中的所有 1 替换为 T,并将数字中的所有 0 替换为 H。然后它将结果(带有 H 和 T 替换的二进制数)打印为 3*3 矩阵。

这是想要的输出(不是我拥有的,而是我想要的):

Enter a number between 0 and 511: 299
The binary number is: 100101011
The matrix is:
THH
THT
HTT

我的代码的问题是我不确定如何替换由所有整数组成的数组以使索引的某些部分成为字符或字符串。当然,二进制数转换的部分可以工作,但是替换数组的 0 和 1 是问题所在。我也不确定如何打印出矩阵结果。我假设它采用以下两种方式之一: 1. 程序为先前存储的数组元素创建一个新数组,并打印出矩阵数组。 2. 有一种方法可以一次只打印 3 行数组。我能想到的唯一方法是以某种方式缩短 for 循环并在每 3 个值之后添加一个换行符。我知道我的代码中有一些可指出的错误,但我不知道如何修复它们。

虽然这是 C++ 语言,但我学到的是 C 风格的语法(没有 std:: 之类的代码或类似的东西,因为我还没有学过,也不会理解)已经学习了基本的数组、循环和函数。

#include <iostream>  

using namespace std;  

int main(){  

    int arr[10];

    int input, i;

        cout<<"Enter a number between 0 and 511:  ";

        cin>> input;

        for(i = 0; input > 0; i++){   

        arr[i] = (input % 2);    

        input = input / 2;  

        }
        cout<<"The binary number is: ";  

        
        for(i = i - 1; i >= 0; i--){   

         cout<<arr[i];   
        } 

        string newArr[10] = arr[10]; //the error here states that the array initializer must be an initializer list

        for(int i = 0; i < sizeof(arr)/sizeof(arr[10]); i++){
            if(arr[i] == 1){
                arr[i] = "T"; //the error here mentions that a string/ character cannot be assigned with a integer array
            }
            else{
                arr[i] = "H";
            }
        }

        
        for(int i = 0; i < sizeof(arr)/sizeof(arr[10]); i++){
                cout<<arr[i]<< " ";
        }

}

【问题讨论】:

  • string newArr[10] = arr[10];的初始化没有意义。您将如何使用单个(越界)整数值初始化一个由十个字符串组成的数组?
  • 还要注意sizeof(arr)/sizeof(arr[10])是数组arr中的元素个数。 所有元素,包括你从未初始化过的元素。

标签: c++ arrays loops matrix binary


【解决方案1】:

这就足够了:

#include <iostream>

using namespace std;

int main()
{
    // you never actually checked if the input is valid
    // so you may or may not want this loop:
    int input;
    do
    {
        cout << "Enter a number between 0 and 511:  ";
        cin >> input;
    } while ((input < 0) || (input > 511));

    // space for matrix, new lines and null
    // to construct a null terminated string
    char buffer[3 * (3 + 1) + 1];
    int i = 0;
    // since the bits are read from left to right
    // I will use a mask instead of bit shifting the input
    int bit = 1 << 9;// 2^9 == 512
    for (int r = 0; r < 3; r++)// rows
    {
        for (int c = 0; c < 3; c++)// columns
        {
            // this could come after the check
            // and then bit would start at 256
            bit >>= 1;
            // perform the check and add the corresponding letter
            buffer[i++] = (bit & input) ? 'T' : 'H';
        }
        // add new lines
        buffer[i++] = '\n';
    }
    // if you don't want the last '\n'
    // this could be { buffer[--i] = '\0'; }
    buffer[i++] = '\0';
    
    cout << buffer;
}

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2015-06-01
    • 2020-04-21
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 2016-03-06
    • 2018-06-30
    • 2010-10-31
    相关资源
    最近更新 更多