根據十進位制轉換成其他進位制的計算方法:不斷用十進位制與R相除,然後取餘數,最後再把整個結果顛倒過來,設計的程式如下:#include <iostream>#include <vector>#include <string>using namespace std;string Convert(int num, int R){string res;vector<int> temp;int remainder = 0;while (num != 0){remainder = num % R;num = num / R;temp.push_back(remainder);}vector<int>::reverse_iterator iter;for (iter = temp.rbegin(); iter != temp.rend(); iter++){if (*iter >= 10)res.push_back((char)(*iter - 10) + "A");elseres.push_back(*iter + "0");}return res;}int main(){int num, R;string res;cout << "輸入要轉換的數字:" << endl;cin >> num;cout << "輸入要轉換的進位制:" << endl;cin >> R;res = Convert(num, R);cout << "輸出的" << R << "進位制結果為:" << endl;cout << res << endl;return 0;}結果如圖:程式在附件中
根據十進位制轉換成其他進位制的計算方法:不斷用十進位制與R相除,然後取餘數,最後再把整個結果顛倒過來,設計的程式如下:#include <iostream>#include <vector>#include <string>using namespace std;string Convert(int num, int R){string res;vector<int> temp;int remainder = 0;while (num != 0){remainder = num % R;num = num / R;temp.push_back(remainder);}vector<int>::reverse_iterator iter;for (iter = temp.rbegin(); iter != temp.rend(); iter++){if (*iter >= 10)res.push_back((char)(*iter - 10) + "A");elseres.push_back(*iter + "0");}return res;}int main(){int num, R;string res;cout << "輸入要轉換的數字:" << endl;cin >> num;cout << "輸入要轉換的進位制:" << endl;cin >> R;res = Convert(num, R);cout << "輸出的" << R << "進位制結果為:" << endl;cout << res << endl;return 0;}結果如圖:程式在附件中