Skip to content

Instantly share code, notes, and snippets.

@dvtate
Last active May 28, 2016 20:28
Show Gist options
  • Save dvtate/9b22e425214a87ed4880368812458f70 to your computer and use it in GitHub Desktop.
Save dvtate/9b22e425214a87ed4880368812458f70 to your computer and use it in GitHub Desktop.
yeah... IDK
#include <iostream> //std::cin, std::cout
#include <cstdlib> //don't think this is needed
#include <sstream> //std::stringstream
#include <string> //std::string, str.length(), std::string::npos, etc.
#include <algorithm> //replace()
//custom functions (not just useful for this program):
int countSubstring(const std::string str, const std::string sub);
template <typename T>//copied from internet (to_string() not working)
std::string NumberToString(T Number) { //converts a number to a string
std::stringstream ss;
ss << Number;
return ss.str();
}
//tokipona functions
int texttonumber(std::string text);
std::string numbertotext(int num);
int main() {
int startnum, endnum;
std::string tpnumberinput;
std::cout << "Toki Pona Numbers tool|\n\n";
//text -> numbers
std::cout << "\nEnter a number in Toki Pona:\n";
std::getline(std::cin, tpnumberinput);
std::cout << "\n= " << texttonumber(tpnumberinput);
//numbers -> text
std::cout << "\n\nEnter a starting number:";
std::cin >> startnum;
std::cout << "\nEnter an ending number:";
std::cin >> endnum;
if (startnum < endnum) //counting up
for (int i = startnum; i <= endnum; i++)
std::cout << i << " = " << numbertotext(i) << std::endl;
else //counting down || start==end
for (int i = startnum; i >= endnum; i--)
std::cout << i << " = " << numbertotext(i) << std::endl;
}
int texttonumber(std::string text) {
text += " ";
int value = 0; //defaults to 0 (ali = 0)
value += 100 * countSubstring(text, "ala ");
value += 20 * countSubstring(text, "mute ");
value += 5 * countSubstring(text, "luka ");
value += 2 * countSubstring(text, "tu ");
value += 1 * countSubstring(text, "wan ");
return value;
}
std::string numbertotext(int num) { //convert Toki Pona numbers into Toki Pona text
if (num < 0) return "Toki Pona doesn't have negative numbers.";
else if (num == 0) return "ali";
std::string text;
while (num > 0) {
if (num >= 100) {
text += "ala ";
num -= 100;
} else if (num >= 20) {
text += "mute ";
num -= 20;
} else if (num >= 5) {
text += "luka ";
num -= 5;
} else if (num >= 2) {
text += "tu ";
num -= 2;
} else if (num == 1) {
text += "wan ";
num -= 1;
}
}
return text;
}
//this is a useful function...
int countSubstring(const std::string& str, const std::string& sub) {
if (sub.length() == 0) return 0;//cannot find zero-length string
int count = 0;
for (size_t offset = str.find(sub);
offset != std::string::npos;
offset = str.find(sub, offset + sub.length())
) ++count;
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment