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^^");
    }
}[]()
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。