This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,92 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. Test-Prompt-Menu.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num-Options USAGE UNSIGNED-INT VALUE 4.
01 Example-Menu.
03 Example-Options-Data.
05 FILLER PIC X(30) VALUE "fee fie".
05 FILLER PIC X(30) VALUE "huff and puff".
05 FILLER PIC X(30) VALUE "mirror mirror".
05 FILLER PIC X(30) VALUE "tick tock".
03 Example-Options-Values REDEFINES Example-Options-Data.
05 Example-Options PIC X(30) OCCURS 4 TIMES.
01 Chosen-Option PIC X(30).
PROCEDURE DIVISION.
CALL "Prompt-Menu" USING BY CONTENT Num-Options
BY CONTENT Example-Menu
BY REFERENCE Chosen-Option
DISPLAY "You chose: " Chosen-Option
GOBACK
.
END PROGRAM Test-Prompt-Menu.
IDENTIFICATION DIVISION.
PROGRAM-ID. Prompt-Menu.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 User-Input USAGE UNSIGNED-INT.
01 Input-Flag PIC X.
88 Valid-Input VALUE "Y".
01 Options-Index USAGE UNSIGNED-INT.
01 Index-Display PIC Z(10).
LINKAGE SECTION.
01 Num-Options USAGE UNSIGNED-INT.
01 Menu-Options.
03 Options-Table PIC X(30) OCCURS 0 TO 10000000 TIMES
DEPENDING ON Num-Options.
01 Chosen-Option PIC X(30).
PROCEDURE DIVISION USING Num-Options Menu-Options Chosen-Option.
Main.
IF Num-Options = 0
MOVE SPACES TO Chosen-Option
GOBACK
END-IF
PERFORM UNTIL Valid-Input
PERFORM Display-Menu-Options
DISPLAY "Choose an option: " WITH NO ADVANCING
ACCEPT User-Input
PERFORM Validate-Input
END-PERFORM
MOVE Options-Table (User-Input) TO Chosen-Option
GOBACK
.
Display-Menu-Options.
PERFORM VARYING Options-Index FROM 1 BY 1
UNTIL Num-Options < Options-Index
MOVE Options-Index TO Index-Display
DISPLAY
Index-Display ". " Options-Table (Options-Index)
END-DISPLAY
END-PERFORM
.
Validate-Input.
IF User-Input = 0 OR > Num-Options
DISPLAY "Invalid input."
ELSE
SET Valid-Input TO TRUE
END-IF
.
END PROGRAM Prompt-Menu.

View file

@ -1,34 +1,35 @@
import std.stdio, std.conv, std.string, std.array;
import std.stdio, std.conv, std.string, std.array, std.typecons;
string menuSelect(in string[] entries) {
static int validChoice(in string input,
in int nEntries) pure nothrow {
static Nullable!(int, -1) validChoice(in string input,
in int nEntries)
pure nothrow {
try {
immutable n = to!int(input);
return (n >= 0 && n <= nEntries) ? n : -1;
} catch (Exception e) // very generic
return -1; // not valid
immutable n = input.to!int;
return typeof(return)((n >= 0 && n <= nEntries) ? n : -1);
} catch (Exception e) // Very generic
return typeof(return)(-1); // Not valid.
}
if (entries.empty)
return "";
while (true) {
writeln("Choose one:");
foreach (i, const string entry; entries)
"Choose one:".writeln;
foreach (immutable i, const entry; entries)
writefln(" %d) %s", i, entry);
writef("> ");
immutable input = readln().chomp();
immutable choice = validChoice(input, entries.length-1);
if (choice != -1)
return entries[choice]; // we have a valid choice
"> ".write;
immutable input = readln.chomp;
immutable choice = validChoice(input, entries.length - 1);
if (choice.isNull)
"Wrong choice.".writeln;
else
writeln("Wrong choice.");
return entries[choice]; // We have a valid choice.
}
}
void main() {
immutable items = ["fee fie", "huff and puff",
"mirror mirror", "tick tock"];
writeln("You chose '", menuSelect(items), "'.");
writeln("You chose '", items.menuSelect, "'.");
}

80
Task/Menu/Fortran/menu.f Normal file
View file

@ -0,0 +1,80 @@
!-*- 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
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

View file

@ -0,0 +1,9 @@
let select ?(prompt="Choice? ") = function
| [] -> ""
| choices ->
let rec menu () =
List.iteri (Printf.printf "%d: %s\n") choices;
print_string prompt;
try List.nth choices (read_int ())
with _ -> menu ()
in menu ()

View file

@ -0,0 +1,7 @@
# select ["fee fie"; "huff and puff"; "mirror mirror"; "tick tock"];;
0: fee fie
1: huff and puff
2: mirror mirror
3: tick tock
Choice? 2
- : string = "mirror mirror"