Jump to content

Set of real numbers: Difference between revisions

→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the output section.
(→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the output section.)
Line 1,647:
=={{header|REXX}}==
===no error checking, no ∞===
<lang rexx>/*REXX pgmprogram demonstrates a way to represent any set of real #snumbers and usage. */
call set_queryquertySet 1, 3, '(0,[1] union [0,2)'
call set_queryquertySet , , '[0,2) union (1,3)'
call set_queryquertySet , , '[0,3]1) - union (02,1)3]'
call set_queryquertySet , , '[0,32] inter - [0(1,1]3)'
exitcall quertySet , , '(1,2) /*stick a fork in it(2, we3]'re done.*/
call quertySet , , '[0,2) \ (1,3)'
/*──────────────────────────────────SET_empty subroutine────────────────*/
say; say center(' start of required tasks ', 40, "═")
set_empty: parse arg _; nam=set_val(_,00); return @.3>@.4
call quertySet , , '(0,1] union [0,2)'
/*──────────────────────────────────SET_ISIN subroutine─────────────────*/
set_isin:call quertySet , parse arg, # '[0,x;2) call set_val x (1,3)'
call quertySet , , '[0,3] - (0,1)'
if \datatype(#,'N') then call set_bad "number isn't not numeric:" #
call quertySet , , '[0,3] - if (@.[0,1==]'(' & #<=@.2) |,
exit (@.1=='[' & #< @.2) | /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
(@.4==')' & #>=@.3) |,
emptySet: parse arg _; (@.4=nam=']' &valSet(_, 00); #> @.3) then return 0@.3>@.4
/*──────────────────────────────────────────────────────────────────────────────────────*/
return 1
isInSet: parse arg #,x; call valSet x
/*──────────────────────────────────SET_query subroutine────────────────*/
if \datatype(#, 'N') then call set_bad "number isn't not numeric:" #
set_query: parse arg lv,hv,s1 oop s2 .; op=oop; upper op; cop=
if lv=='' then lv=0; if hv(@.1=='(' & then hv#<=@.2;) if op=='' then cop=0|,
if wordpos(op,'| or UNION') \(@.1==0'[' & #< @.2) then cop='|',
(@.4==')' & #>=@.3) |,
if wordpos(op,'& ∩ AND INTER INTERSECTION') \==0 then cop='&'
if wordpos(op,'\ - DIF DIFF DIFFERENCE') \==0 (@.4==']' & #> @.3) then cop='\'return 0
return 1
say
/*──────────────────────────────────────────────────────────────────────────────────────*/
do i=lv to hv; b =set_isin(i,s1)
quertySet: parse arg lv,hv,s1 oop s2 .; op=oop; upper op; cop=
if cop\==0 then do
if lv=='' then lv=0; if hv=='' then b2hv=set_isin(i,s2) 2; if op=='' then cop= 0
if wordpos(op, '| or UNION') if cop \=='&'0 then bcop=b & b2"|"
if wordpos(op, '& ∩ AND INTER INTERSECTION') \==0 if cop=='|' then bcop=b | b2"&"
if wordpos(op, '\ - DIF DIFF DIFFERENCE') \==0 if cop=='\' then bcop=b & "\b2"
endsay
do i=lv to hv; b = isInSet(i, s1)
express = s1 center(oop,max(5,length(oop))) s2
say right(i,5) ' is in set' express": " word('noif yes',b+1)cop\==0 then do
end /* b2= isInSet(i*/, s2)
if cop=='&' then b= b & b2
return
if cop=='|' then b= b | b2
/*──────────────────────────────────SET_VAL subroutine──────────────────*/
set_val: parse arg q; q=space(q,0); L=length(q); @.0 if cop==',\' then b= b & \b2
end
@.4=right(q,1)
express = s1 center(oop, max(5, length(oop) ) ) s2
parse var q @.1 2 @.2 ',' @.3 (@.4)
say right(i, 5) ' is in set' express": " word('no yes', b+1)
if @.2>@.3 then parse var L . @.0 @.2 @.3
end /*i*/
return space(@.1 @.2 @.0 @.3 @.4,0)</lang>
return
'''output''' is the same as version 2
/*──────────────────────────────────────────────────────────────────────────────────────*/
valSet: parse arg q; q=space(q, 0); L=length(q); @.0= ','; @.4= right(q,1)
parse var q @.1 2 @.2 ',' @.3 (@.4)
if @.2>@.3 then parse var L . @.0 @.2 @.3
return space(@.1 @.2 @.0 @.3 @.4, 0)</lang>
{{out|output|text=&nbsp; is the same as the next REXX version (below).}}
 
===has error checking, ∞ support===
<lang rexx>/*REXX pgmprogram demonstrates a way to represent any set of real #snumbers and usage. */
call set_queryquertySet 1, 3, '(0,[1] union [0,2)'
call set_queryquertySet , , '[0,2) union (1,3)'
call set_queryquertySet , , '[0,3]1) - union (02,1)3]'
call set_queryquertySet , , '[0,32] inter - [0(1,1]3)'
exitcall quertySet , , '(1,2) /*stick a fork in it(2, we3]'re done.*/
call quertySet , , '[0,2) \ (1,3)'
/*──────────────────────────────────SET_BAD subroutine─────────-────────*/
say; say center(' start of required tasks ', 40, "═")
set_bad: say; say '***error!*** bad format of SET_def: ('arg(1)")"; exit
call quertySet , , '(0,1] union [0,2)'
/*──────────────────────────────────SET_empty subroutine────────────────*/
set_empty:call quertySet , parse arg _;, '[0,2) nam=set_val(_,00); return @.(1,3>@.4)'
call quertySet , , '[0,3] - (0,1)'
/*──────────────────────────────────SET_ISIN subroutine─────────────────*/
set_isin:call quertySet , parse arg, # '[0,x;3] call- set_val x [0,1]'
exit /*stick a fork in it, we're all done. */
if \datatype(#,'N') then call set_bad "number isn't not numeric:" #
/*──────────────────────────────────────────────────────────────────────────────────────*/
if (@.1=='(' & #<=@.2) |,
badSet: say; say '***error*** bad format of SET_def: ('arg(1)")"; (@.1=='[' & #< @.2) |,exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
(@.4==')' & #>=@.3) |,
emptySet: parse arg _; (@.4=nam=']' &valSet(_, 00); #> @.3) then return 0@.3>@.4
/*──────────────────────────────────────────────────────────────────────────────────────*/
return 1
isInSet: parse arg #,x; call valSet x
/*──────────────────────────────────SET_query subroutine────────────────*/
if \datatype(#, 'N') then call set_bad "number isn't not numeric:" #
set_query: parse arg lv,hv,s1 oop s2 .; op=oop; upper op; cop=
if lv=='' then lv=0; if hv(@.1=='(' & then hv#<=@.2;) if op=='' then cop=0|,
if wordpos(op,'| or UNION') \(@.1==0'[' & #< @.2) then cop='|',
(@.4==')' & #>=@.3) |,
if wordpos(op,'& ∩ AND INTER INTERSECTION') \==0 then cop='&'
if wordpos(op,'\ - DIF DIFF DIFFERENCE') \==0 (@.4==']' & #> @.3) then cop='\'return 0
return 1
if cop=='' then call set_bad 'invalid operation:' oop
/*──────────────────────────────────────────────────────────────────────────────────────*/
say
quertySet: parse arg lv,hv,s1 oop s2 .; op=oop; upper op; cop=
do i=lv to hv; b =set_isin(i,s1)
if cop\lv=='' then lv=0; if hv=='' then dohv= 2; if op=='' then cop= 0
if wordpos(op, '| or UNION') b2 \=set_isin(i,s2)=0 then cop= "|"
if wordpos(op, '& ∩ AND INTER INTERSECTION') \==0 if cop=='&' then bcop=b "& b2"
if wordpos(op, '\ - DIF DIFF DIFFERENCE') \==0 if cop=='|' then bcop=b | b2"\"
if cop=='\' then b=b & \b2say
do i=lv to hv; endb = isInSet(i, s1)
if cop\==0 then do
express = s1 center(oop,max(5,length(oop))) s2
say right(i,5) ' is in set' express": " word('no yes' b2= isInSet(i,b+1 s2)
if cop=='&' then b= b & b2
end /*i*/
if cop=='|' then b= b | b2
return
if cop=='\' then b= b & \b2
/*──────────────────────────────────SET_VAL subroutine──────────────────*/
set_val: parse arg q; q=space(q,0); L=length(q); @.0=',' end
express = s1 center(oop, max(5, length(oop) ) ) s2
infinity = copies(9,digits()-1)'e'copies(9,digits()-1)'0'
if L<2 say right(i, 5) ' is in set' then call set_badexpress": " word('invalidno expressionyes', b+1)
end /*i*/
@.4=right(q,1)
parse var q @.1 2 @.2 ',' @.3 (@.4) return
/*──────────────────────────────────────────────────────────────────────────────────────*/
if @.1\=='(' & @.1\=='[' then call set_bad 'left boundry'
valSet: parse arg q; q=space(q, 0); L= length(q); @.0= ','
if @.4\==')' & @.4\==']' then call set_bad 'right boundry'
infinity = copies(9, digits() - 1)'e'copies(9, digits() - 1)0
if L<2 then call set_bad 'invalid expression'
@.4= right(q, 1)
parse var q @.1 2 @.2 ',' @.3 (@.4)
if @.1\=='(' & @.1\=="[" then call set_bad 'left boundry'
if @.4\==')' & @.4\=="]" then call set_bad 'right boundry'
do j=2 to 3; u=@.j; upper u
if right(@.j, 1)=='∞' | u="INFINITY" then @.j= '-'infinity
if \datatype(@.j, 'N') then call set_bad "value not numeric:" @.j
end /*j*/
if @.2>@.3 then parse var L . @.0 @.2 @.3
return space(@.1 @.2 @.0 @.3 @.4, 0)</lang>
{{out|output|text=&nbsp; when using the (internal) default inputs:}}
<pre>
1 is in set [1,2) : yes
2 is in set [1,2) : no
3 is in set [1,2) : no
 
0 is in set do j=[0,2) union to (1,3; u=@.j; ): upper uyes
1 is in set [0,2) union (1,3): yes
if right(@.j,1)=='∞' | u="INFINITY" then @.j='-'infinity
2 is in set [0,2) union (1,3): yes
if \datatype(@.j,'N') then call set_bad 'value not numeric:' @.j
 
end /*j*/
0 is in set [0,1) union (2,3]: yes
1 is in set [0,1) union (2,3]: no
2 is in set [0,1) union (2,3]: no
 
0 is in set [0,2] inter (1,3): no
1 is in set [0,2] inter (1,3): no
2 is in set [0,2] inter (1,3): yes
 
0 is in set (1,2) ∩ (2,3]: no
1 is in set (1,2) ∩ (2,3]: no
2 is in set (1,2) ∩ (2,3]: no
 
0 is in set [0,2) \ (1,3): yes
1 is in set [0,2) \ (1,3): yes
2 is in set [0,2) \ (1,3): no
 
═══════ start of required tasks ════════
 
if @.2>@.3 then parse var L . @.0 @.2 @.3
return space(@.1 @.2 @.0 @.3 @.4,0)</lang>
'''output'''
<pre style="overflow:scroll">
0 is in set (0,1] union [0,2): yes
1 is in set (0,1] union [0,2): yes
Cookies help us deliver our services. By using our services, you agree to our use of cookies.