Rust 程序设计语言 中文版(第三章习题)

use std::io;

fn main() {
    println!("请输入℉或℃");
    println!("F代表华氏度,C代表摄氏度");
    let mut _fah: f64 = 0.0;
    let mut _cel: f64 = 0.0;
    let mut choose = String::new();
    io::stdin()
        .read_line(&mut choose)
        .expect("读取失败");
    if (&choose[0..1] == "F") || (&choose[0..1] == "f") {
        loop {
            println!("输入当前华氏度");
            choose.clear();
            io::stdin()
                .read_line(&mut choose)
                .expect("读取失败");

            _fah = match (choose[0..choose.len() - 1]).parse::<f64>() {
                Ok(num) => num,
                Err(_) => {
                    println!("输入当前摄氏度");
                    continue;
                }
            };
            _cel = (_fah - 32.0) / 1.8;
            println!("华氏度为: {} °F", _fah);
            println!("摄氏度为: {} °C", _cel);
            break;
        }
    } else if (&choose[0..1] == "C") || (&choose[0..1] == "c") {
        loop {
            println!("输入当前摄氏度");

            choose.clear();

            io::stdin()
                .read_line(&mut choose)
                .expect("读取失败");

            _cel = match (choose[0..choose.len() - 1]).parse::<f64>() {
                Ok(num) => num,
                Err(_) => {
                    println!("请输入数字");
                    continue;
                }
            };
            _fah = _cel * 1.8 + 32.0;
            println!("摄氏度为: {} °C", _cel);
            println!("华氏度为: {} °F", _fah);
            break;
        }
    } else {
        println!("输入无效");
    }
    println!("----------------------------");
    println!("<<< 斐波那契数列 >>>");
    let mut n: u32 = 0;
    loop {
        n += 1;
        if n > 30 {
            break;
        } else {
            print!("{:>2}: {:<20}", n, calculation(n));
        }
        if n % 3 == 0 {
            println!();
        }
    }
    println!("-------------------------");
    let count = [
        "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth",
        "tenth", "event", "Twelve",
    ];

    println!("The Twelve Days of Christmas");

    for (i, _item) in count.iter().enumerate(){
        println!(
            "On the {} day of Christmas
        \rMy true love sent to me",
            count[i]
        );
        if i == 0 {
            println!("A partridge in a pear tree");
            continue;
        }
        for j in 0..=i {
            match i - j {
                0 => println!("And a partridge in a pear tree"),
                1 => println!("Two turtle doves"),
                2 => println!("Three French hens"),
                3 => println!("Four calling birds"),
                4 => println!("Five golden Rings"),
                5 => println!("Six geese a-laying"),
                6 => println!("Seven swans a-swimming"),
                7 => println!("Eight maids a-milking"),
                8 => println!("Nine ladies dancing"),
                9 => println!("Ten Lords a Leaping"),
                10 => println!("Eleven Pipers Piping"),
                11 => println!("Twelve Drummers Drumming"),
                _ => (),
            }
        }
    }
}

fn calculation(num: u32) -> u32 {
    if num == 1 || num == 2 {
        1
    } else {
        calculation(num - 1) + calculation(num - 2)
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值