Recent

Author Topic: Fix my code  (Read 4659 times)

Maboi

  • Newbie
  • Posts: 2
Fix my code
« on: June 22, 2018, 02:46:17 pm »
Well I'm very new to lazarus and I wonder if anyone knows how to fix my code

(Sorry for the foreign language)

So in the first part I initialize a .dat file for asking the size of a square

Code: Pascal  [Select][+][-]
  1. program RechthoekLezen;
  2.  
  3. var
  4.   f: text;
  5.   lengte: Longint;
  6.   breedte: Longint;
  7.   bestandsnaam: string;
  8.  
  9. begin
  10.   bestandsnaam := 'rechthoek.dat';
  11.   ASSIGN(f,bestandsnaam);
  12.   REWRITE(f);
  13.   writeln('geef de lengte van de eerste rechthoeks lengte');
  14.   readln( lengte);
  15.   writeln('Geef de lengte van de eerste rechthoeks breedte');
  16.   readln( breedte);
  17.   while (lengte<>0) or (breedte<>0) do
  18.   begin
  19.     writeln(f,lengte);
  20.     writeln(f,breedte);
  21.     writeln('Geef de lengte van de volgende rechthoek');
  22.     readln( lengte);
  23.     writeln('Geef de breedte van de volgende rechthoek');
  24.     readln( breedte);
  25.   end;
  26.   CLOSE(f);
  27.   writeln;
  28.   writeln('Druk op <ENTER> om het bestand te sluiten');
  29.   readln();
  30. end.     program RechthoekLezen;
  31.  
  32. var
  33.   f: text;
  34.   lengte: Longint;
  35.   breedte: Longint;
  36.   bestandsnaam: string;
  37.  
  38. begin
  39.   bestandsnaam := 'rechthoek.dat';
  40.   ASSIGN(f,bestandsnaam);
  41.   REWRITE(f);
  42.   writeln('geef de lengte van de eerste rechthoeks lengte');
  43.   readln( lengte);
  44.   writeln('Geef de lengte van de eerste rechthoeks breedte');
  45.   readln( breedte);
  46.   while (lengte<>0) or (breedte<>0) do
  47.   begin
  48.     writeln(f,lengte);
  49.     writeln(f,breedte);
  50.     writeln('Geef de lengte van de volgende rechthoek');
  51.     readln( lengte);
  52.     writeln('Geef de breedte van de volgende rechthoek');
  53.     readln( breedte);
  54.   end;
  55.   CLOSE(f);
  56.   writeln;
  57.   writeln('Druk op <ENTER> om het bestand te sluiten');
  58.   readln();
  59. end.    




In the second part I try to write the square using a multidimensional array but it gives me an error 203 code


Code: Pascal  [Select][+][-]
  1. program RechthoekTekenen;
  2.  
  3. var
  4.   oppervlakte: Longint;
  5.   lengte: Longint;
  6.   f: file of integer;
  7.   breedte: Longint;
  8.   bestandsnaam: string;
  9.   arrRechthoek: array of array of integer;
  10.  
  11. begin
  12.   bestandsnaam := 'rechthoek.dat';
  13.   ASSIGN(f,bestandsnaam);
  14.   RESET(f);
  15.   while not (EOF(f)) do
  16.   begin
  17.     read( f,lengte);
  18.     read( f,breedte);
  19.     SETLENGTH(arrRechthoek,lengte,breedte);
  20.     for lengte := 0 to lengte do
  21.     begin
  22.       for breedte := 0 to breedte do
  23.       begin
  24.         writeln(arrRechthoek[lengte,breedte]);
  25.       end;
  26.     end;
  27.     oppervlakte := lengte*breedte;
  28.     writeln('Deze rechthoek heeft een oppervlakte van ',oppervlakte,' vierkante centimeter');
  29.     lengte := 0;
  30.     breedte := 0;
  31.   end;
  32.   CLOSE(f);
  33.   writeln;
  34.   writeln('Druk op <ENTER> om het bestand te sluiten');
  35.   readln();
  36. end.        



I know it's not the best code but as I mentioned I'm very new

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Fix my code
« Reply #1 on: June 22, 2018, 03:27:31 pm »
0 to lengte -1 of 0 to pred(lengte)
0 to breedte- 1 of 0 to pred(breedte)

You are using one too many. programmers count from zero so the range must be size - 1.
In this case (203) this leads to a heap overflow,because you are accessing a value that is undetermined.

In Dutch:
De fout is dat je lengte en breedte telt vanaf een. Maar jouw code telt vanaf 0... begrijp je? 1..10 is een range elementen van 10, maar 0..9 is ook een range van 10. 0 to 10 = 11!!
Dus je moet breedte en lengte met een verminderen om de juiste range te krijgen. Anders loop je het risico dat - zoals in jouw geval - het programma niet meer weet waar het moet stoppen....
Daarom heet jouw fout een "over-indexing bug"
« Last Edit: June 22, 2018, 03:42:30 pm by Thaddy »
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Fix my code
« Reply #2 on: June 22, 2018, 03:29:11 pm »
In the second part I try to write the square using a multidimensional array but it gives me an error 203 code
Code: Pascal  [Select][+][-]
  1. program RechthoekTekenen;
  2. ...
  3.   f: file of integer; // file: text;
  4.   arrRechthoek: array of array of integer; // Remove
  5. ...
  6. begin
  7. ...
  8.     SETLENGTH(arrRechthoek,lengte,breedte); // Remove
  9.     for lengte := 0 to lengte do // to lengte - 1 and use other variable for loop, not lengte
  10.     begin
  11.       for breedte := 0 to breedte do // to breedte - 1 and use other variable for loop, not breedte, because you messed it up in the previous cycle
  12.       begin
  13.         writeln(arrRechthoek[lengte,breedte]); // Writeln('0'); and output is unsightly. May be write in loop, and writeln after it?
  14. ...
  15. end.
See comments.

Maboi

  • Newbie
  • Posts: 2
Re: Fix my code
« Reply #3 on: June 22, 2018, 05:11:24 pm »
So I changed to -1 thing but It still doesnt work

also I use the array because I have to, it's a detail from the task I'm doing


Code: Pascal  [Select][+][-]
  1. program RechthoekTekenen;
  2.  
  3. var
  4.   oppervlakte: Longint;
  5.   lengte: Longint;
  6.   f: file of integer;
  7.   breedte: Longint;
  8.   bestandsnaam: string;
  9.   arrRechthoek: array of array of integer;
  10.  
  11. begin
  12.   bestandsnaam := 'rechthoek.dat';
  13.   ASSIGN(f,bestandsnaam);
  14.   RESET(f);
  15.   while not (EOF(f)) do
  16.   begin
  17.     read( f,lengte);
  18.     read( f,breedte);
  19.     SETLENGTH(arrRechthoek,lengte,breedte);
  20.     for lengte := 0 to lengte-1 do
  21.     begin
  22.       for breedte := 0 to breedte-1 do
  23.       begin
  24.         writeln(arrRechthoek[lengte,breedte]);
  25.       end;
  26.     end;
  27.     oppervlakte := lengte*breedte;
  28.     writeln('Deze rechthoek heeft een oppervlakte van ',oppervlakte,' vierkante centimeter');
  29.     lengte := 0;
  30.     breedte := 0;
  31.   end;
  32.   CLOSE(f);
  33.   writeln;
  34.   writeln('Druk op <ENTER> om het bestand te sluiten');
  35.   readln();
  36. end.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Fix my code
« Reply #4 on: June 22, 2018, 05:48:18 pm »
So I changed to -1 thing but It still doesnt work
Read ALL(!) comments, please.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Fix my code
« Reply #5 on: June 22, 2018, 06:38:20 pm »
@Maboi

They've already told you your mistakes. But if you still more clue, see the example below:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: Integer;
  4. begin
  5.   i := 10;
  6.   for i := 1 to i-1 do Sleep(10);
  7.   ShowMessage(i.ToString);
  8. end;

Try the guess the result of the code above. What's your answer? And then try run the code, was your guess correct?

Your code still have other problems, but if you read ASerge source code comments carefully you can fix them.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Fix my code
« Reply #6 on: June 22, 2018, 10:47:30 pm »
Hmm. Your rectangles are allowed to have width or height of zero (as long as they are not both zero).
Is that really what you intended?

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Fix my code
« Reply #7 on: June 22, 2018, 10:49:17 pm »
It just means that Elvis has left the building....Bart
Specialize a type, not a var.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Fix my code
« Reply #8 on: June 22, 2018, 10:52:07 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: Integer;
  4. begin
  5.   i := 10;
  6.   for i := 1 to i-1 do Sleep(10);
  7.   ShowMessage(i.ToString);
  8. end;

If the compiler is really clever it wil eliminate the entire for-loop and substitute the last line with ShowMessage('10').
If it does not, then the value of i will be undefined after the loop, as per specs.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Fix my code
« Reply #9 on: June 22, 2018, 10:57:06 pm »
We try to help everyone, but we try to NOT solve homework. We give pointers on how to solve it (almost all programmers here have solved your problem!). The rest is up to you.... success!
In Dutch:
Wij helpen iedereen,, maar we gaan geen opdrachten kant en klaar maken. Wij geven aanwijzingen hoe je het kan oplossen. (bijna alle andere reagerende programmeurs hebben dat ook voor je gedaan! Dat besef je niet maar dat is zo!!)
We geven je alleen ons antwoord niet..... Dat moet je zelf doen!!!  :P :o 8)  Zo werkt dat bij ons.
Ter geruststelling: Jouw code is niet slecht, zelfs als het niet werkt... Van mij zou je toch een 6 krijgen. Maar met onze tips kan het een 9 worden...
« Last Edit: June 22, 2018, 11:08:12 pm by Thaddy »
Specialize a type, not a var.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Fix my code
« Reply #10 on: June 22, 2018, 11:31:49 pm »
OK.

First.

When you write the file, you declare the file as Text.
So you create a plain textfile.

When you read the file, you declare the exact same file as a file of Integer.
So, when reading, it will interpret the binary contents as being a sequence of integers.
This is plain wrong.

E.g. if you write a length of 10, the file will contain 4 bytes: a byte coding for the characte '1', a byte coding for the character '0', a byte coding for carriage return and a byte coding for line-ending.
So you get these four bytes: $31 $30 $0D $0A
Now, when you read this textfile and make the compiler believe that what it reads is a binarytrepresentation of integers, it will read the first four bytes (=sizeof(integer)) and this will then be a value of  168636465!

So declare the type of file the same in both program (preferrably text, this way you can open the file in a text editor to see if the contenst are what you expect, this makes debugging easier).

Second.

When reading, the array of array of integer makes no sense.
You do not need that at all, plus you use it wrong.
Just read length and with and display them.

Mind you, I interpret the assignement so that if you have an input file like:
Code: [Select]
4
3
7
6

output should be like:
'lengte = 4, breedte = 3, oppervlak = 12'
'lengte = 7, breedte = 6, oppervlak = 42'

If you want to store the entire files contents in some sort of array, I would define a type that holds breedte en lengte as a record and have an array of that type.
Code: Pascal  [Select][+][-]
  1. type
  2.   TRechtHoek = record
  3.      lengte, breedte: Longint;
  4.   end;
  5.  
  6. var
  7.   arrRechthoek: array of TRechtHoek;

Third.

In the "RechthoekLezen" program you use a while loop.
Since you are going to ask the user at least once, a reapeat loop is more appropriate.
This also gets rid of the duplicate lines in that code.
Also, you should not write a RechtHoek with invalid values (currently you do write a 0 x 0  one to the file).
Pseudocode:
Code: Pascal  [Select][+][-]
  1.   repeat
  2.     ask for lengte
  3.     read lengte
  4.     ask for breedte
  5.     read breedte
  6.     if (lengte>0) and (breedte>0) then write values to file
  7.   until either lengte or breedte <= 0

Fourth.

In general you should validate user input before storing them in a datatype you actually use for calculation.
User input should never crash a program.
In the "RechthoekLezen" program, what happens if the user inputs 9999999999999999999, or nonsense like 'foobar' when you read lengte or breedte?

The same goes for reading and writing to a file, there should be error checking there.

Also you allow negative values for lengte and breedte.

I know this is probably not part of your assignment, but still it's wrong.

Fifth

The structure of the data file makes it accident prone: you rely on the fact  there will always be an even number of lines (assuming you use text files).
To me it would make more sense to punt lengte and breedte on 1 line, separated by a space.
But, the file format may actually be specified in the assignment, I don't know.

Bart
« Last Edit: June 22, 2018, 11:48:18 pm by Bart »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Fix my code
« Reply #11 on: June 23, 2018, 07:09:04 pm »
Here's a Semi-Enterprise version.
(For real Enterprise it needs Classes, Singletons, a RDBMS, a RectangleFactory and Enterprise style logging, I leave that up to you.)

Common types and constants:
Code: Pascal  [Select][+][-]
  1. unit rshared;
  2. {$modeswitch advancedrecords}
  3.  
  4. interface
  5.  
  6. type
  7.  
  8.   { TRectangle }
  9.  
  10.   TRectangle = record
  11.     Length, Width: DWORD; //unsigned 32-bit
  12.     function Area: UInt64;
  13.   end;
  14.   TRectangleArr = array of TRectangle;
  15.  
  16. const
  17.     RectangleFilename = 'rectangle.dat';
  18.  
  19. implementation
  20.  
  21. { TRectangle }
  22.  
  23. function TRectangle.Area: UInt64;
  24. begin
  25.   Result := UInt64(Self.Length) * UInt64(Self.Width);
  26. end;
  27.  
  28. end.

Creating the rectangles:

Code: Pascal  [Select][+][-]
  1. program rwrite;
  2.  
  3. {$mode objfpc}
  4. {$h+}
  5.  
  6. uses
  7.   crt, sysutils, classes, rshared;
  8.  
  9. const
  10.   prLength = 'Enter length for rectangle %d';
  11.   prWidth = 'Enter width for rectangle %d';
  12.   prRectangles = 'Enter length and width for your rectangles. Enter zero to quit.';
  13.   FileWriteErrorMsg = 'Error writing data to %s.';
  14.  
  15. function PromptForInteger(const APrompt: String; X: Integer = -1; Y: Integer = -1): Integer;
  16. var
  17.   Ans, Err: String;
  18.   OK: Boolean;
  19. begin
  20.   if (X < 0) then X := WhereX;
  21.   if (Y < 0) then Y := WhereY;
  22.   Err := '';
  23.   repeat
  24.     GotoXY(X,Y);
  25.     ClrEol;
  26.     write(Err,APrompt,': ');
  27.     readln(Ans);
  28.     OK := TryStrToInt(Ans, Result);
  29.     if not OK then Err := Format('"%s" is not a valid value, try again. ',[Ans]);
  30.   until OK;
  31. end;
  32.  
  33. function PromptForRectangles(const APrompt: String; X: Integer = -1; Y: Integer = -1): TRectangleArr;
  34. var
  35.   L, W: Integer;
  36. begin
  37.   SetLength(Result,0);
  38.   if (X < 0) then X := WhereX;
  39.   if (Y < 0) then Y := WhereY;
  40.   GotoXY(X,Y);
  41.   ClrEol;
  42.   writeln(APrompt);
  43.   Inc(Y);
  44.   repeat
  45.     L := PromptForInteger(Format(prLength,[Length(Result)+1]), 1, Y);
  46.     if (L > 0) then
  47.     begin
  48.       W := PromptForInteger(Format(prWidth,[Length(Result)+1]), 1, Y);
  49.       if (W > 0) then
  50.       begin
  51.         SetLength(Result, Length(Result)+1);
  52.         Result[High(Result)].Length := L;
  53.         Result[High(Result)].Width := W;
  54.       end;
  55.     end;
  56.   until (L <=0) or (W <= 0);
  57. end;
  58.  
  59. procedure WriteRectangles(TheRectangles: TRectangleArr; const Filename: String);
  60. var
  61.   SL: TStringList;
  62.   i: Integer;
  63. begin
  64.   SL := TStringList.Create;
  65.   try
  66.     for i := 0 to Length(TheRectangles) - 1 do
  67.     begin
  68.       SL.Add(Format('%d %d',[TheRectangles[i].Length, TheRectangles[i].Width]));
  69.     end;
  70.     SL.SaveToFile(Filename);
  71.   finally
  72.     SL.Free;
  73.   end;
  74. end;
  75.  
  76. var
  77.   Rectangles: TRectangleArr;
  78.  
  79. begin
  80.   ClrScr;
  81.   Rectangles := PromptForRectangles(prRectangles, 1, 1);
  82.   try
  83.     WriteRectangles(Rectangles, RectangleFilename);
  84.   except
  85.     GotoXY(1, WhereY+1);
  86.     writeln(Format(FileWriteErrorMsg,[RectangleFilename]));
  87.     ExitCode := 1;
  88.   end;
  89. end.

Displaying rectangles:

Code: Pascal  [Select][+][-]
  1. program rread;
  2.  
  3. {$mode objfpc}
  4. {$h+}
  5.  
  6. uses
  7.   crt, sysutils, classes, rshared;
  8.  
  9. type
  10.   EInvalidData = class(Exception);
  11.  
  12. const
  13.   FileReadErrorMsg = 'Error reading data from %s.';
  14.   InvalidDataAtLine = 'Invalid length or width at line %d (data is: "%s")';
  15.   MissingWidthAtLine = 'Missing width at line %d (data is: "%s")';
  16.   NoData = 'There are no data to display.';
  17.   RectangleDims = 'length = %8u, width = %8u, area = %16u.';
  18.   DisplayMsg = 'Displaying rectangles';
  19.  
  20.  
  21. function GetRectangles(const AFilename: String): TRectangleArr;
  22. var
  23.   SL: TStringList;
  24.   i: Integer;
  25.   S, StrL, StrW: String;
  26.   procedure SplitLW(const S: String; out L, W: String);
  27.   var
  28.     P: SizeInt;
  29.   begin
  30.     P := Pos(#32, S);
  31.     if (P = 0) then
  32.     begin
  33.       L := S;
  34.       W := '';
  35.     end
  36.     else
  37.     begin
  38.       StrL := Copy(S,1,P-1);
  39.       StrW := Copy(S,P,MaxInt);
  40.     end;
  41.   end;
  42.  
  43. begin
  44.   SetLength(Result, 0);
  45.   SL := TStringList.Create;
  46.   try
  47.     SL.LoadFromFile(AFilename);
  48.     for i := 0 to SL.Count - 1 do
  49.     begin
  50.       S := SL[i];
  51.       if (S <> '') then
  52.       begin
  53.         SetLength(Result, Length(Result)+1);
  54.         SplitLW(S, StrL, StrW);
  55.         //dont give message about missing width if length is garbage
  56.         if (StrW = '') and TryStrToDWord(StrL,Result[High(Result)].Length) then
  57.           Raise EInvalidData.CreateFmt(MissingWidthAtLine,[i+1,S]);
  58.         if not (TryStrToDWord(StrL, Result[High(Result)].Length)
  59.                 and TryStrToDWord(StrW, Result[High(Result)].Width)) then
  60.           Raise EInvalidData.CreateFmt(InvalidDataAtLine,[i+1,S]);
  61.       end;
  62.     end;
  63.   finally
  64.     SL.Free;
  65.   end;
  66. end;
  67.  
  68. procedure DisplayRectangles(TheRectangles: TRectangleArr; const AMsg: String; X: Integer = -1; Y: Integer = -1);
  69. var
  70.   i: Integer;
  71. begin
  72.   if (X < 0) then X := WhereX;
  73.   if (Y < 0) then Y := WhereY;
  74.   GotoXY(X,Y);
  75.   ClrEol;
  76.   writeln(AMsg);
  77.   if (Length(TheRectangles) > 0) then
  78.   begin
  79.     for i := 0 to Length(TheRectangles) - 1 do
  80.     begin
  81.       write(Format('# %d: ',[i+1]));
  82.       writeln(Format(RectangleDims,[TheRectangles[i].Length, TheRectangles[i].Width, TheRectangles[i].Area]));
  83.     end;
  84.   end
  85.   else
  86.     writeln(NoData);
  87. end;
  88.  
  89. var
  90.   Rectangles: TRectangleArr;
  91. begin
  92.   ClrScr;
  93.   try
  94.     Rectangles := GetRectangles(RectangleFilename);
  95.     DisplayRectangles(Rectangles, DisplayMsg, 1, 1);
  96.   except
  97.     on E: EStreamError do
  98.     begin
  99.       writeln(Format(FileReadErrorMsg,[RectangleFilename]));
  100.       ExitCode := 2;
  101.     end;
  102.     on E: EInvalidData do
  103.     begin
  104.       writeln(Format('%s: %s',[RectangleFilename, E.Message]));
  105.       ExitCode := $FF;
  106.     end;
  107.   end;
  108. end.

I dare you to submit that in class   O:-) O:-)

Bart
« Last Edit: June 24, 2018, 01:08:14 pm by Bart »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Fix my code
« Reply #12 on: June 23, 2018, 07:45:46 pm »
@Bart
Sure the teacher would give up.... :D :D Just like the guardian in Monty Python and the Holy Grail... 8-)
On a more serious note: there are all pointers to a perfect solution and the initial code wasn't THAT bad. We've seen much worse. (hence I would still rate it as a 6)
« Last Edit: June 23, 2018, 07:49:08 pm by Thaddy »
Specialize a type, not a var.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Fix my code
« Reply #13 on: June 24, 2018, 01:30:25 pm »
Well, the array of array of integer in the reading program was a WTF.
Programs that crash on valid data (as it did) should not be rewarded with a 6.
Not grabbing the concept that Text files are  interpreted completely different than binary files (file of integer) also indicates a serious lack of understanding.

So, an "onvoldoende" would be the correct rate.
We have given him enough pointer to improve, so it should be relatively easy to improve the original code to get "voldoende".

Factoring out code you would otherwise write more than once also will give extra points.
(Note: I made some of them a little too generic on purpose.)
A program should be easy to read.
Hence the main "begin end." block should, for such a program be as simple as (pseudo code):

Code: Pascal  [Select][+][-]
  1. begin
  2.   WelcomeMessage;
  3.   ReadData;
  4.   DisplayData;
  5.   ExitMessage;
  6. end.


Even my code can be rewritten to be much simpler when:
  • User input is guaranteed to be safe (as per the teachers specs)
  • Input data is guaranteed to be safe
  • Overflow when calculating the area is guaranteed to never happen
  • Disk IO is guaranteed to never fail
  • Don't care about nice output, don't use crt unit
  • Use file of TRectangle to store and read data (if you don't care about human readable data files)
  • Display data as you read, don't collect them and display them later

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Fix my code
« Reply #14 on: June 24, 2018, 01:55:11 pm »
De simpele variant. (The simple variant)

Maken: (Create)

Code: Pascal  [Select][+][-]
  1. program MaakRechthoeken;
  2.  
  3. {
  4. Copyright (C) by Flying Sheep Inc.
  5. You are not allowed to use any part of this code for homework/school assigments
  6. without the express prior consent of the copyright holder.
  7. }
  8.  
  9. type
  10.   TRechthoek = packed record //packed omdat we data op disk gaan opslaan en zo is formaat gegarandeerd zelfde op alle computers.
  11.     Lengte, Breedte: Word;
  12.   end;
  13.   TRechthoekBestand = file of TRechthoek;
  14.  
  15.  
  16. procedure Welkom;
  17. begin
  18.   writeln('Welkom bij het Rechthoeken invoer programma.');
  19.   writeln;
  20. end;
  21.  
  22. procedure VraagEnSchrijfGegevens;
  23. var
  24.   R: TRechthoek;
  25.   Bestand: TRechthoekBestand;
  26. begin
  27.   Assign(Bestand, 'rechthoek.dat');
  28.   Rewrite(Bestand);
  29.   repeat
  30.     write('Geef lengte: ');
  31.     read(R.Lengte);
  32.     write('Geef breedte: ');
  33.     read(R.Breedte);
  34.     if (R.Breedte > 0) and (R.Lengte > 0) then
  35.       write(Bestand, R);
  36.   until (R.Breedte = 0) or (R.Lengte = 0);
  37.   Close(Bestand);
  38. end;
  39.  
  40. procedure TotZiens;
  41. begin
  42.   writeln;
  43.   writeln('De gegevens zijn opgeslagen in het bestand "rechthoek.dat".');
  44.   writeln('Bedankt en tot ziens.');
  45. end;
  46.  
  47. begin
  48.   Welkom;
  49.   VraagEnSchrijfGegevens;
  50.   TotZiens;
  51. end.

Lezen: (Read)

Code: Pascal  [Select][+][-]
  1. program LeesRechthoeken;
  2.  
  3. {
  4. Copyright (C) by Flying Sheep Inc.
  5. You are not allowed to use any part of this code for homework/school assigments
  6. without the express prior consent of the copyright holder.
  7. }
  8.  
  9. type
  10.   TRechthoek = packed record //packed omdat we data op disk gaan opslaan en zo is formaat gegarandeerd zelfde op alle computers.
  11.     Lengte, Breedte: Word;
  12.   end;
  13.   TRechthoekBestand = file of TRechthoek;
  14.  
  15.  
  16. procedure Welkom;
  17. begin
  18.   writeln('Welkom bij het Rechthoeken weergave programma.');
  19.   writeln;
  20. end;
  21.  
  22. procedure LeesEnToonGegevens;
  23. var
  24.   R: TRechthoek;
  25.   Bestand: TRechthoekBestand;
  26. begin
  27.   Assign(Bestand, 'rechthoek.dat');
  28.   Reset(Bestand);
  29.   while not Eof(Bestand) do
  30.   begin
  31.     Read(Bestand,R);
  32.     writeln('Lengte is: ',R.Lengte, ', Breedte is: ', R.Breedte, ', Oppervlakte is: ', R.Lengte * R.Breedte);
  33.   end;
  34.   Close(Bestand);
  35. end;
  36.  
  37. procedure TotZiens;
  38. begin
  39.   writeln;
  40.   writeln('Bedankt en tot ziens.');
  41. end;
  42.  
  43. begin
  44.   Welkom;
  45.   LeesEnToonGegevens;
  46.   TotZiens;
  47. end.

Bart
« Last Edit: June 24, 2018, 07:14:10 pm by Bart »

 

TinyPortal © 2005-2018