📄 ckedemo.ksc
字号:
#!/usr/local/bin/kermit; UNIX: Change previous line to contain full pathname of C-Kermit 7.0 binary.COMMENT - File CKEDEMO.KSC;; Exercises Kermit's programming constructs.; Converted to block-structured format, March 1996.; Updated to C-Kermit 7.0, April 1999.;echo If you don't see the message "Proceeding..."echo on the next line, C-Kermit was not configured for script programming.check ifecho Proceeding...echoswitch \v(program) { :C-Kermit, if ( < \v(version) 70000 ) stop 1 Version 7.0 or later required... echo C-Kermit Programming-Constructs Test break :default stop 1 Sorry - this demo only works with C-Kermit 7.0 or later.}echoecho Defining macros:COMMENT - SPELLNUM macro.;echo { SPELLNUM}define SPELLNUM { local \%x \&a[] dcl \&a[9] = one two three four five six seven eight nine .\&a[0] = zero if ( not def \%1 ) end 1 .\%1 ::= \%1 if ( < \%1 0 ) { .\%x = { minus} .\%1 ::= 0-\%1 } if ( > \%1 9 ) end 1 { Sorry, too hard} echo \%x \&a[\%1]}COMMENT - CALC macro. "Pocket calculator". No arguments.;echo { CALC}define CALC { echo Press Return to exit ; Say how to exit. while 1 { ; Loop until they want to exit ask \%1 { expression: } ; Ask for an expression if ( not def \%1 ) break echo \flpad(\feval(\%1),10) ; Evaluate and print answer } echo Back to... ; All done}echo { ADDINGMACHINE}define ADDINGMACHINE { local total \%s \%x echo Type numbers (one per line) or press Return to quit... assign total 0 ; Initialize the sum while true { ; Loop till done askq \%s ; Wait for a number if ( not def \%s ) break ; Return quits loop .\%x ::= \%s if ( not def \%x ) { echo "\%s" invalid - try again, continue } increment total \%x ; Add it to the sum if fail { echo Can't add "\%s", continue } xecho \flpad(\%s,10)\flpad(\m(total),10) ; Print number and subtotal } echo Total\flpad(\m(total),15,.)}COMMENT - SMALLEST macro, recursive. Arguments:; 1 = a number or expression; 2 = a number or expression; 3 = a number or expression; Prints the smallest of the three.;echo { SMALLEST}def SMALLEST { local \%a \%i \&a[] dcl \&a[3] if ( != \v(argc) 4 ) end 1 { Sorry - three numbers required.} for \%i 1 3 1 { .\&a[\%i] ::= \&_[\%i] if not numeric \&a[\%i] end 1 { Bad number or expression } } if ( < \&a[1] \&a[2] ) { ; Compare first two arguments echo \&a[1] is less than \&a[2] ; The first one is smaller if ( < \&a[1] \&a[3] ) { ; Compare it with the third echo \&a[1] is less than \&a[3] ; The first one is smaller .\%a := \&a[1] ; Copy it to \%a } else { ; The third is smaller echo \&a[1] is not less than \&a[3] .\%a := \&a[3] ; Copy it to \%a } } else { ; Otherwise echo \&a[1] is not less than \&a[2] ; The second is smaller if ( < \&a[2] \&a[3] ) { ; Compare it with the third echo \&a[2] is less than \&a[3] ; The second is smaller .\%a := \&a[2] ; Copy it to \%a } else { ; The third is smaller echo \&a[2] is not less than \&a[3] .\%a := \&a[3] ; Copy it to \%a } } echo So the smallest is \%a. ; Announce the winner}ec Spelling some numbers...for \%i -5 9 1 { spellnum \%i }echo Calculator demo...calcecho Adding machine demo - Enter an empty line to quit...addingmachineCOMMENT - SUM macro, recursive. Argument:; 1 = limit of sum, a positive number.; Returns sum of 1 through the number.;echo { SUM}def SUM { if not def \%1 return ; Make sure there is an argument if not numeric \%1 return ; Make sure argument is numeric if not > \%1 0 return ; Make sure argument is positive if = \%1 1 return 1 ; If argument is 1, the sum is 1 else return \feval(\%1+\fexecute(sum,\feval(\%1-1)))}COMMENT - ADDEMUP macro, for calling SUM.;echo { ADDEMUP}def ADDEMUP { local total assign total \fexec(sum,\%1) if def total echo SUM(\%1) = \m(total) else echo SUM doesn't work for \%1}addemup 1addemup 2addemup 3addemup 4addemup 5addemup 10addemup 20:SMALLESTwhile true { ask \%x { Type 3 numbers separated by spaces or an empty line to quit: } if not def \%x break smallest \%x}echo WHILE-LOOP TEST...echo You should see:echo { 0 1 2 3 4}def \%a 0while < \%a 5 { xecho { \%a}, incr \%a }echoecho NESTED WHILE-LOOP TEST...echo You should see:echo { 0:0 0:1 0:2 1:0 1:1 1:2 2:0 2:1 2:2}def \%a 0while ( < \%a 3 ) { def \%b 0 while ( < \%b 3 ) { xecho { \%a:\%b} incr \%b } incr \%a}echoecho FOR-LOOP INSIDE WHILE-LOOPecho You should see:echo { 1:1 1:2 1:3 2:1 2:2 2:3 3:1 3:2 3:3}def \%a 1while ( < \%a 4 ) { for \%i 1 3 1 { xecho { \%a:\%i} } inc \%a}echoecho WHILE-LOOP INSIDE FOR-LOOPecho You should see:echo { 1:1 1:2 1:3 2:1 2:2 2:3 3:1 3:2 3:3}for \%i 1 3 1 { .\%a = 1 while < \%a 4 { xecho { \%i:\%a} incr \%a }}echoecho NESTED FOR LOOP TESTecho You should see:echo { 1:1 1:2 1:3 2:2 2:3 3:3}for \%i 1 3 1 { for \%j \%i 3 1 { xecho { \%i:\%j} }}echoecho NESTED FOR/WHILE/BREAK/CONTINUE TESTecho You should see:echo { 1:1 1:3 3:1 3:3}for \%i 1 4 1 { if = \%i 2 continue else if = \%i 4 break asg \%j 0 while < \%j 4 { incr \%j if = \%j 2 continue else if = \%j 4 break xecho { \%i:\%j} }}echoecho END from inside nested FOR loopsecho You should see:echo { 1:1 1:2 1:3 2:1 2:2 2:3 3:1}define xx { for \%i 1 3 1 { for \%j 1 3 1 { xecho { \%i:\%j} if = \%i 3 if = \%j 1 end } }}do xxechoecho RETURN from inside nested FOR loopsecho You should see "IT WORKS":define xx { local \%i \%j for \%i 1 3 1 { for \%j 1 3 1 { if = \%i 3 if = \%j 1 return IT \%1 } } echo YOU SHOULD NOT SEE THIS}echo "\fexec(xx WORKS)":IFENDTESTecho END message from inside IFecho You should see "IT WORKS"def xx if = 1 1 { end 0 "IT \%1"}xx WORKSecho Grouping of words in IF EQUALecho You should see "IT WORKS":def \%a one two threeif equal {\%a} {one two three} echo "IT WORKS"else echo It doesn't work, foo.ececho Use of expressions and braces in FOR-loop variablesecho You should see "1 2 3":def \%a 2for \%i 1 { 1 + \%a } 1 { xecho {\%i } }echoecho A macro that echoes its argumentsdef XX { local \%i for \%i 1 { \v(argc) - 1 } 1 { echo \%i. "\&_[\%i]" }}while true { ask \%a {Type some words (or just carriage return to quit): } if not def \%a break xx \%a}echoif not eq {\v(connection)} {remote} forward arraysec MINPUT test...ec Please type one of the following (without the number):ec { 1. ab cd}ec { 2. abcd}ec { 3. xyz}ec You have 10 seconds...minput 10 {ab cd} abcd xyzif success { echo, echo You typed Number \v(minput).}else { echo, echo You did not type any of them within the time limit.}echo:ARRAYSecho ARRAY TEST I (SLOW)...; Note that there are much better ways to do this.; Here we're just testing subscript evaluation, looping, etc.;declare \&a[26]local \%i \%j \%t ; Local variablesassign \%i 1asg \&a[\%i] zebraincr \%iasg \&a[\%i] x-rayincr \%i 1asg \&a[\%i] bakerincr \%i 3-2asg \&a[\%i] abledecr \%i -1asg \&a[\%i] charlieasg \&a[\%i+1] easyasg \&a[\%i+2] georgeasg \&a[\%i+3] dogasg \%n \%i+2+8/4asg \&a[\%n] foxecho Sorting ...for \%i 1 (\%n)-1 1 { ; Outer loop: i from 1 to n-1 for \%j \%i \%n 1 { ; Inner loop: j from i to n if lgt \&a[\%i] \&a[\%j] { ; Compare array elements asg \%t \&a[\%i] ; If out of order, asg \&a[\%i] \&a[\%j] ; exchange them asg \&a[\%j] \%t } }}echo You should see \feval(\%n) words in alphabetical order:for \%i 1 \%n 1 { echo { \%i. \&a[\%i]} } ; All sorted - print themechoecho ARRAY TEST II (FAST)...;; Same thing again the easy (and fast) way...;declare \&a[] = alpha beta gamma delta epsilon zeta eta theta iotaecho Sorting ...sort aecho You should see \fdimension(&a) words in alphabetical order:show array aechoexit 0 End of \v(cmdfile)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -