Frage

Ich muss einen neuen Typ für rationale Zahlen erstellen, das als Zähler/Nenner dargestellt wird, um Fraktionen wie 3/5 zu speichern. Ich fand Folgendes, um es zu tun.

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)
);

Wie kann ich eine Stärke hinzufügen, dass der Nenner nicht Null sein kann?

War es hilfreich?

Lösung

Sie müssen einen Konstruktor für Ihren Typ deklarieren. Dann können Sie die Validierung, die Sie mögen, im Körper einsetzen.

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,
   constructor function rational
            (n integer, d integer)
            return self as result,    
   PRAGMA RESTRICT_REFERENCES (DEFAULT, RNDS,WNDS,RNPS,WNPS)
);

Fügen Sie in Ihrem Typ Körper den Konstruktorcode hinzu:

constructor function rational
            (n integer, d integer)
            return self as result
is     
begin
     if d = 0 then
         raise_application_error(-20000, 'Denominator cannot be zero!');
     end if;
     self.num := n;
     self.den := d;
end rational; 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top