Advertisement
haunted_mind

mylib

Nov 24th, 2020
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.17 KB | None | 0 0
  1. UNIT MYLIB;
  2.  
  3. INTERFACE
  4.  
  5. uses crt;
  6. type
  7.   arr=array[1..18] of integer;
  8.   mat=array[1..18,1..18] of real;
  9.   store=array[1..5] of string [23];
  10. const
  11.   menus1:store= ('Input array',
  12.                                 'Output original array',
  13.                                 'Conditional sorting',
  14.                                 'Output changed array',
  15.                                 'Exit');
  16.   menus2:store= ('Input matrix',
  17.                                 'Output original matrix',
  18.                                 'Conditional sorting',
  19.                                 'Output changed matrix',
  20.                                 'Exit');
  21.  
  22. procedure Frame (x1,y1,x2,y2,txt_col:Integer);
  23. Procedure menu(var mode:Byte;const menus:store);
  24. procedure input_array(var a,b:arr; var n:byte);
  25. procedure output_array(a:arr;n:byte; const s:string);
  26. procedure condit_sort1(var a:arr; n:byte);
  27. procedure input_matrix(var a,b:mat; var n,m:byte);
  28. procedure output_matrix(a:mat;n,m:byte; const s:string);
  29. procedure condit_sort2(ori:mat;var ch:mat; n,m:byte);
  30. function max_mat(a:mat;n,m:byte):real;
  31. function ar_mean_under_main(a:mat;n,m:byte):real;
  32. function row_sum(i:integer;a:mat;m:byte):real;
  33.  
  34. IMPLEMENTATION
  35.  
  36. procedure Frame (x1,y1,x2,y2,txt_col:Integer);
  37. const
  38.  a=#186; b=#187;
  39.  c=#188; d=#200;
  40.  e=#201; f=#205;
  41. var
  42.  i:Integer;
  43. begin
  44.  window(1,1,80,25);
  45.  textColor(txt_col);
  46.  gotoXY(x1,y1);
  47.  Write(e);
  48.  for i:=x1+1 to x2-1 do
  49.   Write(f);
  50.  Write(b);
  51.  for i:=y1+1 to y2-1 do
  52.  begin
  53.   gotoXY(x1,i);
  54.   write(a);
  55.   gotoXY(x2,i);
  56.   Write(a);
  57.  end;
  58.  gotoXY(x1,y2);
  59.  Write(d);
  60.  for i:=x1+1 to x2-1 do
  61.   Write(f);
  62.  Write(c);
  63.  window(x1+1,y1+1,x2-1,y2-1);
  64.  textColor(txt_col);
  65.  clrscr;
  66. end;
  67.  
  68.  
  69.  
  70.  
  71.  
  72. Procedure menu(var mode:Byte;const menus:store);
  73. Var
  74.  k,i:Byte;
  75.  code:Char;
  76. Begin
  77.  frame (1,1,23,8,14);
  78.  writeln('MENU');
  79.  k:=1;
  80.  code:=' ';
  81.  While code<>#13 do
  82.  begin
  83.    For i:=1 to 5 do
  84.    begin
  85.      If i=k then
  86.        TextBackGround(Green)
  87.      else
  88.        textbackground(black);
  89.      GotoXY(1,i+1);
  90.      Write(menus[i]);
  91.    end;
  92.    code:=ReadKey;
  93.    If code=#0 then
  94.    begin
  95.      code:=ReadKey;
  96.      If code=#72 then
  97.        If k>1 then k:=k-1
  98.        else k:=5
  99.      else if code=#80 then
  100.        If k<5 then k:=k+1
  101.        else k:=1;
  102.    end;
  103.    mode:=k;
  104.  end;
  105. end;
  106.  
  107. procedure input_array(var a,b:arr; var n:byte);
  108. var
  109.   i:integer;
  110. begin
  111.   write('Enter num: ');
  112.   read(n);
  113.   writeln('Enter array: ');
  114.   for i:=1 to n do
  115.   begin
  116.     read(a[i]);
  117.     b[i]:=a[i];
  118.   end;
  119. end;
  120.  
  121. procedure output_array(a:arr;n:byte; const s:string);
  122. var
  123.   i:integer;
  124. begin
  125.   writeln(s);
  126.   for i:=1 to n do
  127.     write(a[i],' ');
  128. end;
  129. procedure condit_sort1(var a:arr; n:byte);
  130. var
  131.   s,i,k,r:integer;
  132. begin
  133.   s:=0;
  134.   i:=1;
  135.   while i<=n do
  136.   begin
  137.     s:=s+a[i];
  138.     i:=i+2;
  139.   end;
  140.   writeln('s =',s,'; first el is ',a[1]);
  141.   if s>a[1] then
  142.   begin
  143.     writeln('s>first el. Array should be sorted');
  144.  
  145.     for i:=1 to n-1 do
  146.     begin
  147.       if a[i]<a[i+1] then
  148.       begin
  149.         r:=a[i];
  150.         a[i]:=a[i+1];
  151.         a[i+1]:=r;
  152.       end;
  153.       if i>1 then  for k:=i downto 2 do
  154.       begin
  155.         if a[k-1]<a[k] then
  156.         begin
  157.           r:=a[k];
  158.           a[k]:=a[k-1];
  159.           a[k-1]:=r;
  160.         end
  161.         else break;
  162.       end;
  163.     end;
  164.   end
  165.   else write('s<=first el. Array should not be sorted');
  166. end;
  167. {
  168. procedure condit_sort1(var a:arr; n:byte);
  169. var
  170.   s,i,k,r:integer;
  171. begin
  172.   s:=1;
  173.   i:=1;
  174.   while i<=n do
  175.   begin
  176.     s:=s+a[i];
  177.     i:=i+2;
  178.   end;
  179.   writeln('s =',s,'; first el is ',a[i]);
  180.   if s>a[1] then
  181.   begin
  182.     writeln('s>first el. Array should be sorted');
  183.  
  184.     for i:=1 to n-1 do
  185.     begin
  186.       if a[i]<a[i+1] then
  187.       begin
  188.         r:=a[i];
  189.         a[i]:=a[i+1];
  190.         a[i+1]:=a[i];
  191.       end;
  192.       if i>1 then  for k:=i+1 downto 2 do
  193.       begin
  194.         if a[k-1]<a[k] then
  195.         begin
  196.           r:=a[k];
  197.           a[k]:=a[k-1];
  198.           a[k-1]:=r;
  199.         end
  200.         else break;
  201.       end;
  202.   end;
  203. end;
  204.  
  205.  
  206.   }
  207.  
  208.  
  209.  
  210.  
  211.  
  212. procedure input_matrix(var a,b:mat; var n,m:byte);
  213. var
  214.   i,j:integer;
  215. begin
  216.   write('Enter num of rows: ');
  217.   read(n);
  218.   write('Enter num of columns: ');
  219.   read(m);
  220.   writeln('Enter matrix: ');
  221.   for i:=1 to n do
  222.     for j:=1 to n do
  223.   begin
  224.     write('[',i,',',j,']=');
  225.     read(a[i,J]);
  226.     b[i,J]:=a[i,J];
  227.   end;
  228. end;
  229.  
  230. procedure output_matrix(a:mat;n,m:byte; const s:string);
  231. var
  232.   i,j:integer;
  233. begin
  234.   writeln(s);
  235.   for i:=1 to n do
  236.   begin
  237.     for j:=1 to m do
  238.       write(a[i,j]:5:1);
  239.     writeln;
  240.   end;
  241. end;
  242.  
  243. function max_mat(a:mat;n,m:byte):real;
  244. var
  245.   i,j:integer;
  246.   max:real;
  247. begin
  248.   max:=a[1,1];
  249.   for i:=1 to n do
  250.     for j:=1 to m do
  251.       if max<a[i,j] then
  252.         max:=a[i,j];
  253.   max_mat:=max;
  254. end;
  255.  
  256. function ar_mean_under_main(a:mat;n,m:byte):real;
  257. var
  258.   i,j:integer;
  259.   sum,num:real;
  260. begin
  261.   sum:=0;
  262.   num:=0;
  263.   for i:=2 to n do
  264.     for j:=1 to i-1 do
  265.     begin
  266.       sum:=sum+a[i,j];
  267.       num:=num+1;
  268.     end;
  269.   ar_mean_under_main:=sum/num;
  270. end;
  271.  
  272. function row_sum(i:integer;a:mat;m:byte):real;
  273. var
  274.   j:integer;
  275.   sum:real;
  276. begin
  277.   sum:=0;
  278.   for j:=1 to m do
  279.     sum:=sum+a[i,j];
  280.   row_sum:=sum;
  281. end;
  282.  
  283. procedure condit_sort2(ori:mat;var ch:mat; n,m:byte);
  284. var
  285.   s,i,j:integer;
  286.   r:real;
  287.   a:mat;
  288. begin
  289.   writeln('max el of mat:');
  290.   writeln(max_mat(ori,n,m):5:2 );
  291.   writeln('2*ar_mean_under_main:');
  292.   writeln(2*ar_mean_under_main(ori,n,m):5:2);
  293.     if max_mat(ori,n,m)<2*ar_mean_under_main(ori,n,m) then
  294.     begin
  295.     writeln('1>2 => sort');
  296.       for i:=1 to n do
  297.       begin
  298.         a[i,1]:=i;
  299.  
  300.         a[i,2]:=row_sum(i,ori,m);
  301.         write('sum[',i,']=', a[i,2]:5:1 );
  302.       end;
  303.  
  304.       for i:=n-1 downto 1 do
  305.         for j:=1 to i do
  306.          if a[j,2]<a[j+1,2] then
  307.          begin
  308.            r:=a[j,2];
  309.            a[j,2]:=a[j+1,2];
  310.            a[j+1,2]:=r;
  311.  
  312.            r:=a[j,1];;
  313.            a[j,1]:=a[j+1,1];
  314.            a[j+1,1]:=r;
  315.          end;
  316.  
  317.       output_matrix(a,n,2,'sorted rows');
  318.       for i:=1 to n do
  319.       begin
  320.         for j:=1 to m do
  321.         begin
  322.           s:=round(a[i,1]);
  323.           ch[i,j]:=ori[s,j];
  324.         end;
  325.       end;
  326. end;
  327.  
  328.  
  329. end;
  330.  
  331. begin
  332.  
  333. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement