[英]how to store an input into an array? C++
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 **
請幫忙謝謝。
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
之外,這幾乎是與問題一起發布的原始代碼。
您可以使用boot動態bitset
#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";
}
如果沒有boost,請使用std :: bitset。
std :: bitset唯一的問題是它有一個固定的大小
#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 array
。
#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;
}
這個動態地設置數組的大小,使其正確匹配。
以下示例根據需要將位存儲到原始C樣式數組。 但如果需要,可以用std :: vector替換它。
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
這里我們有一個字符數組。 我們將二進制數輸入到數組中,然后打印數組的每個元素以顯示每個位。 第一個打印行訪問第一個元素[1] [0] [1] [0] [0]。 第二個打印行訪問第二個元素[1] [0] [1] [0] [0]。
假設我們有一個包含100個字符的二進制數。 我們輸出數組的每個元素的方式需要很長時間。
1.)您能想到一種更有效的打印陣列內容的方法嗎?
2.)我們如何確保用戶只輸入一個和/或零?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.