Einzelnen Beitrag anzeigen

DerVicko

Registriert seit: 2. Jun 2013
3 Beiträge
 
#1

TString führt "pop" nicht aus

  Alt 2. Jun 2013, 00:41
Hallo Delphi-Praxis Community,
ich soll in meinen Informatikunterricht ein Programm schreiben, in dem es drei Gleise gibt, im ersten Gleis sind mehrere Waggons mit zufälligen Zahlenwerten. Diese sollen sich nun sortiert in das dritte Gleis einreihen, also wie Türme von Hanoi.
Mein Problem ist jetzt aber nicht die Schleife zum Sortieren oder die Ausgabe, sondern, dass nichts passiert, wenn ich StackA.pop ausführe.
Dieser Befehl sollte den ersten Waggon löschen. Ich habe das Programm schon so extrem reduziert, wie mir möglich ist, also kein Verschieben oder Generieren von mehreren Zahlen durch eine Schleife etc... Nur das Auswählen und Löschen von StackA.top, und Button3 für die Ausgabe.
Ich gebe euch hier mal den Code vom Programm und von den zwei Klassen (die mStack Klasse ist von meinem Lehrer gegeben).
Ich hoffe mal jemand sieht den vermutlich viel zu offensichtlichen Fehler (denn mein Lehrer, diverse andere Schüler und ich haben es nicht getan).
Danke schon im Voraus!

Delphi-Quellcode:
unit ZUUUG;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, mStack, mWaggon, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    ListBox1: TListBox;
    ListBox2: TListBox;
    ListBox3: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    ListeA:TStack;
    Zug:Waggon;
    //ListeB:TStack;
    //ListeC:TStack;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Zug.SetzeInhalt(random(100));
ListeA.push(Zug);
ListBox1.Items.Insert(0,(inttostr(Zug.GibInhalt)));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ListeA.top;
ListeA.pop;
{if Listec.isEmpty then
begin
  Listec.push(Listea.top);
  Listea.top;
  listea.pop;
  Zug.SetzeInhalt((ListeA.top as waggon).GibInhalt);
  Listbox3.Items.Insert(0,(inttostr((Listec.top as waggon).GibInhalt)));
end
  else
  begin
    if ((Listec.top as Waggon).GibInhalt)<((Listea.top as Waggon).GibInhalt) then
    begin
      Listec.push(Listea.top);
      listea.top;
      Listea.pop;
      Listbox3.Items.Insert(0,(inttostr((Listec.top as waggon).GibInhalt)));
    end;
  end;}

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
showmessage(inttostr((Listea.top as waggon).GibInhalt))
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ListeA:= TStack.create;
//ListeB:= TStack.create;
//ListeC:= TStack.create;
Zug:= Waggon.Create;
end;

end.
Delphi-Quellcode:
UNIT mWaggon;

interface

//-------------------- ggf Uses-Liste einfügen ! --------------------
//uses ....;

type
  Waggon = class

  protected //Attribute
    Inhalt : integer;

  public //Methoden
    procedure SetzeInhalt (pInhalt: integer); virtual;
    function GibInhalt : integer; virtual;

   end;

implementation

//+---------------------------------------------------------------------
//| Waggon: Methodendefinition
//+---------------------------------------------------------------------

//-------- SetzeInhalt (public) ----------------------------------------
procedure Waggon.SetzeInhalt (pInhalt: integer);
begin
  Inhalt := pInhalt
end;

//-------- GibInhalt (public) ------------------------------------------
function Waggon.GibInhalt : integer;
begin
  result := Inhalt
end;

end.
Delphi-Quellcode:
(*
* Materialien zu den zentralen Abiturpruefungen
* im Fach Informatik ab 2012 in Nordrhein-Westfalen.
*
* Klasse TStack
*
* NW-Arbeitsgruppe:
* Materialentwicklung zum Zentralabitur im Fach Informatik
*
* Version 2010-12-28
*)


unit mStack;

interface
type
  TNode = class
  private
    content: TObject;
    nextNode: TNode;
    constructor create(pObject: TObject);
    procedure setNext(pNode: TNode);
    function getContent: TObject;
    function next: TNode;
    destructor destroy; override;
  end;

  TStack = class
  private
    topNode: TNode;
  public
    constructor create; virtual;
    function isEmpty: boolean; virtual;
    function top: TObject; virtual;
    procedure pop; virtual;
    procedure push(pObject: TObject); virtual;
    destructor destroy; override;
  end;

implementation

// TNode

constructor TNode.create(pObject: TObject);
begin
  content := pObject;
  nextNode := nil;
end;

procedure TNode.setNext(pNode: TNode);
begin
  nextNode := pNode;
end;

function TNode.next: TNode;
begin
  result := nextNode;
end;

function TNode.getContent: TObject;
begin
  result := content;
end;

destructor TNode.destroy;
begin
  inherited destroy;
end;

// TStack

constructor TStack.create;
begin
  topNode := nil;
end;

function TStack.isEmpty: boolean;
begin
  result := topNode = nil;
end;

procedure TStack.push(pObject: TObject);
var
  tempNode: TNode;
begin
  if pObject <> nil then
  begin
    tempNode := TNode.create(pObject);
    if self.isEmpty then
      topNode := tempNode
    else
    begin
      tempNode.setnext(topNode);
      topNode := tempNode;
    end;
  end;
end;

procedure TStack.pop;
var
  tempNode: TNode;
begin
  if not self.isEmpty then
  begin
    tempNode := topNode;
    topNode := topNode.next;
    tempNode.destroy;
  end;
end;

function TStack.top: TObject;
begin
  if not self.isEmpty then
    result := topNode.getContent
  else
    result := nil;
end;

destructor TStack.destroy;
begin
  while not self.isEmpty do
    self.pop;
  inherited destroy;
end;

end.
  Mit Zitat antworten Zitat