我正在嘗試創建給定整數的二進制表示,但是,當我嘗試在代碼末尾輸出字串 binaryNum 時,沒有列印任何內容。但是,如果我在 for 回圈中運行 cout,它將在程式添加 0 和 1 時列印出二進制表示(我只想要最終輸出,而不是沿途的步驟)。
我錯過了什么?
#include <string>
#include <iostream>
using namespace std;
int main() {
int num;
string binaryNum = "";
int divisor = 1;
cin >> num;
while (num > 0) {
while (num / divisor > 1) {
divisor *= 2;
}
if (num / divisor == 1) {
binaryNum.push_back('1');
num = num - divisor;
divisor /= 2;
while (num / divisor < 1) {
divisor /= 2;
binaryNum.push_back('0');
}
}
}
cout << binaryNum << endl;
return 0;
}
謝謝!
uj5u.com熱心網友回復:
我錯過了什么?
您除以零,導致未定義的行為。
考慮 whennum和divisorare 1。
while (num / divisor > 1) {
divisor *= 2;
}
上面的代碼不會回圈。 1 > 1不是true。
if (num / divisor == 1) {
然后輸入上面if的內容,因為1 == 1.
binaryNum.push_back('1');
num = num - divisor;
divisor /= 2;
while (num / divisor < 1) {
然后上面的代碼divisor等于0。
然后你的演算法除以零,
uj5u.com熱心網友回復:
使用bitsetC 11 的頭檔案來簡化您的作業流程。例如:
#include <bitset>
#include <string>
#include <iostream>
#include <limits>
int main(void)
{
int my_num = 0;
std::cout << "Enter a number: ";
std::cin >> my_num;
std::bitset<std::numeric_limits<int>::digits> foo(my_num);
std::cout << my_num << " in binary = " << foo << std::endl;
return 0;
}
延伸閱讀:
- https://www.geeksforgeeks.org/c-bitset-and-its-application/
- https://docs.microsoft.com/en-us/cpp/standard-library/bitset-class?view=msvc-170
uj5u.com熱心網友回復:
您可以使用 bitset 將十進制轉換為二進制,如下所示:
#include <string>
#include <iostream>
#include <bitset>
int main()
{
unsigned int num;
std::cin >> num;
std::cout << std::bitset<8>{num} << std::endl; // Replace 8 with the number of binary digits you wish to print.
return 0;
}
但這里的問題是,這樣,二進制數被限制為 8 位(在這種情況下)。所以讓我們談談你的代碼。
您的代碼的問題是,在某一時刻,除數的值變為 0。現在您正在執行以下操作:
while (num / divisor < 1)
我們都知道,除以 0 是不可能的。因此,要修復您的錯誤,請在 'divisor /= 0;' 之后添加以下行:
if (divisor == 0) break;
如果除數 == 0,這將跳出主要的 while 回圈“while (num > 0)”,然后將列印 binaryNum。
最終代碼:
#include <string>
#include <iostream>
int main() {
int num;
std::string binaryNum = "";
int divisor = 1;
std::cin >> num;
while (num > 0) {
while (num / divisor > 1) {
divisor *= 2;
}
if (num / divisor == 1) {
binaryNum.push_back('1');
num = num - divisor;
divisor /= 2;
if (divisor == 0) break;
while (num / divisor < 1) {
divisor /= 2;
binaryNum.push_back('0');
}
}
}
std::cout << binaryNum << std::endl;
return 0;
}
另外,請考慮不要在代碼中使用以下行:
using namespace std;
...因為它被認為是床上練習。
uj5u.com熱心網友回復:
我只是想補充一下……如果你好奇有不同的方法來顯示數字的二進制表示,這里有一些我知道的方法。
- using
std::bitset- stl 庫將完成這項作業。 - 使用移位 - 在這里我們使用移位獲得給定數字的每一位,我建議學習左移位和右移位的作業原理。
- 使用除法 - 你在學校最常學習的方法以及你可能想要做的事情,這里我們將數字除以 2 并得到余數,此時余數只能是 1 或 0,而這些余數就是我們的二進制,我們還設定了 num 的值與每次迭代的地板除法的商。
筆記:
- 直接使用位時,您應該注意系統的位元組順序、變數的位大小以及您的數字是否無符號。
#include <iostream>
#include <bitset>
#define BITS_OF_INT sizeof(int)*8
int main(){
int num = 788;
// using bitset
std::cout << "std:bitset\n";
std::bitset<BITS_OF_INT> binary(num);
std::cout << binary << "\n";
std::cout << binary.to_string() << "\n";
// using shifts
std::cout << "\n\nusing shifts\n";
for(size_t i=0; i<(BITS_OF_INT); i){
unsigned int bit = num;
bit <<= i;
bit = bit >> ((BITS_OF_INT)-1);
std::cout << bit; // you can convert this to a string or char then add to another string
}
std::cout << "\n\n";
// using division and remainder = (mod = %)
// this only works for positive values
std::cout << "using division\n";
int temp = num;
std::string binary_str;
for(size_t i=0; i<BITS_OF_INT; i){
int remainder = temp % 2;
temp /= 2;
binary_str.insert(binary_str.begin(),'0' remainder);
}
std::cout << binary_str << "\n";
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429501.html
下一篇:在谷歌模擬中執行呼叫函式的順序
