Page 1 of 1

Απορία για ένα μικρό πρόγραμμα σε Java - InFix to PostFix

Posted: Sat Nov 10, 2007 10:38 pm
by Kandy_Kandy
Παιδιά αν μπορεί κάποιος να βοηθήσει. Είναι για μια φίλη μου το πρόγραμμα. Τρέχει αλλά κάπου υπάρχει λάθος και δεν εμφανίζει το αποτέλεσμα. Κάτι με 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);
}
}
}

Posted: Sun Nov 11, 2007 12:29 am
by Sreak
Τρεις παρατηρήσεις:
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);
        }
    }

Posted: Sun Nov 11, 2007 3:16 pm
by Kandy_Kandy
έχω κάνει τη σύγκριση για την προτεραιότητα των τελεστών αλλά κάποιο πρόβλημα υπάρχει με το topStack. Δεν ξέρω τι...Έχω ψιλομπερδευτεί.

Posted: Sun Nov 11, 2007 11:36 pm
by Sreak
Αν θέλεις να σε βοηθήσουμε πρέπει να προσπαθήσεις να είσαι πιο συγκεκριμένη ;)

Προσπάθησες να υλοποιήσεις τον παραπάνω αλγόριθμο που σου έστειλα? Έκανες googling? Πόσταρε το πρόγραμμα σου (the new version) πες τι σου βγάζει κτλ κτλ κτλ

Posted: Sun Nov 11, 2007 11:39 pm
by Sreak

Posted: Mon Nov 12, 2007 12:25 am
by elsupreme
Βασικά, στο eclass στο μάθημα δομές δεδομένων υπάρχει διάλεξη με ακριβώς ό,τι χρειάζεσαι σε java...