Решение на упр.02 задача 1 от Тихомир Тинков

Обратно към всички решения

Към профила на Тихомир Тинков

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)

Код

#[derive(Debug)]
struct SensorData {
temperature: f64,
radiation_level: u32,
}
#[derive(Debug)]
struct Probe {
name: String,
battery_percentage: u8,
last_reading: SensorData,
status: String,
}
fn create_sensor_data(temperature: f64, radiation_level: u32) -> SensorData {
SensorData {
temperature,
radiation_level,
}
}
fn create_probe(name: &str, battery_percentage: u8, sensor_data: SensorData) -> Probe {
Probe {
name: name.to_string(),
battery_percentage,
last_reading: sensor_data,
status: "Standby".to_string(),
}
}
fn diagnostic_check(probe: &Probe) {
if probe.battery_percentage < 20 {
println!("Warning: Low battery!");
} else {
println!("{:?}", probe);
}
}
fn recharge_probe(probe: &mut Probe, amount: u8) {
if probe.battery_percentage + amount < 100 {
probe.battery_percentage += amount;
} else {
probe.battery_percentage = 100;
}
}
fn update_status(probe: &mut Probe, new_status: &str) {
probe.status = new_status.to_string();
}
fn analyze_critical_sensor(sensor: SensorData) {
if sensor.radiation_level > 100 || sensor.temperature > 40. || sensor.temperature < -40. {
println!("Warning: Critical conditions!");
}
}
fn main() {
let battery_percentage = 65;
let temperature = 15.;
let radiation_level = 5;
let amount = 15;
let new_status = "Exploring";
//let battery_percentage = 19;
//let temperature = -75.;
//let radiation_level = 120;
//let amount = 40;
//let new_status = "Error";
let mut probe1 = create_probe(
"Voyager-X",
battery_percentage,
create_sensor_data(temperature, radiation_level),
);
diagnostic_check(&probe1);
recharge_probe(&mut probe1, amount);
update_status(&mut probe1, new_status);
//diagnostic_check(&probe1);
//probe1.last_reading is moved and no longer holds any value
let reading = probe1.last_reading;
analyze_critical_sensor(reading);
//diagnostic_check(&probe1); //attempted use of moved value (probe1.last_reading); will fail
}

История (1 версия и 0 коментара)

Тихомир качи първо решение на 21.10.2026 23:33 (преди 6 дена)