September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,22 +1,24 @@
# syntax: GAWK -f MENU.AWK
BEGIN {
n = split("fee fie:huff and puff:mirror mirror:tick tock",arr,":")
print("you picked:",menu(""))
print("you picked:",menu("fee fie:huff and puff:mirror mirror:tick tock"))
exit(0)
}
function menu(str, ans,arr,i,n) {
if (str == "") {
return
}
n = split(str,arr,":")
while (1) {
print("")
for (i=1; i<=n; i++) {
printf("%d - %s\n",i,arr[i])
}
print("0 - exit")
printf("enter number: ")
printf("? ")
getline ans
if (ans in arr) {
printf("you picked '%s'\n",arr[ans])
continue
}
if (ans == 0) {
break
return(arr[ans])
}
print("invalid choice")
}
exit(0)
}

View file

@ -0,0 +1,30 @@
@echo off & setlocal enabledelayedexpansion
set "menuChoices="fee fie","huff and puff","mirror mirror","tick tock""
call :menu
pause>nul & exit
:menu
if defined menuChoices (
set "counter=0" & for %%a in (%menuChoices%) do (
set /a "counter+=1"
set "currentMenuChoice=%%a"
set option[!counter!]=!currentMenuChoice:"=!
)
)
:tryagain
cls&echo.
for /l %%a in (1,1,%counter%) do echo %%a^) !option[%%a]!
echo.
set /p "input=Choice 1-%counter%: "
echo.
for /l %%a in (1,1,%counter%) do (
if !input! equ %%a echo You chose [ %%a^) !option[%%a]! ] & goto :EOF
)
echo.
echo.Invalid Input. Please try again...
pause
goto :tryagain

View file

@ -1,28 +1,26 @@
@echo off
setlocal enabledelayedexpansion
::The Main Thing...
set choices="fee fie","huff and puff","mirror mirror","tick tock"
set "quest=Which is from the three pigs?"
call :select
call:menu "fee fie" "huff and puff" "mirror mirror" "tick tock"
pause>nul
exit /b 0
::/The Main Thing.
exit /b
::The Function...
:select
set number=0
for %%A in (%choices%) do set tmpvar=%%A&set /a number+=1&set opt!number!=!tmpvar:"=!
:tryagain
cls&echo.
for /l %%A in (1,1,%number%) do echo. Option %%A - !opt%%A!
echo.
set /p input=%quest%
for /l %%A in (1,1,%number%) do (
if !input! equ %%A echo.&echo.You chose option %%A - !opt%%A!&goto :EOF
:menu
cls
setlocal enabledelayedexpansion
set count=0
set reset=endlocal ^& goto menu
:menuloop
for %%i in (%*) do (
set /a count+=1
set string[!count!]=%%~i
echo string[!count!] = %%~i
)
echo.
echo.Invalid Input. Please try again...
pause>nul
goto tryagain
::/The Function.
set /p choice=^>
if "%choice%"=="" %reset%
set "isNum="
for /f "delims=0123456789" %%i in ("%choice%") do set isNum=%%i
if defined isNum %reset%
if %choice% gtr %count% %reset%
echo.!string[%choice%]!
goto:eof

View file

@ -0,0 +1,42 @@
\ Rosetta Code Menu Idiomatic Forth
\ vector table compiler
: CASE: ( -- ) CREATE ;
: | ( -- <text>) ' , ; IMMEDIATE
: ;CASE ( -- ) DOES> SWAP CELLS + @ EXECUTE ;
: NIL ( -- addr len) S" " ;
: FEE ( -- addr len) S" fee fie" ;
: HUFF ( -- addr len) S" huff and puff" ;
: MIRROR ( -- addr len) S" mirror mirror" ;
: TICKTOCK ( -- addr len) S" tick tock" ;
CASE: SELECT ( n -- addr len)
| NIL | FEE | HUFF | MIRROR | TICKTOCK
;CASE
CHAR 1 CONSTANT '1'
CHAR 4 CONSTANT '4'
: BETWEEN ( n low hi -- ?) 1+ WITHIN ;
: MENU ( addr len -- addr len )
DUP 0=
IF
2DROP NIL EXIT
ELSE
BEGIN
CR
CR 2DUP 3 SPACES TYPE
CR ." 1 " 1 SELECT TYPE
CR ." 2 " 2 SELECT TYPE
CR ." 3 " 3 SELECT TYPE
CR ." 4 " 4 SELECT TYPE
CR ." Choice: " KEY DUP EMIT
DUP '1' '4' BETWEEN 0=
WHILE
DROP
REPEAT
-ROT 2DROP \ drop input string
CR [CHAR] 0 - SELECT
THEN
;

View file

@ -0,0 +1,64 @@
\ Rosetta Menu task with Simple lists in Forth
: STRING, ( caddr len -- ) HERE OVER CHAR+ ALLOT PLACE ;
: " ( -- ) [CHAR] " PARSE STRING, ;
: { ( -- ) ALIGN 0 C, ;
: } ( -- ) { ;
: {NEXT} ( str -- next_str) COUNT + ;
: {NTH} ( n array_addr -- str) SWAP 0 DO {NEXT} LOOP ;
: {LEN} ( array_addr -- ) \ count strings in a list
0 >R \ Counter on Rstack
{NEXT} \ skip 1st empty string
BEGIN
{NEXT} DUP C@ \ Fetch length byte
WHILE \ While true
R> 1+ >R \ Inc. counter
REPEAT
DROP
R> ; \ return counter to data stack
: {TYPE} ( $ -- ) COUNT TYPE ;
: '"' ( -- ) [CHAR] " EMIT ;
: {""} ( $ -- ) '"' SPACE {TYPE} '"' SPACE ;
: }PRINT ( n array -- ) {NTH} {TYPE} ;
\ ===== TASK BEGINS =====
CREATE GOODLIST
{ " fee fie"
" huff and puff"
" mirror mirror"
" tick tock" }
CREATE NIL { }
CHAR 1 CONSTANT '1'
CHAR 4 CONSTANT '4'
CHAR 0 CONSTANT '0'
: BETWEEN ( n low hi -- ?) 1+ WITHIN ;
: .MENULN ( n -- n) DUP '0' + EMIT SPACE OVER }PRINT ;
: MENU ( list -- string )
DUP {LEN} 0=
IF
DROP NIL
ELSE
BEGIN
CR
CR 1 .MENULN
CR 2 .MENULN
CR 3 .MENULN
CR 4 .MENULN
CR ." Choice: " KEY DUP EMIT
DUP '1' '4' BETWEEN
0= WHILE
DROP
REPEAT
[CHAR] 0 -
CR SWAP {NTH}
THEN
;

View file

@ -1,80 +1,56 @@
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Mon Jun 3 23:08:36
!
!a=./f && make $a && OMP_NUM_THREADS=2 $a
!gfortran -std=f2008 -Wall -fopenmp -ffree-form -fall-intrinsics -fimplicit-none f.f08 -o f
!
!$ ./f
! Choose fairly a tail
! 1: fee fie
! 2: huff and puff
! 3: mirror mirror
! 4: tick tock
!bad input
! Choose fairly a tail
! 1: fee fie
! 2: huff and puff
! 3: mirror mirror
! 4: tick tock
!^D
!
!STOP Unexpected end of file
!$ ./f
! Choose fairly a tail
! 1: fee fie
! 2: huff and puff
! 3: mirror mirror
! 4: tick tock
!88
! Choose fairly a tail
! 1: fee fie
! 2: huff and puff
! 3: mirror mirror
! 4: tick tock
!-88
! Choose fairly a tail
! 1: fee fie
! 2: huff and puff
! 3: mirror mirror
! 4: tick tock
!3.2
! Choose fairly a tail
! 1: fee fie
! 2: huff and puff
! 3: mirror mirror
! 4: tick tock
!2
! huff and puff
!$
module menu
contains
function selector(title, options, n) result(choice)
integer, optional, intent(in) :: n
character(len=*), intent(in) :: title
character(len=*),dimension(:),intent(in) :: options
!character(len=:), allocatable :: choice ! requires deallocation
!allocate(character(len=8)::choice)
character(len=128) :: choice
integer :: i, L, ios
L = merge(n, size(options), present(n))
if (L .lt. 1) stop 'Silly input'
if (len(choice) .lt. len(options(1))) stop 'menu choices are excessively long'
i = 0
do while ((ios.ne.0) .or. ((i.lt.1) .or. (L.lt.i)))
write(6,*) title
write(6,"(i8,': ',a)")(i,options(i),i=1,L)
read(5,*,iostat=ios,end=666) i
end do
choice = options(i)
return
666 continue
stop 'Unexpected end of file'
end function selector
end module menu
module menu
public :: selector
contains
program menu_demo
use menu
character(len=14), dimension(4) :: items = (/'fee fie ', 'huff and puff ', 'mirror mirror ','tick tock '/)
print*,selector('Choose fairly a tail', items)
end program menu_demo
function selector(title,options) result(choice)
character(len=*),intent(in) :: title
character(len=*),dimension(:),intent(in) :: options
character(len=len(options)) :: choice
integer :: i,ichoose,ios,n
choice = ""
n = size(options)
if (n > 0) then
do
print "(a)",title
print "(i8,"", "",a)",(i,options(i),i=1,n)
read (*,fmt="(i8)",iostat=ios) ichoose
if (ios == -1) exit ! EOF error
if (ios /= 0) cycle ! other error
if (ichoose < 1) cycle
if (ichoose > n) cycle ! out-of-bounds
choice = options(ichoose)
exit
end do
end if
end function selector
end module menu
program menu_demo
use menu
character(len=14),dimension(:),allocatable :: zero_items,fairytale
character(len=len(zero_items)) :: s
!! empty list demo
allocate(zero_items(0))
print "(a)","input items:",zero_items
s = selector('Choose from the empty list',zero_items)
print "(a)","returned:",s
if (s == "") print "(a)","(an empty string)"
!! Fairy tale demo
allocate(fairytale(4))
fairytale = (/'fee fie ','huff and puff ', &
'mirror mirror ','tick tock '/)
print "(a)","input items:",fairytale
s = selector('Choose a fairy tale',fairytale)
print "(a)","returned: ",s
if (s == "") print "(a)","(an empty string)"
end program menu_demo

18
Task/Menu/J/menu.j Normal file
View file

@ -0,0 +1,18 @@
CHOICES =: ];._2 'fee fie;huff and puff;mirror mirror;tick tock;'
PROMPT =: 'Which is from the three pigs? '
showMenu =: smoutput@:(,"1~ (' ' ,.~ 3 ": i.@:(1 ,~ #)))
read_stdin =: 1!:1@:1:
menu =: '? '&$: :(4 : 0)
NB. use: [prompt] menu choice_array
CHOICES =. y
if. 0 = # CHOICES do. return. end.
PROMPT =. x
whilst. RESULT -.@:e. i. # CHOICES do.
showMenu CHOICES
smoutput PROMPT
RESULT =. _1 ". read_stdin''
end.
RESULT {:: CHOICES
)

View file

@ -1,20 +1,36 @@
function select(question, choices) {
var prompt = "";
for (var i in choices)
prompt += i + ". " + choices[i] + "\n";
prompt += question;
const readline = require('readline');
var input;
while (1) {
WScript.Echo(prompt);
input = parseInt( WScript.StdIn.readLine() );
if (0 <= input && input < choices.length)
break;
WScript.Echo("\nTry again.");
}
return input;
async function menuSelect(question, choices) {
if (choices.length === 0) return '';
const prompt = choices.reduce((promptPart, choice, i) => {
return promptPart += `${i + 1}. ${choice}\n`;
}, '');
let inputChoice = -1;
while (inputChoice < 1 || inputChoice > choices.length) {
inputChoice = await getSelection(`\n${prompt}${question}: `);
}
return choices[inputChoice - 1];
}
var choices = ['fee fie', 'huff and puff', 'mirror mirror', 'tick tock'];
var choice = select("Which is from the three pigs?", choices);
WScript.Echo("you selected: " + choice + " -> " + choices[choice]);
function getSelection(prompt) {
return new Promise((resolve) => {
const lr = readline.createInterface({
input: process.stdin,
output: process.stdout
});
lr.question(prompt, (response) => {
lr.close();
resolve(parseInt(response) || -1);
});
});
}
const choices = ['fee fie', 'huff and puff', 'mirror mirror', 'tick tock'];
const question = 'Which is from the three pigs?';
menuSelect(question, choices).then((answer) => {
console.log(`\nYou chose ${answer}`);
});

View file

@ -1,4 +1,8 @@
textMenu[data_List] := (MapIndexed[Print[#2[[1]], ") ", #] &, {a, b, c}];
While[! (IntegerQ[choice] && Length[data] > choice > 0),
choice = Input["Enter selection"]];
data[[choice]])
textMenu[data_List] := Module[{choice},
If[Length@data == 0, Return@""];
While[!(IntegerQ@choice && Length@data >= choice > 0),
MapIndexed[Print[#2[[1]], ") ", #1]&, data];
choice = Input["Enter selection..."]
];
data[[choice]]
]

View file

@ -1,23 +1,27 @@
function menu_select(sequence items, object prompt)
sequence res
integer nres
if length(items)=0 then return 0 end if
while 1 do
for i = 1 to length(items) do
printf(1,"%d) %s\n",{i,items[i]})
end for
puts(1,iff(atom(prompt)?"Choice?":prompt))
res = scanf(trim(gets(0)),"%d")
puts(1,"\n")
if length(res)=1 then
nres = res[1][1]
if nres>0 and nres<=length(items) then exit end if
end if
end while
return nres
sequence res = ""
items = remove_all("",items)
if length(items)!=0 then
while 1 do
for i=1 to length(items) do
printf(1,"%d) %s\n",{i,items[i]})
end for
puts(1,iff(atom(prompt)?"Choice?":prompt))
res = scanf(trim(gets(0)),"%d")
puts(1,"\n")
if length(res)=1 then
integer nres = res[1][1]
if nres>0 and nres<=length(items) then
res = items[nres]
exit
end if
end if
end while
end if
return res
end function
constant items = {"fee fie", "huff and puff", "mirror mirror", "tick tock"}
integer n = menu_select(items,"Which is from the three pigs? ")
printf(1,"You chose %d(%s).\n",{n,items[n]})
constant prompt = "Which is from the three pigs? "
string res = menu_select(items,prompt)
printf(1,"You chose %s.\n",{res})

View file

@ -4,8 +4,8 @@
(for (I . Item) Items
(prinl I ": " Item) )
(prin Prompt " ")
(flush)
(NIL (setq N (in NIL (read))))
(T (>= (length Items) N 1) (get Items N)) ) ) )
(T (>= (length Items) N 1) (prinl (get Items N))) ) ) )
(choose "Which is from the three pigs?"
'("fee fie" "huff and puff" "mirror mirror" "tick tock") )

View file

@ -1,7 +1,9 @@
showmenu <- function()
showmenu <- function(choices = NULL)
{
choices <- c("fee fie", "huff and puff", "mirror mirror", "tick tock")
if (is.null(choices)) return("")
ans <- menu(choices)
if(ans==0) "" else choices[ans]
}
str <- showmenu(c("fee fie", "huff and puff", "mirror mirror", "tick tock"))
str <- showmenu()