View previous topic :: View next topic
|
Author |
Message |
bbharathiraj Warnings : 1 New User
Joined: 26 Oct 2006 Posts: 50 Location: Chennai
|
|
|
|
Could some one help me to provide the COBOL code for the below logic?
The calculation of an ISBN-13 check digit begins with the first 12 digits of the thirteen-digit ISBN (thus excluding the check digit itself). Each digit, from left to right, is alternately multiplied by 1 or 3, then those products are summed modulo 10 to give a value ranging from 0 to 9. Subtracted from 10, that leaves a result from 1 to 10. A zero (0) replaces a ten (10), so, in all cases, a single check digit results.
For example, the ISBN-13 check digit of 978-0-306-40615-? is calculated as follows:
s = 9×1 + 7×3 + 8×1 + 0×3 + 3×1 + 0×3 + 6×1 + 4×3 + 0×1 + 6×3 + 1×1 + 5×3
= 9 + 21 + 8 + 0 + 3 + 0 + 6 + 12 + 0 + 18 + 1 + 15
= 93
93 / 10 = 9 remainder 3
10 – 3
= 7
Thus, the check digit is 7, and the complete sequence is ISBN 978-0-306-40615-7. |
|
Back to top |
|
|
Manuneedhi K
Active User
Joined: 07 May 2008 Posts: 115 Location: Chennai
|
|
|
|
Try this.
Code: |
IDENTIFICATION DIVISION.
PROGRAM-ID. TESTPGMP.
AUTHOR. XXXXXXXXXXXXXX.
*
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WK-SUM PIC 9(5).
01 WK-REM PIC 9(5).
01 WK-ISBN.
05 WK-NUM1 PIC 9(01) .
05 WK-NUM2 PIC 9(01) .
05 WK-NUM3 PIC 9(01) .
05 FILLER PIC X(1) .
05 WK-NUM4 PIC 9(01) .
05 FILLER PIC X(1) .
05 WK-NUM5 PIC 9(01) .
05 WK-NUM6 PIC 9(01) .
05 WK-NUM7 PIC 9(01) .
05 WK-NUM7 PIC 9(01) .
05 FILLER PIC X(1) .
05 WK-NUM8 PIC 9(01) .
05 WK-NUM9 PIC 9(01) .
05 WK-NUM10 PIC 9(01) .
05 WK-NUM11 PIC 9(01) .
05 WK-NUM12 PIC 9(01) .
05 FILLER PIC X(1) .
05 WK-NUM13 PIC 9(01) .
PROCEDURE DIVISION.
0000-MAIN-PARA.
MOVE '978-0-306-40615-' TO WK-ISBN
COMPUTE WK-SUM = WK-NUM1 * 1 + WK-NUM2 * 3 +
WK-NUM3 * 1 + WK-NUM4 * 3 +
WK-NUM5 * 1 + WK-NUM6 * 3 +
WK-NUM7 * 1 + WK-NUM8 * 3 +
WK-NUM9 * 1 + WK-NUM10 * 3 +
WK-NUM11 * 1 + WK-NUM12 * 3
COMPUTE WK-REM = FUNCTION MOD(WK-SUM,10)
COMPUTE WK-NUM13 = 10 - WK-REM
DISPLAY WK-ISBN.
STOP RUN
. |
|
|
Back to top |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
|
|
How about?
Code: |
WORKING-STORAGE SECTION.
01 CD PIC 9(5).
01 WK-ISBN PIC X(17).
01 N REDEFINES
WK-ISBN PIC 9(17).
PROCEDURE DIVISION.
0000-MAIN-PARA.
MOVE '978-0-306-40615-' TO WK-ISBN
COMPUTE CD = N(01:1)
+ N(02:1) * 3
+ N(03:1)
+ N(05:1) * 3
+ N(07:1)
+ N(08:1) * 3
+ N(09:1)
+ N(11:1) * 3
+ N(12:1)
+ N(13:1) * 3
+ N(14:1)
+ N(15:1) * 3
COMPUTE N(17:1) = 10 - FUNCTION MOD(WK-SUM,10)
DISPLAY WK-ISBN.
|
|
|
Back to top |
|
|
Antonio Barata Warnings : 1 New User
Joined: 04 Apr 2007 Posts: 37 Location: Lisbon, Portugal
|
|
|
|
CICS Guy
I think there is something wrong with your code.
Did you compile it? |
|
Back to top |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
|
|
Antonio Barata wrote: |
CICS Guy
I think there is something wrong with your code.
Did you compile it? |
Could be, no compiler, what do you see as an error? |
|
Back to top |
|
|
Antonio Barata Warnings : 1 New User
Joined: 04 Apr 2007 Posts: 37 Location: Lisbon, Portugal
|
|
|
|
[quote=Could be, no compiler, what do you see as an error?[/quote]
The message is the following:
IGYPA3074-S "N (ALPHANUMERIC REFERENCE MODIFIED ITEM)" was not numeric, but was sender in an arithmetic expression. The statement was discarded.
It refers to the COMPUTE statement where you pick up each digit an multiply it. |
|
Back to top |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
Back to top |
|
|
Antonio Barata Warnings : 1 New User
Joined: 04 Apr 2007 Posts: 37 Location: Lisbon, Portugal
|
|
|
|
I think this code will do the job, providing that you put all the numbers in the numeric field "ws-array":
WORKING-STORAGE SECTION.
01 ws-ind PIC S9(4) COMP VALUE 0.
01 ws-multiplier-factor PIC S9(3) COMP-3 VALUE 0.
01 ws-sum PIC S9(5) COMP-3 VALUE 0.
01 ws-ckd PIC 9(02) VALUE 0.
01 ws-array PIC 9(13) VALUE 1234567890321.
01 FILLER REDEFINES ws-array.
02 ws-number OCCURS 13 PIC 9.
PROCEDURE DIVISION.
0000-MAIN.
PERFORM VARYING ws-ind FROM 1 BY 1
UNTIL ws-ind > LENGTH OF ws-array
IF FUNCTION REM(ws-ind 2) = 1
MOVE 1 TO ws-multiplier-factor
ELSE
MOVE 3 TO ws-multiplier-factor
END-IF
COMPUTE ws-sum = ws-sum
+ ( ws-number(ws-ind)
* ws-multiplier-factor )
END-PERFORM.
COMPUTE ws-ckd = 10 - FUNCTION MOD(ws-sum 10)
DISPLAY ws-ckd.
I compiled it Ok. Didn't test it. Please do. |
|
Back to top |
|
|
bbharathiraj Warnings : 1 New User
Joined: 26 Oct 2006 Posts: 50 Location: Chennai
|
|
|
|
Thanks to every one. All your codes are working fine. |
|
Back to top |
|
|
|