1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <random>
#include <chrono>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
struct Action
{
string description;
void (*func)( string &str );
};
mt19937 rng( chrono::system_clock::now().time_since_epoch().count() );
int main()
{
vector<Action> opt = { { "Exit" , []( string &str ){ exit( 0 ); } },
{ "Enter word", []( string &str ){ cout << "Enter word: "; cin >> str; } },
{ "Reverse" , []( string &str ){ reverse( str.begin(), str.end() ); } },
{ "Randomise" , []( string &str ){ shuffle( str.begin(), str.end(), rng ); } },
{ "Uppercase" , []( string &str ){ for ( char &c : str ) c = toupper( c ); } },
{ "Lowercase" , []( string &str ){ for ( char &c : str ) c = tolower( c ); } },
{ "Sort" , []( string &str ){ sort( str.begin(), str.end() ); } } };
string word;
int N = opt.size();
int ans;
while( true )
{
for ( int i = 0; i < N; i++ ) cout << i << ": " << opt[i].description << '\n';
cin >> ans; if ( ans < 0 || ans >= N ) continue;
opt[ans].func( word );
cout << "Current word is " << word << "\n\n";
}
}
|