Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MODULE z;
- FROM InOut IMPORT WriteString, WriteLn, WriteInt, ReadInt;
- FROM RealInOut IMPORT ReadReal, WriteReal;
- VAR
- a, result : REAL;
- n : INTEGER;
- ok : BOOLEAN;
- PROCEDURE Stepen(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 Stepen;
- BEGIN
- WriteString('Enter a: ');
- ReadReal(a);
- WriteLn;
- WriteString('Enter n: ');
- ReadInt(n);
- WriteLn;
- ok := TRUE;
- result := Stepen(a,n,ok);
- IF ok THEN
- WriteString('Result for a^n is: ');
- WriteReal(result,0)
- ELSE
- WriteString('Invalid input')
- END
- END z.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement