sort problem - Programmers Heaven

Howdy, Stranger!

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

Categories

sort problem

MaStMuNdAMaStMuNdA Posts: 17Member
[b][red]This message was edited by MaStMuNdA at 2003-3-31 10:3:57[/red][/b][hr]
hi every one

i have another problem. ok wats happening is that since i created a unit to hold some of my program my sorting of one of the files is giving me a problem.

i create an invoice do all my stuf and when i exit it its is suposed to sort the file out by the invoice reference number. but it doesnt do that. further more when i deleted the stupied sort file procedure and made it again the first 2 records would come as blank then the file would show the third record. so after this i reverted back to the sort procedure i deleted.


ok these are the variables in my unit
[code]

unit things;

interface

uses
crt, dos;

type
string3 = string[3];
string4 = string[4];
string5 = string[5];
string9 = string[9];
string10 = string[10];
string12 = string[12];
string13 = string[13];
string20 = string[20];
string25 = string[25];
string30 = string[30];
string40 = string[40];
string80 = string[80];
string100 = string[100];
string150 = string[150];

crec = record
cref : string4;
ctitle : string4;
csurname : string30;
cfname : string25;
caddress : string40;
cpcode : string9;
cpartnership : string3;
cshare : string5;
ctitle2 : string4;
csurname2 : string30;
cfname2 : string25;
caddress2 : string40;
cpcode2 : string9;
cpartnership2 : string3;
cshare2 : string5;
cbname : string20;
cbaddress : string40;
cbpcode : string9;
cbtel : string13;
cbfax : string13;
ccomment : string150;
end;

prec = record
pref : string5;
pcref : string4;
pdate : string10;
pcharges : string100;
ptotal : string9;
end;

urec = record
uref : string5;
ucref : string4;
udate : string10;
ucharges : string100;
utotal : string9;
end;

const
space = ' ';
vat = 0.175;
vatno = 'VAT Reg. No. 250 376 825';

var

clientrec : crec;
clientfile : file of crec;
paidrec : prec;
paidfile : file of prec;
unpaidrec : urec;
unpaidfile : file of urec;
year,month,day,dayofweek : word;
numc : integer;
clientref : array[0..20] of string4;
clienttitle : array[0..20] of string4;
clientsurname : array[0..20] of string30;
clientfname : array[0..20] of string25;
clientaddress : array[0..20] of string40;
clientpcode : array[0..20] of string9;
clientpartnership : array[0..20] of string3;
clientshare : array[0..20] of string5;
clienttitle2 : array[0..20] of string4;
clientsurname2 : array[0..20] of string30;
clientfname2 : array[0..20] of string25;
clientaddress2 : array[0..20] of string40;
clientpcode2 : array[0..20] of string9;
clientpartnership2 : array[0..20] of string3;
clientshare2 : array[0..20] of string5;
clientbname : array[0..20] of string20;
clientbaddress : array[0..20] of string40;
clientbpcode : array[0..20] of string9;
clientbtel : array[0..20] of string13;
clientbfax : array[0..20] of string13;
clientcomment : array[0..20] of string150;
nump : integer;
paidref : array[0..20] of string5;
paidcref : array[0..20] of string4;
paiddate : array[0..20] of string10;
paidcharges : array[0..20] of string100;
paidtotal : array[0..20] of string9;
numu : integer;
unpaidref : array[0..20] of string5;
unpaidcref : array[0..20] of string4;
unpaiddate : array[0..20] of string10;
unpaidcharges : array[0..20] of string100;
unpaidtotal : array[0..20] of string9;

procedure check;
procedure searchmenu;
procedure invoicesearchrefno;
procedure invoicesearchoverdue;
procedure clientsearchrefno;
procedure errorscreen;


These are the 3 procedures in my main program - sortunpaid file,, swap AND createinvoice


procedure swap5(var a,b : string5);
var
temp : string5;
begin
temp := a;
a := b;
b:= temp
end;


procedure sortunpaidfile;
var
x,flag : integer;
begin
repeat
flag := 0;
for x := 1 to (nump-1) do
begin
if unpaidref[x] > unpaidref[x+1] then
begin
swap5(unpaidref[x],unpaidref[x+1]);
swap4(unpaidcref[x],unpaidcref[x+1]);
swap10(unpaiddate[x],unpaiddate[x+1]);
swap100(unpaidcharges[x],unpaidcharges[x+1]);
swap9(unpaidtotal[x],unpaidtotal[x+1]);
flag := 1;
end
end;
until flag = 0
end;
[/code]
«1

Comments

  • MaStMuNdAMaStMuNdA Posts: 17Member
    [b][red]This message was edited by MaStMuNdA at 2003-3-31 10:6:14[/red][/b][hr]
    [b][red]This message was edited by MaStMuNdA at 2003-3-30 15:45:33[/red][/b][hr]
    [code]
    procedure createinvoice;
    var
    client : string;
    top,bottom,mid : integer;
    top1,bottom1,mid1 : integer;
    top2,bottom2,mid2 : integer;
    confirm : char;
    fig1, fig2, fig3 : string;
    fig4, fig5, fig6 : string;
    fig7, fvalue, fvat : string;
    total : string;
    t1, t2, t3, t4 : string;
    t5, t6, t7 : string;
    r1, r2, r3, r4 : string;
    r5, r6, r7 : string;
    if1, if2, if3 : real;
    if4, if5, if6, if7 : real;
    it1, it2, it3, it4 : real;
    it5, it6, it7 : real;
    ir1, ir2, ir3, ir4 : real;
    ir5, ir6, ir7 : real;
    vat2, tc : real;
    value : real;
    check : integer;
    xleft,yleft : integer;
    xright,yright : integer;
    invoiceref : string;
    day2, month2, year2 : string;
    date : string;
    list : text;

    begin
    numu := numu + 1;
    if numu > 10 then
    begin
    writeln(chr(7));
    textbackground(red);
    clrscr;
    textcolor(white+blink);
    gotoxy(17,12);
    writeln('NO SPACE LEFT IN FILE!');
    gotoxy(17,15);
    writeln('Consult your program provider');
    numu := numu - 1;
    delay(1000);
    end
    else
    begin
    textbackground(blue);
    clrscr;
    textcolor(white);
    writeln;
    writeln;
    writeln(space:25,' SEARCH FOR CLIENT''S DETAILS');
    writeln;
    writeln(space:25,'=============================');
    writeln;
    writeln;
    write(space:10,'Enter reference number of client to produce invoice : ');
    readln(client);
    top := numc;
    bottom := 0;
    repeat
    mid := trunc((top+bottom)/2);
    if client < clientref[mid] then
    top := mid - 1;
    if client > clientref[mid] then
    bottom := mid + 1;
    until (client = clientref[mid]) or (top < bottom);
    if client = clientref[mid] then
    begin
    writeln;
    writeln(space:10,'Client details');
    writeln(space:10,'==============');
    repeat
    write(space:4,'Please confirm if you wish to create an invoice for this client Y/N : ');
    confirm := readkey;
    until (confirm='Y') or (confirm='y') or (confirm='N') or (confirm='n');
    if (confirm='Y') or (confirm='y') then
    begin
    textbackground(blue);
    textcolor(white);
    clrscr;
    write(space:6,'Please Enter The Invoice Ref Number : ' );
    readln(invoiceref);
    top1 := numu;
    bottom1 := 0;
    repeat
    mid1 := trunc((top1+bottom1)/2);
    if invoiceref < unpaidref[mid1] then
    top1 := mid1 - 1;
    if invoiceref > unpaidref[mid1] then
    bottom1 := mid1 + 1;
    until (invoiceref = unpaidref[mid1]) or (top1 < bottom1);
    if invoiceref = unpaidref[mid1] then
    begin
    writeln(chr(7));
    textbackground(red);
    clrscr;
    textcolor(white+blink);
    gotoxy(17,12);
    writeln('THE REF NUMBER ALREADY EXISTS!');
    gotoxy(17,15);
    writeln('Please Try Again.');
    delay(2000);
    numu:= numu - 1;
    end
    else
    begin
    top2 := nump;
    bottom2 := 0;
    repeat
    mid2 := trunc((top2+bottom2)/2);
    if invoiceref < paidref[mid2] then
    top2 := mid2 - 1;
    if invoiceref > paidref[mid2] then
    bottom2 := mid2 + 1;
    until (invoiceref = paidref[mid2]) or (top2 < bottom2);
    if invoiceref = paidref[mid2] then
    begin
    writeln(chr(7));
    textbackground(red);
    clrscr;
    textcolor(white+blink);
    gotoxy(17,12);
    writeln('THE REF NUMBER ALREADY EXISTS!');
    gotoxy(17,15);
    writeln('Please Try Again.');
    delay(2000);
    numu := numu - 1;
    end
    else
    begin
    writeln;
    repeat
    gotoxy(3,45);
    write(space:4,'Please confirm if the above is correct Y/N : ');
    confirm := readkey;
    until (confirm='Y') or (confirm='y') or (confirm='N') or (confirm='n');

    [/code]

  • MaStMuNdAMaStMuNdA Posts: 17Member
    [b][red]This message was edited by MaStMuNdA at 2003-3-31 10:6:29[/red][/b][hr]
    nothing to see ppl :p go to next post



  • MaStMuNdAMaStMuNdA Posts: 17Member
    [b][red]This message was edited by MaStMuNdA at 2003-3-31 10:7:44[/red][/b][hr]
    [code]

    if (confirm = 'Y') or (confirm = 'y') then
    begin
    clrscr;
    val(r1,ir1,check);
    val(t1,it1,check);
    val(r2,ir2,check);
    val(t2,it2,check);
    val(r3,ir3,check);
    val(t3,it3,check);
    val(r4,ir4,check);
    val(t4,it4,check);
    val(r5,ir5,check);
    val(t5,it5,check);
    val(r6,ir6,check);
    val(t6,it6,check);
    val(r7,ir7,check);
    val(t7,it7,check);
    if1 := it1 * ir1;
    if2 := it2 * ir2;
    if3 := it3 * ir3;
    if4 := it4 * ir4;
    if5 := it5 * ir5;
    if6 := it6 * ir6;
    if7 := it7 * ir7;
    value :=(if1 + if2 + if3 + if4 + if5 + if6 + if7);
    vat2 := value * vat;
    tc := vat2 + value;
    writeln;
    writeln(space:30,'PROPOSED INVOICE');
    writeln(space:30,'----------------');
    repeat
    write(space:4,'Please confirm if you wish to save the invoice and print Y/N : ');
    confirm := readkey;
    until (confirm='Y') or (confirm='y') or (confirm='N') or (confirm='n');
    if (confirm='Y') or (confirm='y') then
    begin
    clrscr;
    str(if1:8:2,fig1);
    str(if2:8:2,fig2);
    str(if3:8:2,fig3);
    str(if4:8:2,fig4);
    str(if5:8:2,fig5);
    str(if6:8:2,fig6);
    str(if7:8:2,fig7);
    str(value:9:2,fvalue);
    str(vat2:8:2,fvat);
    str(tc:9:2,total);
    assign(list,'LPT1');
    rewrite(list);
    {prints invoice}
    date := year2 + '/' + month2 + '/' + day2;
    (unpaidref[numu]) := invoiceref;
    (unpaidcref[numu]) := (clientref[mid]);
    (unpaiddate[numu]) := date;
    (unpaidcharges[numu]) := fig1+','+fig2+','+fig3+','+fig4+','+fig5+','+fig6+
    ','+fig7+','+fvalue+','+fvat;
    (unpaidtotal[numu]) := total;

    sortunpaidfile;

    textbackground(blue);
    textcolor(yellow+blink);
    clrscr;
    gotoxy(20,15);
    writeln('The Invoice Is Being Printed!!!');
    delay(4000);
    end
    else
    numu:= numu - 1;
    end
    else
    numu:= numu - 1;
    end;
    end;
    end
    else
    numu:= numu - 1;
    end
    else
    begin
    writeln(chr(7));
    textbackground(red);
    clrscr;
    textcolor(white+blink);
    gotoxy(17,12);
    writeln('NO SUCH MEMBER REF NUMBER EXISTS!');
    gotoxy(17,15);
    writeln('Please check the number again');
    numu := numu - 1;
    delay(2000);
    end;
    end;
    end;

    [/code]


    IF ANY ONE CAN HELP ME I WOULD BE SOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO GREATFUL THAT I WOULDNT BE ABLE TO PUT MY FEELINGS INTO WORD :D



    wow this code thing is really gd :p and phat nat i appreciate u looking at my coding
  • Phat NatPhat Nat Posts: 757Member
    : IF ANY ONE CAN HELP ME I WOULD BE SOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO GREATFUL THAT I WOULDNT BE ABLE TO PUT MY FEELINGS INTO WORD :D

    Too tired right now, but I will tomorrow. Please edit your messages and add the [ code ] [ /code ] to them before I take a look to make them easier to read.

    Phat Nat

  • Phat NatPhat Nat Posts: 757Member
    : [b][red]This message was edited by MaStMuNdA at 2003-3-31 10:3:57[/red][/b][hr]
    : hi every one
    :
    : i have another problem. ok wats happening is that since i created a unit to hold some of my program my sorting of one of the files is giving me a problem.
    :
    : i create an invoice do all my stuf and when i exit it its is suposed to sort the file out by the invoice reference number. but it doesnt do that. further more when i deleted the stupied sort file procedure and made it again the first 2 records would come as blank then the file would show the third record. so after this i reverted back to the sort procedure i deleted.

    I looked at it, but I am missing a lot of your procedures. Without these, it will be very difficult to find. If you ZIP me your entire code and send it to me I will track down the problem for you and send you a reply.

    Phat Nat

    PS - Thank you for formatting your code. It makes it alot easier to understand. :)
  • MaStMuNdAMaStMuNdA Posts: 17Member
    here is the link

    for the unit

    http://www.waleed.plus.com/THINGS.PAS

    for the main program

    http://www.waleed.plus.com/PRO14P3.PAS

    ths stupied explorer displays it so u can always copy and paste on to not pad then change it to .pas

    i would be greatful if u could do it by today plzzzzzzzzzzzzzzzzzzzz
  • ibyiby Posts: 323Member
    Hmmm... At first I tought your loops were not setup
    correctly but after I've analysed code from your
    first post I don't see why it wouldn't work.
    It is a bit strange, sure but loops are apparently
    doing the same as clasic code bellow
    (except that yours runs through more passes):

    [code]
    for i:=1 to n-1 do
    for j:=i to n do
    if arr[i]<arr[j] then
    begin
    arrtemp:=arr[i];
    arr[i]:=arr[j];
    arr[j]:=arrtemp;
    end;
    [/code]


    by the way, why don't you use records?
    you have created one variable called clientrec,
    why don't you make it an array:
    [code]

    var clientrec:array[1..20] of crec;

    procedure sort_clientrec_by_cbtel;
    var i,j:integer;
    temp:crec;
    begin
    for i:=1 to 19 do
    for j:=i to 20 do
    if clientrec[i].cbtel<clientrec[j].cbtel then
    begin
    temp:=clientrec[i];
    clientrec[i]:=clientrec[j];
    clientrec[j]:=temp;
    end;
    end;
    [/code]


  • ibyiby Posts: 323Member
    You could also dramatically reduce code length
    of your unit by using [b]ONE[/b] swap routine:

    [code]
    procedure swap(var a,b : string);
    var
    temp : string;
    begin
    temp := a;
    a := b;
    b:= temp
    end;
    [/code]


  • MaStMuNdAMaStMuNdA Posts: 17Member
    [b][red]This message was edited by MaStMuNdA at 2003-4-1 23:51:21[/red][/b][hr]
    hi iby

    thanx for looking at the codeing. the hting is that i have been taught basic programing and to be honest what ever the teacher has showed us to do thats wat i am doing. i kwno u r right i have loads of procedures which are useless but the thing is that my client sort is working so y isnt my unpaidsort working. i mean the thing even gets saved onto the file. my teacher is saying that your looping is probably rong in the create invoice procedure.


    oh and i forgot all of my records are arrays ( look at the link i gave for my unit ) all the global variables are there.

  • Phat NatPhat Nat Posts: 757Member
    : thanx for looking at the codeing. the hting is that i have been taught basic programing and to be honest what ever the teacher has showed us to do thats wat i am doing. i kwno u r right i have loads of procedures which are useless but the thing is that my client sort is working so y isnt my unpaidsort working. i mean the thing even gets saved onto the file. my teacher is saying that your looping is probably rong in the create invoice procedure.
    :
    :
    : oh and i forgot all of my records are arrays ( look at the link i gave for my unit ) all the global variables are there.

    I just looked through it and ran it. No problems sorting at all. The unpaid comes out sorted by reference # (as long as the add invoice section has been accessed). Looks good to me.

    Phat Nat

    PS - Send me your client/paid/unpaid files if you are still having problems.

«1
Sign In or Register to comment.