int b;
int array[12];
cout << "Enter binary number: ";
cin >> b;
(例如:b将变为10100)
**如何将b(10100)存储到数组中,使其成为[1] [0] [1] [0] [0] **
cout << array[0] << endl;
**输出结果应该是1**
cout << array[1] << endl;
** 输出结果应该是0 **
请帮忙,谢谢。
int b;
int array[12];
cout << "Enter binary number: ";
cin >> b;
cout << array[0] << endl;
**输出结果应该是1**
cout << array[1] << endl;
** 输出结果应该是0 **
请帮忙,谢谢。
一个 string
也可以被视为一组 char
数组。因此,您可以将输入存储到一个字符串中,然后编写的 cout
语句应该可以工作。但是,它们将是 char
而不是 int,因此您将存储 '1' 和 '0' 而不是 1 和 0。它们之间的转换很容易,只需使用 array[0]-'0'
#include <string>
#include <iostream>
using namespace std;
int main()
{
string array;
cout << "Enter binary number: "; cin >> array;
// Suppose the user inputs 10100
cout << array[0] << endl; // outputs '1'
cout << array[1] << endl; // outputs '0'
cout << array[2] << endl; // outputs '1'
return 0;
}
更新:添加了可编译代码。请注意,这基本上是发布问题时的原始代码,除了输入一个string
。
cin >> array
将用户输入的任何内容存储到 array
变量中。也许我没有理解你的问题。 - Pablo你可以使用 Boost 动态位集
#include "boost/dynamic_bitset.hpp"
#include <sstream>
#include <iostream>
int main()
{
boost::dynamic_bitset<> val;
std::stringstream input("1010101010");
input >> val; // Input a binary number
// Can use std::cin or another stream
std::cout << val.to_ulong() << "\n";
std::cout << val[5] << "\n";
}
#include <bitset>
#include <sstream>
#include <iostream>
int main()
{
std::bitset<16> val; // fixed 16 bit size
std::cout << "Enter binary number:\n";
std::cin >> val; // Input a binary number
// Can use std::cin or another stream
std::cout << val.to_ulong() << "\n";
std::cout << val[5] << "\n";
}
我本来写了一个类似Pablo的答案,但既然他已经发布了,那么这里是另一个与给定信息一致的答案。
它接受一个int
输入并将其放入一个int数组
中。
#include <iostream>
#include <cmath>
int main( )
{
int b;
int numDigits;
// Get bit string int
std::cin >> b;
// Get the number of digits
numDigits = std::floor( std::log10( ( float )std::abs( b != 0 ? b : 1 ) ) ) + 1;
// Dynamically create a new array of the appropriate size
int* arr = new int[ numDigits ];
// Initialize all the blocks of memory
std::memset( arr, 0, numDigits );
// Fill the array
for( int i = 0; i < numDigits; i++ )
{
arr[ numDigits - i - 1 ] = b % 10;
b /= 10;
}
system( "PAUSE" );
// Delete the array
delete [] arr;
return 0;
}
这个动态设置数组大小,以便它正确适应。
int main()
{
// assume sizeof(int) is 32, or you can use heap-allocated array or std::vector
int array[32];
unsigned int mask = 1;
// mask is initially 0x80000000
mask = mask << (sizeof(int)*8 - 1);
int i = 0;
// we start counting from the first "1",
// preceding "0"s are ignored to display
bool started = false;
int b;
cin >> b;
while (mask != 0)
{
// if current bit is "1" or "0"
if (mask & b)
{
started = true;
array[i++] = 1;
}
// if current bit is "0" and counting started
else if (started)
{
array[i++] = 0;
}
// ready to test next bit
mask = mask >> 1;
}
// test result
for (int j = 0; j < i; ++j) cout << array[j];
cout << endl;
return 0;
}
Test cases:
1. b = 8 => array: 1000
2. b = -8 => array: 11111111111111111111111111111000
3. ..
将二进制数存储到数组中
char binaryArray [5]; // The array looks like this: [][][][][]
cout << "Enter binary number: ";
cin >> binaryArray; // Stores the binary number into the array: [1][0][1][0][0]
cout << binaryArray[0] << endl; // The first element is sent to standard output.
cout << binaryArray[1] << endl; // The second element is sent to standard output.
对于您的输入,输出将是:
1
0