tw38122.pp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. program tw38122;
  2. {$mode objfpc}
  3. {$modeswitch advancedrecords}
  4. {$modeswitch typehelpers}
  5. uses
  6. Math;
  7. type float = double;
  8. pfloat = ^float;
  9. type TFloatHelper = type helper for float
  10. procedure sub (const a: float);
  11. end;
  12. type TMatrix = record
  13. sx,sy: sizeint;
  14. procedure Init (x,y: sizeint; content: array of float);
  15. function GetAdr (x,y: sizeint): pfloat;
  16. procedure print;
  17. private
  18. data: array of float;
  19. end;
  20. procedure TFloatHelper.sub (const a: float);
  21. begin
  22. self := self-a;
  23. end;
  24. function TMatrix.GetAdr (x,y: sizeint): pfloat;
  25. begin
  26. result := @data[x*sy+y];
  27. end;
  28. procedure TMatrix.Init (x,y: sizeint; content: array of float);
  29. var i: sizeint;
  30. begin
  31. sx :=x;
  32. sy :=y;
  33. Data := nil;
  34. SetLength (data, sx*sy);
  35. for i := 0 to sx*sy-1 do data[i] := content[i];
  36. end;
  37. procedure TMatrix.print;
  38. var x,y: sizeint;
  39. begin
  40. for y := 0 to sy-1 do begin
  41. writeln;
  42. for x := 0 to sx-1 do begin
  43. write (GetAdr(x,y)^:2:2,' ');
  44. end;
  45. end;
  46. writeln;
  47. end;
  48. var A: TMatrix;
  49. px: pfloat;
  50. begin
  51. A.Init (2,2,[1,2,3,4]);
  52. A.print;
  53. if not SameValue(A.data[3],4,1e-1) then
  54. Halt(1);
  55. A.GetAdr(1,1)^ := 0; //I can set an element like this...
  56. A.Print;
  57. if not SameValue(A.data[3],0,1e-1) then
  58. Halt(2);
  59. px := A.GetAdr(1,1);
  60. px^.sub(100); //and this works as well.
  61. A.Print;
  62. if not SameValue(A.data[3],-100,1e-1) then
  63. Halt(3);
  64. A.GetAdr(1,1)^.sub(1000); //but that does not change the Matrix !?!
  65. A.print;
  66. if not SameValue(A.data[3],-1100,1e-1) then
  67. Halt(4);
  68. end.