N'th: Difference between revisions

708 bytes added ,  11 months ago
→‎{{header|REXX}}: Refurbished and two alternate solutions added to the oiginal
(→‎{{header|REXX}}: Refurbished and two alternate solutions added to the oiginal)
Line 3,525:
This version adds suffixes without apostrophes.
 
Negative numbers and fractions are also handled.
 
<syntaxhighlight lang="rexx">/*REXX program shows ranges of numbers with ordinal (st/nd/rd/th) suffixes attached.*/
Two alternate solutions added to the oiginal
call tell 0, 25 /*display the 1st range of numbers. */
<syntaxhighlight lang="rexx">/*REXX program shows ranges of numbers with ordinal (st/nd/rd/th) suffixes attached.*/
call tell 250, 265 /* " " 2nd " " " */
callCall tell 10000, 1025 25 /* display " " 3rd " " the "1st range of numbers */
exitCall tell 250,265 /* " " 2nd " " /*stick a fork in it," we're all done. */
callCall tell 1000,1025 0, 25 /* " " 3rd " /*display the " 1st " range of numbers. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell:Exit procedure; parse arg L,H,,$ /*get thestick Lowa andfork Highin #sit, nullify listwe're all done */
/*-------------------------------------------------------------------------*/
do j=L to H; $=$ th(j); end /*process the range, from low ───► high*/
tell: Procedure
say 'numbers from ' L " to " H ' (inclusive):' /*display the title. */
Parse Arg low,high say strip($); say; say /* get the Low and High numbers /*display line; 2 sep*/
out.=''
return /*return to invoker. */
Do n=low To high do j=L to H; $=$ th(j); end /* process the range, from low ───► high */
/*──────────────────────────────────────────────────────────────────────────────────────*/
out.1=out.1 th(n)
th: parse arg z; x=abs(z); return z||word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))</syntaxhighlight>
out.2=out.2 th2(n)
out.3=out.3 th3(n)
End
saySay 'numbers from' low ' L " to' " H high ' (inclusive):' /*display the title.output */
Say strip(out.1)
Say ''
If out.2<>out.1 Then Say 'th2 must be wrong'
If out.3<>out.1 Then Say 'th3 must be wrong'
Return
/*-------------------------------------------------------------------------*/
callth: tell 250, 265 /* compact " " original 2nd " " " */
Parse Arg z
x=abs(z)
th: parse arg z; x=abs(z); returnReturn z||word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))</syntaxhighlight>
/*-------------------------------------------------------------------------*/
th2: /* rather verbose logic */
Parse Arg z
x=abs(z)
Select
When length(x)=1 Then
If x<4 Then
suffix=word('th st nd rd',x+1)
Else
suffix='th'
When suffixstr(x,length(x)-1,1)=1 Then
suffix='th'
When right(x,1)<4 Then
suffix=word('th st nd rd',right(x,1)+1)
Otherwise
suffix='th'
End
Return z||suffix
/*-------------------------------------------------------------------------*/
th3: /* compact yet quite readable */
Parse Arg z
Parse Value reverse(z) with last +1 prev +1
If last<4 &, /* last digit is 0,1,2,3 */
prev<>1 Then /* and number is not **1x */
suffix=word('th st nd rd',last+1)
Else return /* all oher cases /*return to invoker. */
suffix='th'
Return z||suffix</syntaxhighlight>
'''output''' &nbsp; using the default inputs:
<pre>
2,295

edits