Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. --1.Napisz i użyj procedury wysyłające i odbierające
  2. --ze strumienia macierze liczb rzeczywistych.
  3.  
  4. with Ada.Text_IO, Ada.Numerics.Float_Random;
  5. use Ada.Text_IO, Ada.Numerics.Float_Random;
  6. with Ada.Streams;
  7. use Ada.Streams;
  8. with Ada.Text_IO.Text_Streams;
  9. use Ada.Text_IO.Text_Streams;
  10. with Ada.Numerics.Discrete_Random;
  11.  
  12. procedure StreamMatrix is
  13.  
  14. --definicja nowego typu Macierzy
  15. type Macierz is array(Integer range <>, Integer range<>) of Float;
  16.  
  17. --deklaracja operacji Odczytu i Zapisu dla Macierzy
  18. procedure Read (S : access Root_Stream_Type'Class; Matx : out Macierz);
  19. procedure Write (S : access Root_Stream_Type'Class; Matx : in Macierz);
  20. for Macierz'Read use Read;
  21. for Macierz'Write use Write;
  22.  
  23. --implementacja operacji odczytu i zapisu dla macierzy
  24. procedure Read (S : access Root_Stream_Type'Class; Matx : out Macierz) is
  25. begin
  26. for E of Matx loop
  27. Float'Read(S,E);
  28. end loop;
  29. end Read;
  30.  
  31. procedure Write (S : access Root_Stream_Type'Class; Matx : in Macierz) is
  32. begin
  33. for EE of Matx loop
  34. Float'Write(S,EE);
  35. end loop;
  36. end Write;
  37.  
  38. --funkcja wypisujaca macierz
  39. function To_String(M: Macierz; Str: String:=""; I: Integer:=1) return String is
  40. begin
  41. if (I>M'Last and Str'Length>0) then
  42. return Str;
  43. else
  44. if Str'Length=0 then
  45. return To_String(M,Str&" ",M'First);
  46. else
  47. for EE of M loop
  48. return To_String(M,Str&ASCII.LF&"["&I'Img&"]=>"&EE'Img,I+1);
  49. end loop;
  50. end if;
  51. end if;
  52. end To_String;
  53.  
  54. --zadeklarowanie zmiennej typu macierz 10x10 i wypelnienie zerami
  55. G: Generator;
  56. Pl: File_Type;
  57. Filename: String := "macierz.txt";
  58. Strumien: Stream_Access;
  59. Mat : Macierz(0..10,0..10):=(others=>(others => 0.0));
  60. begin
  61. Reset(G);
  62. Mat := (others =>(others => Random(G)));
  63.  
  64. --zapis macierzy do pliku przez strumien
  65. Put_Line("* Zapis do pliku -> " & Filename);
  66. Create(Pl, Out_File, Filename); --utworz plik i zapisz
  67. Strumien := Stream(Pl);
  68. Put_Line("* Zapisuje macierz : " & To_String(Mat) );
  69. Macierz'Output(Strumien, Mat);
  70. Close(Pl);
  71.  
  72. --zerowanie Macierzy
  73. Mat := (others=>(others=>0.0));
  74.  
  75. --odczyt macierzy z pliku przez strumien
  76. Put_Line("* Odczyt z pliku <- " & Filename);
  77. Open(Pl, In_File, Filename);
  78. Strumien := Stream(Pl);
  79.  
  80. Mat := Macierz'Input(Strumien);
  81. Put_Line("* Odczytałem tablicę : " & To_String(Mat) );
  82. Close(Pl);
  83. Put_Line("* Koniec");
  84.  
  85. exception
  86. when End_Error =>
  87. if Is_Open(Pl) then
  88. Close(Pl);
  89. end if;
  90.  
  91. end StreamMatrix;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement