Added ability to translate to Astral.

This commit is contained in:
TheSkyWinds 2024-12-20 14:20:26 +01:00
parent c63b8ac6a2
commit b0277f267a
3 changed files with 155 additions and 16 deletions

View File

@ -16,15 +16,46 @@
// Sky's translations:
// A=; B=@ C=* D=< E=& F=( G=" H=) I=[ J=x K=x L=| M=x N=/ O=? P=+ Q=x R=] S={ T=} U=. V=x W=- X=x Y=x Z=x
std::string usrInput() {
int usrOption() {
int option{0};
bool finished{false};
do {
std::cout << "Enter 1. Translate from Astral" << '\n';
std::cout << "Enter 2. Translate to Astral" << '\n';
std::cout << "Enter 3. Exit the program" << '\n';
std::cout << "Prompt: ";
std::cin >> option;
if (option > 3 || error::cinError()) {
std::cout << "Unrecognised option." << '\n' << std::endl;
continue;
}
if (option == 3)
std::exit(EXIT_SUCCESS);
finished = true;
} while (finished != true);
error::ignoreLine();
return option;
}
std::string usrInput(int option) {
std::string input{};
// Ensures that whitespace is preserved.
std::cout << "Enter the untranslated text: ";
std::getline(std::cin, input);
if (option == 1) {
std::cout << "Enter the untranslated text: ";
std::getline(std::cin, input);
}
else if (option == 2) {
std::cout << "Enter the text to translate: ";
std::getline(std::cin, input);
}
// Checks if there's any error & clears the buffer.
error::cinErrorVerbose();
return input;
@ -34,14 +65,16 @@ std::string usrInput() {
int main() {
std::string untranslatedText{};
int option{};
while (true) {
untranslatedText = {usrInput()};
do {
option = {usrOption()};
untranslatedText = {usrInput(option)};
if (untranslatedText == "exit") {
break;
}
std::cout << "Translation: " << translation::translateInput(untranslatedText) << std::endl;
}
if (option == 1)
std::cout << "Translation: " << translation::translateFromAstral(untranslatedText) << std::endl;
if (option == 2)
std::cout << "Translation: " << translation::translateToAstral(untranslatedText) << std::endl;
} while (option != 3);
return 0;
}

View File

@ -2,7 +2,7 @@
#include "translator.h"
namespace translation {
std::string translateInput(const std::string& input) {
std::string translateFromAstral(const std::string& input) {
std::string translatedText{};
for (std::size_t i = 0; i < input.length(); i++) {
@ -85,7 +85,7 @@ namespace translation {
// MISSING V
case 'w':
case '-':
translatedText.push_back('w');
break;
@ -106,4 +106,109 @@ namespace translation {
}
return translatedText;
}
std::string translateToAstral(const std::string& input) {
std::string translatedText{};
for (std::size_t i = 0; i < input.length(); i++) {
switch (input[i]) {
case 'a':
translatedText.push_back(';');
break;
case 'b':
translatedText.push_back('@');
break;
case 'c':
translatedText.push_back('*');
break;
case 'd':
translatedText.push_back('<');
break;
case 'e':
translatedText.push_back('&');
break;
case 'f':
translatedText.push_back('(');
break;
case 'g':
translatedText.push_back('"');
break;
case 'h':
translatedText.push_back(')');
break;
case 'i':
translatedText.push_back('[');
break;
// MISSING J
// MISSING K
case 'l':
translatedText.push_back('|');
break;
// MISSING M
case 'n':
translatedText.push_back('/');
break;
case 'o':
translatedText.push_back('?');
break;
case 'p':
translatedText.push_back('+');
break;
// MISSING Q
case 'r':
translatedText.push_back(']');
break;
case 's':
translatedText.push_back('{');
break;
case 't':
translatedText.push_back('}');
break;
case 'u':
translatedText.push_back('.');
break;
// MISSING V
case 'w':
translatedText.push_back('-');
break;
// MISSING X
// MISSING Y
// MISSING Z
case ' ':
translatedText.push_back(' ');
break;
default:
translatedText.push_back('E');
break;
}
}
return translatedText;
}
}

View File

@ -3,7 +3,8 @@
#include "translator.h"
namespace translation {
std::string translateInput(const std::string& i);
std::string translateFromAstral(const std::string& input);
std::string translateToAstral(const std::string& input);
}
#endif //TRANSLATOR_H