逆波兰表示法 C# 无法正常工作

2023-12-01

我写了一个 rpn,带有一个结构图。

最新问题:现在无法正常工作。

如果输入字符串为“5 + ((1 + 2) * 4) - 3”

我的输出是: 5 1 2 + 4 * 3 - +

我必须得到这个结果: 5 1 2 + 4 * + 3 -

编辑了源码

*这是原来的问题,但对我有帮助,现在原来的错误已修复:*,

在调试时循环或int i = 12,c值为0\0或其他并且这个值被添加到输出(名称:公式)字符串作为“(”括号。我不知道为什么。还有最后一个 '-' 操作符号,不要添加到(或不看)在末尾输出字符串(公式) 我错误地认为这个问题是由“(”引起的。我尝试了使用其他字符串输入值的程序,但总是在我的字符串中添加一个“(”,我不知道为什么......我看到它与括号的数量无关。总是只有一个“(”添加到我的绳子...*) 是的,英语 LengyelFormula = rpn (匈牙利语)*

static void Main(string[] args)
    {
        String str = "5 + ( ( 1 + 2 ) *  4 ) −3";
        String result=LengyelFormaKonvertalas(str);
        Console.WriteLine(result.ToString());
        Console.ReadLine();
    }

    static String LengyelFormaKonvertalas(String input) // this is the rpn method
    {
       Stack stack = new Stack();
       String str = input.Replace(" ",string.Empty);
       StringBuilder formula = new StringBuilder();
       for (int i = 0; i < str.Length; i++)
       {
           char x=str[i];
           if (x == '(')
               stack.Push(x);
           else if (IsOperandus(x)) // is it operand
           {
               formula.Append(x);
           }
           else if (IsOperator(x))  // is it operation
           {
               if (stack.Count>0 && (char)stack.Peek()!='(' && Prior(x)<=Prior((char)stack.Peek()) )
               {
                   char y = (char)stack.Pop();
                   formula.Append(y);
               }
               if (stack.Count > 0 && (char)stack.Peek() != '(' && Prior(x) < Prior((char)stack.Peek()))
               {
                   char y = (char)stack.Pop();
                   formula.Append(y);
               }
               stack.Push(x);
           }
           else
           {
              char y=(char)stack.Pop();
              if (y!='(')
              {
                  formula.Append(y);
              }
           }
       }
       while (stack.Count>0)
       {
           char c = (char)stack.Pop();
           formula.Append(c);
       }
       return formula.ToString();
    }

    static bool IsOperator(char c)
    {
        return (c=='-'|| c=='+' || c=='*' || c=='/');
    }
    static bool IsOperandus(char c)
    {
        return (c>='0' && c<='9' || c=='.');
    }
    static int Prior(char c)
    {
        switch (c)
        {
            case '=':
                return 1;
            case '+':
                return 2;
            case '-':
                return 2;
            case '*':
                return 3;
            case '/':
                return 3;
            case '^':
                return 4;
            default:
                throw new ArgumentException("Rossz paraméter");                                          
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

class Sample {
    static void Main(string[] args){
        String str = "5 + ( ( 1 + 2 ) *  4 ) -3";
        String result=LengyelFormaKonvertalas(str);
        Console.WriteLine(result);
        Console.ReadLine();
    }

    static String LengyelFormaKonvertalas(String input){
       Stack<char> stack = new Stack<char>();
       String str = input.Replace(" ", string.Empty);
       StringBuilder formula = new StringBuilder();
       for (int i = 0; i < str.Length; i++){
           char x=str[i];
           if (x == '(')
               stack.Push(x);
           else if (x == ')'){
               while(stack.Count>0 && stack.Peek() != '(')
                   formula.Append(stack.Pop());
               stack.Pop();
           } else if (IsOperandus(x)){
               formula.Append(x);
           } else if (IsOperator(x)) {
               while(stack.Count>0 && stack.Peek() != '(' && Prior(x)<=Prior(stack.Peek()) )
                   formula.Append(stack.Pop());
               stack.Push(x);
           }
           else {
              char y= stack.Pop();
              if (y!='(') 
                  formula.Append(y);
           }
       }
       while (stack.Count>0) {
           formula.Append(stack.Pop());
       }
       return formula.ToString();
    }

    static bool IsOperator(char c){
        return (c=='-'|| c=='+' || c=='*' || c=='/');
    }
    static bool IsOperandus(char c){
        return (c>='0' && c<='9' || c=='.');
    }
    static int Prior(char c){
        switch (c){
            case '=':
                return 1;
            case '+':
                return 2;
            case '-':
                return 2;
            case '*':
                return 3;
            case '/':
                return 3;
            case '^':
                return 4;
            default:
                throw new ArgumentException("Rossz parameter");                                          
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

逆波兰表示法 C# 无法正常工作 的相关文章

随机推荐