Napisałem klasę konwertującą i obliczającą na podstawie wejściowego string'a wyrażenie matematyczne podane w wejściowym stringu:
public class rpn
{
private Dictionary<char, int> priorities;
public object Arrays { get; private set; }
public rpn()
{
priorities = new Dictionary<char, int>();
priorities.Add('(', 0);
priorities.Add('+', 1);
priorities.Add('-', 1);
priorities.Add('*', 2);
priorities.Add('/', 2);
priorities.Add('^', 3);
}
public double calclate(string text)
{
List<string> input_list = new List<string>(convert_to_rpn(division(text)));
double value = 0;
Stack<double> stack = new Stack<double>();
double a, b; //zmienne pomocnicze do zapamiętywania wartosci ze stosu
string asl = String.Empty;
for (int i = 0; i < input_list.Count; i++)
{
// asl = String.Empty;
// asl = input_list[i];
//jest liczbą
if (double.TryParse(input_list[i], out value))
{
stack.Push(value);
}
else
{
a = stack.Pop(); //zapamiętywanie i ściąganie ze stosu liczb
b = stack.Pop();
switch (Convert.ToChar(input_list[i]))
{
case '+':
stack.Push(b + a);
break;
case '-':
stack.Push(b - a);
break;
case '*':
stack.Push(b * a);
break;
case '/':
stack.Push(b / a);
break;
case '^':
stack.Push(Math.Pow(b, a));
break;
}
}
}
return stack.Peek();
}
private List<string> convert_to_rpn(String[] input)
{
var to_return = new List<string>();
var stack = new Stack<string>();
double value;
for (int i = 0; i < input.Length; i++)
{
//jest liczbą
if (double.TryParse(input[i], out value))
{
to_return.Add(input[i]);
}
else
{
if (input[i] == "(")
{
stack.Push(input[i]);
}//jeśli aktualnego element jest prawym nawiasem wszstko do lewego nawiasu wyślij na wyjście i lewy nawias zrzuć
if (input[i] == ")")
{
while (stack.Peek() != "(")
{
to_return.Add(stack.Pop());
}
stack.Pop();
}
//aktualnego element jest operatorem
if (input[i] == "+" || input[i] == "-" || input[i] == "*" || input[i] == "*" || input[i] == "/" || input[i] == "^")
{
if (stack.Count == 0)
{
stack.Push(input[i]);
}//stos nie jest pusty
else
{
if (priorities[Convert.ToChar(input[i])] > priorities[Convert.ToChar(stack.Peek())])
{
stack.Push(input[i]);
}
//jeśli priorytet aktualnego elementu jest mniejszy lub równy priorytetowi elementu ze stosu
else
{
while (stack.Count != 0 && priorities[Convert.ToChar(input[i])] <= priorities[Convert.ToChar(stack.Peek())])
{
to_return.Add(stack.Pop());
}
stack.Push(input[i]);
}
}
}
}
}
while (stack.Count != 0)
{
to_return.Add(stack.Pop());
}
return to_return;
}
//dzielenie wejściowego tekstu na liczby/operatory
private String[] division(string input_text)
{
input_text = input_text.Replace(" ", "");
input_text = input_text.Replace("+", " + ").Replace("-", " - ").Replace("/", " / ");
input_text = input_text.Replace("*", " * ").Replace("^", " ^ ");
input_text = input_text.Replace("(", " ( ").Replace(")", " ) ").Trim();
String[] to_return = input_text.Split(' ');
return to_return;
}
}
Proszę o ocenę i wytknięcie możliwych błędów lub nieprawidłowego podejścia do problemu (program działa poprawnie, chodzi mi o zobaczenie czy mój kod ma cokolwiek związanego z pojęciem dobrego kodu ;) )