问题
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.
回答1:
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;
来源:https://stackoverflow.com/questions/15581551/how-to-use-pascal-string-in-equation