Giter VIP home page Giter VIP logo

30-seconds-of-rs's Introduction

30-seconds-of-rs

30 seconds to collect useful rust snippet.

License PRs Welcome

你可以在 30 秒或更短时间内收集有用的 rust 代码片段。

  • 使用 Ctrl + F 或者 command + F 来查找代码片段。
  • 代码片段基于 Rust,如果你还不熟悉可以在这里学习。

目录

📚 应用类

详细信息

calc

输入表达式,格式如 1 + 2 或 3 * 4,输入 'exit' 退出:

use std::io;

fn main() {
    loop {
        println!("请输入表达式,格式如 1 + 2 或 3 * 4,输入 'exit' 退出:");

        let mut input = String::new();
        io::stdin().read_line(&mut input).expect("读取输入失败");

        // 去除首尾空格
        let input = input.trim();

        if input == "exit" {
            println!("退出计算器");
            break;
        }

        // 解析表达式
        let tokens: Vec<&str> = input.split_whitespace().collect();

        if tokens.len() != 3 {
            println!("无效的表达式,请重新输入");
            continue;
        }

        // 获取操作数和运算符
        let operand1: f64 = match tokens[0].parse() {
            Ok(num) => num,
            Err(_) => {
                println!("无效的数字: {}", tokens[0]);
                continue;
            }
        };

        let operator = tokens[1];

        let operand2: f64 = match tokens[2].parse() {
            Ok(num) => num,
            Err(_) => {
                println!("无效的数字: {}", tokens[2]);
                continue;
            }
        };

        // 执行计算
        let result = match operator {
            "+" => operand1 + operand2,
            "-" => operand1 - operand2,
            "*" => operand1 * operand2,
            "/" => {
                if operand2 != 0.0 {
                    operand1 / operand2
                } else {
                    println!("除数不能为零");
                    continue;
                }
            }
            _ => {
                println!("无效的运算符: {}", operator);
                continue;
            }
        };

        // 输出结果
        println!("结果: {}", result);
    }
}


⬆ 回到顶部

30-seconds-of-rs's People

Contributors

sunny-117 avatar

Stargazers

dev-fes avatar Zhazha_JiaYiZhen avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.