Trinary.cpp
// Trinary.cpp
#include <iostream>
#include "Trinary.h"
// Trinary.cpp
#include <iostream>
#include "Trinary.h"
using namespace std;
// Trinary.cpp
#include <iostream>
#include "Trinary.h"
using namespace std;
std::string IntToTrinary(int number){
}
int TrinaryToInt(std::string number){
}
char IntDigitToTrinary(int d){
}
int TrinaryDigitToInt(char d){
}
char IntDigitToTrinary(int d){
int value{' '};
if(d >= 0 and d < static_cast<int>(TRINARY_DIGITS.size())) {
value = TRINARY_DIGITS[d];
}
return value;
}
int TrinaryDigitToInt(char d){
int value{-1};
size_t pos;
pos = TRINARY_DIGITS.find(d);
if (pos != string::npos) {
value = static_cast<int>(pos);
}
return value;
}
Start with the most significant digit 1
Set value to be 0
While there are digits
Multiply value by 3
Add the digit to value
digit value new value Think
0 0
1 0x3+1 1 $1x3^0$
2 1x3+1 5 $1x3^1 + 2x3^0$
0 5x3+0 15 $1x3^2 + 2x3^1 + 0x3^0$
Let's try 122010
digit value newvalue Think
0 0
1 0x3+1 1 $1x3^0$
2 1x3+2 5 $1x3^1 + 2x3^0$
2 5x3+2 17 $1x3^2 + 2x3^1 + 2x3^0$
0 17x3+0 51 $1x3^3 + 2x3^2 + 2x3^1 + 0x3^0$
1 51x3+1 154 $1x3^4 + 2x3^3 + 2x3^2 + 0x3^1 + 1x3^0$
0 154x3+0 462 $1x3^5 + 2x3^4 + 2x3^3 + 0x3^2 + 2x3^1 + 0x3^0$