Mail calculator

Creating a console application in C sharp to solve expressions in postfix notation using a stack, for example:

Expression: 43 + 2 * Answer: 14

What i have done so far:

using System; using System.Collections; using System.Linq; using System.Text; namespace ConsoleApplication7 { class Program { static void Main(string[] args) { string input = "23+"; int counter = 0; Stack values = new Stack(); while (counter < input.Length) { int temp1, temp2, answer; char x = char.Parse(input.Substring(counter, 1)); if ( ); else if (x == '+') { temp1 = (int)values.Pop(); temp2 = (int)values.Pop(); values.Push(answer = temp1 + temp2); } else if (x == '-') { temp1 = (int)values.Pop(); temp2 = (int)values.Pop(); values.Push(answer = temp1 - temp2); } else if (x == '*') { temp1 = (int)values.Pop(); temp2 = (int)values.Pop(); values.Push(answer = temp1 / temp2); } else if (x == '/') { temp1 = (int)values.Pop(); temp2 = (int)values.Pop(); values.Push(answer = temp1 * temp2); } counter++; } Console.WriteLine(values.Pop()); } } 

For the if statement, what can I use as a condition to check if x is an operand?

+4
source share
4 answers

Is your input example 2, 3, + (which is 5) or 23, + (which is invalid input)? I guess the first one. How could you write two-digit numbers? Your current approach does not seem to support this. I think you should not parse this char -by-char, but first separate it into separate components, possibly using a regular expression that recognizes numbers and punctuation. As a short example: Regex.Matches("10 3+", @"(\d+|[\+\-\*/ ])") split into 10 , , 3 and + , which can be easily analyzed and understood with the code that you already have (spaces should be ignored, re is just the punctuation that I chose to separate the numbers so you can have multi-valued numbers) and int.TryParse (or double , which requires a more complex regex pattern, see Matching floating-point numbers for this pattern) to see if the input is a number.

You must use the Stack<int> to avoid casting and make it safe to compile.

+1
source

Of course, this is wrong:

 ((int)Char.GetNumericValue(x) <= 0 && (int)Char.GetNumericValue(x) >= 0) 

I think it should be

 ((int)Char.GetNumericValue(x) <= 9 && (int)Char.GetNumericValue(x) >= 0) 
0
source

I really think this looks more like a code review, but let it be so - first: please share some problems - you baked everything into a big dirty monster - think about the parts of the problem and put them in separate methods to start with.

Then: if you cannot solve the problem with the hole, first make it smaller: let the user enter some kind of separator for the parts or assume that he is doing now - the space will be fine. You can think about how to handle statements without pre / postfixed spaces later. So try parsing "2 3+" instead of "23+" or "2 3+" ... If you do this, you can just use String.Split to make your life a lot easier!

As for how you can recognize the operand: very simple - try Double.TryParse , it will tell you if you gave it a valid number and you do not need to spend your time parsing numbers yourself

Instead of using it for a while, you should use for or even better foreach - hell, you can even do it with LINQ and [Enumerable.Aggregate][1] and get FUNctional: D

And finally, do not use this if / then / else mess if the switch is doing the job ...

0
source

We can say that essentially there are no operands. Even numbers can be thought of as operators that multiply the top of the stack by 10 and add the value of the number; accumulating a value in a few digits as needed. Then you just need an operator to plant it by pushing zero on the stack (perhaps a space for this).

http://blogs.msdn.com/b/ashleyf/archive/2009/10/23/tinyrpn-calculator.aspx

0
source

Source: https://habr.com/ru/post/1447844/


All Articles