// 查验身份证
from std import console.*
from std import convert.*
from std import collection.*
from std import math.*
from std import format.*
//import std.format.*
//import std.math.*
//import std.collection.*
//import std.console.*
//import std.convert.*
//判断输入的字符串是否为空。
func getOrThrow(p: ?String): String{
match (p) {
case Some(x) => x
case None => ""
}
}
//判断输入的字符串是否为数字。返回真假。
func isNumber(number_string :String):Bool{
var number_rune :Array<Rune>=number_string.toRuneArray()
for(number in number_rune){
if(!number.isAsciiNumber()){
return false
}
}
return true
}
//获取输入的数字字符串。返回字符串。
func getInputNumber():String{
var input=Console.stdIn.readln()
let input_to_strig: Option<String> = input
var input_to_strig_to :String=getOrThrow(input_to_strig)
if(isNumber(input_to_strig_to)){
return input_to_strig_to
}
return ""
}
//获取输入的字符串。返回字符串。
func getInputString():String{
var input=Console.stdIn.readln()
let input_to_strig: Option<String> = input
var input_to_strig_to :String=getOrThrow(input_to_strig)
return input_to_strig_to
return ""
}
func getMatch(temp :Int64):String{
match (temp) {
case 0 => return "1"
case 1 => return "0"
case 2 => return "x"
case 3 => return "9"
case 4 => return "8"
case 5 => return "7"
case 6 => return "6"
case 7 => return "5"
case 8 => return "4"
case 9 => return "3"
case 10 => return "2"
case _ => return ""
}
}
//权重对比。
func getCheckW(temp :String):Bool{
var w :Array<Int64> =[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
var tempW:Array<Rune>=temp.toRuneArray()
var tempNumber:Int64 =0
for(i in 0..w.size){
tempNumber+=w[i]*Int64.parse(tempW[i].toString())
}
var tempWNumber=tempNumber%11
if(getMatch(tempWNumber)!=tempW[17].toString()){
return false
}else{
return true
}
}
//检测身份证号是否合格。
func getCheck(temp :String):Array<(Bool,String)>{
if(temp.size==18){
var tempRune:Array<Rune>=temp.toRuneArray()
var tempN=""
for(i in 0..tempRune.size-1){
tempN+=tempRune[i].toString()
}
if(isNumber(tempN)){
if(getCheckW(temp)){
var tempArray:Array<(Bool,String)> = [(true,temp)]
return tempArray
}else{
var tempArray:Array<(Bool,String)> = [(false,temp)]
return tempArray
}
}else{
var tempArray:Array<(Bool,String)> = [(false,temp)]
return tempArray
}
}
return []
}
//输入输出模块。
func getOutPut(){
var numberString=getInputNumber()
var numberTime:Int64 =0
if(numberString.size>0){
numberTime=Int64.parse(numberString)
}
var arrayListN=ArrayList<Array<(Bool,String)>>()
if(numberTime>0&&numberTime<=100){
var boo:Bool=true
for(i in 0..numberTime){
var tempArray:Array<(Bool,String)>=getCheck(getInputString())
if(!tempArray.isEmpty()){
if(tempArray[0][0]==false){
boo=false
}
arrayListN.append(tempArray)
} else{
boo=false
}
}
for(i in arrayListN){
if(!i[0][0]){
println("${i[0][1]}")
}
}
if(boo){
println("All passed")
}
}
}
main(){
getOutPut()
}
仓颉PAT程序设计 L1-016 查验身份证(cangjie)
最新推荐文章于 2024-08-22 17:27:46 发布