program operace_s_maticemi;

uses crt;

const maximum = 100;
      nelze: boolean = FALSE;

type matice = array[1..maximum,1..maximum]of integer;

var m1, m2, m3: matice;
    r1, s1, r2, s2: integer;
    podminka: char;
    i, j, k, pom: integer;

procedure Vstup;
begin
  textbackground(0);
  clrscr;
  textbackground(6);
  write('Vloz pocet radku prvni matice:   '); readln(r1);
  write('Vloz pocet sloupcu prvni matice: '); readln(s1);
  write('Vloz pocet radku druhe matice:   '); readln(r2);
  write('Vloz pocet sloupcu druhe matice: '); readln(s2);
  if (podminka = '1') and ((r1 <> r2) or (s1 <> s2)) then
  begin
    gotoxy(25,10);
    writeln('Nelze scitat matice ruznych typu.');
    nelze := TRUE;
    delay(2000);
    Exit;
  end;
  if (podminka = '2') and ((r1 <> r2) or (s1 <> s2)) then
  begin
    gotoxy(25,10);
    writeln('Nelze odcitat matice ruznych typu.');
    nelze := TRUE;
    delay(2000);
    Exit;
  end;
  if(podminka = '3') and (s1 <> r2) then
  begin
    gotoxy(25,10);
    writeln('Matice techto typu nelze vynasobit.');
    nelze := TRUE;
    delay(2000);
    Exit;
  end;
  writeln('Vkladej jednotlive hodnoty prvni matice (cti po radcich): ');
  for i := 1 to r1 do
    for j := 1 to s1 do
    begin
      gotoxy(6*j,i+6);
      readln(m1[i,j]);
    end;
  writeln;
  writeln('Vkladej jednotlive hodnoty druhe matice (cti po radcich): ');
  for i := 1 to r1 do
    for j := 1 to s1 do
    begin
      gotoxy(6*j,i+11);
      readln(m2[i,j]);
    end;
  writeln;
end;

procedure Vstup2;
begin
  textbackground(0);
  clrscr;
  textbackground(6);
  write('Vloz pocet radku matice:   '); readln(r1);
  write('Vloz pocet sloupcu matice: '); readln(s1);
  if (podminka = '4') and (r1 <> s1) then
  begin
    gotoxy(1,10);
    writeln('Tato matice neni ctvercova, takze nelze urcit, zda je symetricka.');
    nelze := TRUE;
    delay(3000);
    Exit;
  end;
  if (podminka = '5') and (r1 <> s1) then
  begin
    gotoxy(1,10);
    writeln('Tato matice neni ctvercova, takze nelze urcit, zda je diagonalni.');
    nelze := TRUE;
    delay(3000);
    Exit;
  end;
  writeln('Vkladej jednotlive hodnoty matice (cti po radcich): ');
  for i := 1 to r1 do
    for j := 1 to s1 do
    begin
      gotoxy(6*j,i+6);
      readln(m1[i,j]);
    end;
  writeln;
end;

procedure Soucet;
begin
  for i := 1 to r1 do
    for j := 1 to s1 do
      m3[i,j] := m1[i,j] + m2[i,j];
end;

procedure Rozdil;
begin
  for i := 1 to r1 do
    for j := 1 to s1 do
      m3[i,j] := m1[i,j] - m2[i,j];
end;

procedure Soucin;
begin
  for i := 1 to r1 do
    for j := 1 to s2 do
    begin
      pom := 0;
      for k := 1 to s1 do pom := pom + (m1[i,k] * m2 [k,j]);
      m3[i,j] := pom;
    end;
end;

function Symetricka : boolean;
begin
  Symetricka := true;
  for i := 1 to r1 do
    for j := 1 to s1 do
      if m1[i,j] <> m1[j,i] then
      begin
        Symetricka := false;
        Exit;
      end;
end;

function Diagonalni : boolean;
begin
  Diagonalni := true;
  for i := 1 to r1 do
    for j := 1 to s1 do
      if i <> j then
        if m1[i,j] <> 0 then
        begin
          Diagonalni := false;
          Exit;
      end;
end;

procedure Vystup;
begin
  writeln('Vysledna matice: ');
  writeln;
  for i := 1 to r1 do
  begin
    for j := 1 to s2 do
    begin
      write(m3[i,j]:6);
    end;
  writeln;
  end;
  repeat until keypressed;
end;

procedure Vystup2;
begin
  if (podminka = '4') and Symetricka then
  begin
    writeln;
    writeln('Tato matice je symetricka.');
    writeln;
    writeln('Stiskni ENTER ...');
    readln;
  end;
  if (podminka = '4') and not(Symetricka) then
  begin
    writeln;
    writeln('Tato matice neni symetricka.');
    writeln;
    writeln('Stiskni ENTER ...');
    readln;
  end;
  if (podminka = '5') and Diagonalni then
  begin
    writeln;
    writeln('Tato matice je diagonalni.');
    writeln;
    writeln('Stiskni ENTER ...');
    readln;
  end;
  if (podminka = '5') and not(Diagonalni) then
  begin
    writeln;
    writeln('Tato matice neni diagonalni.');
    writeln;
    writeln('Stiskni ENTER ...');
    readln;
  end;
end;

procedure Rozhrani;
begin
repeat
  textbackground(0);
  clrscr;
  textcolor(10);
  textbackground(6);
  gotoxy(15,6);  writeln('Program na scitani, odcitani a nasobeni dvou matic.');
  gotoxy(31,8);  writeln('David Padrta - IVT 1');
  gotoxy(25,12); writeln('Soucet dvou matic  -  zmackni 1');
  gotoxy(25,13); writeln('Rozdil dvou matic  -  zmackni 2');
  gotoxy(25,14); writeln('Soucin dvou matic  -  zmackni 3');
  gotoxy(25,15); writeln('Symetricka matice  -  zmackni 4');
  gotoxy(25,16); writeln('Diagonalni matice  -  zmackni 5');
  gotoxy(25,20); writeln('Ukonceni programu  -  zmackni 9');
  podminka := readkey;
    case podminka of
     '1': begin
            Vstup;
            if (nelze = FALSE) then
            begin
              Soucet;
              Vystup;
            end;
          end;
     '2': begin
            Vstup;
            if (nelze = FALSE) then
            begin
              Rozdil;
              Vystup;
            end;
          end;
     '3': begin
            Vstup;
            if (nelze = FALSE) then
            begin
              Soucin;
              Vystup;
            end;
          end;
     '4': begin
            Vstup2;
            if (nelze = FALSE) then
              Vystup2;
          end;
     '5': begin
            Vstup2;
            if (nelze = FALSE) then
              Vystup2;
          end;
    end;
  until (podminka = '9');
  textbackground(0);
  clrscr;
end;

BEGIN
  clrscr;
  Rozhrani;
END.