Pascal Movie Database Management Procedures

Classified in Computers

Written at on English with a size of 8.83 KB.

Pascal Movie Database Management

This document details several procedures written in Pascal for managing a movie database. The database is stored in a file of records, where each record represents a movie.

Data Structure

The code defines a pelicula (movie) record with the following fields:

  • codigo: integer (Movie ID)
  • genero: string (Genre)
  • nombre: string (Title)
  • duracion: integer (Duration)
  • director: string (Director)
  • copias: integer (Number of Copies)
  • precio: real (Price)

A constant valoralto is defined as 9999, used as a sentinel value.

And the file is defined as:

maestro= file of pelicula ;

Procedures

leerp(var p: pelicula)

Reads movie data from the input (keyboard) and stores it in the pelicula record p. It reads the codigo first. If it's not equal to valoralto, it proceeds to read the rest of the fields.

carga(var mae: maestro)

Creates and initializes the movie database file (mae). It writes an initial record with codigo = 0 and then repeatedly calls leerp to read movie data and write it to the file until a movie with codigo = valoralto is entered.

leerM(var mae: maestro; var p: pelicula)

Reads a movie record from the file mae into the pelicula variable p. If the end of the file is reached, it sets p.codigo to valoralto.

bajafisicaARCHNUE(var mae: maestro; var mae2: maestro; nombre: string)

Performs a "physical deletion" of records marked for logical deletion. It reads from mae, creates a new file mae2, copies only records with codigo > 0, deletes the original file (erase(mae)), renames the temporary file to the original name (rename('sd', nombre)), and closes the new file.

borrarlogi(var mae: maestro)

Performs a "logical deletion" of a movie record. It reads a codigo to delete from the input. It then searches for the record. If found, it marks the record for deletion by negating its position and overwriting the first record of the file.

borradofisico(var mae: maestro)

Another "physical deletion" procedure. It reads a movie to delete (p2) from input. It finds the matching record in the file. Then, it shifts all subsequent records back one position, effectively removing the target record. Finally, it truncates the file to remove the last (duplicated) record.

borradofisicoLI(var mae: maestro)

A third "physical deletion" procedure, optimized for removing logically deleted records. It iterates through the file, identifies logically deleted records (those with codigo <= 0), and compacts the file by moving valid records to fill the gaps. It uses a counter (canT) to track the new end of the file and truncates it at the end.

agregarpeli(var mae: maestro)

Adds a new movie record (pnue) to the database. It reads the new movie data using leerp. If the file is empty (first record has codigo = 0), it appends the new record. Otherwise, it reuses a logically deleted record's space, if available.

modificar(var mae: maestro)

Modifies an existing movie record. It reads the updated movie data (p2) from input. It searches for the record with the matching codigo and overwrites it with the new data.

listar(var mae: maestro; var t: text)

Lists the contents of the movie database to a text file (t). It iterates through the movie file and writes the details of each movie to the output file.

Main Program

The main program prompts the user for the filename, assigns it to the mae variable, and assigns 'peliculas.txt' to the text file t. The code structure suggests a menu-driven interaction (opc: a..c), but the menu options are not included in the provided snippet.


program ejercicio1;
const
  valoralto = 9999;
type
  pelicula = record
    codigo: integer;
    genero: string;
    nombre: string;
    duracion: integer;
    director: string;
    copias: integer;
    precio: real;
  end;

  maestro = file of pelicula;

procedure leerp(var p: pelicula);
begin
  readln(p.codigo);
  if (p.codigo <> valoralto) then
  begin
    readln(p.genero);
    readln(p.nombre);
    readln(p.duracion);
    readln(p.director);
    readln(p.copias);
    readln(p.precio);
  end;
end;

procedure carga(var mae: maestro);
var
  p: pelicula;
begin
  rewrite(mae);
  p.codigo := 0;
  write(mae, p);
  leerp(p);
  while (p.codigo <> valoralto) do
  begin
    write(mae, p);
    leerp(p);
  end;
  close(mae);
end;

procedure leerM(var mae: maestro; var p: pelicula);
begin
  if (not eof(mae)) then
    read(mae, p)
  else
    p.codigo := valoralto;
end;

procedure bajafisicaARCHNUE(var mae: maestro; var mae2: maestro; nombre: string);
var
  p: pelicula;
begin
  assign(mae2, 'sd');
  reset(mae);
  rewrite(mae2);
  leerM(mae, p);
  while (p.codigo <> valoralto) do
  begin
    if (p.codigo > 0) then
      write(mae2, p);
    leerM(mae, p);
  end;
  erase(mae);
  rename('sd', nombre);
  close(mae2);
end;

procedure borrarlogi(var mae: maestro);
var
  p1, p2: pelicula;
  codigo: integer;
begin
  reset(mae);
  readln(codigo);
  leerM(mae, p1);
  leerM(mae, p2);
  while (p2.codigo <> valoralto) and (p2.codigo <> codigo) do
    leerM(mae, p2);
  if (p2.codigo = codigo) then
  begin
    seek(mae, filepos(mae) - 1);
    p2.codigo := p1.codigo;
    p1.codigo := (filepos(mae)) * -1;
    write(mae, p2);
    seek(mae, 0);
    write(mae, p1);
  end;
  close(mae);
end;

procedure borradofisico(var mae: maestro);
var
  p, p2, p3: pelicula;
begin
  reset(mae);
  seek(mae, filesize(mae) - 1);
  leerM(mae, p);
  leerp(p2);
  seek(mae, 0);
  leerM(mae, p3);
  while (p3.codigo <> valoralto) and (p3.codigo <> p2.codigo) do
    leerM(mae, p3);
  if (p3.codigo = p2.codigo) then
  begin
    leerM(mae, p3);
    while (p3.codigo <> valoralto) do
    begin
      seek(mae, filepos(mae) - 2);
      write(mae, p3);
      seek(mae, filepos(mae) + 1);
      leerM(mae, p3);
    end;
    truncate(mae);
  end;
end;

procedure borradofisicoLI(var mae: maestro);
var
  p1, p2: pelicula;
  i, canT, canB: integer;
begin
  reset(mae);
  cantB := 0;
  canT := filesize(mae) - 1;
  leerM(mae, p1);
  while (p1.codigo <= 0) and (p1.codigo <> valoralto) do
  begin
    seek(mae, (p1.codigo) * -1);
    canB := canB + 1;
    leerM(mae, p1);
  end;
  seek(mae, 0);
  for i := 1 to canB do
  begin
    leerM(mae, p1);
    seek(mae, canT);
    leerM(mae, p2);
    while (p2.codigo < 0) do
    begin
      seek(mae, (filepos(mae)) - 2);
      leerM(mae, p2);
    end;
    seek(mae, (p1.codigo) * -1);
    write(mae, p2);
    seek(mae, canT);
    write(mae, p1);
    seek(mae, filepos(mae) - 1);
    canT := canT - 1;
  end;
  seek(mae, 0);
  leerM(mae, p1);
  while (p1.codigo <> valoralto) and (p1.codigo > 0) do
    leerM(mae, p1);
  seek(mae, filepos(mae) - 1);
  truncate(mae);
end;

procedure agregarpeli(var mae: maestro);
var
  codigo: integer;
  pnue, p, p2: pelicula;
begin
  reset(mae);
  leerp(pnue);
  leerM(mae, p);
  if (p.codigo = 0) then
  begin
    seek(mae, filesize(mae));
    write(mae, pnue);
  end
  else
  begin
    seek(mae, (p.codigo) * -1);
    leerM(mae, p2);
    p.codigo := p2.codigo;
    seek(mae, filepos(mae) - 1);
    write(mae, pnue);
    seek(mae, 0);
    write(mae, p);
  end;
  close(mae);
end;

procedure modificar(var mae: maestro);
var
  p, p2: pelicula;
begin
  reset(mae);
  leerM(mae, p);
  leerp(p2);
  while (p.codigo <> valoralto) and (p.codigo <> p2.codigo) do
    leerM(mae, p);
  if (p.codigo = p2.codigo) then
  begin
    seek(mae, filepos(mae) - 1);
    write(mae, p2);
  end;
  close(mae);
end;

procedure listar(var mae: maestro; var t: text);
var
  p: pelicula;
begin
  reset(mae);
  rewrite(t);
  leerM(mae, p);
  while (p.codigo <> valoralto) do
  begin
    writeln(t, p.codigo, p.duracion, p.copias, p.precio, p.genero);
    writeln(t, p.nombre);
    writeln(t, p.director);
    leerM(mae, p);
  end;
  close(mae);
  close(t);
end;

var
  mae: maestro;
  opc: char; { Corrected type to char }
  nombre: string;
  t: text;
begin
  writeln('Enter the filename:'); { More descriptive prompt }
  readln(nombre);
  assign(mae, nombre);
  assign(t, 'peliculas.txt');
end.

Entradas relacionadas: