Abbreviations, easy: Difference between revisions

Content added Content deleted
(Added C solution)
m (Reformatted to reduce line count)
Line 357: Line 357:
"RIght LEft SAVE SET SHift SI SORT SOS STAck STATus TOP TRAnsfer Type Up";
"RIght LEft SAVE SET SHift SI SORT SOS STAck STATus TOP TRAnsfer Type Up";


class command
class command {
{
public:
public:
command(const std::string&, size_t);
command(const std::string&, size_t);
Line 371: Line 370:
// cmd is assumed to be all uppercase
// cmd is assumed to be all uppercase
command::command(const std::string& cmd, size_t min_len)
command::command(const std::string& cmd, size_t min_len)
: cmd_(cmd), min_len_(min_len)
: cmd_(cmd), min_len_(min_len) {}
{
}


// str is assumed to be all uppercase
// str is assumed to be all uppercase
bool command::match(const std::string& str) const
bool command::match(const std::string& str) const {
{
size_t olen = str.length();
size_t olen = str.length();
return olen >= min_len_ && olen <= cmd_.length()
return olen >= min_len_ && olen <= cmd_.length()
Line 384: Line 380:


// convert string to uppercase
// convert string to uppercase
void uppercase(std::string& str)
void uppercase(std::string& str) {
{
std::transform(str.begin(), str.end(), str.begin(),
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c) -> unsigned char { return std::toupper(c); });
[](unsigned char c) -> unsigned char { return std::toupper(c); });
}
}


size_t get_min_length(const std::string& str)
size_t get_min_length(const std::string& str) {
{
size_t len = 0, n = str.length();
size_t len = 0, n = str.length();
while (len < n && std::isupper(static_cast<unsigned char>(str[len])))
while (len < n && std::isupper(static_cast<unsigned char>(str[len])))
Line 398: Line 392:
}
}


class command_list
class command_list {
{
public:
public:
explicit command_list(const char*);
explicit command_list(const char*);
Line 407: Line 400:
};
};


command_list::command_list(const char* table)
command_list::command_list(const char* table) {
{
std::vector<command> commands;
std::vector<command> commands;
std::istringstream is(table);
std::istringstream is(table);
std::string word;
std::string word;
while (is >> word)
while (is >> word) {
{
// count leading uppercase characters
// count leading uppercase characters
size_t len = get_min_length(word);
size_t len = get_min_length(word);
Line 422: Line 413:
}
}


const command* command_list::find_command(const std::string& word) const
const command* command_list::find_command(const std::string& word) const {
for (const command& command : commands_) {
{
for (const command& command : commands_)
{
if (command.match(word))
if (command.match(word))
return &command;
return &command;
Line 432: Line 421:
}
}


std::string test(const command_list& commands, const std::string& input)
std::string test(const command_list& commands, const std::string& input) {
{
std::string output;
std::string output;
std::istringstream is(input);
std::istringstream is(input);
std::string word;
std::string word;
while (is >> word)
while (is >> word) {
{
if (!output.empty())
if (!output.empty())
output += ' ';
output += ' ';
Line 451: Line 438:
}
}


int main()
int main() {
{
command_list commands(command_table);
command_list commands(command_table);
std::string input("riG rePEAT copies put mo rest types fup. 6 poweRin");
std::string input("riG rePEAT copies put mo rest types fup. 6 poweRin");