Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MODULE Polinomic;
- FROM InOut IMPORT WriteString, WriteLn;
- FROM RealInOut IMPORT WriteReal;
- FROM Storage IMPORT ALLOCATE, DEALLOCATE;
- FROM PolinomL IMPORT Polinom, UbaciMonom, Stampaj, Anuliraj;
- (*izracunaj //polinom, naprednija*)
- PROCEDURE izracunaj(a: REAL;p: Polinom;VAR pro: BOOLEAN): REAL;
- (*Izracunavanje stepena uzastopnim kvadriranjem*)
- 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;
- VAR
- tekuci: Polinom;
- akum: REAL;
- rez: REAL;
- BEGIN
- rez := 0.0;
- tekuci := p;
- pro := TRUE;
- WHILE (tekuci <> NIL) AND pro DO
- akum := Stepen(a, tekuci^.st, pro);
- rez := rez + tekuci^.k * akum;
- tekuci := tekuci^.veza;
- END;
- RETURN rez;
- END izracunaj;
- VAR
- monom, polinom: Polinom;
- rez : REAL;
- ok : BOOLEAN;
- BEGIN
- Anuliraj(polinom);
- NEW(monom);
- monom^.st := 3;
- monom^.k := 2.0;
- monom^.veza := NIL;
- UbaciMonom(monom, polinom);
- DISPOSE(monom);
- NEW(monom);
- monom^.st := 2;
- monom^.k := 5.0;
- monom^.veza := NIL;
- UbaciMonom(monom, polinom);
- DISPOSE(monom);
- NEW(monom);
- monom^.st := 1;
- monom^.k := 3.0;
- monom^.veza := NIL;
- UbaciMonom(monom, polinom);
- DISPOSE(monom);
- NEW(monom);
- monom^.st := 0;
- monom^.k := 5.0;
- monom^.veza := NIL;
- UbaciMonom(monom, polinom);
- DISPOSE(monom);
- WriteString('Polinom pre procedure: ');
- Stampaj(polinom,0);
- WriteLn; WriteLn;
- rez := izracunaj(2.0,polinom,ok);
- WriteString('Rez je: ');
- WriteReal(rez,0);
- END Polinomic.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement