Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by A_R_T_A_G_E · May 02, 2016 at 05:07 PM · c#unity 2djavaconvertjava to c#

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)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · May 02, 2016 at 04:29 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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



Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image A_R_T_A_G_E · May 02, 2016 at 07:45 PM 0
Share

Can you give me a normal code ? (I don't understand)

avatar image 3ddave · May 05, 2016 at 10:33 PM 1
Share

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;
     }
 }
avatar image A_R_T_A_G_E 3ddave · May 06, 2016 at 11:56 PM 0
Share

Thanks! Is there any way to do expression without spaces ?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges