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

Συζητήσεις για γλώσσες προγραμματισμού και θέματα σχετικά με προγραμματισμό.
Post Reply
User avatar
Kandy_Kandy
Kilobyte level
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

Post by Kandy_Kandy » 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);
}
}
}
The Grand essentials of happiness are: something to do, something to love, and something to hope for.
User avatar
Sreak
Venus Project Founder
Venus Project Founder
Posts: 956
Joined: Fri Apr 02, 2004 9:56 am
Academic status: PhD
Location: eltrun.gr

Post by Sreak » Sun Nov 11, 2007 12:29 am

Τρεις παρατηρήσεις:
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);
        }
    }
User avatar
Kandy_Kandy
Kilobyte level
Kilobyte level
Posts: 159
Joined: Mon May 31, 2004 10:48 am
Academic status: N>4
Gender:
Location: Where the mind is...

Post by Kandy_Kandy » Sun Nov 11, 2007 3:16 pm

έχω κάνει τη σύγκριση για την προτεραιότητα των τελεστών αλλά κάποιο πρόβλημα υπάρχει με το topStack. Δεν ξέρω τι...Έχω ψιλομπερδευτεί.
The Grand essentials of happiness are: something to do, something to love, and something to hope for.
User avatar
Sreak
Venus Project Founder
Venus Project Founder
Posts: 956
Joined: Fri Apr 02, 2004 9:56 am
Academic status: PhD
Location: eltrun.gr

Post by Sreak » Sun Nov 11, 2007 11:36 pm

Αν θέλεις να σε βοηθήσουμε πρέπει να προσπαθήσεις να είσαι πιο συγκεκριμένη ;)

Προσπάθησες να υλοποιήσεις τον παραπάνω αλγόριθμο που σου έστειλα? Έκανες googling? Πόσταρε το πρόγραμμα σου (the new version) πες τι σου βγάζει κτλ κτλ κτλ
User avatar
Sreak
Venus Project Founder
Venus Project Founder
Posts: 956
Joined: Fri Apr 02, 2004 9:56 am
Academic status: PhD
Location: eltrun.gr

Post by Sreak » Sun Nov 11, 2007 11:39 pm

User avatar
elsupreme
Gbyte level
Gbyte level
Posts: 1573
Joined: Mon Nov 21, 2005 10:16 pm
Academic status: N>4
Gender:

Post by elsupreme » Mon Nov 12, 2007 12:25 am

Βασικά, στο eclass στο μάθημα δομές δεδομένων υπάρχει διάλεξη με ακριβώς ό,τι χρειάζεσαι σε java...
"Must float like lotus on river... and kill old lady!"
Post Reply

Return to “Προγραμματισμός”