Problem with a list in a procedure?


Hello again,

im having troble with a sorting a list in a procedure. I will give you all code just in case someone needs it, but the problems as I see it starts in procedure Transaktion. The program should be able to connect transaktions dynamically to an account, and then when you ask for saldo, the transaktions should be written to screen.

Thanx in advance


PROGRAM bank(Input,Output);

TYPE pointer =^transaktionsrec;

transaktionsrec=RECORD
trans:Integer; {+ -, mste lnkas till saldo}
date:Integer;
uttag:Char;
next:pointer;
END;

text_string=PACKED ARRAY[1..15] OF Char;

kundrecord=RECORD
namn : text_string;
adress : text_string;
phonenr : Integer;
saldo : Integer;
first : pointer;
costumnr : Integer;
kredit: Integer;
END;

kundarray = ARRAY[1..100] OF kundrecord;

VAR kom:Char;
kundnr:Integer;
continue:Boolean;
kundar:kundarray;
x:Integer;
temp,first,ny:pointer;

PROCEDURE newcostum(VAR a:kundarray);

VAR name,adres:text_string;
kred1,kn,phoneNumber,kred:Integer;
sum:Real;
ok:Boolean;
BEGIN

Writeln;
ok:=false;
Repeat
BEGIN
Write('What number should the costumer have? Between 1 & 100:');
Readln(kn);
If (kn>=1) AND (kn<=100) Then
BEGIN
With a[kn] DO
IF phonenr=9 THEN ok:=true;
END;
END;

Until ok=true;

Write('Costumers name:');
Readln(name);
Write('Costumers street:');
Readln(adres);
Write('Costumers phonenr:');
Readln(phoneNumber);

Write('Credit? Max 5000.');
Readln(kred1);
IF kred1<0 THEN kred1:=0;
IF kred1>5000 THEN kred1:=5000;
Write('Amount of money to start with:');
Readln(kred);

With a[kn] DO BEGIN
namn:=name;
adress:=adres;
phonenr:=phoneNumber;
saldo:=kred;
first:=NIL;
END;
END; {end of procedure newcostum}


PROCEDURE transaktion(VAR a:kundarray;kn:Integer);

VAR d:Integer;
temp,first,ny:Pekare;
u,s,k:Integer;
utag:Char;

BEGIN

s:=a[kn].saldo;
k:=a[kn].kredit;

Write('What date is it?DDMMYY!');
Readln(d);
Write('Withdrawal/Deposit:W/D');
Readln(utag);
Write('Amount of money?');
Readln(u);

CASE utag OF

'W','w' : BEGIN
if(u>a[kn].saldo+a[kn].kredit) then
writeln('Tyvrr har du inte tckning fr uttaget.')
else
BEGIN
a[kn].saldo:=a[kn].saldo-u;
if(a[kn].first=NIL) then
BEGIN
New(a[kn].first); {pointer som pekar p ett transaktionsrecord.}
a[kn].first^.next:=NIL;
a[kn].first^.trans:=u; {lagrar all info i ny-transaktionsrecordet}
a[kn].first^.date:=d;
a[kn].first^.uttag:=utag;
Temp := First; { Get the "last" valid record }
END
else
BEGIN
Temp := First; { Set an index-pointer to the first }
New(Temp^.next); { Create a new record beyond the last }
Temp^.Next^.Next := nil;
temp^.next^.trans:=u;
temp^.next^.date:=d;
temp^.next^.uttag:=utag;
Temp := Temp^.Next; { Update the last record }
END;{2:a if}
END;{1:a if}
END;{case uU}

'D','d' : BEGIN
a[kn].saldo:=a[kn].saldo+u;
if(a[kn].first=NIL) then
BEGIN
New(a[kn].first); {pointer som pekar p ett transaktionsrecord.}
a[kn].first^.next:=NIL;
a[kn].first^.trans:=u; {lagrar all info i ny-transaktionsrecordet}
a[kn].first^.date:=d;
a[kn].first^.uttag:=utag;
Temp := First; { Get the "last" valid record }
END
else
BEGIN
New(Temp^.next); { Create a new record beyond the last }
Temp^.Next^.Next := nil;
temp^.next^.trans:=u;
temp^.next^.date:=d;
temp^.next^.uttag:=utag;
Temp := Temp^.Next; { Update the last record }
END;{if}
END;{case iI}


Otherwise Writeln('Wrong input.');

END; {end of p case-satsen}

END; {end of p procedure transaktion}


PROCEDURE saldo(VAR a:kundarray;kn:Integer);

VAR s:Real;
next,temp:Pekare;
x,raknare:Integer;
BEGIN
Writeln;
s:=a[kn].saldo;
Write('Kund nr:',kn:2,' har saldot: ',s:4:2);
Writeln;

Temp := a[kn].First; { Set an index-pointer to the first }
repeat
Writeln(Temp^.trans:5, ':', Temp^.date:5, ':', Temp^.uttag:2); { write the current record }
Temp := Temp^.Next; { move to the next record }
until Temp=nil { loop until the end of the list }


END; {end of procedure saldo}





BEGIN {Mainprogram}

FOR X:=1 TO 100 DO BEGIN
With kundar[X] DO
BEGIN
costumnr:=x;{Kundnr 1->100 fylls i}
phonenr:=9;
first:=NIL;
END;
END;
continue:=false;

REPEAT

Writeln;
Writeln('New cosumer:N, Make a transaktion:T, Saldo:S. Quit:Q');
Readln(kom);

kundnr:=0;

CASE kom OF
'N','n': BEGIN Writeln;
newcostum(kundar);
continue:=false;
END;

'T','t': BEGIN Writeln;
kundnr:=0;
While(kundnr<1) OR (kundnr>100) DO BEGIN

Write('Costumnr.');
Readln(kundnr);
END;

transaktion(kundar,kundnr);
continue:=false;

END;

'S','s': BEGiN Writeln;
kundnr:=0;
While (kundnr<1) OR (kundnr>100) DO
BEGIN
Write('Costumnr.');
Readln(kundnr);
END;

saldo(kundar,kundnr);
continue:=false;
END;

'Q','q': BEGIN Writeln('The program will quit!');
continue:=true;
END;

OTHERWISE Writeln('Wrong input!');
END;
UNTIL (continue);
Writeln('Bye!');

END.{end of main program}

Comments

  • :
    : Hello again,
    :
    : im having troble with a sorting a list in a procedure. I will give you all code just in case someone needs it, but the problems as I see it starts in procedure Transaktion. The program should be able to connect transaktions dynamically to an account, and then when you ask for saldo, the transaktions should be written to screen.
    :
    : Thanx in advance
    :
    :
    : PROGRAM bank(Input,Output);
    :
    : TYPE pointer =^transaktionsrec;
    :
    : transaktionsrec=RECORD
    : trans:Integer; {+ -, mste lnkas till saldo}
    : date:Integer;
    : uttag:Char;
    : next:pointer;
    : END;
    :
    : text_string=PACKED ARRAY[1..15] OF Char;
    :
    : kundrecord=RECORD
    : namn : text_string;
    : adress : text_string;
    : phonenr : Integer;
    : saldo : Integer;
    : first : pointer;
    : costumnr : Integer;
    : kredit: Integer;
    : END;
    :
    : kundarray = ARRAY[1..100] OF kundrecord;
    :
    : VAR kom:Char;
    : kundnr:Integer;
    : continue:Boolean;
    : kundar:kundarray;
    : x:Integer;
    : temp,first,ny:pointer;
    :
    : PROCEDURE newcostum(VAR a:kundarray);
    :
    : VAR name,adres:text_string;
    : kred1,kn,phoneNumber,kred:Integer;
    : sum:Real;
    : ok:Boolean;
    : BEGIN
    :
    : Writeln;
    : ok:=false;
    : Repeat
    : BEGIN
    : Write('What number should the costumer have? Between 1 & 100:');
    : Readln(kn);
    : If (kn>=1) AND (kn<=100) Then
    : BEGIN
    : With a[kn] DO
    : IF phonenr=9 THEN ok:=true;
    : END;
    : END;
    :
    : Until ok=true;
    :
    : Write('Costumers name:');
    : Readln(name);
    : Write('Costumers street:');
    : Readln(adres);
    : Write('Costumers phonenr:');
    : Readln(phoneNumber);
    :
    : Write('Credit? Max 5000.');
    : Readln(kred1);
    : IF kred1<0 THEN kred1:=0;
    : IF kred1>5000 THEN kred1:=5000;
    : Write('Amount of money to start with:');
    : Readln(kred);
    :
    : With a[kn] DO BEGIN
    : namn:=name;
    : adress:=adres;
    : phonenr:=phoneNumber;
    : saldo:=kred;
    : first:=NIL;
    : END;
    : END; {end of procedure newcostum}
    :
    :
    : PROCEDURE transaktion(VAR a:kundarray;kn:Integer);
    :
    : VAR d:Integer;
    : temp,first,ny:Pekare;
    : u,s,k:Integer;
    : utag:Char;
    :
    : BEGIN
    :
    : s:=a[kn].saldo;
    : k:=a[kn].kredit;
    :
    : Write('What date is it?DDMMYY!');
    : Readln(d);
    : Write('Withdrawal/Deposit:W/D');
    : Readln(utag);
    : Write('Amount of money?');
    : Readln(u);
    :
    : CASE utag OF
    :
    : 'W','w' : BEGIN
    : if(u>a[kn].saldo+a[kn].kredit) then
    : writeln('Tyvrr har du inte tckning fr uttaget.')
    : else
    : BEGIN
    : a[kn].saldo:=a[kn].saldo-u;
    : if(a[kn].first=NIL) then
    : BEGIN
    : New(a[kn].first); {pointer som pekar p ett transaktionsrecord.}
    : a[kn].first^.next:=NIL;
    : a[kn].first^.trans:=u; {lagrar all info i ny-transaktionsrecordet}
    : a[kn].first^.date:=d;
    : a[kn].first^.uttag:=utag;
    : Temp := First; { Get the "last" valid record }
    : END
    : else
    : BEGIN
    : Temp := First; { Set an index-pointer to the first }
    : New(Temp^.next); { Create a new record beyond the last }
    : Temp^.Next^.Next := nil;
    : temp^.next^.trans:=u;
    : temp^.next^.date:=d;
    : temp^.next^.uttag:=utag;
    : Temp := Temp^.Next; { Update the last record }
    : END;{2:a if}
    : END;{1:a if}
    : END;{case uU}
    :
    : 'D','d' : BEGIN
    : a[kn].saldo:=a[kn].saldo+u;
    : if(a[kn].first=NIL) then
    : BEGIN
    : New(a[kn].first); {pointer som pekar p ett transaktionsrecord.}
    : a[kn].first^.next:=NIL;
    : a[kn].first^.trans:=u; {lagrar all info i ny-transaktionsrecordet}
    : a[kn].first^.date:=d;
    : a[kn].first^.uttag:=utag;
    : Temp := First; { Get the "last" valid record }
    : END
    : else
    : BEGIN
    : New(Temp^.next); { Create a new record beyond the last }
    : Temp^.Next^.Next := nil;
    : temp^.next^.trans:=u;
    : temp^.next^.date:=d;
    : temp^.next^.uttag:=utag;
    : Temp := Temp^.Next; { Update the last record }
    : END;{if}
    : END;{case iI}
    :
    :
    : Otherwise Writeln('Wrong input.');
    :
    : END; {end of p case-satsen}
    :
    : END; {end of p procedure transaktion}
    :
    :
    : PROCEDURE saldo(VAR a:kundarray;kn:Integer);
    :
    : VAR s:Real;
    : next,temp:Pekare;
    : x,raknare:Integer;
    : BEGIN
    : Writeln;
    : s:=a[kn].saldo;
    : Write('Kund nr:',kn:2,' har saldot: ',s:4:2);
    : Writeln;
    :
    : Temp := a[kn].First; { Set an index-pointer to the first }
    : repeat
    : Writeln(Temp^.trans:5, ':', Temp^.date:5, ':', Temp^.uttag:2); { write the current record }
    : Temp := Temp^.Next; { move to the next record }
    : until Temp=nil { loop until the end of the list }
    :
    :
    : END; {end of procedure saldo}
    :
    :
    :
    :
    :
    : BEGIN {Mainprogram}
    :
    : FOR X:=1 TO 100 DO BEGIN
    : With kundar[X] DO
    : BEGIN
    : costumnr:=x;{Kundnr 1->100 fylls i}
    : phonenr:=9;
    : first:=NIL;
    : END;
    : END;
    : continue:=false;
    :
    : REPEAT
    :
    : Writeln;
    : Writeln('New cosumer:N, Make a transaktion:T, Saldo:S. Quit:Q');
    : Readln(kom);
    :
    : kundnr:=0;
    :
    : CASE kom OF
    : 'N','n': BEGIN Writeln;
    : newcostum(kundar);
    : continue:=false;
    : END;
    :
    : 'T','t': BEGIN Writeln;
    : kundnr:=0;
    : While(kundnr<1) OR (kundnr>100) DO BEGIN
    :
    : Write('Costumnr.');
    : Readln(kundnr);
    : END;
    :
    : transaktion(kundar,kundnr);
    : continue:=false;
    :
    : END;
    :
    : 'S','s': BEGiN Writeln;
    : kundnr:=0;
    : While (kundnr<1) OR (kundnr>100) DO
    : BEGIN
    : Write('Costumnr.');
    : Readln(kundnr);
    : END;
    :
    : saldo(kundar,kundnr);
    : continue:=false;
    : END;
    :
    : 'Q','q': BEGIN Writeln('The program will quit!');
    : continue:=true;
    : END;
    :
    : OTHERWISE Writeln('Wrong input!');
    : END;
    : UNTIL (continue);
    : Writeln('Bye!');
    :
    : END.{end of main program}
    :
    Here is a sorting routine based on the bubble sort algorithm for the transaktionsrec records. If you need to sort the other list, simply replace the correct fields and type declarations.
    [code]
    procedure SwapData(Item1, Item2: transaktionsrec);
    var
    Temp: transaktionsrec;
    begin
    New(Temp);
    Temp^.trans:=Item1^.trans; { Store 1 record in the temporary record }
    Temp^.date:=Item1^.date;
    Temp^.uttag:=Item1^.uttag;
    Item1^.trans:=Item2^.trans; { Copy the data from the other record }
    Item1^.date:=Item2^.date;
    Item1^.uttag:=Item2^.uttag;
    Item2^.trans:=Temp^.trans; { Copy the origional data back into the list }
    Item2^.date:=Temp^.date;
    Item2^.uttag:=Temp^.uttag;
    Dispose(Temp);
    end;

    procedure SortList(First: transaktionsrec);
    var
    Temp1, Temp2: transaktionsrec;
    begin
    Temp1 := First; { Get the head of the list }
    if Temp1^.Next = nil then
    Exit; { If only 1 item then no sorting required }
    repeat
    Temp2 := Temp1; { select the head again, and loop the entire list }
    repeat
    if Temp2^.date>Temp2^.Next^.date then { If the data needs to be switched around, }
    SwapData(Temp2, Temp2^.Next); {then call SwapData to do the actual switch }
    Temp2 := Temp2^.Next; { Next pair of records }
    until Temp2^.Next=nil;
    Temp1 := Temp1^.Next; { Get the next element and start over, }
    until Temp1^.Next=nil; { unless the last pair has been sorted }
    end;
    [/code]

    PS: next time you add code to your question, please use the stylecodes. It makes your code much easier to read for us. The stylecodes are explained below the input field.
  • : :
    : : Hello again,
    : :
    : : im having troble with a sorting a list in a procedure. I will give you all code just in case someone needs it, but the problems as I see it starts in procedure Transaktion. The program should be able to connect transaktions dynamically to an account, and then when you ask for saldo, the transaktions should be written to screen.
    : :
    : : Thanx in advance
    : :
    : :
    : : PROGRAM bank(Input,Output);
    : :
    : : TYPE pointer =^transaktionsrec;
    : :
    : : transaktionsrec=RECORD
    : : trans:Integer; {+ -, mste lnkas till saldo}
    : : date:Integer;
    : : uttag:Char;
    : : next:pointer;
    : : END;
    : :
    : : text_string=PACKED ARRAY[1..15] OF Char;
    : :
    : : kundrecord=RECORD
    : : namn : text_string;
    : : adress : text_string;
    : : phonenr : Integer;
    : : saldo : Integer;
    : : first : pointer;
    : : costumnr : Integer;
    : : kredit: Integer;
    : : END;
    : :
    : : kundarray = ARRAY[1..100] OF kundrecord;
    : :
    : : VAR kom:Char;
    : : kundnr:Integer;
    : : continue:Boolean;
    : : kundar:kundarray;
    : : x:Integer;
    : : temp,first,ny:pointer;
    : :
    : : PROCEDURE newcostum(VAR a:kundarray);
    : :
    : : VAR name,adres:text_string;
    : : kred1,kn,phoneNumber,kred:Integer;
    : : sum:Real;
    : : ok:Boolean;
    : : BEGIN
    : :
    : : Writeln;
    : : ok:=false;
    : : Repeat
    : : BEGIN
    : : Write('What number should the costumer have? Between 1 & 100:');
    : : Readln(kn);
    : : If (kn>=1) AND (kn<=100) Then
    : : BEGIN
    : : With a[kn] DO
    : : IF phonenr=9 THEN ok:=true;
    : : END;
    : : END;
    : :
    : : Until ok=true;
    : :
    : : Write('Costumers name:');
    : : Readln(name);
    : : Write('Costumers street:');
    : : Readln(adres);
    : : Write('Costumers phonenr:');
    : : Readln(phoneNumber);
    : :
    : : Write('Credit? Max 5000.');
    : : Readln(kred1);
    : : IF kred1<0 THEN kred1:=0;
    : : IF kred1>5000 THEN kred1:=5000;
    : : Write('Amount of money to start with:');
    : : Readln(kred);
    : :
    : : With a[kn] DO BEGIN
    : : namn:=name;
    : : adress:=adres;
    : : phonenr:=phoneNumber;
    : : saldo:=kred;
    : : first:=NIL;
    : : END;
    : : END; {end of procedure newcostum}
    : :
    : :
    : : PROCEDURE transaktion(VAR a:kundarray;kn:Integer);
    : :
    : : VAR d:Integer;
    : : temp,first,ny:Pekare;
    : : u,s,k:Integer;
    : : utag:Char;
    : :
    : : BEGIN
    : :
    : : s:=a[kn].saldo;
    : : k:=a[kn].kredit;
    : :
    : : Write('What date is it?DDMMYY!');
    : : Readln(d);
    : : Write('Withdrawal/Deposit:W/D');
    : : Readln(utag);
    : : Write('Amount of money?');
    : : Readln(u);
    : :
    : : CASE utag OF
    : :
    : : 'W','w' : BEGIN
    : : if(u>a[kn].saldo+a[kn].kredit) then
    : : writeln('Tyvrr har du inte tckning fr uttaget.')
    : : else
    : : BEGIN
    : : a[kn].saldo:=a[kn].saldo-u;
    : : if(a[kn].first=NIL) then
    : : BEGIN
    : : New(a[kn].first); {pointer som pekar p ett transaktionsrecord.}
    : : a[kn].first^.next:=NIL;
    : : a[kn].first^.trans:=u; {lagrar all info i ny-transaktionsrecordet}
    : : a[kn].first^.date:=d;
    : : a[kn].first^.uttag:=utag;
    : : Temp := First; { Get the "last" valid record }
    : : END
    : : else
    : : BEGIN
    : : Temp := First; { Set an index-pointer to the first }
    : : New(Temp^.next); { Create a new record beyond the last }
    : : Temp^.Next^.Next := nil;
    : : temp^.next^.trans:=u;
    : : temp^.next^.date:=d;
    : : temp^.next^.uttag:=utag;
    : : Temp := Temp^.Next; { Update the last record }
    : : END;{2:a if}
    : : END;{1:a if}
    : : END;{case uU}
    : :
    : : 'D','d' : BEGIN
    : : a[kn].saldo:=a[kn].saldo+u;
    : : if(a[kn].first=NIL) then
    : : BEGIN
    : : New(a[kn].first); {pointer som pekar p ett transaktionsrecord.}
    : : a[kn].first^.next:=NIL;
    : : a[kn].first^.trans:=u; {lagrar all info i ny-transaktionsrecordet}
    : : a[kn].first^.date:=d;
    : : a[kn].first^.uttag:=utag;
    : : Temp := First; { Get the "last" valid record }
    : : END
    : : else
    : : BEGIN
    : : New(Temp^.next); { Create a new record beyond the last }
    : : Temp^.Next^.Next := nil;
    : : temp^.next^.trans:=u;
    : : temp^.next^.date:=d;
    : : temp^.next^.uttag:=utag;
    : : Temp := Temp^.Next; { Update the last record }
    : : END;{if}
    : : END;{case iI}
    : :
    : :
    : : Otherwise Writeln('Wrong input.');
    : :
    : : END; {end of p case-satsen}
    : :
    : : END; {end of p procedure transaktion}
    : :
    : :
    : : PROCEDURE saldo(VAR a:kundarray;kn:Integer);
    : :
    : : VAR s:Real;
    : : next,temp:Pekare;
    : : x,raknare:Integer;
    : : BEGIN
    : : Writeln;
    : : s:=a[kn].saldo;
    : : Write('Kund nr:',kn:2,' har saldot: ',s:4:2);
    : : Writeln;
    : :
    : : Temp := a[kn].First; { Set an index-pointer to the first }
    : : repeat
    : : Writeln(Temp^.trans:5, ':', Temp^.date:5, ':', Temp^.uttag:2); { write the current record }
    : : Temp := Temp^.Next; { move to the next record }
    : : until Temp=nil { loop until the end of the list }
    : :
    : :
    : : END; {end of procedure saldo}
    : :
    : :
    : :
    : :
    : :
    : : BEGIN {Mainprogram}
    : :
    : : FOR X:=1 TO 100 DO BEGIN
    : : With kundar[X] DO
    : : BEGIN
    : : costumnr:=x;{Kundnr 1->100 fylls i}
    : : phonenr:=9;
    : : first:=NIL;
    : : END;
    : : END;
    : : continue:=false;
    : :
    : : REPEAT
    : :
    : : Writeln;
    : : Writeln('New cosumer:N, Make a transaktion:T, Saldo:S. Quit:Q');
    : : Readln(kom);
    : :
    : : kundnr:=0;
    : :
    : : CASE kom OF
    : : 'N','n': BEGIN Writeln;
    : : newcostum(kundar);
    : : continue:=false;
    : : END;
    : :
    : : 'T','t': BEGIN Writeln;
    : : kundnr:=0;
    : : While(kundnr<1) OR (kundnr>100) DO BEGIN
    : :
    : : Write('Costumnr.');
    : : Readln(kundnr);
    : : END;
    : :
    : : transaktion(kundar,kundnr);
    : : continue:=false;
    : :
    : : END;
    : :
    : : 'S','s': BEGiN Writeln;
    : : kundnr:=0;
    : : While (kundnr<1) OR (kundnr>100) DO
    : : BEGIN
    : : Write('Costumnr.');
    : : Readln(kundnr);
    : : END;
    : :
    : : saldo(kundar,kundnr);
    : : continue:=false;
    : : END;
    : :
    : : 'Q','q': BEGIN Writeln('The program will quit!');
    : : continue:=true;
    : : END;
    : :
    : : OTHERWISE Writeln('Wrong input!');
    : : END;
    : : UNTIL (continue);
    : : Writeln('Bye!');
    : :
    : : END.{end of main program}
    : :
    : Here is a sorting routine based on the bubble sort algorithm for the transaktionsrec records. If you need to sort the other list, simply replace the correct fields and type declarations.
    : [code]
    : procedure SwapData(Item1, Item2: transaktionsrec);
    : var
    : Temp: transaktionsrec;
    : begin
    : New(Temp);
    : Temp^.trans:=Item1^.trans; { Store 1 record in the temporary record }
    : Temp^.date:=Item1^.date;
    : Temp^.uttag:=Item1^.uttag;
    : Item1^.trans:=Item2^.trans; { Copy the data from the other record }
    : Item1^.date:=Item2^.date;
    : Item1^.uttag:=Item2^.uttag;
    : Item2^.trans:=Temp^.trans; { Copy the origional data back into the list }
    : Item2^.date:=Temp^.date;
    : Item2^.uttag:=Temp^.uttag;
    : Dispose(Temp);
    : end;
    :
    : procedure SortList(First: transaktionsrec);
    : var
    : Temp1, Temp2: transaktionsrec;
    : begin
    : Temp1 := First; { Get the head of the list }
    : if Temp1^.Next = nil then
    : Exit; { If only 1 item then no sorting required }
    : repeat
    : Temp2 := Temp1; { select the head again, and loop the entire list }
    : repeat
    : if Temp2^.date>Temp2^.Next^.date then { If the data needs to be switched around, }
    : SwapData(Temp2, Temp2^.Next); {then call SwapData to do the actual switch }
    : Temp2 := Temp2^.Next; { Next pair of records }
    : until Temp2^.Next=nil;
    : Temp1 := Temp1^.Next; { Get the next element and start over, }
    : until Temp1^.Next=nil; { unless the last pair has been sorted }
    : end;
    : [/code]
    :
    : PS: next time you add code to your question, please use the stylecodes. It makes your code much easier to read for us. The stylecodes are explained below the input field.
    :

  • : : :
    : : : Hello again,
    : : :
    : : : im having troble with a sorting a list in a procedure. I will give you all code just in case someone needs it, but the problems as I see it starts in procedure Transaktion. The program should be able to connect transaktions dynamically to an account, and then when you ask for saldo, the transaktions should be written to screen.
    : : :
    : : : Thanx in advance
    : : :
    : : :
    : : : PROGRAM bank(Input,Output);
    : : :
    : : : TYPE pointer =^transaktionsrec;
    : : :
    : : : transaktionsrec=RECORD
    : : : trans:Integer; {+ -, mste lnkas till saldo}
    : : : date:Integer;
    : : : uttag:Char;
    : : : next:pointer;
    : : : END;
    : : :
    : : : text_string=PACKED ARRAY[1..15] OF Char;
    : : :
    : : : kundrecord=RECORD
    : : : namn : text_string;
    : : : adress : text_string;
    : : : phonenr : Integer;
    : : : saldo : Integer;
    : : : first : pointer;
    : : : costumnr : Integer;
    : : : kredit: Integer;
    : : : END;
    : : :
    : : : kundarray = ARRAY[1..100] OF kundrecord;
    : : :
    : : : VAR kom:Char;
    : : : kundnr:Integer;
    : : : continue:Boolean;
    : : : kundar:kundarray;
    : : : x:Integer;
    : : : temp,first,ny:pointer;
    : : :
    : : : PROCEDURE newcostum(VAR a:kundarray);
    : : :
    : : : VAR name,adres:text_string;
    : : : kred1,kn,phoneNumber,kred:Integer;
    : : : sum:Real;
    : : : ok:Boolean;
    : : : BEGIN
    : : :
    : : : Writeln;
    : : : ok:=false;
    : : : Repeat
    : : : BEGIN
    : : : Write('What number should the costumer have? Between 1 & 100:');
    : : : Readln(kn);
    : : : If (kn>=1) AND (kn<=100) Then
    : : : BEGIN
    : : : With a[kn] DO
    : : : IF phonenr=9 THEN ok:=true;
    : : : END;
    : : : END;
    : : :
    : : : Until ok=true;
    : : :
    : : : Write('Costumers name:');
    : : : Readln(name);
    : : : Write('Costumers street:');
    : : : Readln(adres);
    : : : Write('Costumers phonenr:');
    : : : Readln(phoneNumber);
    : : :
    : : : Write('Credit? Max 5000.');
    : : : Readln(kred1);
    : : : IF kred1<0 THEN kred1:=0;
    : : : IF kred1>5000 THEN kred1:=5000;
    : : : Write('Amount of money to start with:');
    : : : Readln(kred);
    : : :
    : : : With a[kn] DO BEGIN
    : : : namn:=name;
    : : : adress:=adres;
    : : : phonenr:=phoneNumber;
    : : : saldo:=kred;
    : : : first:=NIL;
    : : : END;
    : : : END; {end of procedure newcostum}
    : : :
    : : :
    : : : PROCEDURE transaktion(VAR a:kundarray;kn:Integer);
    : : :
    : : : VAR d:Integer;
    : : : temp,first,ny:Pekare;
    : : : u,s,k:Integer;
    : : : utag:Char;
    : : :
    : : : BEGIN
    : : :
    : : : s:=a[kn].saldo;
    : : : k:=a[kn].kredit;
    : : :
    : : : Write('What date is it?DDMMYY!');
    : : : Readln(d);
    : : : Write('Withdrawal/Deposit:W/D');
    : : : Readln(utag);
    : : : Write('Amount of money?');
    : : : Readln(u);
    : : :
    : : : CASE utag OF
    : : :
    : : : 'W','w' : BEGIN
    : : : if(u>a[kn].saldo+a[kn].kredit) then
    : : : writeln('Tyvrr har du inte tckning fr uttaget.')
    : : : else
    : : : BEGIN
    : : : a[kn].saldo:=a[kn].saldo-u;
    : : : if(a[kn].first=NIL) then
    : : : BEGIN
    : : : New(a[kn].first); {pointer som pekar p ett transaktionsrecord.}
    : : : a[kn].first^.next:=NIL;
    : : : a[kn].first^.trans:=u; {lagrar all info i ny-transaktionsrecordet}
    : : : a[kn].first^.date:=d;
    : : : a[kn].first^.uttag:=utag;
    : : : Temp := First; { Get the "last" valid record }
    : : : END
    : : : else
    : : : BEGIN
    : : : Temp := First; { Set an index-pointer to the first }
    : : : New(Temp^.next); { Create a new record beyond the last }
    : : : Temp^.Next^.Next := nil;
    : : : temp^.next^.trans:=u;
    : : : temp^.next^.date:=d;
    : : : temp^.next^.uttag:=utag;
    : : : Temp := Temp^.Next; { Update the last record }
    : : : END;{2:a if}
    : : : END;{1:a if}
    : : : END;{case uU}
    : : :
    : : : 'D','d' : BEGIN
    : : : a[kn].saldo:=a[kn].saldo+u;
    : : : if(a[kn].first=NIL) then
    : : : BEGIN
    : : : New(a[kn].first); {pointer som pekar p ett transaktionsrecord.}
    : : : a[kn].first^.next:=NIL;
    : : : a[kn].first^.trans:=u; {lagrar all info i ny-transaktionsrecordet}
    : : : a[kn].first^.date:=d;
    : : : a[kn].first^.uttag:=utag;
    : : : Temp := First; { Get the "last" valid record }
    : : : END
    : : : else
    : : : BEGIN
    : : : New(Temp^.next); { Create a new record beyond the last }
    : : : Temp^.Next^.Next := nil;
    : : : temp^.next^.trans:=u;
    : : : temp^.next^.date:=d;
    : : : temp^.next^.uttag:=utag;
    : : : Temp := Temp^.Next; { Update the last record }
    : : : END;{if}
    : : : END;{case iI}
    : : :
    : : :
    : : : Otherwise Writeln('Wrong input.');
    : : :
    : : : END; {end of p case-satsen}
    : : :
    : : : END; {end of p procedure transaktion}
    : : :
    : : :
    : : : PROCEDURE saldo(VAR a:kundarray;kn:Integer);
    : : :
    : : : VAR s:Real;
    : : : next,temp:Pekare;
    : : : x,raknare:Integer;
    : : : BEGIN
    : : : Writeln;
    : : : s:=a[kn].saldo;
    : : : Write('Kund nr:',kn:2,' har saldot: ',s:4:2);
    : : : Writeln;
    : : :
    : : : Temp := a[kn].First; { Set an index-pointer to the first }
    : : : repeat
    : : : Writeln(Temp^.trans:5, ':', Temp^.date:5, ':', Temp^.uttag:2); { write the current record }
    : : : Temp := Temp^.Next; { move to the next record }
    : : : until Temp=nil { loop until the end of the list }
    : : :
    : : :
    : : : END; {end of procedure saldo}
    : : :
    : : :
    : : :
    : : :
    : : :
    : : : BEGIN {Mainprogram}
    : : :
    : : : FOR X:=1 TO 100 DO BEGIN
    : : : With kundar[X] DO
    : : : BEGIN
    : : : costumnr:=x;{Kundnr 1->100 fylls i}
    : : : phonenr:=9;
    : : : first:=NIL;
    : : : END;
    : : : END;
    : : : continue:=false;
    : : :
    : : : REPEAT
    : : :
    : : : Writeln;
    : : : Writeln('New cosumer:N, Make a transaktion:T, Saldo:S. Quit:Q');
    : : : Readln(kom);
    : : :
    : : : kundnr:=0;
    : : :
    : : : CASE kom OF
    : : : 'N','n': BEGIN Writeln;
    : : : newcostum(kundar);
    : : : continue:=false;
    : : : END;
    : : :
    : : : 'T','t': BEGIN Writeln;
    : : : kundnr:=0;
    : : : While(kundnr<1) OR (kundnr>100) DO BEGIN
    : : :
    : : : Write('Costumnr.');
    : : : Readln(kundnr);
    : : : END;
    : : :
    : : : transaktion(kundar,kundnr);
    : : : continue:=false;
    : : :
    : : : END;
    : : :
    : : : 'S','s': BEGiN Writeln;
    : : : kundnr:=0;
    : : : While (kundnr<1) OR (kundnr>100) DO
    : : : BEGIN
    : : : Write('Costumnr.');
    : : : Readln(kundnr);
    : : : END;
    : : :
    : : : saldo(kundar,kundnr);
    : : : continue:=false;
    : : : END;
    : : :
    : : : 'Q','q': BEGIN Writeln('The program will quit!');
    : : : continue:=true;
    : : : END;
    : : :
    : : : OTHERWISE Writeln('Wrong input!');
    : : : END;
    : : : UNTIL (continue);
    : : : Writeln('Bye!');
    : : :
    : : : END.{end of main program}
    : : :
    : : Here is a sorting routine based on the bubble sort algorithm for the transaktionsrec records. If you need to sort the other list, simply replace the correct fields and type declarations.
    : : [code]
    : : procedure SwapData(Item1, Item2: transaktionsrec);
    : : var
    : : Temp: transaktionsrec;
    : : begin
    : : New(Temp);
    : : Temp^.trans:=Item1^.trans; { Store 1 record in the temporary record }
    : : Temp^.date:=Item1^.date;
    : : Temp^.uttag:=Item1^.uttag;
    : : Item1^.trans:=Item2^.trans; { Copy the data from the other record }
    : : Item1^.date:=Item2^.date;
    : : Item1^.uttag:=Item2^.uttag;
    : : Item2^.trans:=Temp^.trans; { Copy the origional data back into the list }
    : : Item2^.date:=Temp^.date;
    : : Item2^.uttag:=Temp^.uttag;
    : : Dispose(Temp);
    : : end;
    : :
    : : procedure SortList(First: transaktionsrec);
    : : var
    : : Temp1, Temp2: transaktionsrec;
    : : begin
    : : Temp1 := First; { Get the head of the list }
    : : if Temp1^.Next = nil then
    : : Exit; { If only 1 item then no sorting required }
    : : repeat
    : : Temp2 := Temp1; { select the head again, and loop the entire list }
    : : repeat
    : : if Temp2^.date>Temp2^.Next^.date then { If the data needs to be switched around, }
    : : SwapData(Temp2, Temp2^.Next); {then call SwapData to do the actual switch }
    : : Temp2 := Temp2^.Next; { Next pair of records }
    : : until Temp2^.Next=nil;
    : : Temp1 := Temp1^.Next; { Get the next element and start over, }
    : : until Temp1^.Next=nil; { unless the last pair has been sorted }
    : : end;
    : : [/code]
    : :
    : : PS: next time you add code to your question, please use the stylecodes. It makes your code much easier to read for us. The stylecodes are explained below the input field.
    : :
    :
    Hello,

    thanx for the codes, but to be honest i dont really understand how the procedures you send me could be useful in the program. The problem is to have the procedure Transaktions that will build a list with trans.rec. that are pointed out with the "first" pointer in every "kundrecord" that are placed in an array[1..100]. Im not very good at dynamical lists, as you notice, and I dont know how to deal with a "temp" in a procedure.



  • : : : :
    : : : : Hello again,
    : : : :
    : : : : im having troble with a sorting a list in a procedure. I will give you all code just in case someone needs it, but the problems as I see it starts in procedure Transaktion. The program should be able to connect transaktions dynamically to an account, and then when you ask for saldo, the transaktions should be written to screen.
    : : : :
    : : : : Thanx in advance
    : : : :
    : : : :
    : : : : PROGRAM bank(Input,Output);
    : : : :
    : : : : TYPE pointer =^transaktionsrec;
    : : : :
    : : : : transaktionsrec=RECORD
    : : : : trans:Integer; {+ -, mste lnkas till saldo}
    : : : : date:Integer;
    : : : : uttag:Char;
    : : : : next:pointer;
    : : : : END;
    : : : :
    : : : : text_string=PACKED ARRAY[1..15] OF Char;
    : : : :
    : : : : kundrecord=RECORD
    : : : : namn : text_string;
    : : : : adress : text_string;
    : : : : phonenr : Integer;
    : : : : saldo : Integer;
    : : : : first : pointer;
    : : : : costumnr : Integer;
    : : : : kredit: Integer;
    : : : : END;
    : : : :
    : : : : kundarray = ARRAY[1..100] OF kundrecord;
    : : : :
    : : : : VAR kom:Char;
    : : : : kundnr:Integer;
    : : : : continue:Boolean;
    : : : : kundar:kundarray;
    : : : : x:Integer;
    : : : : temp,first,ny:pointer;
    : : : :
    : : : : PROCEDURE newcostum(VAR a:kundarray);
    : : : :
    : : : : VAR name,adres:text_string;
    : : : : kred1,kn,phoneNumber,kred:Integer;
    : : : : sum:Real;
    : : : : ok:Boolean;
    : : : : BEGIN
    : : : :
    : : : : Writeln;
    : : : : ok:=false;
    : : : : Repeat
    : : : : BEGIN
    : : : : Write('What number should the costumer have? Between 1 & 100:');
    : : : : Readln(kn);
    : : : : If (kn>=1) AND (kn<=100) Then
    : : : : BEGIN
    : : : : With a[kn] DO
    : : : : IF phonenr=9 THEN ok:=true;
    : : : : END;
    : : : : END;
    : : : :
    : : : : Until ok=true;
    : : : :
    : : : : Write('Costumers name:');
    : : : : Readln(name);
    : : : : Write('Costumers street:');
    : : : : Readln(adres);
    : : : : Write('Costumers phonenr:');
    : : : : Readln(phoneNumber);
    : : : :
    : : : : Write('Credit? Max 5000.');
    : : : : Readln(kred1);
    : : : : IF kred1<0 THEN kred1:=0;
    : : : : IF kred1>5000 THEN kred1:=5000;
    : : : : Write('Amount of money to start with:');
    : : : : Readln(kred);
    : : : :
    : : : : With a[kn] DO BEGIN
    : : : : namn:=name;
    : : : : adress:=adres;
    : : : : phonenr:=phoneNumber;
    : : : : saldo:=kred;
    : : : : first:=NIL;
    : : : : END;
    : : : : END; {end of procedure newcostum}
    : : : :
    : : : :
    : : : : PROCEDURE transaktion(VAR a:kundarray;kn:Integer);
    : : : :
    : : : : VAR d:Integer;
    : : : : temp,first,ny:Pekare;
    : : : : u,s,k:Integer;
    : : : : utag:Char;
    : : : :
    : : : : BEGIN
    : : : :
    : : : : s:=a[kn].saldo;
    : : : : k:=a[kn].kredit;
    : : : :
    : : : : Write('What date is it?DDMMYY!');
    : : : : Readln(d);
    : : : : Write('Withdrawal/Deposit:W/D');
    : : : : Readln(utag);
    : : : : Write('Amount of money?');
    : : : : Readln(u);
    : : : :
    : : : : CASE utag OF
    : : : :
    : : : : 'W','w' : BEGIN
    : : : : if(u>a[kn].saldo+a[kn].kredit) then
    : : : : writeln('Tyvrr har du inte tckning fr uttaget.')
    : : : : else
    : : : : BEGIN
    : : : : a[kn].saldo:=a[kn].saldo-u;
    : : : : if(a[kn].first=NIL) then
    : : : : BEGIN
    : : : : New(a[kn].first); {pointer som pekar p ett transaktionsrecord.}
    : : : : a[kn].first^.next:=NIL;
    : : : : a[kn].first^.trans:=u; {lagrar all info i ny-transaktionsrecordet}
    : : : : a[kn].first^.date:=d;
    : : : : a[kn].first^.uttag:=utag;
    : : : : Temp := First; { Get the "last" valid record }
    : : : : END
    : : : : else
    : : : : BEGIN
    : : : : Temp := First; { Set an index-pointer to the first }
    : : : : New(Temp^.next); { Create a new record beyond the last }
    : : : : Temp^.Next^.Next := nil;
    : : : : temp^.next^.trans:=u;
    : : : : temp^.next^.date:=d;
    : : : : temp^.next^.uttag:=utag;
    : : : : Temp := Temp^.Next; { Update the last record }
    : : : : END;{2:a if}
    : : : : END;{1:a if}
    : : : : END;{case uU}
    : : : :
    : : : : 'D','d' : BEGIN
    : : : : a[kn].saldo:=a[kn].saldo+u;
    : : : : if(a[kn].first=NIL) then
    : : : : BEGIN
    : : : : New(a[kn].first); {pointer som pekar p ett transaktionsrecord.}
    : : : : a[kn].first^.next:=NIL;
    : : : : a[kn].first^.trans:=u; {lagrar all info i ny-transaktionsrecordet}
    : : : : a[kn].first^.date:=d;
    : : : : a[kn].first^.uttag:=utag;
    : : : : Temp := First; { Get the "last" valid record }
    : : : : END
    : : : : else
    : : : : BEGIN
    : : : : New(Temp^.next); { Create a new record beyond the last }
    : : : : Temp^.Next^.Next := nil;
    : : : : temp^.next^.trans:=u;
    : : : : temp^.next^.date:=d;
    : : : : temp^.next^.uttag:=utag;
    : : : : Temp := Temp^.Next; { Update the last record }
    : : : : END;{if}
    : : : : END;{case iI}
    : : : :
    : : : :
    : : : : Otherwise Writeln('Wrong input.');
    : : : :
    : : : : END; {end of p case-satsen}
    : : : :
    : : : : END; {end of p procedure transaktion}
    : : : :
    : : : :
    : : : : PROCEDURE saldo(VAR a:kundarray;kn:Integer);
    : : : :
    : : : : VAR s:Real;
    : : : : next,temp:Pekare;
    : : : : x,raknare:Integer;
    : : : : BEGIN
    : : : : Writeln;
    : : : : s:=a[kn].saldo;
    : : : : Write('Kund nr:',kn:2,' har saldot: ',s:4:2);
    : : : : Writeln;
    : : : :
    : : : : Temp := a[kn].First; { Set an index-pointer to the first }
    : : : : repeat
    : : : : Writeln(Temp^.trans:5, ':', Temp^.date:5, ':', Temp^.uttag:2); { write the current record }
    : : : : Temp := Temp^.Next; { move to the next record }
    : : : : until Temp=nil { loop until the end of the list }
    : : : :
    : : : :
    : : : : END; {end of procedure saldo}
    : : : :
    : : : :
    : : : :
    : : : :
    : : : :
    : : : : BEGIN {Mainprogram}
    : : : :
    : : : : FOR X:=1 TO 100 DO BEGIN
    : : : : With kundar[X] DO
    : : : : BEGIN
    : : : : costumnr:=x;{Kundnr 1->100 fylls i}
    : : : : phonenr:=9;
    : : : : first:=NIL;
    : : : : END;
    : : : : END;
    : : : : continue:=false;
    : : : :
    : : : : REPEAT
    : : : :
    : : : : Writeln;
    : : : : Writeln('New cosumer:N, Make a transaktion:T, Saldo:S. Quit:Q');
    : : : : Readln(kom);
    : : : :
    : : : : kundnr:=0;
    : : : :
    : : : : CASE kom OF
    : : : : 'N','n': BEGIN Writeln;
    : : : : newcostum(kundar);
    : : : : continue:=false;
    : : : : END;
    : : : :
    : : : : 'T','t': BEGIN Writeln;
    : : : : kundnr:=0;
    : : : : While(kundnr<1) OR (kundnr>100) DO BEGIN
    : : : :
    : : : : Write('Costumnr.');
    : : : : Readln(kundnr);
    : : : : END;
    : : : :
    : : : : transaktion(kundar,kundnr);
    : : : : continue:=false;
    : : : :
    : : : : END;
    : : : :
    : : : : 'S','s': BEGiN Writeln;
    : : : : kundnr:=0;
    : : : : While (kundnr<1) OR (kundnr>100) DO
    : : : : BEGIN
    : : : : Write('Costumnr.');
    : : : : Readln(kundnr);
    : : : : END;
    : : : :
    : : : : saldo(kundar,kundnr);
    : : : : continue:=false;
    : : : : END;
    : : : :
    : : : : 'Q','q': BEGIN Writeln('The program will quit!');
    : : : : continue:=true;
    : : : : END;
    : : : :
    : : : : OTHERWISE Writeln('Wrong input!');
    : : : : END;
    : : : : UNTIL (continue);
    : : : : Writeln('Bye!');
    : : : :
    : : : : END.{end of main program}
    : : : :
    : : : Here is a sorting routine based on the bubble sort algorithm for the transaktionsrec records. If you need to sort the other list, simply replace the correct fields and type declarations.
    : : : [code]
    : : : procedure SwapData(Item1, Item2: transaktionsrec);
    : : : var
    : : : Temp: transaktionsrec;
    : : : begin
    : : : New(Temp);
    : : : Temp^.trans:=Item1^.trans; { Store 1 record in the temporary record }
    : : : Temp^.date:=Item1^.date;
    : : : Temp^.uttag:=Item1^.uttag;
    : : : Item1^.trans:=Item2^.trans; { Copy the data from the other record }
    : : : Item1^.date:=Item2^.date;
    : : : Item1^.uttag:=Item2^.uttag;
    : : : Item2^.trans:=Temp^.trans; { Copy the origional data back into the list }
    : : : Item2^.date:=Temp^.date;
    : : : Item2^.uttag:=Temp^.uttag;
    : : : Dispose(Temp);
    : : : end;
    : : :
    : : : procedure SortList(First: transaktionsrec);
    : : : var
    : : : Temp1, Temp2: transaktionsrec;
    : : : begin
    : : : Temp1 := First; { Get the head of the list }
    : : : if Temp1^.Next = nil then
    : : : Exit; { If only 1 item then no sorting required }
    : : : repeat
    : : : Temp2 := Temp1; { select the head again, and loop the entire list }
    : : : repeat
    : : : if Temp2^.date>Temp2^.Next^.date then { If the data needs to be switched around, }
    : : : SwapData(Temp2, Temp2^.Next); {then call SwapData to do the actual switch }
    : : : Temp2 := Temp2^.Next; { Next pair of records }
    : : : until Temp2^.Next=nil;
    : : : Temp1 := Temp1^.Next; { Get the next element and start over, }
    : : : until Temp1^.Next=nil; { unless the last pair has been sorted }
    : : : end;
    : : : [/code]
    : : :
    : : : PS: next time you add code to your question, please use the stylecodes. It makes your code much easier to read for us. The stylecodes are explained below the input field.
    : : :
    : :
    : Hello,
    :
    : thanx for the codes, but to be honest i dont really understand how the procedures you send me could be useful in the program. The problem is to have the procedure Transaktions that will build a list with trans.rec. that are pointed out with the "first" pointer in every "kundrecord" that are placed in an array[1..100]. Im not very good at dynamical lists, as you notice, and I dont know how to deal with a "temp" in a procedure.
    :
    Sorry, I misunderstood your question.
    Let me first start out with the "temp". This I use for temporary data. So each time I need to store a single record for a short time, I use a temp variable to do this. It works exactly the same as any other variable.
    The question how to build link the transaction list to the kundrecord is easy. Instead of usinf a first variable, you use the a[##].first (where ## is a number). The best way to find the line, which need to be updated, is to remove "First" from your variable declaration. Then compile your program. It will probably give an error with each solitary First, and not the "a[kn].First".
    Also in your code to add a new transaction to an existing list there is a part missing. Here is the updated code for the withdrawal. You can base your deposit part on this.
    [code]
    a[kn].saldo:=a[kn].saldo-u;
    if(a[kn].first=NIL) then
    BEGIN
    New(a[kn].first); {pointer som pekar p ett transaktionsrecord.}
    a[kn].first^.next:=NIL;
    a[kn].first^.trans:=u; {lagrar all info i ny-transaktionsrecordet}
    a[kn].first^.date:=d;
    a[kn].first^.uttag:=utag;
    END
    else
    BEGIN
    Temp := a[kn].First; { Set an index-pointer to the first }
    while Temp^.next<> nil do
    Temp := Temp^.Next; { find the last record in the list }
    New(Temp^.next); { Create a new record beyond the last }
    temp^.next^.trans:=u;
    temp^.next^.date:=d;
    temp^.next^.uttag:=utag;
    END;{2:a if}
    END;{1:a if}
    END;{case uU}
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories