home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.pascal
- Path: sparky!uunet!mcsun!sun4nl!star.cs.vu.nl!sbakker
- From: sbakker@cs.vu.nl (Bakker S)
- Subject: Re: Comparing record types
- Message-ID: <BxLM1t.841@cs.vu.nl>
- Sender: news@cs.vu.nl
- Organization: Fac. Wiskunde & Informatica, VU, Amsterdam
- References: <92316.095144F0O@psuvm.psu.edu>
- Date: Thu, 12 Nov 1992 10:20:17 GMT
- Lines: 88
-
- <F0O@psuvm.psu.edu> writes:
-
- >Hi!
-
- > Take a look at the following example program:
-
-
- >Type
- >TPoint = Record
- > X, Y: Integer;
- >End;
-
- >Var
- > P1, P2: TPoint;
-
-
- >Begin
- > P1.X := 10; P1.Y := 20;
- > P2 := P1;
- > If (P1 = P2) then Writeln('Equal'); { This line is incorrect }
- >End.
-
- > When I run the program, I get the message, 'Operand types do not
- >match operator'.
-
- That's right. '=' only applies to atomic types (scalar, floating point,
- string, set).
-
- > P1 and P2 are two variables of the same record type. Now you can
- >directly assign one record to another, as long as they are the same type.
- >If there some simple way to compare P1 and P2 to see if they are equal,
- >without having to check each field for equality in my code?
- > I was hoping I could say If (P1 = P2) then ..., but this does not
- >work. Does anyone know of some trick so I can easily compare records?
-
- The problem is, the compiler cannot tell on which fields to compare the
- records. Should all fields match? Should only one field match (`primary key')?
- For this reason you cannot simply say `If (P1 = P2) then ...' or use
- any other relational operator for that matter.
-
- What you might do is what C does with its strcmp(): write a PointCmp function.
-
- For simple comparison (`equal or not?'):
-
- function PointCmp( P1, P2 : TPoint ) : Boolean;
- begin
- with P1 do
- PointCmp := ( X = P2.X ) and ( Y = P2.Y );
- end;
-
- Now your line would become:
- > If PointCmp(P1, P2) then Writeln('Equal');
-
-
- For full comparison ( <, =, > ):
-
- function PointCmp( P1, P2 : TPoint ) : Integer;
- begin
- with P1 do
- if X < P2.X then
- PointCmp := -1
- else if X > P2.X then
- PointCmp := 1
- else if Y < P2.Y then
- PointCmp := -1
- else if Y > P2.Y then
- PointCmp := 1
- else PointCmp := 0;
- end;
-
- Now your line would become:
- > If PointCmp(P1, P2) = 0 then Writeln('Equal');
- And you could write:
- If PointCmp(P1, P2) > 0 then Writeln('Greater');
- If PointCmp(P1, P2) < 0 then Writeln('Smaller');
-
- Hope this helps,
- Steven.
-
- [=============================================================================]
- | Steven Bakker, Vrije Universiteit Amsterdam. |
- | sbakker@cs.vu.nl |
- [-----------------------------------------------------------------------------]
- | ".. if a tree falls in the woods, and there's no one there to hear it, does |
- | it fall upwards or downwards. And its corollary, if a deaf man falls in the |
- | woods, does he make a sound?" |
- | -- Captain Rick. |
- [=============================================================================]
-