美文网首页简村消零派(洛九幽)
Rust每日练习---货币换算

Rust每日练习---货币换算

作者: 梦想实现家_Z | 来源:发表于2021-06-28 12:01 被阅读0次

编写一个货币兑换程序。具体来说,是将欧元兑换成美元。

提示输入手动的欧元数,以及欧元的当前汇率。打印可以兑换的美元数。

货币兑换的公式为:


image-20210628112823364.png

其中

  • amountto 是美元
  • amountfrom 是欧元数
  • ratefrom 是欧元的当前汇率
  • rateto 是美元的当前汇率

示例输出

How mang euros are you exchanging? 81

What is the exchange rate? 137.51

81 euros at an exchange rate of 137.51 is

111.38 U.S. dollars.

约束

  • 注意小数部分,不足1美分的向上取整。
  • 使用单条输出语句。
fn main(){
    let amountfrom = read_from_console(String::from("How mang euros are you exchanging?"));
    println!("euros is:{}", amountfrom);

    let ratefrom = read_from_console(String::from("What is the exchange rate?"));
    println!("exchange rate is:{}", ratefrom);

    const RATE_TO: f32 = 100.0;

    let result = (amountfrom * ratefrom).ceil() / RATE_TO;
    println!(
        "{} euros at an exchange rate of {} is {} U.S. dollars.",
        amountfrom, ratefrom, result
    );
}
// 从控制台读取数据
fn read_from_console(notice: String) -> f32 {
    println!("{}", notice);
    loop {
        let mut amountfrom = String::new();
        // 控制台输入欧元金额
        std::io::stdin().read_line(&mut amountfrom).unwrap();
        // 注意要对输入的字符串进行trim()操作后再做类型转换
        if let Ok(res) = amountfrom.trim().parse::<f32>() {
            return res;
        } else {
            println!("输入的数据类型错误:{}", amountfrom);
        }
    }
}

结果:

How mang euros are you exchanging?
81
euros is:81
What is the exchange rate?
137.51
exchange rate is:137.51
81 euros at an exchange rate of 137.51 is 111.39 U.S. dollars.

相关文章

  • Rust每日练习---货币换算

    编写一个货币兑换程序。具体来说,是将欧元兑换成美元。 提示输入手动的欧元数,以及欧元的当前汇率。打印可以兑换的美元...

  • Web3极客日报 #5

    Rust 编程小练习 Rustlings https://github.com/rust-lang/rustlin...

  • rust leetcode zigzag-conversion

    每日小刷 leetcode RuntimeMemory16ms2.5m 好好学习rust和基础算法 目标:rust工程师

  • rust leetcode 最大回文序列

    每日小刷 leetcode RuntimeMemory16ms2.5m 好好学习rust和基础算法 目标:rust工程师

  • Rust语言编程实例100题-035

    Rust语言编程实例100题-035 题目:字符串反转练习,如将字符串 "i like rust!" 反转为"!t...

  • Rust语言编程实例100题-066

    Rust语言编程实例100题-066 题目:Rust指针练习。先来理解下引用和借用的概念。引用是作为参数传递给函数...

  • rust练习-1

    to be continued

  • rust练习-2

    总结 string 遍历,char比较vec 当做stack使用,出栈 入栈vec 变量 修改数据,usize 索...

  • rust练习-5

    总结 二分查找,容易溢出 用 u64String chars next 遍历,match匹配值String int...

  • rust练习-3

    总结 VecDequeVec reverseas 类型转换usize类型 -1 不会小于0,永远>=0双指针合并v...

网友评论

    本文标题:Rust每日练习---货币换算

    本文链接:https://www.haomeiwen.com/subject/vcjqultx.html