This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,44 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. Int-Arithmetic.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC S9(10).
01 B PIC S9(10).
01 Result PIC S9(10).
PROCEDURE DIVISION.
DISPLAY "First number: " WITH NO ADVANCING
ACCEPT A
DISPLAY "Second number: " WITH NO ADVANCING
ACCEPT B
* *> Note: The various ADD/SUBTRACT/etc. statements can be
* *> replaced with COMPUTE statements, which allow those
* *> operations to be defined similarly to other languages,
* *> e.g. COMPUTE Result = A + B
ADD A TO B GIVING Result
DISPLAY "A + B = " Result
SUBTRACT B FROM A GIVING Result
DISPLAY "A - B = " Result
MULTIPLY A BY B GIVING Result
DISPLAY "A * B = " Result
* *> Division here truncates towards zero. DIVIDE can take a
* *> ROUNDED clause, which will round the result to the nearest
* *> integer.
DIVIDE A BY B GIVING Result
DISPLAY "A / B = " Result
COMPUTE Result = A ^ B
DISPLAY "A ^ B = " Result
* *> Matches sign of first argument.
DISPLAY "A % B = " FUNCTION REM(A, B)
GOBACK
.

View file

@ -0,0 +1,28 @@
MODULE Arithmetic;
IMPORT CPmain,Console,RTS;
VAR
x,y : INTEGER;
arg : ARRAY 128 OF CHAR;
status : BOOLEAN;
PROCEDURE Error(IN str : ARRAY OF CHAR);
BEGIN
Console.WriteString(str);Console.WriteLn;
HALT(1)
END Error;
BEGIN
IF CPmain.ArgNumber() < 2 THEN Error("Give me two integers!") END;
CPmain.GetArg(0,arg); RTS.StrToInt(arg,x,status);
IF ~status THEN Error("Can't convert '"+arg+"' to Integer") END;
CPmain.GetArg(1,arg); RTS.StrToInt(arg,y,status);
IF ~status THEN Error("Can't convert '"+arg+"' to Integer") END;
Console.WriteString("x + y >");Console.WriteInt(x + y,6);Console.WriteLn;
Console.WriteString("x - y >");Console.WriteInt(x - y,6);Console.WriteLn;
Console.WriteString("x * y >");Console.WriteInt(x * y,6);Console.WriteLn;
Console.WriteString("x / y >");Console.WriteInt(x DIV y,6);Console.WriteLn;
Console.WriteString("x MOD y >");Console.WriteInt(x MOD y,6);Console.WriteLn;
END Arithmetic.

View file

@ -0,0 +1,33 @@
MODULE Arithmetic;
IMPORT StdLog,DevCommanders,TextMappers;
PROCEDURE DoArithmetic(x,y: INTEGER);
BEGIN
StdLog.String("x + y >");StdLog.Int(x + y);StdLog.Ln;
StdLog.String("x - y >");StdLog.Int(x - y);StdLog.Ln;
StdLog.String("x * y >");StdLog.Int(x * y);StdLog.Ln;
StdLog.String("x / y >");StdLog.Int(x DIV y);StdLog.Ln;
StdLog.String("x MOD y >");StdLog.Int(x MOD y);StdLog.Ln;
END DoArithmetic;
PROCEDURE Go*;
VAR
params: DevCommanders.Par;
s: TextMappers.Scanner;
p : ARRAY 2 OF INTEGER;
current: INTEGER;
BEGIN
current := 0;
params := DevCommanders.par;
s.ConnectTo(params.text);
s.SetPos(params.beg);
s.Scan;
WHILE(~s.rider.eot) DO
IF (s.type = TextMappers.int) THEN
p[current] := s.int; INC(current);
END;
s.Scan;
END;
IF current = 2 THEN DoArithmetic(p[0],p[1]) END;
END Go;
END Arithmetic.

View file

@ -1,14 +1,17 @@
#define std'basic'*.
#define math'* = std'math'*.
#define system.
#define extensions'io.
#define extensions'math.
#symbol Program =
// --- Program ---
#symbol program =
[
#var a := 'program'input >> Integer.
#var b := 'program'input >> Integer.
#var a := consoleEx readLine:(Integer new).
#var b := consoleEx readLine:(Integer new).
'program'output << a << " + " << b << " = " << a + b << "%n".
'program'output << a << " - " << b << " = " << a - b << "%n".
'program'output << a << " * " << b << " = " << a * b << "%n".
'program'output << a << " / " << b << " = " << a / b << "%n". // truncates towards 0
'program'output << a << " %% " << b << " = " << a~math'eops math'modulus:b << "%n". // matches sign of first operand
consoleEx << a << " + " << b << " = " << a + b << "%n".
consoleEx << a << " - " << b << " = " << a - b << "%n".
consoleEx << a << " * " << b << " = " << a * b << "%n".
consoleEx << a << " / " << b << " = " << a / b << "%n". // truncates towards 0
consoleEx << a << " %% " << b << " = " << (modulus:a:b) << "%n". // matches sign of first operand
].

View file

@ -0,0 +1,12 @@
MODULE Arithmetic;
IMPORT In, Out;
VAR
x,y:INTEGER;
BEGIN
Out.String("Give two numbers: ");In.Int(x);In.Int(y);
Out.String("x + y >");Out.Int(x + y,6);Out.Ln;
Out.String("x - y >");Out.Int(x - y,6);Out.Ln;
Out.String("x * y >");Out.Int(x * y,6);Out.Ln;
Out.String("x / y >");Out.Int(x DIV y,6);Out.Ln;
Out.String("x MOD y >");Out.Int(x MOD y,6);Out.Ln;
END Arithmetic.