2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,11 +1,19 @@
|
|||
;Task:
|
||||
Given a prompt and a list containing a number of strings of which one is to be selected, create a function that:
|
||||
|
||||
* prints a textual menu formatted as an index value followed by its corresponding string for each item in the list;
|
||||
* prompts the user to enter a number;
|
||||
* returns the string corresponding to the selected index number.
|
||||
|
||||
<br>
|
||||
The function should reject input that is not an integer or is out of range by redisplaying the whole menu before asking again for a number. The function should return an empty string if called with an empty list.
|
||||
|
||||
For test purposes use the four phrases: “<tt>fee fie</tt>”, “<tt>huff and puff</tt>”, “<tt>mirror mirror</tt>” and “<tt>tick tock</tt>” in a list.
|
||||
For test purposes use the following four phrases in a list:
|
||||
fee fie
|
||||
huff and puff
|
||||
mirror mirror
|
||||
tick tock
|
||||
|
||||
Note: This task is fashioned after the action of the [http://www.softpanorama.org/Scripting/Shellorama/Control_structures/select_statements.shtml Bash select statement].
|
||||
;Note:
|
||||
This task is fashioned after the action of the [http://www.softpanorama.org/Scripting/Shellorama/Control_structures/select_statements.shtml Bash select statement].
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1,50 +1,49 @@
|
|||
#include <iostream>
|
||||
#include <boost/regex.hpp>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
#include <vector>
|
||||
|
||||
void printMenu(const string *, int);
|
||||
//checks whether entered data is in required range
|
||||
bool checkEntry(string, const string *, int);
|
||||
|
||||
string dataEntry(string prompt, const string *terms, int size) {
|
||||
if (size == 0) { //we return an empty string when we call the function with an empty list
|
||||
return "";
|
||||
}
|
||||
|
||||
string entry;
|
||||
do {
|
||||
printMenu(terms, size);
|
||||
cout << prompt;
|
||||
|
||||
cin >> entry;
|
||||
}
|
||||
while( !checkEntry(entry, terms, size) );
|
||||
|
||||
int number = atoi(entry.c_str());
|
||||
return terms[number - 1];
|
||||
void print_menu(const std::vector<std::string>& terms)
|
||||
{
|
||||
for (size_t i = 0; i < terms.size(); i++) {
|
||||
std::cout << i + 1 << ") " << terms[i] << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
void printMenu(const string *terms, int num) {
|
||||
for (int i = 1 ; i < num + 1 ; i++) {
|
||||
cout << i << ')' << terms[ i - 1 ] << '\n';
|
||||
}
|
||||
int parse_entry(const std::string& entry, int max_number)
|
||||
{
|
||||
int number = std::stoi(entry);
|
||||
if (number < 1 || number > max_number) {
|
||||
throw std::invalid_argument("");
|
||||
}
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
bool checkEntry(string myEntry, const string *terms, int num) {
|
||||
boost::regex e("^\\d+$");
|
||||
if (!boost::regex_match(myEntry, e))
|
||||
return false;
|
||||
int number = atoi(myEntry.c_str());
|
||||
if (number < 1 || number > num)
|
||||
return false;
|
||||
return true;
|
||||
std::string data_entry(const std::string& prompt, const std::vector<std::string>& terms)
|
||||
{
|
||||
if (terms.empty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int choice;
|
||||
while (true) {
|
||||
print_menu(terms);
|
||||
std::cout << prompt;
|
||||
|
||||
std::string entry;
|
||||
std::cin >> entry;
|
||||
|
||||
try {
|
||||
choice = parse_entry(entry, terms.size());
|
||||
return terms[choice - 1];
|
||||
} catch (std::invalid_argument&) {
|
||||
// std::cout << "Not a valid menu entry!" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main( ) {
|
||||
const string terms[ ] = { "fee fie" , "huff and puff" , "mirror mirror" , "tick tock" };
|
||||
int size = sizeof terms / sizeof *terms;
|
||||
cout << "You chose: " << dataEntry("Which is from the three pigs: ", terms, size);
|
||||
return 0;
|
||||
int main()
|
||||
{
|
||||
std::vector<std::string> terms = {"fee fie", "huff and puff", "mirror mirror", "tick tock"};
|
||||
std::cout << "You chose: " << data_entry("> ", terms) << std::endl;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,8 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Menu
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
List<string> menu_items = new List<string>() { "fee fie", "huff and puff", "mirror mirror", "tick tock" };
|
||||
|
|
@ -23,3 +28,4 @@
|
|||
} while (!int.TryParse(input, out i) || i >= items.Count || i < 0);
|
||||
return items[i];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
21
Task/Menu/Elixir/menu.elixir
Normal file
21
Task/Menu/Elixir/menu.elixir
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
defmodule Menu do
|
||||
def select(_, []), do: ""
|
||||
def select(prompt, items) do
|
||||
IO.puts ""
|
||||
Enum.with_index(items) |> Enum.each(fn {item,i} -> IO.puts " #{i}. #{item}" end)
|
||||
answer = IO.gets("#{prompt}: ") |> String.strip
|
||||
case Integer.parse(answer) do
|
||||
{num, ""} when num in 0..length(items)-1 -> Enum.at(items, num)
|
||||
_ -> select(prompt, items)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# test empty list
|
||||
response = Menu.select("Which is empty", [])
|
||||
IO.puts "empty list returns: #{inspect response}"
|
||||
|
||||
# "real" test
|
||||
items = ["fee fie", "huff and puff", "mirror mirror", "tick tock"]
|
||||
response = Menu.select("Which is from the three pigs", items)
|
||||
IO.puts "you chose: #{inspect response}"
|
||||
73
Task/Menu/PowerShell/menu.psh
Normal file
73
Task/Menu/PowerShell/menu.psh
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
function Select-TextItem
|
||||
{
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Prints a textual menu formatted as an index value followed by its corresponding string for each object in the list.
|
||||
.DESCRIPTION
|
||||
Prints a textual menu formatted as an index value followed by its corresponding string for each object in the list;
|
||||
Prompts the user to enter a number;
|
||||
Returns an object corresponding to the selected index number.
|
||||
.PARAMETER InputObject
|
||||
An array of objects.
|
||||
.PARAMETER Prompt
|
||||
The menu prompt string.
|
||||
.EXAMPLE
|
||||
“fee fie”, “huff and puff”, “mirror mirror”, “tick tock” | Select-TextItem
|
||||
.EXAMPLE
|
||||
“huff and puff”, “fee fie”, “tick tock”, “mirror mirror” | Sort-Object | Select-TextItem -Prompt "Select a string"
|
||||
.EXAMPLE
|
||||
Select-TextItem -InputObject (Get-Process)
|
||||
.EXAMPLE
|
||||
(Get-Process | Where-Object {$_.Name -match "notepad"}) | Select-TextItem -Prompt "Select a Process" | Stop-Process -ErrorAction SilentlyContinue
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipeline=$true)]
|
||||
$InputObject,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]
|
||||
$Prompt = "Enter Selection"
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
$menuOptions = @()
|
||||
}
|
||||
Process
|
||||
{
|
||||
$menuOptions += $InputObject
|
||||
}
|
||||
End
|
||||
{
|
||||
do
|
||||
{
|
||||
[int]$optionNumber = 1
|
||||
|
||||
foreach ($option in $menuOptions)
|
||||
{
|
||||
Write-Host ("{0,3}: {1}" -f $optionNumber,$option)
|
||||
|
||||
$optionNumber++
|
||||
}
|
||||
|
||||
Write-Host ("{0,3}: {1}" -f 0,"To cancel")
|
||||
|
||||
[int]$choice = Read-Host $Prompt
|
||||
|
||||
$selectedValue = ""
|
||||
|
||||
if ($choice -gt 0 -and $choice -le $menuOptions.Count)
|
||||
{
|
||||
$selectedValue = $menuOptions[$choice - 1]
|
||||
}
|
||||
}
|
||||
until ($choice -eq 0 -or $choice -le $menuOptions.Count)
|
||||
|
||||
return $selectedValue
|
||||
}
|
||||
}
|
||||
|
||||
“fee fie”, “huff and puff”, “mirror mirror”, “tick tock” | Select-TextItem -Prompt "Select a string"
|
||||
|
|
@ -1,38 +1,37 @@
|
|||
/*REXX program shows a list, asks user for a selection number (integer).*/
|
||||
/*REXX program displays a list, then prompts the user for a selection number (integer).*/
|
||||
do forever /*keep prompting until response is OK. */
|
||||
call list_create /*create the list from scratch. */
|
||||
call list_show /*display (show) the list to the user.*/
|
||||
if #==0 then return '' /*if list is empty, then return null.*/
|
||||
say right(' choose an item by entering a number from 1 ───►' #, 70, '═')
|
||||
parse pull x /*get the user's choice (if any). */
|
||||
|
||||
do forever /*keep asking until response OK. */
|
||||
call list_create /*create the list from scratch. */
|
||||
call list_show /*display (show) the list to user*/
|
||||
if #==0 then return '' /*if empty list, then return null*/
|
||||
say right(' choose an item by entering a number from 1 ───►' #, 70, '═')
|
||||
parse pull x /*get the user's choice (if any).*/
|
||||
|
||||
select
|
||||
when x='' then call sayErr "a choice wasn't entered"
|
||||
when words(x)\==1 then call sayErr 'too many choices entered:'
|
||||
when \datatype(x,'N') then call sayErr "the choice isn't numeric:"
|
||||
when \datatype(x,'W') then call sayErr "the choice isn't an integer:"
|
||||
when x<1 | x># then call sayErr "the choice isn't within range:"
|
||||
otherwise leave /*this leaves the DO FOREVER loop*/
|
||||
end /*select*/
|
||||
end /*forever*/
|
||||
/*user might've entered 2. or 003*/
|
||||
x=x/1 /*normalize the number (maybe). */
|
||||
select
|
||||
when x='' then call sayErr "a choice wasn't entered"
|
||||
when words(x)\==1 then call sayErr 'too many choices entered:'
|
||||
when \datatype(x,'N') then call sayErr "the choice isn't numeric:"
|
||||
when \datatype(x,'W') then call sayErr "the choice isn't an integer:"
|
||||
when x<1 | x># then call sayErr "the choice isn't within range:"
|
||||
otherwise leave /*this leaves the DO FOREVER loop.*/
|
||||
end /*select*/
|
||||
end /*forever*/
|
||||
/*user might've entered 2. or 003 */
|
||||
x=x/1 /*normalize the number (maybe). */
|
||||
say; say 'you chose item' x": " #.x
|
||||
return #.x /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────LIST_CREATE─────────────────────────*/
|
||||
list_create: #.1='fee fie' /*one method for list-building. */
|
||||
#.2='huff and puff'
|
||||
#.3='mirror mirror'
|
||||
#.4='tick tock'
|
||||
#=4 /*store number of choices in #. */
|
||||
return /*(above) is just one convention.*/
|
||||
/*──────────────────────────────────LIST_SHOW───────────────────────────*/
|
||||
list_show: say /*display a blank line. */
|
||||
do j=1 for # /*display the list of choices. */
|
||||
say '[item' j"] " #.j /*display item # with its choice.*/
|
||||
end /*j*/
|
||||
say /*display another blank line. */
|
||||
return
|
||||
/*──────────────────────────────────SAYERR──────────────────────────────*/
|
||||
sayErr: say; say '***error!***' arg(1) x; say; return
|
||||
return #.x /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
list_create: #.1= 'fee fie' /*this is one method for list-building.*/
|
||||
#.2= 'huff and puff'
|
||||
#.3= 'mirror mirror'
|
||||
#.4= 'tick tock'
|
||||
#=4 /*store the number of choices in # */
|
||||
return /*(above) is just one convention. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
list_show: say /*display a blank line. */
|
||||
do j=1 for # /*display the list of choices. */
|
||||
say '[item' j"] " #.j /*display item number with its choice. */
|
||||
end /*j*/
|
||||
say /*display another blank line. */
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sayErr: say; say '***error***' arg(1) x; say; return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue