How to use Pascal string in equation

后端 未结 1 1262
花落未央
花落未央 2020-12-20 09:40

I have a little problem. I have written a program which asks for user for a code which contains 11 digits. I defined it as string but now I would like to use every digit fro

相关标签:
1条回答
  • 2020-12-20 10:13

    Use a loop instead. (I'm only showing the total value and check digit calculation - you need to get the user input first into a variable named UserISBN yourself.)

    function AddCheckDigit(const UserISBN: string): string;    
    var
      i, Sum: Integer;
      CheckDigit: Integer;
      LastCharValue: string;
    begin
      Assert(Length(UserISBN) = 10, 'Invalid ISBN number.');
      Sum := 0;
      for i := 1 to 10 do
        Sum := Sum + (Ord(UserISBN[i]) * i);
    
      { Calculate the check digit }
      CheckDigit := 11 - (Sum mod 11);
    
      { Determine check digit character value }
      if CheckDigit = 10 then
        LastCharValue := 'X'
      else
        LastCharValue := IntToStr(CheckDigit);
    
      { Add to string for full ISBN Number }
      Result := UserISBN + LastCharValue;
    end;
    
    0 讨论(0)
提交回复
热议问题