一、請使用 C、C++、C#、Java 或 Python 程式語言,撰寫一個進位制轉換程式的方法,方法接受一個合法的整數,並以參數形式回傳十進位制分別 轉成以 string 資料型別表示的二、八、及十六進位制結果;主程式負責可以持續接受輸入,檢查輸入為合法的整數值後,呼叫轉換程式的方法, 並顯示結果,如果輸入不是合法的整數,結束程式的執行。執行範例如 下: (25 分)
詳解 (共 2 筆)
詳解
https://onlinegdb.com/Nbu4r1h9E
C原始碼:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
// Function to convert decimal to binary, octal, and hexadecimal
void tenBaseTo2_8_16base(int num, char *binary, char *octal, char *hex) {
unsigned int temp = (num < 0) ? -num : num; // Use unsigned for bitwise operations
int i = 0, j = 0;
char bin_rev[33] = {0}, oct_rev[12] = {0}, hex_rev[9] = {0};
// Handle special case for 0
if (num == 0) {
strcpy(binary, "0");
strcpy(octal, "0");
strcpy(hex, "0");
return;
}
// Convert to binary
while (temp > 0) {
bin_rev[i++] = (temp & 1) + '0';
temp >>= 1;
}
// Reverse binary
for (int x = i - 1; x >= 0; x--) {
binary[j++] = bin_rev[x];
}
binary[j] = '\0';
// Reset temp
temp = (num < 0) ? -num : num;
i = 0;
j = 0;
// Convert to octal
while (temp > 0) {
oct_rev[i++] = (temp % 8) + '0';
temp /= 8;
}
// Reverse octal
for (int x = i - 1; x >= 0; x--) {
octal[j++] = oct_rev[x];
}
octal[j] = '\0';
// Reset temp
temp = (num < 0) ? -num : num;
i = 0;
j = 0;
// Convert to hexadecimal
while (temp > 0) {
int remainder = temp % 16;
if (remainder < 10)
hex_rev[i++] = remainder + '0';
else
hex_rev[i++] = remainder - 10 + 'A';
temp /= 16;
}
// Reverse hexadecimal
for (int x = i - 1; x >= 0; x--) {
hex[j++] = hex_rev[x];
}
hex[j] = '\0';
// Handle negative numbers
if (num < 0) {
memmove(binary + 1, binary, strlen(binary) + 1);
binary[0] = '-';
memmove(octal + 1, octal, strlen(octal) + 1);
octal[0] = '-';
memmove(hex + 1, hex, strlen(hex) + 1);
hex[0] = '-';
}
}
void tenBaseTo2_8_16base(int num, char *binary, char *octal, char *hex) {
unsigned int temp = (num < 0) ? -num : num; // Use unsigned for bitwise operations
int i = 0, j = 0;
char bin_rev[33] = {0}, oct_rev[12] = {0}, hex_rev[9] = {0};
// Handle special case for 0
if (num == 0) {
strcpy(binary, "0");
strcpy(octal, "0");
strcpy(hex, "0");
return;
}
// Convert to binary
while (temp > 0) {
bin_rev[i++] = (temp & 1) + '0';
temp >>= 1;
}
// Reverse binary
for (int x = i - 1; x >= 0; x--) {
binary[j++] = bin_rev[x];
}
binary[j] = '\0';
// Reset temp
temp = (num < 0) ? -num : num;
i = 0;
j = 0;
// Convert to octal
while (temp > 0) {
oct_rev[i++] = (temp % 8) + '0';
temp /= 8;
}
// Reverse octal
for (int x = i - 1; x >= 0; x--) {
octal[j++] = oct_rev[x];
}
octal[j] = '\0';
// Reset temp
temp = (num < 0) ? -num : num;
i = 0;
j = 0;
// Convert to hexadecimal
while (temp > 0) {
int remainder = temp % 16;
if (remainder < 10)
hex_rev[i++] = remainder + '0';
else
hex_rev[i++] = remainder - 10 + 'A';
temp /= 16;
}
// Reverse hexadecimal
for (int x = i - 1; x >= 0; x--) {
hex[j++] = hex_rev[x];
}
hex[j] = '\0';
// Handle negative numbers
if (num < 0) {
memmove(binary + 1, binary, strlen(binary) + 1);
binary[0] = '-';
memmove(octal + 1, octal, strlen(octal) + 1);
octal[0] = '-';
memmove(hex + 1, hex, strlen(hex) + 1);
hex[0] = '-';
}
}
int main() {
char input[20];
int num;
char binary[33], octal[12], hex[9];
char input[20];
int num;
char binary[33], octal[12], hex[9];
while (1) {
printf("Enter an integer (or 'q' to quit): ");
scanf("%s", input);
if (input[0] == 'q' || input[0] == 'Q') {
break;
}
// Check if input is a valid integer
char *endptr;
num = strtol(input, &endptr, 10);
if (*endptr != '\0') {
printf("Invalid input. Please enter a valid integer.\n");
break;
}
tenBaseTo2_8_16base(num, binary, octal, hex);
printf("Binary: %s\n", binary);
printf("Octal: %s\n", octal);
printf("Hexadecimal: %s\n", hex);
}
printf("Enter an integer (or 'q' to quit): ");
scanf("%s", input);
if (input[0] == 'q' || input[0] == 'Q') {
break;
}
// Check if input is a valid integer
char *endptr;
num = strtol(input, &endptr, 10);
if (*endptr != '\0') {
printf("Invalid input. Please enter a valid integer.\n");
break;
}
tenBaseTo2_8_16base(num, binary, octal, hex);
printf("Binary: %s\n", binary);
printf("Octal: %s\n", octal);
printf("Hexadecimal: %s\n", hex);
}
return 0;
}
}
執行結果:

https://onlinegdb.com/YB2SItIl5
C++原始碼
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cstdlib>
// Function to convert decimal to binary, octal, and hexadecimal
void tenBaseTo2_8_16base(int num, std::string& binary, std::string& octal, std::string& hex) {
unsigned int temp = (num < 0) ? -num : num; // Use unsigned for bitwise operations
std::string bin_rev, oct_rev, hex_rev;
void tenBaseTo2_8_16base(int num, std::string& binary, std::string& octal, std::string& hex) {
unsigned int temp = (num < 0) ? -num : num; // Use unsigned for bitwise operations
std::string bin_rev, oct_rev, hex_rev;
// Handle special case for 0
if (num == 0) {
binary = octal = hex = "0";
return;
}
if (num == 0) {
binary = octal = hex = "0";
return;
}
// Convert to binary
while (temp > 0) {
bin_rev.push_back((temp & 1) + '0');
temp >>= 1;
}
binary = std::string(bin_rev.rbegin(), bin_rev.rend());
while (temp > 0) {
bin_rev.push_back((temp & 1) + '0');
temp >>= 1;
}
binary = std::string(bin_rev.rbegin(), bin_rev.rend());
// Reset temp
temp = (num < 0) ? -num : num;
temp = (num < 0) ? -num : num;
// Convert to octal
while (temp > 0) {
oct_rev.push_back((temp % 8) + '0');
temp /= 8;
}
octal = std::string(oct_rev.rbegin(), oct_rev.rend());
while (temp > 0) {
oct_rev.push_back((temp % 8) + '0');
temp /= 8;
}
octal = std::string(oct_rev.rbegin(), oct_rev.rend());
// Reset temp
temp = (num < 0) ? -num : num;
temp = (num < 0) ? -num : num;
// Convert to hexadecimal
while (temp > 0) {
int remainder = temp % 16;
if (remainder < 10)
hex_rev.push_back(remainder + '0');
else
hex_rev.push_back(remainder - 10 + 'A');
temp /= 16;
}
hex = std::string(hex_rev.rbegin(), hex_rev.rend());
while (temp > 0) {
int remainder = temp % 16;
if (remainder < 10)
hex_rev.push_back(remainder + '0');
else
hex_rev.push_back(remainder - 10 + 'A');
temp /= 16;
}
hex = std::string(hex_rev.rbegin(), hex_rev.rend());
// Handle negative numbers
if (num < 0) {
binary = "-" + binary;
octal = "-" + octal;
hex = "-" + hex;
}
}
if (num < 0) {
binary = "-" + binary;
octal = "-" + octal;
hex = "-" + hex;
}
}
int main() {
std::string input;
int num;
std::string binary, octal, hex;
std::string input;
int num;
std::string binary, octal, hex;
while (true) {
std::cout << "Enter an integer (or 'q' to quit): ";
std::cin >> input;
std::cout << "Enter an integer (or 'q' to quit): ";
std::cin >> input;
if (input[0] == 'q' || input[0] == 'Q') {
break;
}
break;
}
// Check if input is a valid integer
try {
num = std::stoi(input);
} catch (const std::exception&) {
std::cout << "Invalid input. Please enter a valid integer." << std::endl;
break;
}
try {
num = std::stoi(input);
} catch (const std::exception&) {
std::cout << "Invalid input. Please enter a valid integer." << std::endl;
break;
}
tenBaseTo2_8_16base(num, binary, octal, hex);
std::cout << "Binary: " << binary << std::endl;
std::cout << "Octal: " << octal << std::endl;
std::cout << "Hexadecimal: " << hex << std::endl;
}
std::cout << "Binary: " << binary << std::endl;
std::cout << "Octal: " << octal << std::endl;
std::cout << "Hexadecimal: " << hex << std::endl;
}
return 0;
}
}
執行結果:


詳解
C++
有錯誤敬請指教
#include<iostream>
#include<string>
#include<vector>
#include<climits>
int tenBaseTo2_8_16(int num){
std::vector<int> num2;
std::vector<int> num8;
std::vector<std::string> num16;
int length;
int neg=0;
if(num<0){
num=abs(num);
neg++;
}
//二進位
int temp=num;
while(temp!=0){
num2.push_back(temp%2);
temp=temp/2;
}
length=num2.size();
if(neg==1){
std::cout<<"-";
}
for(int i=0;i<length;i++){
std::cout<<num2.back();
num2.pop_back();
}
std::cout<<" ";
//八進位
temp=num;
while(temp!=0){
num8.push_back(temp%8);
temp=temp/8;
}
length=num8.size();
if(neg==1){
std::cout<<"-";
}
for(int i=0;i<length;i++){
std::cout<<num8.back();
num8.pop_back();
}
std::cout<<" ";
//十六進位
temp=num;
while(temp!=0){
int r=temp%16;
if(r>=0&&r<=9){num16.push_back(std::to_string(r));
}else if(r==10){ num16.push_back("A");}
else if(r==11){ num16.push_back("B");}
else if(r==12){ num16.push_back("C");}
else if(r==13){ num16.push_back("D");}
else if(r==14){ num16.push_back("E");}
else if(r==15){ num16.push_back("F");}
temp=temp/16;
}
length=num16.size();
if(neg==1){
std::cout<<"-";
}
for(int i=0;i<length;i++){
std::cout<<num16.back();
num16.pop_back();
}
std::cout<<std::endl;
}
int main() {
std::string num;
while (getline(std::cin, num)) {
try {
// 檢查輸入是否為空
if (num.empty()) {
std::cout << "按Enter幹嘛" << std::endl;//考試請好好輸出
break; // 結束程式
}
// 檢查輸入是否為數字或負號開頭
size_t startPos = 0;
if (num[0] == '-') {
startPos = 1; // 如果是負號,從第二個字元開始檢查
}
// 檢查數字是否在 int 範圍內
int temp = std::stoi(num, &startPos);
if (startPos != num.length() || temp < INT_MIN || temp > INT_MAX) {
throw std::invalid_argument("Invalid input");
}
// 如果是 0,直接輸出
if (temp == 0) {
std::cout << "0 0 0" << std::endl;
} else {
tenBaseTo2_8_16(temp);
}
} catch (const std::invalid_argument& e) {
std::cout << "工三小" << std::endl; //考試時請好好輸出
} catch (const std::out_of_range& e) {
std::cout << "超過了啦==" << std::endl; //考試請好好輸出
}
}
return 0;
}
terminal
