Convert Java to C#
I'm trying to convert this Java code (Evaluates arithmetic expressions using Dijkstra's two-stack algorithm) to C# :
using System;
using System.Collections.Generic;
using System.IO;
public class Evaluate
{
public double Eval(string expression)
{
Stack<string> ops = new Stack<string>();
Stack<double> vals = new Stack<double>();
string s = expression;
while (!s.Equals(""))
{
if (s.Equals("(")) ;
if (s.Equals("+")) ops.Push(s);
else if (s.Equals("-")) ops.Push(s);
else if (s.Equals("*")) ops.Push(s);
else if (s.Equals("/")) ops.Push(s);
else if (s.Equals("sqrt")) ops.Push(s);
else if (s.Equals(")"))
{
string op = ops.Pop();
double v = vals.Pop();
if (op.Equals("+")) v = vals.Pop() + v;
else if (op.Equals("-")) v = vals.Pop() - v;
else if (op.Equals("*")) v = vals.Pop() * v;
else if (op.Equals("/")) v = vals.Pop() / v;
else if (op.Equals("sqrt")) v = Math.Sqrt(v);
vals.Push(v);
}
else vals.Push(double.Parse(s));
}
return vals.Pop();
}
}
But Unity stop working when I'm try to test it. What have I done wrong? (I'm note that Java not a javascript)
at first blush, line 16 is missing an else
ins$$anonymous$$d of
while (!s.Equals(""))
you might consider
while (!s.IsNullOrEmpty())
you "might" also need to perform some parsing before perfor$$anonymous$$g the operations on lines 25-29 - i haven't tried the code.
Answer by 3ddave · May 02, 2016 at 06:48 PM
You are looping on the condition that "s" is not empty on line 13. The value of "s" does not change in the body of your loop, thus you will loop forever (which will effectively hang Unity, like you are seeing). The Java example you provide is reading in an entire String from StdIn and processing that whole string as one argument in the loop (e.g. the Java code has tokenized the input so each read gives a full operator). Your code does not do this. For this to work (my recommendation) you will need to break up your expression input into tokens prior to processing. It would probably make sense to break up the expression into tokens using a space as the delimiter (see msdn for an example). Then your while loop should go over each token and process it. Pseudocode:
func Eval expression:str
tokens = splitStringOnSpaces(expression)
for each token in tokens
switch(token)
case "(" .....do processing.....
case "+" .....etc
.....etc
Next Token
output result of processing
end func
Here is some working code. Input is hard-coded into the $$anonymous$$ain method. Spaces are required between each character.
public class Evaluate {
public static void $$anonymous$$ain() {
string input = "( 1 ( ( 2 3 + ) ( 4 5 * ) * ) + )";
Eval(input);
}
public static double Eval(string expression) {
Stack<string> ops = new Stack<string>();
Stack<double> vals = new Stack<double>();
string[] tokens = expression.Split(' ');
foreach (var token in tokens) {
switch (token) {
case "(":
break;
case "+":
case "-":
case "*":
case "/":
case "sqrt":
ops.Push(token);
break;
case ")":
string op = ops.Pop();
double v = vals.Pop();
if (op.Equals("+")) v = vals.Pop() + v;
else if (op.Equals("-")) v = vals.Pop() - v;
else if (op.Equals("*")) v = vals.Pop() * v;
else if (op.Equals("/")) v = vals.Pop() / v;
else if (op.Equals("sqrt")) v = $$anonymous$$ath.Sqrt(v);
vals.Push(v);
break;
default:
vals.Push(Double.Parse(token));
break;
}
}
double result = vals.Pop();
System.Console.WriteLine("Result is " + result.ToString());
return result;
}
}
Thanks! Is there any way to do expression without spaces ?
Your answer
Follow this Question
Related Questions
JavaScript to C# 1 Answer
How to change from C# to JAVA? 4 Answers
Convert Keyboard controlls to UI Buttons 1 Answer
Unity job is failed error. 0 Answers
Convert Java to C# 2 Answers