{$R+} 
{$B+} 

program listeMitWorten(input, output);
{testet die Prozedur add}
  var
  bestanden:boolean;

  type	
  tRefIndexListe = ^tIndexListe;
  tIndexListe = record
                     index:integer;
                     wort:string;
                     next:tRefIndexListe
                   end;

{----------- hier fügen Sie bitte Ihre Prozedur ein --------------------------}
  procedure add(var ioListe:tRefIndexListe; inIndex:integer; inWort:String);
  {fügt in die sortierte Liste ioListe ein neues Element mit dem Index
   inIndex und dem Wort inWort ein und erhöht ggf. gleiche Indizes}
    
    var
    Zeiger,
    RefNeu : tIndexListe;
    gefunden : boolean;
    
    procedure IndizesAktualisieren(inZeig : tIndexListe);
    {  falls das Element, auf dem inZeig zeigt, 
      densleben Index wie das nächste Elelement hat,
      wird den Index von dem nächsten Element erhöht.
      Die Aktualisierung den Indizes wird propagiert,
      sodass keine Indizes in der Liste doppelt vorkommen }

      var
      Zeiger : tIndexZeiger;
      fertig : boolean;

    begin
      Zeiger := inZeig;
      fertig := false;
      while (Zeiger^.next <> nil) and not fertig do
      begin
        if Zeiger^.next^.index = Zeiger^.index then
        { der Index kommt doppelt vor }
        begin
          Zeiger^.next^.index := Zeiger^.next^.index + 1;
          Zeiger := Zeiger^.next; { Aktalsierung wird propagiert }
        end
        else
        { die Indizes sind verschieden oder das Ende der Liste ist erreicht }
            fertig := false
      end { while (Zeiger^.next <> nil) and not fertig }
    end ; { IndizesAktualisieren }
    
    
  begin
    { neues Element erzeugen }
    new (RefNeu);
    RefNeu^.index := inIndex;
    RefNeu^.wort := inWort;
    
    if ioListe = nil then
    { Sonderfall: Liste ist leer }
    begin
      RefNeu^.next := ioListe;
      ioListe := RefNeu
    end
    else
      if ioListe^.index >= inIndex then
      { Sonderfall: Einfügen am Listenanfang }
      begin
        RefNeu^.next := ioListe;
        ioListe := RefNeu;
        { erhöhe Indizes in dem Rest der Liste,
          wenn sie nach dem Einfügen doppelt vorkommen }
        IndizesAktualisieren(RefNeu)
      end
      else 
      { Einfügeposition muss gesucht werden }
      begin
        { Einfügeposition suchen }
        gefunden := false;
        Zeiger := ioListe;
        while (Zeiger^.next <> nil) and
              (not gefunden) do
          if Zeiger^.next^.index >= inIndex then
            gefunden := true
          else
            Zeiger := Zeiger^.next;
        {Jetzt ist die Einfügeposition gefunden oder das Ende der Liste  wurde erreicht}
        
        if gefunden then
        { Normalfall: Einfügen in die Liste }
        begin
          RefNeu^.next := Zeiger^.next;
          Zeiger^.next := RefNeu;
          { erhöhe Indizes im Rest der Liste, 
            wenn sie nach dem Einfügen doppelt vorkommen }
          IndizesAktualisieren(RefNeu)
        end
        else
        { Sonderfall: Einfügen am Ende der Liste }
        begin
          RefNeu^.next := nil;
          Zeiger^.next :=RefNeu;        
        end
      end { else : Einfügeposition muss gesucht werden}
  end; { add }
{----------- hier endet Ihre Prozedur ----------------------------------------}

  function stringToListe(inS:string):tRefIndexListe;
  {baut eine Liste aus einem String}
    var
    liste,lauf:tRefIndexListe;
    i:integer;
    c:char;
    modus:integer;
    r:integer;
    w:string;

  begin
    liste := nil;
    lauf := nil;
    i := 1;
    modus := 0;
    r := 0;
    w := '';
    while (i <= Length(inS)) do
    begin
      c := inS[i];
      if ((c = '[') and (liste <> nil)) then 
      begin
        new(lauf^.next);
        lauf := lauf^.next;
        lauf^.next := nil
      end;
      if ((c = '[') and (liste = nil)) then 
      begin
        new(liste);
        lauf := liste;
        lauf^.next := nil
      end;
      if (c = '[') then modus := 1;
      if (c = ',') then modus := 2;
      if (c = ']') then modus := 0;
      if (c = '0') then r := r * 10;
      if (c = '1') then r := r * 10 + 1;
      if (c = '2') then r := r * 10 + 2;
      if (c = '3') then r := r * 10 + 3;
      if (c = '4') then r := r * 10 + 4;
      if (c = '5') then r := r * 10 + 5;
      if (c = '6') then r := r * 10 + 6;
      if (c = '7') then r := r * 10 + 7;
      if (c = '8') then r := r * 10 + 8;
      if (c = '9') then r := r * 10 + 9;
      if ((modus = 2) and not (c = ',')) then w := w + c;
      if (c = ']') then 
      begin 
        lauf^.index := r; 
        lauf^.wort := w; 
        r := 0;
        w := ''
      end;
      i := i + 1;
    end;
    lauf := nil;
    stringToListe := liste;
  end;

  function compare(inListeA:tRefIndexListe; inListeB:tRefIndexListe):boolean;
  {Vergleicht zwei Listen}
  
  begin
   if (inListeA = nil) or (inListeB = nil) then 
     compare := (inListeA = inListeB)
   else 
     compare := (inListeA^.index = inListeB^.index) and (inListeA^.wort = inListeB^.wort) 
                and compare(inListeA^.next, inListeB^.next);
  end;
  
  function toString(inListe:tRefIndexListe):string;
  {baut einen String aus einer Liste}  
    var
    s:string;
    x:string;
    lauf:tRefIndexListe;
    
  begin
    lauf := inListe;
    s := '';
    x := '';
    while lauf <> nil do
    begin 
      Str(lauf^.index,x);
      s := s + '[' + x + ',' + lauf^.wort + ']';
      lauf := lauf^.next 
    end;
    toString := s
  end;

  function printTestDatum(inListe:string; inIndex:integer; inWort:string; inErwartet:string):boolean;
  {testet ein Testdatum und gibt das Ergebniss zurück}
    var
    liste:tRefIndexListe;
    ok:boolean;
  
  begin
    liste := stringToListe(inListe);
    add(liste,inIndex,inWort);
    ok := compare(liste,stringToListe(inErwartet));
    if (not ok) then 
    begin 
      write('Test (');
      write(inListe + ', ');
      write(inIndex);
      write(', ');
      write(inWort);
      write(') ist fehlgeschlagen. Die Funktion liefert ');
      write(toString(liste));
      write(' und nicht ');
      writeln(inErwartet)
    end;
    printTestDatum:=ok;
  end;
 
begin
  writeln('**** Funktion testen ****');
  bestanden := printTestDatum('',2,'Elch','[2,Elch]')
    AND printTestDatum('[13,Fuchs]',2,'Elch','[2,Elch][13,Fuchs]')
    AND printTestDatum('[3,Fuchs]',3,'Elch','[3,Elch][4,Fuchs]')
    AND printTestDatum('[2,Fuchs]',4,'Dachs','[2,Fuchs][4,Dachs]')
    AND printTestDatum('[31,Fuchs][42,Floh][55,Huhn]',2,'Elch','[2,Elch][31,Fuchs][42,Floh][55,Huhn]')
    AND printTestDatum('[2,Fuchs][3,Tiger]',2,'Elch','[2,Elch][3,Fuchs][4,Tiger]')
    AND printTestDatum('[2,Fuchs][3,Tiger][7,Maus]',2,'Elch','[2,Elch][3,Fuchs][4,Tiger][7,Maus]')
    AND printTestDatum('[20,Fuchs][31,Tiger][32,Floh]',42,'Dachs','[20,Fuchs][31,Tiger][32,Floh][42,Dachs]')
    AND printTestDatum('[1,Hund][3,Aal][4,Katze][5,Maus]',3,'Tiger','[1,Hund][3,Tiger][4,Aal][5,Katze][6,Maus]')
    AND printTestDatum('[1,Hund][3,Aal][4,Katze][7,Maus]',3,'Tiger','[1,Hund][3,Tiger][4,Aal][5,Katze][7,Maus]')
    AND printTestDatum('[1,Hund][4,Aal][7,Katze][9,Maus][13,Floh][18,Huhn]',11,'Tiger','[1,Hund][4,Aal][7,Katze][9,Maus][11,Tiger][13,Floh][18,Huhn]')
    AND printTestDatum('[1,Hund][4,Aal][7,Katze][9,Maus][13,Floh][18,Huhn]',15,'Tiger','[1,Hund][4,Aal][7,Katze][9,Maus][13,Floh][15,Tiger][18,Huhn]')
    AND printTestDatum('[1,Hund][4,Aal][7,Katze][9,Maus][13,Floh][18,Huhn]',18,'Tiger','[1,Hund][4,Aal][7,Katze][9,Maus][13,Floh][18,Tiger][19,Huhn]');      
  if bestanden then 
  begin 
    writeln('Alle Tests erfolgreich!');
  end;
end.
 

Pascal online compiler

Write, Run & Share Pascal code online using OneCompiler’s Pascal online compiler for free. It’s a straightforward, accessible way to learn and experiment with Pascal programming right from your browser. OneCompiler supports modern Pascal syntax and provides a ready-to-use editor for immediate execution.

About Pascal

Pascal is a procedural programming language developed in the 1970s by Niklaus Wirth. It was designed to encourage good programming practices and structured programming. Pascal is widely used in teaching computer science fundamentals and has influenced many modern languages.

Sample Code

The following is a simple Pascal program that prints a greeting:

program HelloWorld;
begin
  writeln('Hello, OneCompiler!');
end.

Taking inputs (stdin)

OneCompiler’s Pascal editor supports stdin through the I/O tab. Here’s an example that reads a user's name and prints a greeting:

program GreetUser;
var
  name: string;
begin
  readln(name);
  writeln('Hello, ', name, '!');
end.

Syntax Basics

Variables

var
  age: integer;
  name: string;
  score: real;
  flag: boolean;

Data Types

TypeDescription
integerWhole numbers
realFloating-point numbers
charSingle character
stringSequence of characters
booleanTrue or False

Conditionals

if score >= 50 then
  writeln('Pass')
else
  writeln('Fail');

Loops

For loop

for i := 1 to 5 do
  writeln(i);

While loop

i := 1;
while i <= 5 do
begin
  writeln(i);
  i := i + 1;
end;

Repeat-Until loop

i := 1;
repeat
  writeln(i);
  i := i + 1;
until i > 5;

Procedures and Functions

procedure SayHello;
begin
  writeln('Hello!');
end;

function Add(a, b: integer): integer;
begin
  Add := a + b;
end;

This guide provides a quick reference to Pascal programming syntax and features. Start coding in Pascal using OneCompiler’s Pascal online compiler today!