I am currently working with a reverse polish notation calculator. It is fully functional but I am running into issues with text input that will then be used to calculate the values. The program breaks if introduce a formula like ( 8 5 + ) 6 * = . Is there a way to simply ignore any parentheses that show up in the input value? Also my program does split accordingly to space between each number but if I for get to add a space between operands or parentheses it also breaks: (8 5 +)6 * =. If it is an invalid formulas 12 + = ( missing a number) i would like to ignore them and just display in output textBox an error message.
Side Note: every formula is triggered by an ending =.
Code
namespace rpncalc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string inputValue = "";
private void RPNCalc(string rpnValue)
{
Stack<int> stackCreated = new Stack<int>();
stackCreated.Clear();
string[] inputArray = rpnValue.Split();
int end = inputArray.Length - 1;
int numInput;
int i = 0;
do
{
if ("=+-*/%^".IndexOf(inputArray[i]) == -1)
{
try
{
numInput = Convert.ToInt32(inputArray[i]);
stackCreated.Push(numInput);
}
catch
{
MessageBox.Show("Please check the input");
}
}
else if (inputArray[i]== "+")
{
try
{
int store1 = stackCreated.Pop();
int store2 = stackCreated.Pop();
stackCreated.Push(store2 + store1);
}
catch
{
}
}
else if (inputArray[i]== "-")
{
try
{
int store1 = stackCreated.Pop();
int store2 = stackCreated.Pop();
stackCreated.Push(store2 - store1);
}
catch
{
}
}
else if (inputArray[i]== "%")
{
try
{
int store1 = stackCreated.Pop();
int store2 = stackCreated.Pop();
stackCreated.Push(store2 % store1);
}
catch
{
}
}
else if (inputArray[i]== "*")
{
try
{
int store1 = stackCreated.Pop();
int store2 = stackCreated.Pop();
stackCreated.Push(store2 * store1);
}
catch
{
}
}
else if (inputArray[i]== "/")
{
try
{
int store1 = stackCreated.Pop();
int store2 = stackCreated.Pop();
stackCreated.Push(store2 / store1);
}
catch
{
}
}
else if (inputArray[i] == "^")
{
try
{
int store1 = stackCreated.Pop();
int store2 = stackCreated.Pop();
stackCreated.Push((int)Math.Pow(store1, store2));
}
catch
{
}
}
}
while(i++ < end && inputArray[i]!= "=" && stackCreated.Count != 0);
string result = inputValue + " " + stackCreated.Pop().ToString() + Environment.NewLine;
TxtOutputBox.AppendText(result);
TxtInputBox.Clear();
}
private void TxtOutputBox_TextChanged(object sender, EventArgs e)
{
}
private void Btn_Calc_Click(object sender, EventArgs e)
{
inputValue = TxtInputBox.Text + " ";
RPNCalc(inputValue);
}
}
}

First of all, doing something like
as Sam’s answer to this question recommends, would require a major rewrite of the entire algorithm. The problem is with the Replace(” “, “”) part which eliminates spaces. However, the algorithm relies on Split to tokenise the input. If you eliminate spaces before that Split, instead of getting an array of numbers and operators, you will get a single string which cannot be processed by the existing code. Worse still both “12 3 * =” and “1 23 * =” will be converted to “123*=” !!!
To avoid making big changes change the code as follows:
Insert the following code before the Split:
This makes sure sure that parentheses are ignored as requested but in a way that makes (8 5 +)6 * = work.
And then make the following addition:
Also the code at the end of the loop needs to change:
Hope that helps 🙂
—————————The answer proper ends here ———————————-
Additional recommendation:
The question does not cover the issue of error reporting, but there was also something else I noticed: operations on the stack are surrounded by try catch statements and nothing happens in the catch part. This way an ill formatted input is not appropriately flagged. I would recommend deleting those try catch statements and putting a single one around the body of the function.
Some fun: My aim was to just answer the very specific question asked, but since seeing LB’s beautiful piece of code was posted here, I updated the answer to include an alternative definition of RPNCalc that is just as consise but closer to your original approach which does not rely on regular expressions (except that expressions to be calculated do not end with an
=).