我在使用递归添加字母到基数10 - 基数12转换时遇到了麻烦.我如何在函数中添加字母?我正在考虑添加一个if语句,但我不知道在哪里以及如何去做.指针表示赞赏谢谢!
从1到12的计数:
Dec 1 2 3 4 5 6 7 8 9 10 11 12
Duo 1 2 3 4 5 6 7 8 9 X E 10
Run Code Online (Sandbox Code Playgroud)
我的功能:
template<class myType>
myType convertDec(myType number){
if(number == 0)
return number;
//if statement somewhere in here? not sure considering i can't touch the return statement
return (number % 12) + 10*convertDec(number / 12);
}
Run Code Online (Sandbox Code Playgroud)
示例理想输出:
65280 = 31940 (工作正常)
2147483626 = 4EE23088X (不起作用!)
#include <iostream>
#include <string>
using namespace std;
string ConvertToDuodecimal(unsigned long long n)
{
if (n < 12)
return string() + "0123456789XE"[n];
return ConvertToDuodecimal(n / 12) + ConvertToDuodecimal(n % 12);
}
int main()
{
cout << ConvertToDuodecimal(0) << endl;
cout << ConvertToDuodecimal(1) << endl;
cout << ConvertToDuodecimal(10) << endl;
cout << ConvertToDuodecimal(11) << endl;
cout << ConvertToDuodecimal(12) << endl;
cout << ConvertToDuodecimal(13) << endl;
cout << ConvertToDuodecimal(65280) << endl;
cout << ConvertToDuodecimal(2147483626) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出(ideone):
0
1
X
E
10
11
31940
4EE23088X
Run Code Online (Sandbox Code Playgroud)