Outils pour utilisateurs

Outils du site


nsi:langages:rust:solutions:isbn

Warning: Undefined array key 2 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 214

Warning: Undefined array key 2 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 214

Vérification ISBN

Fiche de l'exercice

use std::env;

fn is_valid(s: &String) -> bool{
    let mut colonne = 1;
    let mut total_pair = 0;
    let mut total_impair = 0;

    for car in s.chars() {
        let digit:u32 = match car.to_digit(10) {
            Some(nombre) => nombre,
            None => continue,
        };
        if colonne%2 == 0 {
            total_pair += digit;
        } else {
            total_impair += digit;
        }
        colonne += 1;
    }
    let total = total_impair + 3*total_pair;
    if total%10 == 0 {
        true
    } else {
        false
    }
}


fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() != 2 {
        println!("Entrez l'isbn à tester dans la ligne de commande.");
        println!("Par exemple {} 9-782123-456803", &args[0]);
        return;
    }
    if is_valid(&args[1]) {
        println!("{} est valide.", &args[1]);
    } else {
        println!("{} est invalide.", &args[1]);
    }
}
nsi/langages/rust/solutions/isbn.txt · Dernière modification : de goupillwiki