Na std::string, także można zrobić switch(...) ale to trochę więcej zabawy (constexpr i hash).
To jest na tyle prosty interpreter, że wystarczy brutalne szukanie sekwencyjne. Prosty szkic:
#include <string>
#include <iostream>
#include <vector>
#include <utility>
#include <functional>
bool startsWith(const std::string& msg, const std::string& pattern) {
return msg.find(pattern) == 0;
}
std::function<void()> selectFunction(const std::string& pattern) {
// Komend jest tak mało że brutalne wyszukanie sekwencyjne
// będzi wystarczająco wydajne.
static std::vector<std::pair<const char *, std::function<void()>>> commands = {
{ "!U", []{ std::cout << "User\n"; } },
{ "!C", []{ std::cout << "Computer Name.\n"; } },
{ "!P", []{ std::cout << "Current Path.\n"; } },
{ "!D", []{ std::cout << "Current Disk.\n"; } },
// ...
};
auto result = []{ std::cout << "Unknown command.\n"; };
for(const auto& pr: commands) {
if(startsWith(pr.first, pattern)) {
return pr.second;
}
}
return result;
}
int main() {
std::string command;
std::cout << "Enter command: ";
std::cin >> command;
// TODO: Sprawdzenie czy poprawna etc...
selectFunction(command)();
}
Jak będziesz chciał mieć bardziej skomplikowane zachowanie (bazujące na komendzie), dodaj do funkcji komendy parametr i tyle :)