Musterlösung zu Aufgabe 4 -- Persons.m2


IMPLEMENTATION MODULE Persons;
 FROM FtdIO IMPORT Done, FreadCard, FwriteCard, 
       FwriteString, FreadString, FwriteLn, FwriteChar;
  FROM Strings IMPORT StrCmp;
  FROM StdIO IMPORT FILE, MODE, stderr, stdout,
                stdin, Fopen, Feof;

  PROCEDURE nameIsLess(x,y: PersDat): BOOLEAN;
  (* returns TRUE, if x.famName less than y.famName 
   * otherwise FALSE
   *)
  BEGIN
	IF StrCmp(x.famName,y.famName) < 0 THEN
		RETURN TRUE
	ELSE
		RETURN FALSE;
	END
  END nameIsLess;

  PROCEDURE nameIsEqual(x,y: PersDat):BOOLEAN;
  (* returns TRUE, if x.famName = y.famName
   * otherwise FALSE
   *)
  BEGIN
	IF StrCmp(x.famName,y.famName) = 0 THEN
		RETURN TRUE
	ELSE
		RETURN FALSE;
	END
  END nameIsEqual;

  PROCEDURE isYounger(x,y: PersDat): BOOLEAN;
  (* returns TRUE, if person x is younger than person y
   * otherwise FALSE
   *)
  BEGIN
	IF (x.birthDate.year <> y.birthDate.year) THEN
		RETURN (x.birthDate.year > y.birthDate.year)
	END;
	(* x.birthDate.year = y.birthDate.year *)
	IF (x.birthDate.month <> y.birthDate.month) THEN
		RETURN (x.birthDate.month > y.birthDate.month)
	END;
	(* same year and same month*)
	RETURN (x.birthDate.day > y.birthDate.day)
  END isYounger;
	
  PROCEDURE sameAge(x,y: PersDat): BOOLEAN;
  (* returns TRUE, if persons x and y have the same birthdate 
   * otherwise FALSE
   *)
  BEGIN
	RETURN ( (x.birthDate.year = y.birthDate.year) 
		&
		 (x.birthDate.month = y.birthDate.month) 
		&
		 (x.birthDate.day = y.birthDate.day) )
  END sameAge;

  PROCEDURE WriteRec(f: FILE; pers: PersDat);
        (* f - open stream for writing 
	 * writes data with one blank as separator 
	 *)
  BEGIN
     WITH pers DO
        FwriteString(f,famName);
        FwriteChar(f,' ');
        FwriteString(f,firstName);
        FwriteChar(f,' ');
        FwriteCard(f,birthDate.day,0);
        FwriteChar(f,' ');
        FwriteCard(f,birthDate.month,0);
        FwriteChar(f,' ');
        FwriteCard(f,birthDate.year,0);
        FwriteChar(f,' ');
        FwriteString(f,phone);
        FwriteLn(f);
      END;
  END WriteRec;
     
  PROCEDURE ReadRec(f:FILE; VAR pers: PersDat):BOOLEAN;
        (* f - open stream for reading !
	 * reads one record assuming whitespace as separator
	 * TRUE, if done, otherwise FALSE
	 *)
        VAR i, d,m,y: CARDINAL;
  BEGIN
     IF Feof(f) 
     THEN
           RETURN FALSE
     END;
     (* necessary because of type compatibility *)
      WITH pers DO
        FreadString(f,famName);
        FreadString(f,firstName);
        FreadCard(f,d); birthDate.day:=d;
        FreadCard(f,m); birthDate.month:=m;
        FreadCard(f,y); birthDate.year:=y;
        FreadString(f,phone);
      END;
      IF Done 
      THEN
            RETURN TRUE
      ELSE
            RETURN FALSE
      END;
  END ReadRec;
BEGIN
END Persons.

Musterlösung zu Aufgabe 4 || Übungen || Vorlesung || SS 97 || SAI

Franz Schweiggert, 13.06.1997