Απορία για ένα μικρό πρόγραμμα σε Java - InFix to PostFix
- Kandy_Kandy
- Kilobyte level
- Posts: 159
- Joined: Mon May 31, 2004 10:48 am
- Academic status: N>4
- Gender: ♀
- Location: Where the mind is...
Απορία για ένα μικρό πρόγραμμα σε Java - InFix to PostFix
Παιδιά αν μπορεί κάποιος να βοηθήσει. Είναι για μια φίλη μου το πρόγραμμα. Τρέχει αλλά κάπου υπάρχει λάθος και δεν εμφανίζει το αποτέλεσμα. Κάτι με 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);
}
}
}
The Grand essentials of happiness are: something to do, something to love, and something to hope for.
- Sreak
- Venus Project Founder
- Posts: 956
- Joined: Fri Apr 02, 2004 9:56 am
- Academic status: PhD
- Location: eltrun.gr
Τρεις παρατηρήσεις:
1)Η Scanner έχει σαν default delimiter το κενό επομένως η είσοδος που πρέπει να βάλεις θα πρέπει να είναι:
( 3 + 5 ) * ( 6 - 4 ) και όχι (3+5)*(6-4)
2)Για να είναι σωστό το παραπάνω πρέπει να βάλεις μέσα στο while το s = scan.next(); έτσι ώστε να διαβάζονται όλα τα στοιχεία και να αφεραίσεις το System.exit(1);
3)Έχεις κάνει κάποιο λογικό λάθος στον αλγόριθμο.
1)Η Scanner έχει σαν default delimiter το κενό επομένως η είσοδος που πρέπει να βάλεις θα πρέπει να είναι:
( 3 + 5 ) * ( 6 - 4 ) και όχι (3+5)*(6-4)
2)Για να είναι σωστό το παραπάνω πρέπει να βάλεις μέσα στο while το s = scan.next(); έτσι ώστε να διαβάζονται όλα τα στοιχεία και να αφεραίσεις το System.exit(1);
3)Έχεις κάνει κάποιο λογικό λάθος στον αλγόριθμο.
Σκέψου πως με τον παρακάτω κώδικα μου εμφάνισε ακριβώς την έκφραση που του έδινα. Δοκίμασε και θα καταλάβεις* Scan the Infix string from left to right.
* Initialise an empty stack.
* If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack.
o If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.
Repeat this step till all the characters are scanned.
* (After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.
* Return the Postfix string.
Code: Select all
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().toString()+"");
else stack.push(s);
//System.out.println("Postfix:");
s = scan.next();
//System.exit(1);
}
}
- Kandy_Kandy
- Kilobyte level
- Posts: 159
- Joined: Mon May 31, 2004 10:48 am
- Academic status: N>4
- Gender: ♀
- Location: Where the mind is...