1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> #include <string> using namespace std; int main(){ string bin_n; int dec_n = 0; int i; cout << "請輸入二進位數字:"; cin >> bin_n; for(i = 0; i < bin_n.length(); i++) if (bin_n[i] >= 0 && bin_n[i] <= 1) dec_n = dec_n * 2 + (bin_n[i] - '0'); cout << "轉成十進位為:" << dec_n << endl; else cout << "error" << return 0; } |
Direct link: https://paste.plurk.com/show/269310