如何在方程式中使用Pascal字串 [英] How to use Pascal string in equation

查看:85
本文介绍了如何在方程式中使用Pascal字串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题.我写了一个程序,要求用户输入包含11位数字的代码.我将其定义为字符串,但现在我想分别使用此代码中的每个数字并建立一个方程式.

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.

例如,如果代码是37605030299,我需要做公式:

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

找出什么是MOD.

这是对ISBN校验位的计算.

This is a calculation for an ISBN check digit.

推荐答案

请改为使用循环. (我只显示总价值和校验位计算-您需要先将用户输入输入到名为UserISBN的变量中.)

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;

这篇关于如何在方程式中使用Pascal字串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆