Cod sursa(job #3268223)

Utilizator edyeftimieEftimie Eduard edyeftimie Data 14 ianuarie 2025 04:50:34
Problema A+B Scor 100
Compilator rs Status done
Runda Arhiva de probleme Marime 1.61 kb
//https://www.infoarena.ro/problema/adunare
use std::fs;

/// Read the content of a file and return a Vec<String> where each element is a line from the file
///
/// # Arguments
/// * `filename` - A string slice that holds the name of the file
///
/// # Returns
/// * A Vec<String> where each element is a line from the file
fn read_lines_from_file(filename: &str) -> Vec<String> {
    fs::read_to_string(filename)
        .unwrap() // panic if file not found
        .lines() // split by lines (each line is a slice)
        .map(String::from) // convert &str (each slice) to String
        .collect() // collect into a Vec<String>
}

/// Transform a string into an integer
///
/// # Arguments
/// * `line` - A string slice that holds the number
///
/// # Returns
/// * An integer that is the number from the string
fn tranform_string_in_int(line: String) -> i32 {
    //u32 - unsigned 32-bit integer
    //i32 - signed 32-bit integer
    return line
        .trim() // remove leading and trailing whitespaces
        .parse() // parse the string into a number
        .expect("Invalid number in file"); // panic if the string is not a number
}

/// Main function
/// 
/// Read the content of a file, transform the strings into integers, add them and write the result in a file
fn main() {
    let file_path = "adunare.in";

    let lines = read_lines_from_file(file_path);

    let a = tranform_string_in_int(lines[0].clone());
    let b = tranform_string_in_int(lines[1].clone());

    let result = a + b;

    fs::write("adunare.out", result.to_string())
        .expect("Unable to write file");
}