ISBN号校检程序,C#与SQL版

1

2 /*

3 Author:EF

4 Email:escms@qq.com

5 Date:08-10-14

6 Desc:计算正确ISBN号,错误则返回为空

7 DEMO:select dbo.isbn('7504536093')

8 */

9

10 CREATE FUNCTION ISBN(@isbn varchar(50))

11 --ALTER FUNCTION ISBN(@isbn varchar(50))

12 RETURNS nvarchar(50)

13 AS

14 BEGIN

15 DECLARE @i int

16 DECLARE @sn int

17 DECLARE @endisbn nvarchar(50)

18 SET @endisbn = ''

19

20 IF (len(@isbn) >=12 and left(@isbn,3) = '978')

21 BEGIN

22 SET @isbn = rtrim(ltrim(replace(@isbn,'-','')))

23 SET @isbn = replace(replace(@isbn,'x',''),'.','')

24 IF (ISNUMERIC(@isbn)=1 and len(@isbn) >= 9)

25 BEGIN

26 SET @sn = cast(left(@isbn,1) as int) +

27 cast(right(left(@isbn,2),1) as int) * 3 +

28 cast(right(left(@isbn,3),1) as int) +

29 cast(right(left(@isbn,4),1) as int) * 3 +

30 cast(right(left(@isbn,5),1) as int) +

31 cast(right(left(@isbn,6),1) as int) * 3 +

32 cast(right(left(@isbn,7),1) as int) +

33 cast(right(left(@isbn,8),1) as int) * 3 +

34 cast(right(left(@isbn,9),1) as int) +

35 cast(right(left(@isbn,10),1) as int) * 3 +

36 cast(right(left(@isbn,11),1) as int) +

37 cast(right(left(@isbn,12),1) as int) * 3

38 SET @sn = 10-(@sn % 10)

39 IF @sn = 10

40 SET @sn = 0

41 SET @endisbn = left(@isbn,12) + cast(@sn as varchar)

42 END

43 ELSE

44 SET @endisbn = 'Error'

45

46 END

47 IF (len(@isbn) >=9 and len(@isbn) <= 10 and left(@isbn,1) = '7')

48 BEGIN

49 SET @isbn = rtrim(ltrim(replace(@isbn,'-','')))

50 SET @isbn = replace(replace(@isbn,'x',''),'.','')

51 IF (ISNUMERIC(@isbn)=1 and len(@isbn) >= 9)

52 BEGIN

53 SET @sn = cast(right(left(@isbn,1),1) as int) * 10 +

54 cast(right(left(@isbn,2),1) as int) * 9 +

55 cast(right(left(@isbn,3),1) as int) * 8 +

56 cast(right(left(@isbn,4),1) as int) * 7 +

57 cast(right(left(@isbn,5),1) as int) * 6 +

58 cast(right(left(@isbn,6),1) as int) * 5 +

59 cast(right(left(@isbn,7),1) as int) * 4 +

60 cast(right(left(@isbn,8),1) as int) * 3 +

61 cast(right(left(@isbn,9),1) as int) * 2

62

63 SET @sn = 11-(@sn % 11)

64 IF @sn = 10

65 SET @endisbn = left(@isbn,9) + 'X'

66 ELSE

67 BEGIN

68 IF @sn = 11

69 SET @endisbn = left(@isbn,9) + '0'

70 ELSE

71 SET @endisbn = left(@isbn,9) + cast(@sn as varchar)

72 END

73 END

74 ELSE

75 SET @endisbn = 'Error'

76

77 END

78 IF @endisbn = ''

79 SET @endisbn = @isbn

80

81 RETURN @endisbn

82 END

输入9~10位ISBN号可计算出10位正确的ISBN号。

输入11~14位ISBN号可计算出13位正确的ISBN号。

用于校检和规范ISBN号。

C#版:

1

2 private void button1_Click(object sender, EventArgs e)

3 {

4 if (textBox1.Text == "" || textBox1.Text.Length > 15)

5 {

6 MessageBox.Show("书号输入不正确!","错误");

7 return;

8 }

9 try

10 {

11 string isbnstr;

12 int isbns;

13 isbnstr = textBox1.Text.Trim();

14 isbnstr = isbnstr.Replace("-", "").Replace("X", "").Replace(".", "");

15 if (isbnstr.Length >= 11 && isbnstr.Length <= 14)

16 {

17 isbns=0;

18 isbns += Convert.ToInt32(isbnstr.Substring(0, 1)) * 1;

19 isbns += Convert.ToInt32(isbnstr.Substring(1, 1)) * 3;

20 isbns += Convert.ToInt32(isbnstr.Substring(2, 1)) * 1;

21 isbns += Convert.ToInt32(isbnstr.Substring(3, 1)) * 3;

22 isbns += Convert.ToInt32(isbnstr.Substring(4, 1)) * 1;

23 isbns += Convert.ToInt32(isbnstr.Substring(5, 1)) * 3;

24 isbns += Convert.ToInt32(isbnstr.Substring(6, 1)) * 1;

25 isbns += Convert.ToInt32(isbnstr.Substring(7, 1)) * 3;

26 isbns += Convert.ToInt32(isbnstr.Substring(8, 1)) * 1;

27 isbns += Convert.ToInt32(isbnstr.Substring(9, 1)) * 3;

28 isbns += Convert.ToInt32(isbnstr.Substring(10, 1)) * 1;

29 isbns += Convert.ToInt32(isbnstr.Substring(11, 1)) * 3;

30 isbns = 10 - (isbns % 10);

31 if (isbns == 10) { isbns = 0; }

32 textBox2.Text = isbnstr.Substring(0, 12) + isbns.ToString();

33 }

34 else if (isbnstr.Length >= 9 && isbnstr.Length <= 10)

35 {

36 isbns = 0;

37 isbns += Convert.ToInt32(isbnstr.Substring(0, 1)) * 10;

38 isbns += Convert.ToInt32(isbnstr.Substring(1, 1)) * 9;

39 isbns += Convert.ToInt32(isbnstr.Substring(2, 1)) * 8;

40 isbns += Convert.ToInt32(isbnstr.Substring(3, 1)) * 7;

41 isbns += Convert.ToInt32(isbnstr.Substring(4, 1)) * 6;

42 isbns += Convert.ToInt32(isbnstr.Substring(5, 1)) * 5;

43 isbns += Convert.ToInt32(isbnstr.Substring(6, 1)) * 4;

44 isbns += Convert.ToInt32(isbnstr.Substring(7, 1)) * 3;

45 isbns += Convert.ToInt32(isbnstr.Substring(8, 1)) * 2;

46 isbns = 11 - (isbns % 11);

47 if (isbns == 10) {

48 textBox2.Text = isbnstr.Substring(0, 9) + "X";

49 }

50 else if (isbns == 11)

51 { textBox2.Text = isbnstr.Substring(0, 9) + "0"; }

52 else

53 {

54 textBox2.Text = isbnstr.Substring(0, 9) + isbns.ToString();

55 }

56 }

57 else {

58 MessageBox.Show("不可识别的ISBN号", "错误");

59 return;

60 }

61 }

62 catch

63 {

64 MessageBox.Show("不可识别的ISBN号", "错误");

65 return;

66 }

67

68

69 }

70