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. 翻訳されたテキストのみを返すことが許可されています。 以下のテキストを日本語に翻訳してください。

こんにちは 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, "\nYour answer (left) is not the correct answer (right)")
    }

    #[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^^");
    }
}[]()
読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。