2013-03-23 104 views
1

我有這個任務,我要求用戶輸入11位數字代碼(字符串)。帕斯卡爾字符串的使用(正確的Python例子)

1) 例如,假設用戶輸入代碼是37605030299。

2) 然後我需要檢查最後一個數字是否匹配。這就是你如何得到最後一個數字: nr11 =(nr1 * 1 + nr2 * 2 + nr3 * 3 + nr4 * 4 + nr5 * 5 + nr6 * 6 + nr7 * 7 + nr8 * 8 + nr9 * 9 + nr10 * 1)國防部11

3) 這是我寫的:

var C, nr1, nr2, nr3, nr4, nr5, nr6, nr7, nr8, nr9, nr10, nr11: string; 

begin 

nr1:=(copy(C, 1, 1)); 
nr2:=(copy(C, 2, 1)); 
nr3:=(copy(C, 3, 1)); 
nr4:=(copy(C, 4, 1)); 
nr5:=(copy(C, 5, 1)); 
nr6:=(copy(C, 6, 1)); 
nr7:=(copy(C, 7, 1)); 
nr8:=(copy(C, 8, 1)); 
nr9:=(copy(C, 9, 1)); 
nr10:=(copy(C, 10, 1)); 
nr11:=(copy(C, 11, 1)); 

writeln('Enter the code which contains 11 digits:'); 
readln(C); 

if nr11 = (nr1*1 + nr2*2 + nr3*3 + nr4*4 + nr5*5 + nr6*6 + nr7*7 + nr8*8 + nr9*9 + nr10*1) mod 11 then 
    begin 
    writeln('The code is correct!'); 
    end 

else 
if nr11 <> (nr1*1 + nr2*2 + nr3*3 + nr4*4 + nr5*5 + nr6*6 + nr7*7 + nr8*8 + nr9*9 + nr10*1) mod 11 
    begin 
    writeln('The code is incorrect!'); 
    end; 

    readln(); 
end. 

這不工作,因爲我知道,像我這樣做你無法使用字符串中的方程,但它會工作?我只是在學習帕斯卡,對不起,如果這看起來太愚蠢。

此UI代碼應該是正確的。檢查:

1 * 3 + 2 * 7 + 3 * 6 + 4 * 0 + 5 * 5 + 6 * 0 + 7 * 3 + 8 * 0 + 9 * 2 + 1 * 9 = 108

108/11〜9,8

9 * 11 = 99

108-99 = 9(答案是9,所以最後一個數字必須是如圖9所示,最後的數字是9,其示出了代碼是正確的)

如果你不明白我想做什麼,那麼我在python中找到了一個應該是正確的例子:

def checkIDCode(code): 
    if len(code) != 11 or not code.isdigit(): 
      return False 

    c = map(int,code) 
    w1 = [1,2,3,4,5,6,7,8,9,1] 
    w2 = [3,4,5,6,7,8,9,1,2,3] 

    s1 = sum(map(lambda x,y: x*y, c[:-1], w1))%11 
    s2 = (sum(map(lambda x,y: x*y, c[:-1], w2))%11)%10 

    return s1 == c[-1] or s1 == 10 and s2 == c[-1] 
+0

見http://stackoverflow.com/questions/15581551/how-to-use-pascal-string-in-equation一些線索 – 2013-03-23 20:42:26

回答

0

的第一件事是,你在C複製的東西時,C是空的。你應該把readln(C)您嘗試訪問它的元素之前:

writeln('Enter the code which contains 11 digits:'); 
readln(C); 

nr1:=... 
nr2:=... 

的第二件事情是,如果值僅1個字符,你可以通過索引訪問:

nr1:= C[1]; 
nr2:= C[2]; 
... 

要將字符串轉換爲整數,則必須包括sysutils的使用StrToInt功能:http://www.freepascal.org/docs-html/rtl/sysutils/strtoint.html

的另一種方式(通過炭炭)是計算的char值和48之間的差(其爲「0」的ASCII碼)

示例代碼:

uses sysutils; 
var C:string 
    nr1, nr2, nr3, nr4, nr5, nr6, nr7, nr8, nr9, nr10, nr11: integer; 

begin 
readln(C); 
nr1:=strtoint(C[1]); 
nr2:=strtoint(C[2]); 
nr3:=strtoint(C[3]); 
. 
. 
. 
if nr11 = (nr1*1 + nr2*2 + nr3*3 + nr4*4 + nr5*5 + nr6*6 + nr7*7 + nr8*8 + nr9*9 + nr10*1) mod 11 then 
    writeln('The code is correct!') 
    //if you skip the begin/end statements, only the next statement 
    //is executed in loops/if statements 
else writeln('Not correct'); //notice that I didn't use ELSE IF but ELSE 
readln; 
//in Pascal you can skip the parathenses if you don't pass arguments to the function 
end. 
+0

當我嘗試 nr1:= strtoint(C [1]) 它說:錯誤:不兼容的類型:得到「LongInt」預計「ShortString」 – FredJx 2013-03-23 21:35:09

+0

@ Fr0f0 nr1,nr2,...應該是整數。我糾正了錯誤。 – kren470 2013-03-23 21:37:04

+0

非常感謝你!我試圖弄清楚這麼長時間。謝謝你我的心! – FredJx 2013-03-23 21:50:56

0

看起來像ISBN。要計算校驗位,我建議如下。

function DigitToInt(const c: Char): Integer; 
begin 
    if (c<'0') or (c>'9') then 
    raise Exception.Create('Invalid input'); 
    Result := ord(c)-ord('0'); 
end; 

這將範圍'0'到'9'中的單個字符轉換爲相應的整數值。

function CheckDigit(const s: string): Integer; 
var 
    i: Integer; 
begin 
    if Length(s)<>11 then 
    raise Exception.Create('Invalid input'); 
    Result := 0; 
    for i := 1 to 10 do 
    inc(Result, DigitToInt(s[i])*i); 
    Result := Result mod 11; 
end; 

這將計算11位數字代碼的校驗位。

要與計算出的校驗位比較實際的校驗位,你可以這樣寫:

if CheckDigit(code) <> DigitToInt(code[11]) then 
    .... handle error 
+0

它說,標識的培養,導致與digittoint找不到 – FredJx 2013-03-23 21:43:08

+0

我懂了。謝謝!你正在使用哪種編譯器? – FredJx 2013-03-23 21:51:48

+0

德爾福。該代碼也可以在fpc中正常工作。您接受的答案中的代碼要複雜得多。爲什麼要聲明所有這些變量。我的代碼根本不需要。 – 2013-03-23 21:54:47