What does this code output?
I can't seems to get the output of this code to display in the console window, evening after implementing a System.out.println() for each variable. Could someone give me some suggestion or possible output for the code, thanks.
Code java:
public class Evaluate
{
public static void main(String[] args)
{
Stack<String> ops = new Stack<String>();
Stack<Double> vals = new Stack<Double>();
while (!StdIn.isEmpty())
{ // Read token, push if operator.
String s = StdIn.readString();
if (s.equals("(")) ;
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("/")) ops.push(s);
else if (s.equals("sqrt")) ops.push(s);
else if (s.equals(")"))
{ // Pop, evaluate, and push result if token is ")".
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);
} // Token not operator or paren: push double value.
else vals.push(Double.parseDouble(s));
}
StdOut.println(vals.pop());
}
}
Re: What does this code output?
These forums don't work like that. Perhaps you should be getting to the root of the problem - like why you see no output (does it compile?) - rather than asking someone to do your work for you
Re: What does this code output?
Step 1: Format your code to be easily readable (i.e. add tabs).
Step 2: What issue are you running into specifically? Compiler errors? Runtime errors? Which ones?
fyi, StdOut and StdIn are not defined by default in Java, use System.out and System.in instead.
Re: What does this code output?
Quote:
Originally Posted by
copeg
These forums don't work like that. Perhaps you should be getting to the root of the problem - like why you see no output (does it compile?) - rather than asking someone to do your work for you
That what I really meant to ask? I tried compiling this program several times but did't see any output. So, copeg or anyone could you give me any suggestion on why I'm not seeing any output when I run this code. Hey, I any revise it a little to showing that I am really putting effort into this. Here is the revise version:
Code java:
import java.io.BufferedInputStream;
import java.lang.*;
import java.util.*;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
public class StdIn {
private static Scanner scanner = new Scanner(new BufferedInputStream(System.in));
public static boolean isEmpty() {
return !scanner.hasNext();
}
public static String readString() {
return scanner.next();
}
public static void main (String args[]){
//InputStreamReader input = new InputStreamReader(System.in);
//BufferedReader reader = new BufferedReader(input);
Stack<String> ops = new Stack<String>();
Stack<Double> vals = new Stack<Double>();
//String s = reader.readLine();
while (!StdIn.isEmpty())
{ // Read token, push if operator.
String s = StdIn.readString();
if (s.equals("(")) ;
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("/")) ops.push(s);
else if (s.equals("sqrt")) ops.push(s);
else if (s.equals(")"))
{ // Pop, evaluate, and push result if token is ")".
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);
System.out.println(vals.push(v));
} // Token not operator or paren: push double value.
else vals.push(Double.parseDouble(s));
System.out.println(Double.parseDouble(s));
}
System.out.println(vals.pop());
}
}
Re: What does this code output?
The current issue I am having with this code is: figuring out why don't I see any output. I take some of your suggestion: Here is a revise version of my code:
Code java:
//import java.io.*;
//import java.math.*;
//import java.io.OutputStreamWriter;
import java.io.BufferedInputStream;
//import java.lang.*;
import java.util.*;
import java.util.Scanner;
//import java.io.PrintWriter;
//import java.io.UnsupportedEncodingException;
//import java.util.Locale;
public class StdIn {
private static Scanner scanner = new Scanner(new BufferedInputStream(System.in));
public static boolean isEmpty()
{ return !scanner.hasNext();}
public static String readString()
{ return scanner.next();}
public static void main (String args[]){
Stack<String> ops = new Stack<String>();
Stack<Double> vals = new Stack<Double>();
while (!StdIn.isEmpty())
{ // Read token, push if operator.
String s = StdIn.readString();
if (s.equals("(")) ;
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("/")) ops.push(s);
else if (s.equals("sqrt")) ops.push(s);
else if (s.equals(")"))
{ // Pop, evaluate, and push result if token is ")".
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);
System.out.println(vals.push(v));
} // Token not operator or paren: push double value.
else vals.push(Double.parseDouble(s));
System.out.println(Double.parseDouble(s));
}
System.out.println(vals.pop());
}
}