Delphi simulate angle subtraction expressed in degrees °, minutes' and seconds "

1

Delphi, Lazarus

I need help from friends, a .pas code to simulate the subtraction of angles expressed in degrees °, minutes' and seconds "in Delphi or Lazarus, taking the values of two TEdit and giving the result of the subtraction in a Label.

Example

Subtraction

  179°  59'   60"    TEdit1  
  70°   5'   15"    TEdit2  
 109° 54'  45"   resposta  no  Label1  
    
asked by anonymous 20.03.2018 / 18:57

1 answer

4

Here is the solution in Delphi, I note that Edit1 and Edit2 expect a valid string, with the format: ggº mm' ss"

procedure TForm1.Button1Click(Sender: TObject);

   function GetValorNumerico(const P_Texto: string; const P_Caracter: Char): Integer;
   begin
      case P_Caracter of
         'º': Result := StrToInt(Trim(Copy(P_Texto, 1, Pos(P_Caracter, P_Texto)-1)));
         '''': Result := StrToInt(Trim(Copy(P_Texto, Pos('º', P_Texto)+2, 2)));
         '"': Result := StrToInt(Trim(Copy(P_Texto, Pos('''', P_Texto)+2, 2)));
      end;
   end;

var
   W_Graus1: integer;
   W_Minutos1: integer;
   W_Segundos1: integer;

   W_Graus2: integer;
   W_Minutos2: integer;
   W_Segundos2: integer;

   W_Graus3: integer;
   W_Minutos3: integer;
   W_Segundos3: integer;
begin
   W_Graus1 := GetValorNumerico(Edit1.Text, 'º');
   W_Minutos1 := GetValorNumerico(Edit1.Text, '''');
   W_Segundos1 := GetValorNumerico(Edit1.Text, '"');

   W_Graus2 := GetValorNumerico(Edit2.Text, 'º');
   W_Minutos2 := GetValorNumerico(Edit2.Text, '''');
   W_Segundos2 := GetValorNumerico(Edit2.Text, '"');

   if W_Segundos1 < W_Segundos2 then
   begin
      Dec(W_Minutos1);
      Inc(W_Segundos1, 60);
   end;

   W_Segundos3 := W_Segundos1 - W_Segundos2;

   if W_Minutos1 < W_Minutos2 then
   begin
      Dec(W_Graus1);
      Inc(W_Minutos1, 60);
   end;

   W_Minutos3 := W_Minutos1 - W_Minutos2;

   W_Graus3 := W_Graus1 - W_Graus2;

   Label1.Caption := Format('%dº %d'' %d"', [W_Graus3, W_Minutos3, W_Segundos3]); 
end;
    
26.04.2018 / 20:18