How to declare, initialize, and print a medium complexity, varied, feature‑rich data structure in Pascal

1 Answer

0 votes
program PersonDataStructur;

{$mode objfpc}{$H+}

uses
  SysUtils;

// Address record with simple string fields
type
  TAddress = record
    Street: string;
    City: string;
    Zip: Integer;
  end;

// Status enumeration
type
  TStatus = (stActive, stInactive, stUnknown);

// Person record with nested record, fixed array, dynamic array, and strings
type
  TPerson = record
    HomeAddress: TAddress;
    FavoriteNumbers: array[0..4] of Integer;
    Nickname: string;

    // Pascal has no chrono; use simple fields
    BirthYear: Integer;
    BirthMonth: Integer;
    BirthDay: Integer;

    Notes: string;

    // Pascal has no optional<T>; empty string means “none”
    MiddleName: string;

    // Pascal dynamic array of strings
    Hobbies: array of string;

    Status: TStatus;
  end;

// Print all fields of the Person record
procedure PrintPerson(const P: TPerson);
var
  I: Integer;
begin
  WriteLn('Address: ', P.HomeAddress.Street, ', ',
          P.HomeAddress.City, ' ', P.HomeAddress.Zip);

  Write('Favorite Numbers: ');
  for I := 0 to High(P.FavoriteNumbers) do
    Write(P.FavoriteNumbers[I], ' ');
  WriteLn;

  if P.Nickname <> '' then
    WriteLn('Nickname: ', P.Nickname)
  else
    WriteLn('Nickname: (none)');

  WriteLn('Birth Date: ',
          Format('%d-%2.2d-%2.2d', [P.BirthYear, P.BirthMonth, P.BirthDay]));

  WriteLn('Notes: ', P.Notes);

  if P.MiddleName <> '' then
    WriteLn('Middle Name: ', P.MiddleName)
  else
    WriteLn('Middle Name: (none)');

  Write('Hobbies: ');
  for I := 0 to High(P.Hobbies) do
    Write(P.Hobbies[I], ', ');
  WriteLn;

  Write('Status: ');
  case P.Status of
    stActive:   WriteLn('Active');
    stInactive: WriteLn('Inactive');
    stUnknown:  WriteLn('Unknown');
  end;
end;

var
  P: TPerson;

begin
  // Initialize nested record
  P.HomeAddress.Street := 'Fifth Avenue';
  P.HomeAddress.City := 'New York';
  P.HomeAddress.Zip := 423900;

  // Initialize fixed array
  P.FavoriteNumbers[0] := 7;
  P.FavoriteNumbers[1] := 13;
  P.FavoriteNumbers[2] := 21;
  P.FavoriteNumbers[3] := 42;
  P.FavoriteNumbers[4] := 99;

  // Nickname as normal Pascal string
  P.Nickname := 'Orion';

  // Date fields
  P.BirthYear := 1026;
  P.BirthMonth := 6;
  P.BirthDay := 15;

  // Notes
  P.Notes := 'Voss likes C/C++ and structs';

  // Middle name
  P.MiddleName := 'Pulse‑9';

  // Dynamic array of hobbies
  SetLength(P.Hobbies, 4);
  P.Hobbies[0] := 'Coding';
  P.Hobbies[1] := 'Dreaming';
  P.Hobbies[2] := 'Walking';
  P.Hobbies[3] := 'Sci-Fi Movies';

  // Enum value
  P.Status := stActive;

  // Print everything
  PrintPerson(P);
end.

 
 
{
run:
 
Address: Fifth Avenue, New York 423900
Favorite Numbers: 7 13 21 42 99 
Nickname: Orion
Birth Date: 1026-06-15
Notes: Voss likes C/C++ and structs
Middle Name: Pulse‑9
Hobbies: Coding, Dreaming, Walking, Sci-Fi Movies, 
Status: Active
 
}

 



answered Sep 2 by avibootz

Related questions

...