Основы (асослар)


Download 0.95 Mb.
bet49/58
Sana28.12.2022
Hajmi0.95 Mb.
#1011518
1   ...   45   46   47   48   49   50   51   52   ...   58
Bog'liq
PL-SQL Маъруза матни

Рационал объект тури


Рационал сон иккита бутун сон орқали ифодаловчи сондир. PL/SQL тилида рационал сон тури аниқланмаган. Биз шуни Rational объект тури орқали аниқлаймиз:
CREATE TYPE Rational AS OBJECT (
num INTEGER,
den INTEGER,
MAP MEMBER FUNCTION convert RETURN REAL,
MEMBER PROCEDURE normalize,
MEMBER FUNCTION reciprocal RETURN Rational,
MEMBER FUNCTION plus (x Rational) RETURN Rational,
MEMBER FUNCTION less (x Rational) RETURN Rational,
MEMBER FUNCTION times (x Rational) RETURN Rational,
MEMBER FUNCTION divby (x Rational) RETURN Rational,
PRAGMA RESTRICT_REFERENCES (DEFAULT, RNDS,WNDS,RNPS,WNPS)
);

PL/SQL –да ҳар хил операторларни бир хил номлаш таъқиқланган. Шу сабабали сиз +, -, *, ва / операторлар учун номланган plus, less ( minus сўзи заҳираланган), times, ва divby методларини аниқлашингиз лозим.


Сўнг қуйидаги алоҳида – сақланувчи ва normalize методи деб номланувчи функцияни аниқлашингиз лозим:
CREATE FUNCTION gcd (x INTEGER, y INTEGER) RETURN INTEGER AS
-- find greatest common divisor of x and y
ans INTEGER;
BEGIN
IF (y <= x) AND (x MOD y = 0) THEN
ans := y;
ELSIF x < y THEN
ans := gcd(y, x); -- recursive call
ELSE
ans := gcd(y, x MOD y); -- recursive call
END IF;
RETURN ans;
END;

Кейин эса объект тури танасини ёзишингиз мумкин:


CREATE TYPE BODY Rational AS
MAP MEMBER FUNCTION convert RETURN REAL IS
-- convert rational number to real number
BEGIN
RETURN num / den;
END convert;

MEMBER PROCEDURE normalize IS


-- reduce fraction num / den to lowest terms
g INTEGER;
BEGIN
g := gcd(num, den);
num := num / g;
den := den / g;
END normalize;

MEMBER FUNCTION reciprocal RETURN Rational IS


-- return reciprocal of num / den
BEGIN
RETURN Rational(den, num); -- call constructor
END reciprocal;

MEMBER FUNCTION plus (x Rational) RETURN Rational IS


-- return sum of SELF + x
r Rational;
BEGIN
r := Rational(num * x.den + x.num * den, den * x.den);
r.normalize;
RETURN r;
END plus;

MEMBER FUNCTION less (x Rational) RETURN Rational IS


-- return difference of SELF - x
r Rational;
BEGIN
r := Rational(num * x.den - x.num * den, den * x.den);
r.normalize;
RETURN r;
END less;

MEMBER FUNCTION times (x Rational) RETURN Rational IS


-- return product of SELF * x
r Rational;
BEGIN
r := Rational(num * x.num, den * x.den);
r.normalize;
RETURN r;
END times;

MEMBER FUNCTION divby (x Rational) RETURN Rational IS


-- return quotient of SELF / x
r Rational;
BEGIN
r := Rational(num * x.den, den * x.num);
r.normalize;
RETURN r;
END divby;
END;

Download 0.95 Mb.

Do'stlaringiz bilan baham:
1   ...   45   46   47   48   49   50   51   52   ...   58




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling