2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,5 +1,18 @@
{{basic data operation}} [[Category:Simple]]
Get two integers from the user, and then output if the first one is less than, equal to, or greater than the other.
Test the condition ''for each case separately'', so that ''all three comparison operators are used'' in the code.
{{basic data operation}}
[[Category:Simple]]
See also: [[String comparison]]
Get two integers from the user.
Then,   display a message if the first integer is:
::::*   less than,
::::*   equal to,   or
::::*   greater than
the second integer.
Test the condition   ''for each case separately'',   so that   ''all three comparison operators are used''   in the code.
;Related task:
*   [[String comparison]]
<br><br>

View file

@ -1,20 +1,23 @@
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
if (!(std::cin >> a >> b)) {
std::cerr << "could not read the numbers\n";
return 1;
}
// test for less-than
if (a < b)
cout << a << " is less than " << b << endl;
std::cout << a << " is less than " << b << "\n";
// test for equality
if (a == b)
cout << a << " is equal to " << b << endl;
std::cout << a << " is equal to " << b << "\n";
// test for greater-than
if (a > b)
cout << a << " is greater than " << b << endl;
std::cout << a << " is greater than " << b << "\n";
}

View file

@ -5,7 +5,8 @@ program IntegerCompare;
var
a, b: Integer;
begin
if (a < b) then Writeln(a, ' is less than ', b);
if (a = b) then Writeln(a, ' is equal to ', b);
if (a > b) then Writeln(a, ' is greater than ', b);
Readln(a, b);
if a < b then Writeln(a, ' is less than ', b);
if a = b then Writeln(a, ' is equal to ', b);
if a > b then Writeln(a, ' is greater than ', b);
end.

View file

@ -0,0 +1,17 @@
#import system.
#import extensions.
#symbol program =
[
#var a := console readLine toInt.
#var b := console readLine toInt.
(a < b)
? [ console writeLine:a:" is less than ":b. ].
(a == b)
? [ console writeLine:a:" equals ":b. ].
(a > b)
? [ console writeLine:a:" is greater than ":b. ].
].

View file

@ -9,3 +9,9 @@ main() ->
N == M ->
io:format("~b is equal to ~b~n",[N,M])
end.
if
N =< M ->
io:format("~b is less than or equal to ~b~n",[N,M]);
N >= M ->
io:format("~b is greater than or equal to ~b~n",[N,M])
end.

View file

@ -1,4 +1,4 @@
def comparison = { a, b ->
def rels = [ (-1) : '<', 0 : '==', 1 : '>' ]
final rels = [ (-1) : '<', 0 : '==', 1 : '>' ].asImmutable()
def comparisonSpaceship = { a, b ->
println "a ? b = ${a} ? ${b} = a ${rels[a <=> b]} b"
}

View file

@ -0,0 +1,10 @@
fun main(args: Array<String>) {
val n1 = readLine()!!.toLong()
val n2 = readLine()!!.toLong()
println(when {
n1 < n2 -> "$n1 is less than $n2"
n1 > n2 -> "$n1 is greater than $n2"
n1 == n2 -> "$n1 is equal to $n2"
else -> ""
})
}

View file

@ -0,0 +1,16 @@
CompareNumbers := proc( )
local a, b;
printf( "Enter a number:> " );
a := parse(readline());
printf( "Enter another number:> " );
b := parse(readline());
if a < b then
printf("The first number is less than the second");
elif a = b then
printf("The first number is equal to the second");
elif a > b then
printf("The first number is greater than the second");
end if;
end proc:
CompareNumbers();

View file

@ -1,25 +1,25 @@
/*REXX program prompts for two integers, compares 'em, tells results. */
numeric digits 1000 /*for the users that go ka-razy. */
a=getInt(' Please enter your first integer:') /*get 1st integer.*/
b=getInt(' Please enter your second integer:') /*get 2nd integer.*/
/*REXX program prompts for two integers, compares them, and displays the results.*/
numeric digits 1000 /*for the users that really go ka─razy.*/
@=copies('', 20) /*eyeball catcher for the user's eyen. */
a=getInt(@ 'Please enter your 1st integer:') /*obtain the 1st integer from the user.*/
b=getInt(@ 'Please enter your 2nd integer:') /* " " 2nd " " " " */
say
if a<b then say @ a ' is less than ' b
if a=b then say @ a ' is equal to ' b
if a>b then say @ a ' is greater than ' b
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
getInt: do forever; say /*keep prompting the user 'til success.*/
say arg(1) /*display the prompt message to console*/
parse pull x /*obtain X, and keep its case intact.*/
select
when x='' then call serr "No argument was entered."
when words(x)>1 then call serr 'Too many arguments entered.'
when \datatype(x,'N') then call serr "Argument isn't numeric:" x
when \datatype(x,'W') then call serr "Argument isn't an integer:" x
if a<b then say a ' is less than ' b
if a=b then say a ' is equal to ' b
if a>b then say a ' is greater than ' b
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────GETINT subroutine───────────────────*/
getInt: procedure /*prompt the CBLF, get an integer*/
/*¬geeks: Carbon-Based Life Form.*/
do forever /*keep prompting until success. */
say; say arg(1) /*display the prompt message. */
parse pull x /*get X, and keep its case intact*/
select
when x='' then call serr 'Nothing was entered.'
when words(x)>1 then call serr 'Too many arguments entered.'
when \datatype(x,'N') then call serr "Argument isn't numeric:" x
when \datatype(x,'W') then call serr "Argument isn't an integer:" x
otherwise return x /*Eureka! Return X to invoker.*/
end /*select*/
end /*forever*/
/*──────────────────────────────────SERR subroutine─────────────────────*/
serr: say '***error!*** ' arg(1); say "Please try again."; return
otherwise return x /* [↑] Eureka! Return # to invoker. */
end /*select*/
end /*forever*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
serr: say @ '***error*** ' arg(1); say @ "Please try again."; return