Integer comparison: Difference between revisions
No edit summary |
|||
Line 74: | Line 74: | ||
c |
c |
||
end |
end |
||
==[[Perl]]== |
|||
[[Category:Perl]] |
|||
'''Interpreter:''' [[Perl]] 5.x |
|||
Seperate tests for less than, greater than, and equals |
|||
sub test_num($ $){ |
|||
my ($f, $s) = @_; |
|||
if ($f < $s){ |
|||
return(-1); # returns -1 if $f is less than $s |
|||
} elsif ($f > $s){ |
|||
return(1); # returns 1 if $f is greater than $s |
|||
} elsif ($f == $s){ # note = is a set, == is a test |
|||
return(0); # returns 0 $f is equal to $s |
|||
} |
|||
} |
|||
All three tests in one. if $f is less than $s return -1, greater than return 1, equal to return 0 |
|||
sub test_num($ $){ |
|||
my ($f, $s) = @_; |
|||
return($f <=> $s); |
|||
} |
|||
Note in perl, $a and $b are reserved variable names, used in conjunction with the sort() function. |
Revision as of 14:18, 2 April 2007
You are encouraged to solve this task according to the task description, using any language you may know.
Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:
Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses
Get two integers from the user, and then output if the first one is less, equal or greater than the other. Test the condition for each case separately, so that all three comparison operators are used in the code.
Ada
with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_Io; procedure Compare_Ints is A, B : Integer; begin Get(Item => A); Get(Item => B); -- Test for equality if A = B then Put_Line("A equals B"); end if; -- Test For Less Than if A < B then Put_Line("A is less than B"); end if; -- Test For Greater Than if A > B then Put_Line("A is greater than B"); end if; end Compare_Ints;
C++
#include <iostream> #include <istream> #include <ostream> int main() { int a, b; std::cin >> a >> b; // test for less-than if (a < b) std::cout << a << " is less than " << b << "\n"; // test for equality if (a == b) std::cout << a << " is equal to " << b << "\n"; // test for greater-than if (a > b) std::cout << a << " is greater than " << b << "\n"; }
Fortran
program compare integer a, b read(*,*) a, b c c test for less-than if (a .lt. b) then write(*, *) a, ' is less than ', b end if c c test for equality if (a .eq. b) then write(*, *) a, ' is equal to ', b end if c c test for greater-than if (a .gt. b) then write(*, *) a, ' is greater than ', b end if c end
Perl
Interpreter: Perl 5.x
Seperate tests for less than, greater than, and equals
sub test_num($ $){ my ($f, $s) = @_; if ($f < $s){ return(-1); # returns -1 if $f is less than $s } elsif ($f > $s){ return(1); # returns 1 if $f is greater than $s } elsif ($f == $s){ # note = is a set, == is a test return(0); # returns 0 $f is equal to $s } }
All three tests in one. if $f is less than $s return -1, greater than return 1, equal to return 0
sub test_num($ $){ my ($f, $s) = @_; return($f <=> $s); }
Note in perl, $a and $b are reserved variable names, used in conjunction with the sort() function.