Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 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 Reply

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[1] 怎么让ant的ant-message组件生成的元素在

1.4m articles

1.4m replys

5 comments

57.0k users

...