Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MODULE z;
- FROM InOut IMPORT WriteString, WriteLn, WriteInt, ReadInt, Read;
- FROM RealInOut IMPORT ReadReal, WriteReal;
- VAR
- a, result : REAL;
- n : INTEGER;
- ok, again : BOOLEAN;
- proc : PROCEDURE(REAL,INTEGER,VAR BOOLEAN):REAL;
- smb : CHAR;
- PROCEDURE stepen1(a: REAL; n: INTEGER; VAR ok: BOOLEAN): REAL;
- CONST
- maxr = MAX(REAL);
- VAR
- stepen : REAL;
- BEGIN
- IF (a = 0.0) AND (n <= 0) THEN
- ok := FALSE;
- RETURN 0.0
- ELSE
- ok := TRUE;
- IF (a = 0.0) THEN
- RETURN 0.0
- ELSIF (a = 1.0) OR (n = 0) THEN
- RETURN 1.0
- ELSIF n = 1 THEN
- RETURN a
- ELSE
- IF n < 0 THEN
- a := 1.0 / a;
- n := ABS(n)
- END;
- stepen := 1.0;
- WHILE (n > 0) AND ok DO
- IF ODD(n) THEN
- stepen := stepen * a
- END;
- n := n DIV 2;
- ok := (maxr / a) > a;
- IF (n > 0) AND ok THEN
- a := a * a
- END
- END;
- RETURN stepen;
- END
- END;
- END stepen1;
- PROCEDURE stepen2(a: REAL; n: INTEGER; VAR ok: BOOLEAN): REAL;
- PROCEDURE Sqr(x: REAL): REAL;
- BEGIN
- RETURN x*x
- END Sqr;
- PROCEDURE RekStepen(a: REAL; n: INTEGER): REAL;
- BEGIN
- IF n = 1 THEN
- RETURN a
- ELSIF ODD(n) THEN
- RETURN a * Sqr(RekStepen(a,n DIV 2))
- ELSE
- RETURN Sqr(RekStepen(a,n DIV 2))
- END;
- END RekStepen;
- BEGIN
- IF (a = 0.0) AND (n <= 0) THEN
- ok := FALSE;
- RETURN 0.0
- ELSE
- ok := TRUE;
- IF a = 0.0 THEN
- RETURN 0.0
- ELSIF (n = 0) OR (a = 1.0) THEN
- RETURN 1.0
- ELSIF n = 1 THEN
- RETURN a
- ELSE
- IF n < 0 THEN
- a := 1.0 / a;
- n := ABS(n)
- END;
- RETURN RekStepen(a, n)
- END
- END
- END stepen2;
- BEGIN
- WriteString('Enter a: ');
- ReadReal(a);
- WriteLn;
- WriteString('Enter n: ');
- ReadInt(n);
- WriteLn;
- WriteString('Chose your procedure');
- WriteLn;
- WriteString(' Enter i for iteratively solution;');
- WriteLn;
- WriteString(' or r for recursively solution (same result ofc)!'); WriteLn;
- again := FALSE;
- REPEAT
- IF again THEN
- WriteString('Enter again, input must be <i> OR <r> : ');
- WriteLn;
- END;
- Read(smb);
- WriteLn;
- again := TRUE;
- UNTIL (smb = 'i') OR (smb = 'r');
- CASE smb OF
- 'i' : proc := stepen1|
- 'r' : proc := stepen2
- END;
- result := proc(a,n,ok);
- IF ok THEN
- WriteString('Result for a^n is: ');
- WriteReal(result,10)
- ELSE
- WriteString('Invalid input')
- END
- END z.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement