Advertisement
Guest User

ITSKURS

a guest
Mar 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 14.94 KB | None | 0 0
  1. Program matrixs;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. Uses
  8.   System.SysUtils, Windows;
  9.  
  10. Const
  11.   cExpression = ['0'..'9','*','-','+','A','B'];
  12.   cSigns = ['*','-','+'];
  13.   cWSigns = ['0'..'9','A','B'];
  14.   cExpresTr = ['*','0'..'9','(','t','A'];
  15.  
  16. Type
  17.   tMatr = array of array of real;
  18.  
  19. Var
  20.   A:tMatr;
  21.   B:tMatr;
  22.   expr:string;
  23.   i,i1,j1,i2,j2,count:integer;
  24.   basis,check,checkz:boolean;
  25.   input:char;
  26.   uuzka:extended;
  27.   inm:char;
  28.  
  29. procedure fillManual(var Matr:tMatr; line,col:integer);
  30. var
  31.   i,j:integer;
  32. begin
  33.   for i := 0 to line-1 do
  34.     for j := 0 to col-1 do
  35.     begin
  36.       write('[',i,',',j,']:');
  37.       readln(Matr[i,j]);
  38.       writeln;
  39.     end;
  40. end;
  41.  
  42. procedure fillRandom(var Matr:tMatr; line,col:integer);
  43. var
  44.   i,j:integer;
  45. begin
  46.   randomize;
  47.   for i := 0 to line-1 do
  48.     for j := 0 to col-1 do
  49.       Matr[i,j]:=Random(10);
  50. end;
  51.  
  52. procedure hyphen;
  53. begin
  54.   writeln('--------------------------');
  55. end;
  56.  
  57. procedure swapStr(var Matr:tMatr; col, line1,line2:integer);
  58. var
  59.   j:integer;
  60.   tmp:real;
  61. begin
  62.   for j := 0 to col do
  63.   begin
  64.     tmp:=Matr[line1,j];
  65.     Matr[line1,j]:=Matr[line2,j];
  66.     Matr[line2,j]:=tmp;
  67.   end;
  68. end;
  69.  
  70. procedure OutPutMatrix(Matr:tMatr; line,col:integer);
  71. var
  72.   i,j:integer;
  73. begin
  74.   for i := 0 to line-1 do
  75.   begin
  76.     for j := 0 to col-1 do
  77.       write(Matr[i,j]:10:2,' ':4);
  78.     writeln;
  79.   end;
  80. end;
  81.  
  82. function SumMatrix(M1,M2:tMatr; line,col:integer):tMatr;
  83. var
  84.   i,j:integer;
  85.   XM:tMatr;
  86. begin
  87.   SetLength(XM,line,col);
  88.   for i := 0 to line-1 do
  89.     for j := 0 to col-1 do
  90.       XM[i,j]:=M1[i,j]+M2[i,j];
  91.   SumMatrix:=XM;
  92. end;
  93.  
  94. function SubMatrix(M1,M2:tMatr; line,col:integer):tMatr;
  95. var
  96.   i,j:integer;
  97.   XM:tMatr;
  98. begin
  99.   SetLength(XM,line,col);
  100.   for i := 0 to line-1 do
  101.     for j := 0 to col-1 do
  102.       XM[i,j]:=M1[i,j]-M2[i,j];
  103.   SubMatrix:=XM;
  104. end;
  105.  
  106. function COM(a:integer; Matr:tMatr; line,col:integer):tMatr;
  107. //COM - const*Matrix
  108. var
  109.   i,j:integer;
  110.   XM:tMatr;
  111. begin
  112.   SetLength(XM,line,col);
  113.   for i := 0 to line-1 do
  114.     for j := 0 to col-1 do
  115.       XM[i,j]:=a*Matr[i,j];
  116.   COM:=XM;
  117. end;
  118.  
  119. function MultiMatrix(M1,M2:tMatr; line1,col1,line2,col2:integer):tMatr;
  120. var
  121.   i,k,m:integer;
  122.   XM:tMatr;
  123. begin
  124.   SetLength(XM,line1,col2);
  125.   for k := 0 to line1-1 do
  126.     for i := 0 to col2-1 do
  127.       for m := 0 to col1-1 do
  128.         XM[k,i]:=XM[k,i]+M1[k,m]*M2[m,i];
  129.  
  130.   MultiMatrix:=XM;
  131. end;
  132.  
  133. function TranMatrix(M1:tMatr; line,col:integer):tMatr;
  134. var
  135.   i,j:integer;
  136.   XM:tMatr;
  137. begin
  138.   SetLength(XM,col,line);
  139.   for i := 0 to line-1 do
  140.     for j := 0 to col-1 do
  141.       XM[j,i]:=M1[i,j];
  142.   TranMatrix:=XM;
  143. end;
  144.  
  145. procedure SortStr(var Matr, EMatr:tMatr; line,col:integer);
  146. var
  147.   i,j,k,indMin,x:integer;
  148.   tmp:real;
  149.   flag,check:boolean;
  150. begin
  151.   for i := 1 to line-1 do
  152.     repeat
  153.     flag:=true;
  154.       for k := line-1 downto i do
  155.       begin
  156.         if Matr[k-1,0]>Matr[k,0] then
  157.         begin
  158.           flag:=false;
  159.           for j := 0 to col-1 do
  160.           begin
  161.             tmp:=Matr[k-1,j];
  162.             Matr[k-1,j]:=Matr[k,j];
  163.             Matr[k,j]:=tmp;
  164.  
  165.             tmp:=EMatr[k-1,j];
  166.             EMatr[k-1,j]:=EMatr[k,j];
  167.             EMatr[k,j]:=tmp;
  168.           end;
  169.  
  170.         end;
  171.       end;
  172.     until flag;
  173. end;
  174.  
  175. procedure Gauss(var Matr:tMatr; line,col:integer; var checkSw:Boolean);
  176. var
  177.   AX:tMatr;
  178.   n,j,i,v,zn:integer;
  179.   k:real;
  180.   flag:boolean;
  181. begin
  182.   n:=line-1;
  183.   for v := 0 to n do
  184.   begin
  185.  
  186.     for j := v+1 to n do
  187.     begin
  188.       if Matr[v,v] = 0 then
  189.       begin
  190.         for zn := v+1 to line-1 do
  191.           if Matr[zn,v] <> 0 then
  192.           begin
  193.             swapStr(Matr, n, v, zn);
  194.             flag:=true;
  195.             checkSw:=not checkSw;
  196.             break;
  197.           end
  198.           else
  199.             flag:=false;
  200.  
  201.         if flag=false then
  202.           break;
  203.       end;
  204.      
  205.       k:=Matr[j,v]/Matr[v,v];
  206.       for i := 0 to n do
  207.         Matr[j,i]:=Matr[j,i]-k*Matr[v,i];
  208.     end;
  209.   end;
  210. end;
  211.  
  212. function Dim(Matr:tMatr; line,col:integer):extended;
  213. var
  214.   i,j:integer;
  215.   X:extended;
  216.   Swaps:boolean;
  217.  
  218. begin
  219.   Swaps:=false;
  220.   Gauss(Matr,line,col,Swaps);
  221.   X:=1;
  222.   for i := 0 to line-1 do
  223.     X:=X*Matr[i,i];
  224.  
  225.   if Swaps then
  226.     X:=-X;
  227.  
  228.   Dim:=X;
  229. end;
  230.  
  231. function Degenerate(Matr:tMatr; line,col:integer):Boolean;
  232. begin
  233.   if Dim(Matr,line,col) <> 0 then
  234.     Degenerate:=False
  235.   else
  236.     Degenerate:=True;
  237. end;
  238.  
  239. procedure GaussE(var Matr, EMatr:tMatr; line,col:integer);
  240. var
  241.   i,j,m,v,zn,n:integer;
  242.   k:real;
  243.   flag:boolean;
  244. begin
  245.   n:=line-1;
  246.   for v := 0 to n do
  247.   begin
  248.     for j := v+1 to n do
  249.     begin
  250.       if Matr[v,v] = 0 then
  251.       begin
  252.         for zn := v+1 to n do
  253.           if Matr[zn,v] <> 0 then
  254.           begin
  255.             swapStr(Matr, n, v, zn);
  256.             swapStr(EMatr, n, v, zn);
  257.             flag:=true;
  258.             break;
  259.           end
  260.           else
  261.             flag:=false;
  262.  
  263.         if flag=false then
  264.           break;
  265.       end;
  266.       k:=Matr[j,v]/Matr[v,v];
  267.       for i := 0 to n do
  268.       begin
  269.         Matr[j,i]:=Matr[j,i]-(k*Matr[v,i]);
  270.         EMatr[j,i]:=EMatr[j,i]-(k*EMatr[v,i]);
  271.       end;
  272.     end;
  273.   end;
  274. end;
  275.  
  276. procedure GaussAb(var Matr, EMatr:tMatr; line,col:integer);
  277. var
  278.   i,j,m,v,n,zn:integer;
  279.   k:real;
  280.   flag:boolean;
  281. begin
  282.   n:=line-1;
  283.   for v := n downto 0 do
  284.   begin
  285.     for j := v-1 downto 0 do
  286.     begin
  287.       if Matr[v,v] = 0 then
  288.       begin
  289.         for zn := v-1 to 0 do
  290.           if Matr[zn,v] <> 0 then
  291.           begin
  292.             swapStr(Matr, n, v, zn);
  293.             swapStr(EMatr, n, v, zn);
  294.             flag:=true;
  295.             break;
  296.           end
  297.           else
  298.             flag:=false;
  299.  
  300.         if flag=false then
  301.           break;
  302.       end;
  303.       k:=Matr[j,v]/Matr[v,v];
  304.       for i := n downto 0 do
  305.       begin
  306.         Matr[j,i]:=Matr[j,i]-k*Matr[v,i];
  307.         EMatr[j,i]:=EMatr[j,i]-k*EMatr[v,i];
  308.       end;
  309.     end;
  310.   end;    
  311. end;
  312.  
  313. function InvMatr(Matr:tMatr; line,col:integer):tMatr;
  314. var
  315.   i,j,m:integer;
  316.   E:tMatr;
  317.   tmp:real;
  318. begin
  319.   SetLength(E, line, col);
  320.   for i := 0 to line-1 do
  321.     E[i,i]:=1;
  322.  
  323.   GaussE(Matr,E, line, col);
  324.   GaussAb(Matr,E,line,col);
  325.   for i := 0 to line-1 do
  326.     for j := 0 to col-1 do
  327.       E[i,j]:=E[i,j]/Matr[i,i];
  328.      
  329.   for i := 0 to line-1 do
  330.     for j := 0 to col-1 do
  331.       Matr[i,j]:=Matr[i,j]/Matr[i,i];
  332.            
  333.   InvMatr:=E;
  334. end;
  335.  
  336. Begin
  337.   writeln('dim:');
  338.   readln(i1);
  339.   setlength(A,i1,i1);
  340.   writeln('1 - manual'#10,'2 - random'#10,'else - exit');
  341.   readln(inm);
  342.   case inm of
  343.     '1':  fillManual(A,i1,i1);
  344.     '2':  fillRandom(A,i1,i1)
  345.     else
  346.       exit;
  347.   end;
  348.   OutPutMatrix(A,i1,i1);
  349.   hyphen;
  350. //  writeln('det: ',Dim(A,i1,i1):0:2);
  351.   try
  352.     writeln('Inverse:');
  353.     OutPutMatrix(InvMatr(A,i1,i1),i1,i1);
  354.     hyphen;
  355.     writeln('A:');
  356.     OutPutMatrix(A,i1,i1);
  357.   except
  358.     on E: Exception do
  359.       Writeln(E.ClassName, ': ', E.Message);
  360.   end;
  361.  
  362. {
  363.   basis:=false;
  364.   Writeln('Hello!');
  365.   repeat
  366.     writeln('What do you want to do?');
  367.     writeln('1 - matrix calculator(add,sub,mult)');
  368.     writeln('2 - matrix transformation');
  369.     writeln('0 - close program');
  370.     repeat
  371.       try
  372.         readln(input);
  373.         case input of
  374.           '1':
  375.           begin
  376.             checkz:=true;
  377.             writeln('Set i by j matrix A:');
  378.             repeat
  379.               try
  380.                 readln(i1,j1);
  381.                 if (i1>0) and (j1>0) then
  382.                 begin
  383.                   SetLength(A,i1,j1);
  384.                   check:=true;
  385.                 end
  386.                 else
  387.                   strtoint('V');
  388.               except
  389.                 check:=false;
  390.                 writeln('опять елена павловна виновата?');
  391.               end;
  392.             until check;
  393.  
  394.             writeln('How do you want to fill matrix A?');
  395.             writeln('1 - random',#10,'2 - manually');
  396.             writeln('0 - close program');
  397.             repeat
  398.               try
  399.                 readln(input);
  400.                 case input of
  401.                   '1': fillRandom(A,i1,j1);
  402.                   '2': fillManual(A,i1,j1);
  403.                   '0': exit;
  404.                   else
  405.                     strtoint('V');
  406.                 end;
  407.                 check:=true;
  408.               except
  409.                 check:=false;
  410.                 writeln('опять елена павловна виновата?');
  411.               end;
  412.             until check;
  413.  
  414.             writeln('Set i by j matrix B:');
  415.             repeat
  416.               try
  417.                 readln(i2,j2);
  418.                 if (i2>0) and (j2>0) then
  419.                 begin
  420.                   SetLength(B,i2,j2);
  421.                   check:=true;
  422.                 end
  423.                 else
  424.                   strtoint('V');
  425.               except
  426.                 check:=false;
  427.                 writeln('опять елена павловна виновата?');
  428.               end;
  429.             until check;
  430.  
  431.             writeln('How do you want to fill matrix B?');
  432.             writeln('1 - random',#10,'2 - manually');
  433.             writeln('0 - close program');
  434.             repeat
  435.               try
  436.                 readln(input);
  437.                 case input of
  438.                   '1': fillRandom(B,i2,j2);
  439.                   '2': fillManual(B,i2,j2);
  440.                   '0': exit;
  441.                   else
  442.                     strtoint('V');
  443.                 end;
  444.                 check:=true;
  445.               except
  446.                 check:=false;
  447.                 writeln('опять елена павловна виновата?');
  448.               end;
  449.             until check;
  450.  
  451.             writeln('A[',i1,',',j1,']:');
  452.             OutPutMatrix(A,i1,j1);
  453.  
  454.             hyphen;
  455.             writeln('B[',i2,',',j2,']:');
  456.             OutPutMatrix(B,i2,j2);
  457.  
  458.             writeln('Enter the expression of 2 matrices');
  459.             writeln('Example: A+B or A*B');
  460.             writeln('0 - close program');
  461.             repeat
  462.               try
  463.                 readln(expr);
  464.                 if expr = '0' then
  465.                   exit;
  466.                 if (not (expr[1] in cWSigns)) or (expr[length(expr)] in cSigns) then
  467.                   StrToInt('V');
  468.  
  469.                 for i := 1 to length(expr) do
  470.                   if expr[i] in cExpression then
  471.                     check:=True
  472.                   else
  473.                     StrToInt('V');
  474.  
  475.                 if (expr[2] = '+') or (expr[2] = '-') then
  476.                   if not ((i1 = i2) and (j1=j2)) then
  477.                   begin
  478.                     writeln('For addition / subtraction, dimension must be the same');
  479.                     StrToInt('V');
  480.                   end;
  481.  
  482.                 if (expr[2] = '*') and (not (expr[1] in ['0'..'9'])) then
  483.                   if j1 <> i2 then
  484.                   begin
  485.                     writeln('For multiplication the number of columns of matrix A');
  486.                     writeln('and the number of rows of matrix B MUST BE the same!');
  487.                     StrToInt('V');
  488.                   end;
  489.  
  490.                 count:=0;
  491.                 for i := 1 to length(expr) do
  492.                   if (expr[i] = 'A') or (expr[i] = 'B') then
  493.                     count:=count+1;
  494.                 if count>2 then
  495.                   StrToInt('V');
  496.  
  497.                 if expr[2]=expr[3] then
  498.                   StrToInt('V');
  499.  
  500.  
  501.               except
  502.                 writeln('опять елена павловна виновата?');
  503.                 writeln('0 - close program');
  504.                 readln(expr);
  505.                 if expr = '0' then
  506.                   exit;
  507.                 check:=False;
  508.               end;
  509.             until check;
  510.  
  511.             hyphen;
  512.             writeln(expr,':');
  513.  
  514.             if not (expr[1] in ['0'..'9']) then
  515.               case expr[2] of
  516.                 '+': OutPutMatrix(SumMatrix(A,B,i1,j1),i1,j1);
  517.                 '-': OutPutMatrix(SubMatrix(A,B,i1,j1),i1,j1);
  518.                 '*': OutPutMatrix(MultiMatrix(A,B,i1,j1,i2,j2),i1,j2);
  519.               end;
  520.           end;
  521.           '2':
  522.           begin
  523.             checkz:=true;
  524.             writeln('Set i by j matrix A:');
  525.             repeat
  526.               try
  527.                 readln(i1,j1);
  528.                 if (i1>0) and (j1>0) then
  529.                 begin
  530.                   SetLength(A,i1,j1);
  531.                   check:=true;
  532.                 end
  533.                 else
  534.                   strtoint('V');
  535.               except
  536.                 check:=false;
  537.                 writeln('опять елена павловна виновата?');
  538.               end;
  539.             until check;
  540.  
  541.             writeln('How do you want to fill matrix A?');
  542.             writeln('1 - random',#10,'2 - manually');
  543.             writeln('0 - close program');
  544.             repeat
  545.               try
  546.                 readln(input);
  547.                 case input of
  548.                   '1': fillRandom(A,i1,j1);
  549.                   '2': fillManual(A,i1,j1);
  550.                   '0': exit;
  551.                   else
  552.                     strtoint('V');
  553.                 end;
  554.                 check:=true;
  555.               except
  556.                 check:=false;
  557.                 writeln('опять елена павловна виновата?');
  558.               end;
  559.             until check;
  560.  
  561.             writeln('A[',i1,',',j1,']:');
  562.             OutPutMatrix(A,i1,j1);
  563.  
  564.             writeln('Enter the expression of 2 matrices');
  565.             writeln('Example: 3*A or A( or At');
  566.             writeln('A( - inverse matrix(soon)',#10,'At - transposed matrix');
  567.             writeln('0 - close program');
  568.             repeat
  569.               try
  570.                 readln(expr);
  571.                 if expr = '0' then
  572.                   exit;
  573.                 hyphen;
  574.                 case expr[2] of
  575.                   '*':
  576.                   begin
  577.                     check:=true;
  578.                     OutPutMatrix(COM(StrToInt(expr[1]),A,i1,j1),i1,j1);
  579.                   end;
  580.                   't':
  581.                   begin
  582.                     check:=true;
  583.                     OutPutMatrix(TranMatrix(A,i1,j1),j1,i1);
  584.                   end;
  585.                   '(':
  586.                   begin
  587.                     check:=true;
  588.                     writeln('Oops, not available now =(',#10,'Try again later =)');
  589.                   end
  590.                   else
  591.                     StrToInt('V');
  592.                 end;
  593.               except
  594.                 check:=false;
  595.                 writeln('опять елена павловна виновата?');
  596.                 writeln('0 - close program');
  597.                 readln(expr);
  598.                 if expr = '0' then
  599.                   exit;
  600.               end;
  601.             until check;
  602.           end;
  603.           '0': exit;
  604.           else StrToInt('V');
  605.         end;
  606.       except
  607.         checkz:=false;
  608.         writeln('опять елена павловна виновата?');
  609.       end;
  610.     until checkz;
  611.   until basis;
  612.   }
  613.   hyphen;
  614.   writeln('Press "Enter" to exit...');
  615.   readln;
  616. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement