首页 >

Fortran 校验身份证是否合法

作者:fcode  日期:03-03
来源:Fcode研讨团队
运行效果如下

500109198512168888
身份证错误

Program www_fcode_cn
  Implicit None
  Integer i
  integer:: sfz(18)
  character( len = 18 ) :: c
  Read( * , '(a18)' ) c
  Do i = 1 , 18
    if ( c(i:i) == 'X' .or. c(i:i) == 'x' ) then
      sfz(i) = -1
    else
      sfz(i) = ichar(c(i:i)) - ichar('0')
    end if
  End Do
  if ( SfzCheckNum(sfz) ) then
    write( * , * ) '身份证正确'
  else
    write( * , * ) '身份证错误'
  end if

contains
  
  Logical Function SfzCheckNum( s )
    integer :: s( 18 )
    integer , parameter :: sc(17) = (/7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2/)
    integer , parameter :: sck(0:10) = (/1,0,-1,9,8,7,6,5,4,3,2/)
    integer i
    integer iSum
    iSum = 0
    Do i = 1 , 17
      iSum = iSum + s(i)*sc(i)
    End Do
    iSum = mod( iSum , 11 )
    SfzCheckNum = ( sck(iSum) == s(18) )
  End Function SfzCheckNum
  
End Program www_fcode_cn 
常规|工具|专业|读物|
代码|教学|算法|
首页 >
FortranCoder手机版-导航