Απορία για ένα μικρό πρόγραμμα σε Java - InFix to PostFix
Posted: Sat Nov 10, 2007 10:38 pm
Παιδιά αν μπορεί κάποιος να βοηθήσει. Είναι για μια φίλη μου το πρόγραμμα. Τρέχει αλλά κάπου υπάρχει λάθος και δεν εμφανίζει το αποτέλεσμα. Κάτι με stack.pop θα είναι. Είναι λίγο επείγον μπορεί κάποιος να βοηθήσει; Ο κώδικας είναι:
import java.util.*;
import jss2.*;
import java.util.Scanner;
import java.io.*;
import java.io.IOException;
public class a3
{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
Stack<String> stack = new Stack<String>();
System.out.println("- The program translates the InFix to PostFix");
System.out.println("- The format that you have to enter is: ");
System.out.printf("(X+Y)*(Z-I) or similar expressions with numbers");
System.out.println();
String s = scan.next();
while (!s.equals(0)) {
String postfix = "";
if(s.equals("+")) stack.push(s);
else if(s.equals("-")) stack.push(s);
else if(s.equals("*")) stack.push(s);
else if(s.equals("/")) stack.push(s);
else if(s.equals("(")) System.out.print("");
else if(s.equals(")")) System.out.print(stack.pop()+"");
System.out.println("Postfix:");
System.exit(1);
}
}
}
import java.util.*;
import jss2.*;
import java.util.Scanner;
import java.io.*;
import java.io.IOException;
public class a3
{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
Stack<String> stack = new Stack<String>();
System.out.println("- The program translates the InFix to PostFix");
System.out.println("- The format that you have to enter is: ");
System.out.printf("(X+Y)*(Z-I) or similar expressions with numbers");
System.out.println();
String s = scan.next();
while (!s.equals(0)) {
String postfix = "";
if(s.equals("+")) stack.push(s);
else if(s.equals("-")) stack.push(s);
else if(s.equals("*")) stack.push(s);
else if(s.equals("/")) stack.push(s);
else if(s.equals("(")) System.out.print("");
else if(s.equals(")")) System.out.print(stack.pop()+"");
System.out.println("Postfix:");
System.exit(1);
}
}
}