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,7 +1,18 @@
{{basic data operation}} [[Category:Simple]]
Get two integers from the user, and then output the sum, difference, product, integer quotient and remainder of those numbers.
Don't include error handling.
For quotient, indicate how it rounds (e.g. towards 0, towards negative infinity, etc.).
For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
Also include the exponentiation operator if one exists.
;Task:
Get two integers from the user,   and then (for those two integers), display their:
::::*   sum
::::*   difference
::::*   product
::::*   integer quotient
::::*   remainder
::::*   exponentiation   (if the operator exists)
<br>
Don't include error handling.
For quotient, indicate how it rounds &nbsp; (e.g. towards zero, towards negative infinity, etc.).
For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
<br><br>

View file

@ -0,0 +1,29 @@
package main
import (
"fmt"
"math/big"
)
func main() {
var a, b, c big.Int
fmt.Print("enter two integers: ")
fmt.Scan(&a, &b)
fmt.Printf("%d + %d = %d\n", &a, &b, c.Add(&a, &b))
fmt.Printf("%d - %d = %d\n", &a, &b, c.Sub(&a, &b))
fmt.Printf("%d * %d = %d\n", &a, &b, c.Mul(&a, &b))
// Quo, Rem functions work like Go operators on int:
// quo truncates toward 0,
// and a non-zero rem has the same sign as the first operand.
fmt.Printf("%d quo %d = %d\n", &a, &b, c.Quo(&a, &b))
fmt.Printf("%d rem %d = %d\n", &a, &b, c.Rem(&a, &b))
// Div, Mod functions do Euclidean division:
// the result m = a mod b is always non-negative,
// and for d = a div b, the results d and m give d*y + m = x.
fmt.Printf("%d div %d = %d\n", &a, &b, c.Div(&a, &b))
fmt.Printf("%d mod %d = %d\n", &a, &b, c.Mod(&a, &b))
// as with int, no exponentiation operator
}

View file

@ -0,0 +1,43 @@
# Most of this long script is mere presentation.
# All you really need to do is push two integers onto the stack
# and then execute add, sub, mul, idiv, or pow.
$ClearScreen { # Using ANSI terminal control
`\e[2J\e[1;1H' print flush
} bind def
$Say { # string Say -
`\n' cat print flush
} bind def
$ShowPreamble {
`To show how integer arithmetic in done in Onyx,' Say
`we\'ll use two numbers of your choice, which' Say
`we\'ll call A and B.\n' Say
} bind def
$Prompt { # stack: string --
stdout exch write pop flush
} def
$GetInt { # stack: name -- integer
dup cvs `Enter integer ' exch cat `: ' cat
Prompt stdin readline pop cvx eval def
} bind def
$Template { # arithmetic_operator_name label_string Template result_string
A cvs ` ' B cvs ` ' 5 ncat over cvs ` gives ' 3 ncat exch
A B dn cvx eval cvs `.' 3 ncat Say
} bind def
$ShowResults {
$add `Addition: ' Template
$sub `Subtraction: ' Template
$mul `Multiplication: ' Template
$idiv `Division: ' Template
`Note that the result of integer division is rounded toward zero.' Say
$pow `Exponentiation: ' Template
`Note that the result of raising to a negative power always gives a real number.' Say
} bind def
ClearScreen ShowPreamble $A GetInt $B GetInt ShowResults

View file

@ -8,5 +8,6 @@ echo
"product: ", $a * $b, "\n",
"truncating quotient: ", (int)($a / $b), "\n",
"flooring quotient: ", floor($a / $b), "\n",
"remainder: ", $a % $b, "\n";
"remainder: ", $a % $b, "\n",
"power: ", $a ** $b, "\n"; // PHP 5.6+ only
?>

View file

@ -1,13 +1,13 @@
/*REXX pgm gets 2 integers from the C,L. or via prompt; shows some operations.*/
numeric digits 20 /*#s are round at 20th significant dig.*/
parse arg x y . /*maybe the integers are on the C.L. */
/*REXX program obtains two integers from the C.L. (a prompt); displays some operations.*/
numeric digits 20 /*#s are round at 20th significant dig.*/
parse arg x y . /*maybe the integers are on the C.L. */
do while \datatype(x,'W') | \datatype(y,'W') /*both X and Y must be ints.*/
do while \datatype(x,'W') | \datatype(y,'W') /*both X and Y must be integers. */
say "─────Enter two integer values (separated by blanks):"
parse pull x y . /*accept two items from command line. */
end /*while ··· */
/* [↓] perform this DO loop twice. */
do j=1 for 2 /*show A oper B, then B oper A.*/
parse pull x y . /*accept two thingys from command line.*/
end /*while*/
/* [↓] perform this DO loop twice. */
do j=1 for 2 /*show A oper B, then B oper A.*/
call show 'addition' , "+", x+y
call show 'subtraction' , "-", x-y
call show 'multiplication' , "*", x*y
@ -16,9 +16,9 @@ parse arg x y . /*maybe the integers are on the C.L. */
call show 'division remainder', "//", x//y, ' [sign from 1st operand]'
call show 'power' , "**", x**y
parse value x y with y x /*swap the two values and perform again*/
if j==1 then say copies('', 79) /*display a fence after the 1st round. */
parse value x y with y x /*swap the two values and perform again*/
if j==1 then say copies('', 79) /*display a fence after the 1st round. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
show: parse arg c,o,#,?; say right(c,25)' ' x center(o,4) y ' ' # ?; return
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: parse arg c,o,#,?; say right(c,25)' ' x center(o,4) y " ───► " # ?; return

View file

@ -1,7 +1,7 @@
#lang racket
#lang racket/base
(define (arithmetic x y)
(for ([op '(+ - * / quotient remainder modulo max min gcd lcm)])
(displayln (~a (list op x y) " => "
((eval op (make-base-namespace)) x y)))))
(for ([op (list + - * / quotient remainder modulo max min gcd lcm)])
(printf "~s => ~s\n" `(,(object-name op) ,x ,y) (op x y))))
(arithmetic 8 12)

View file

@ -0,0 +1,7 @@
5 LET a=5: LET b=3
10 PRINT a;" + ";b;" = ";a+b
20 PRINT a;" - ";b;" = ";a-b
30 PRINT a;" * ";b;" = ";a*b
40 PRINT a;" / ";b;" = ";INT (a/b)
50 PRINT a;" mod ";b;" = ";a-INT (a/b)*b
60 PRINT a;" to the power of ";b;" = ";a^b