banner
HoshinoAya

HoshinoAya

github
twitter

你是一个Markdown翻译专家。在翻译过程中,你需要特别注意保持所有Markdown语法和标记的完整性,不改变HTML标记的功能,以确保翻译内容不影响任何语法或标记的呈现。请按照以下规则进行翻译: 1. 识别和翻译文本内容:只识别和翻译Markdown中的纯文本内容,包括标题、段落和列表项中的文本。 2. 保留标记和属性:遇到HTML标记(如<img>、<video>、<a>等),请只翻译标记中可见的文本(如alt属性中的文本),并保留所有标记、属性名称和链接地址不变。 3. 特殊语法处理:对于Markdown特定的语法(如链接、图像标记![alt text](link)),只翻译描述性文本部分(如alt文本),不改变链接或语法结构。 4. 保持格式不变:确保所有Markdown格式(如粗体、斜体、代码块)在翻译过程中保持不变。 5. 你的任务是确保翻译内容准确且不破坏原始Markdown结构和HTML标记的功能。请在翻译过程中仔细检查,确保语法和标记的正确呈现。 6. 你只能返回翻译后的文本,不能返回其他内容。 将以下文本翻译为简体中文: undefined

你好 xlog!#

use std::collections::VecDeque;

fn to_postfix(infix: &str) -> String {
    let preced = |op: char| match op {
        '(' | ')' => 1,
        '^' => 2,
        '*' | '/' => 3,
        '+' | '-' => 4,
        _ => unreachable!()
    };
    let op_left_assoc = |op: char| match op {
        '^' => false,
        _ => true
    };
    let mut stack: VecDeque<char> = VecDeque::new();
    let mut output = String::new();
    for x in infix.chars() {
        match x {
            ch if ch.is_digit(10) => output.push(ch),
            '+' | '-' | '*' | '/' | '^' => {
                while let Some(&op) = stack.back() {
                    if !(op != '(' && ((op_left_assoc(x) && preced(x) >= preced(op)) ||
                        (!op_left_assoc(x) && preced(x) > preced(op)))) {
                        break;
                    }

                    output.push(op);
                    stack.pop_back();
                }
                stack.push_back(x);
            }
            '(' => stack.push_back(x),
            ')' => {
                while let Some(op) = stack.pop_back() {
                    if op == '(' { break; }
                    output.push(op);
                }
            }
            _ => unreachable!()
        }
    }
    output.push_str(stack.iter().rev()
        .filter(|&&x| x != '(' && x != ')').collect::<String>().as_str());
    output
}

// 在这里添加你的测试。
// 参考 https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html

#[cfg(test)]
mod tests {
    use itertools::Itertools;
    use super::to_postfix;
    
    fn do_test(actual: &str, expected: &str) {
        assert_eq!(actual, expected, "\n你的答案(左边)与正确答案(右边)不符")
    }

    #[test]
    fn fixed_tests() {
        do_test(&to_postfix("2+7*5"), "275*+");
        do_test(&to_postfix("3*3/(7+1)"), "33*71+/");
        do_test(&to_postfix("5+(6-2)*9+3^(7-1)"), "562-9*+371-^+");
        do_test(&to_postfix("(5-4-1)+9/5/2-7/1/7"), "54-1-95/2/+71/7/-");
        do_test(&to_postfix("1^2^3"), "123^^");
    }
}[]()
加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。