Reverse polish notation C# don't work correctly

狂风中的少年 提交于 2021-02-05 08:10:41

问题


I write an rpn, with a struktogram.

Newest Problem: It is'nt work correctly now.

If input string is "5 + ((1 + 2) * 4) - 3"

My output is: 5 1 2 + 4 * 3 - +

I have to got this result: 5 1 2 + 4 * + 3 -

Edited the source

*That was the original problem, but helped me, and now the original mistakes fixed: *,

At the debug when the loop or int i = 12, the c value is 0\0 or something else and this value is added to output (name: formula) string as a '(' bracket. And I don't know why. And the last '-' operation symbol, don't added to (or not look) at the end of output string (formula) I misgave this problem cause by the '('. I tried the program with other string input value, but always put an '(' to my string, and I don't know why... I saw that It was independt about the numbers of bracket. Always only one '(' add to my string...*) Yes, in english LengyelFormula = rpn (it is hungarian)*

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");                                          
        }
    }
}

回答1:


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");                                          
        }
    }
}



回答2:


In IsOperator, you check c == '-'.
But in the string, you write −3.
− isn't the same character than -
I don't know about Polish stuff so maybe I'm missing something, but that's why no '-' operator is printed, it fails the IsOperator check and goes into the else clause, which doesn't add it to formula.




回答3:


When you get a ), you should pop all operators and add them to your formula until you reach a (, and pop that '(' as well.

When you get an operator, you should only pop the stack and add this operator to the formula if its priority is greater than or equal to that of x. Your second check is redundant because it is already covered by the first.

As a general rule: try your program with some simple inputs like 1+2+3, 1+2-3, 1*2+3 and 1+2*3 and see if you get the right result. Testing systematically like that should help you find errors faster.



来源:https://stackoverflow.com/questions/7785317/reverse-polish-notation-c-sharp-dont-work-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!