4

重量を石とポンド(ポンド)で表示するために使用されるグラフがあります。

グラフにはレコードからのデータが入力されます。重みの場合、データ型はDoubleです。

レコードデータは実行時に編集されるので、入力したデータを正しくフォーマットする方法を知る必要があります。

理解を深めるために、最初にこれらのサンプル値を見てください。これらはStonesとlbsとして表されます。

  • 8.09(8石と9ポンド)
  • 12.03(12石と3ポンド)
  • 14.16(14石と16ポンド)
  • 11.13(11石と13ポンド)
  • 17.14(17石と14ポンド)

石には14ポンドしかないため、.13を超える値を入力すると、石の値が1増加し、.00からポンドの値が低くなります。そのことを念頭に置いて、上記のサンプルから、これらの値の2つを使用してください。正しくありません:

  • 14.16
  • 17.14

彼らはする必要があります:

  • 15.02
  • 18.00

石やポンドを正しくフォーマット/丸めることができる組み込みの数学関数はありますか?

そうでなければ、これを解決するための論理またはアプローチの方法を示す答えを見ることに本当に興味があります。

ポンドの部分をチェックして、値が0.13を超える場合は石をインクリメントすることを考えましたが、特にこれを行うのに最適な方法がわかりません。値が13.76のようなものである可能性がある場合は、何を変更すればよいかわかりません。石とポンド(これは私が自分自身を混乱させ始めるところです)。

前もって感謝します。

4

3 に答える 3

5

ポンドの場合、で小数部分を取得してからFrac(weight)100を掛けます。次に、を使用Roundして整数形式で取得します。

pounds := Round(100*Frac(weight));

そして石の場合はそれだけTruncです:

stones := Trunc(weight);

他の方向では、次のようにします。

weight := stones + pounds/100.0;

これらの機能を使用すると、残りの作業を簡単に行うことができます。14を超えるポンドの値の妥当性チェックは簡単に処理できます。例えば:

stones := Trunc(weight);
pounds := Round(100*Frac(weight));
stones := stones + pounds div 14;
pounds := pounds mod 14;

このコードを汎用ライブラリのどこかに見つけたら、私は非常に驚きます。それは体重を蓄えるのに非常に貧弱な方法だからです。浮動小数点形式を使用する場合は、次のようにする必要があります。

weight := stones + pounds/14.0;

他の方向では、次のようにします。

stones := Trunc(weight);
pounds := Round(14*Frac(weight));
stones := stones + pounds div 14;
pounds := pounds mod 14;

残念ながら、div/modシャッフルを実行する必要があります。weight=9.99たとえば、次の場合に何が起こるか想像してみてください。

このようにすると、浮動小数点値の計算がより賢明になります。たとえば、10人の体重を測定し、合計を知りたいとします。真の浮動小数点表現でそれを行うのは理にかなっていますが、あなたの表現ではそうではありません。

それを確認するために、これらの10人が、すべてゼロ石、10ポンドの重さであると仮定します。私が知っている非常に小さな人々。しかし、あなたはそれを0.1と呼ぶでしょう。その10ロットを合計すると、1.0の重みがあります。しかし、実際の値が100ポンド、つまり7石2ポンドであることは明らかです。

しかし、あなたが10ポンドを取り、それを以下に与える場合:

weight := stones + pounds/14.0;

次に、10/14の重み値が見つかります。それを10ロット追加して、100/14を取得します。そうですね、きっとあなたは私のドリフトを手に入れるでしょう!

このようなデータを保存するもう1つの明白な方法は、ポンドです。整数または浮動小数点のいずれかが意味をなします。

于 2013-03-18T18:12:31.293 に答える
4

または、このようなことを行うことができます:-)

unit PoundsAndStones;

interface

uses
  SysUtils;

type
  TPoundStone = record
  private
    FWeight : double;
    function GetPounds: integer;
    procedure SetPounds(const Value: integer);
    function GetStones: integer;
    procedure SetStones(const Value: integer);
    procedure SetWeight(const Value: double);
    function GetString: string;
  public
    property Weight : double read FWeight write SetWeight;   //Weight in stones.pounds format
    property Stones : integer read GetStones write SetStones; //Weight in stones, fraction part ignored
    property Pounds : integer read GetPounds write SetPounds; //Weight in pounds
    property AsString : string read GetString;

    class operator Implicit(A : double) : TPoundStone;
    class operator Implicit(A : TPoundStone) : double;
    class operator Add(A, B: TPoundStone): TPoundStone;
  end;

implementation

class operator TPoundStone.Add(A, B: TPoundStone): TPoundStone;
begin
  Result.Weight := A.Weight + B.Weight;
end;

function TPoundStone.GetPounds: integer;
begin
  Result := round(frac(FWeight)*100);
end;

function TPoundStone.GetStones: integer;
begin
  Result := trunc(FWeight);
end;

function TPoundStone.GetString: string;
var
  P,S : string;
begin
  if Stones > 1 then
    S := inttostr(Stones)+' stones'
  else if Stones = 1 then
    S := '1 stone'
  else
    S := '';

  if Pounds > 1 then
    P := inttostr(Pounds)+' pounds'
  else if Pounds = 1 then
    P := '1 pound'
  else
    P := '';

  if (P > '') and (S > '') then
    Result := S + ' and ' + P
  else
    Result := S + P;
end;

class operator TPoundStone.Implicit(A: double): TPoundStone;
begin
  Result.FWeight := A;
end;

class operator TPoundStone.Implicit(A: TPoundStone): double;
begin
  Result := A.FWeight;
end;

procedure TPoundStone.SetWeight(const Value : double);
var
  P,S : integer;
begin
  S := trunc(Value);
  P := round(100*frac(Value));

  S := S + P div 14;
  P := P mod 14;

  FWeight := S + P/100.0;
end;

procedure TPoundStone.SetPounds(const Value: integer);
var
  P,S : integer;
begin
  S := Value div 14;
  P := Value mod 14;

  FWeight := S + P/100.0;
end;

procedure TPoundStone.SetStones(const Value: integer);
begin
  Weight := Value*14;
end;

end.

これにより、このようなことができます

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  PoundsAndStones in 'PoundsAndStones.pas';

var
  P0,P1,P2 : TPoundStone;

begin
  P0 := 1.05;
  P1 := 3.12;

  writeln(P0.AsString);
  writeln(P1.AsString);

  P2 := P0 + P1;
  writeln(P2.AsString);
end.

これはこれを出力します:

1 stone and 5 pounds
3 stones and 12 pounds
5 stones and 3 pounds
于 2013-03-18T19:47:20.750 に答える
1
function tostonesandpounds(p_value : double) : double;
var
  stone : integer;
  pounds : integer;
begin
  stone := trunc(p_value);
  pounds := trunc(100.0*(p_value + 0.009));
  pounds := pounds-(stone*100);
  inc(stone,(pounds div 14));
  pounds := pounds mod 14;
  result := stone + (pounds/100.0);
end;
于 2013-03-18T18:46:23.390 に答える