Решение на упр.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: (if battery_percentage > 100 {
100
} else {
battery_percentage
}),
last_reading: sensor_data,
status: "New".to_string(),
}
}
fn diagnostic_check(probe: &Probe) {
if probe.battery_percentage < 20 {
println!("Probe {} has low battery", probe.name);
} else {
println!("{:?}", probe);
}
}
fn recharge_probe(probe: &mut Probe, amount: u8) {
probe.battery_percentage += amount;
if probe.battery_percentage > 100 {
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.temperature > 80_f64 {
println!(
"Critical temperature of {} *C is reached",
sensor.temperature
)
} else if sensor.radiation_level > 100 {
println!(
"Critical radiation level of {} is reached",
sensor.radiation_level
)
}
}
fn main() {
let s1 = create_sensor_data(92.64, 32);
let s2 = create_sensor_data(112.64, 52);
let s3 = create_sensor_data(42.45, 132);
let s4 = create_sensor_data(56.78, 32);
let mut probe1 = create_probe("Voyager 1", 8, s1);
let mut probe2 = create_probe("Voyager 2", 56, s2);
let mut probe3 = create_probe("Sputnik 1", 123, s3);
let mut probe4 = create_probe("Venus 1", 45, s4);
diagnostic_check(&probe1);
diagnostic_check(&probe2);
diagnostic_check(&probe3);
diagnostic_check(&probe4);
recharge_probe(&mut probe1, 100);
recharge_probe(&mut probe4, 2);
diagnostic_check(&probe1);
diagnostic_check(&probe4);
update_status(&mut probe1, "Exploring");
update_status(&mut probe2, "Destroyed");
update_status(&mut probe3, "Exploring");
diagnostic_check(&probe1);
diagnostic_check(&probe2);
diagnostic_check(&probe3);
diagnostic_check(&probe4);
let reading = probe1.last_reading;
analyze_critical_sensor(reading);
// Полето last_reading липсва в probe1, понеже след let reading = probe1.last_reading, собствеността е преместена на променливата reading
// Това не може да се изпълни -> diagnostic_check(&probe1);
}

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

Николай качи първо решение на 18.10.2026 00:01 (преди 10 дена)