Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

freepascal - How to use Pascal string in equation

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 from this code individually and make an equation.

for example if code is 37605030299 i need to do equation:

(1*3 + 2*7 + 3*6 + 4*0 + 5*5 + 6*0 + 7*3 + 8*0 + 9*2 + 1*9) / 11

and find out what's the MOD.

This is a calculation for an ISBN check digit.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...