Ir para o conteúdo

Teorema de Pitágoras

Um calculador do Teorema de Pitágoras que vai perguntando o que se pretende introduzir, induzindo automaticamente o que se pretende calcular do triângulo rectângulo.

program programa_pitagoras;
uses crt;

procedure pitagoras;
var cateto1, cateto2 : real;
    hipotenusa : real;
    i : smallint;
    key, key2 : char;
    opcoes1 : set of char;
begin
     writeln('PITAGORAS');
     writeln;
     opcoes1 := [#49..#51];
     for i:=1 to 2 do begin
         case i of
              1 : begin
                  writeln('Introduzir:');
                  writeln('   1 > Cateto 1');
                  writeln('   2 > Cateto 2');
                  writeln('   3 > Hipotenusa');
                  repeat
                        key := readkey;
                  until (key in opcoes1);
                  opcoes1 -= [key];
                  write('Introduza ');
                  case key of
                       #49 : begin
                             repeat
                                   write('cateto 1: ');
                                   readln(cateto1);
                             until (cateto1>0);
                             end;
                       #50 : begin
                             repeat
                                   write('cateto 2: ');
                                   readln(cateto2);
                             until (cateto2>0);
                             end;
                       #51 : begin
                             repeat
                                   write('hipotenusa: ');
                                   readln(hipotenusa);
                             until (hipotenusa>0);
                             end;
                  end;
                  writeln;
                  end;
              2 : begin
                  writeln('Introduzir:');
                  if ([#49] <= opcoes1) then writeln('1 > Cateto 1');
                  if ([#50] <= opcoes1) then writeln('2 > Cateto 2');
                  if ([#51] <= opcoes1) then writeln('3 > Hipotenusa')
                  else writeln('4 > Cateto 1 = Cateto 2');
                  repeat
                        key2 := readkey;
                  until (key2 in opcoes1+[#52]);
                  case key2 of
                       #49 : begin
                             repeat
                                   write('Introduza cateto 1: ');
                                   readln(cateto1);
                             until (cateto1>0);
                             end;
                       #50 : begin
                             repeat
                                   write('Introduza cateto 2: ');
                                   readln(cateto2);
                             until (cateto2>0);
                             end;
                       #51 : begin
                             repeat
                                   write('hipotenusa: ');
                                   readln(hipotenusa);
                             until (hipotenusa>0);
                             end;
                  end;
                  writeln;
                  end;
         end;
     end;
     if ([key,key2] = [#49,#50]) then begin
        hipotenusa := SQRT( SQR(cateto1) + SQR(cateto2) );
        writeln('Hipotenusa: ',hipotenusa:0:3);
     end;
     if ([key,key2] = [#49,#51]) then begin
        cateto2 := SQRT( SQR(hipotenusa) - SQR(cateto1) );
        writeln('Cateto 2: ',cateto2:0:3);
     end;
     if ([key,key2] = [#50,#51]) then begin
        cateto1 := SQRT( SQR(hipotenusa) - SQR(cateto2) );
        writeln('Cateto 1: ',cateto1:0:3);
     end;
     if ([key,key2] = [#51,#52]) then begin
        cateto1 := hipotenusa / SQRT(2);
        writeln('Cateto 1 = Cateto 2: ',cateto1:0:3);
     end;
end;



begin
     pitagoras;
     writeln;
     write('ENTER para sair... ');
     readln;
end.