Şimdi Ara

Postfix Notation Yardim

Daha Fazla
Bu Konudaki Kullanıcılar: Daha Az
2 Misafir - 2 Masaüstü
5 sn
2
Cevap
0
Favori
614
Tıklama
Daha Fazla
İstatistik
  • Konu İstatistikleri Yükleniyor
0 oy
Öne Çıkar
Sayfa: 1
Giriş
Mesaj
  • Selam arkadaslar. 2 gundur calistigim odev vardi ama bi turlu yapamadim. yardimci olabilecek varsa sevinirim.
    Odevde txt dosyasindan okunan degerlere gore postfix notation hesaplamasi yapmam gerek. Mesela:

    1 2 + = 3
    4 2 - = 2
    1 2 3 * + 5 - = 2

    Odev hata vermiyor fakat mantik hatasi var. Herturlu sayilari girmeyi denedim hepsi exception hatasi veriyor.

    java.util.NoSuchElementException
    at ArrayStack.pop(PostFixEvaluation.java:72)
    at PostFixEvaluation.evaluatePostfix(PostFixEvaluatio n.java:107)
    at PostFixEvaluation.main(PostFixEvaluation.java:140)

    import java.io.BufferedReader; 
    import java.io.InputStreamReader;
    import java.util.NoSuchElementException;

    interface Stack<E> {

    // The elements of the Stack are any kind of objects

    // Access methods:

    public boolean isEmpty ();
    // Returns true only if the stack is empty.

    public E peek ();
    // Returns the element on the top od the stack.

    // Transformation methods:

    public void clear ();
    // Clears the stack.

    public void push (E x);
    // Adds x on the top of the stack.

    public E pop ();
    // Removes and returns the element on the top.
    }

    class ArrayStack<E> implements Stack<E> {
    private E[] elems;
    private int depth;

    @SuppressWarnings("unchecked")
    public ArrayStack (int maxDepth) {
    // Creating new empty stack
    elems = (E[]) new Object[maxDepth];
    depth = 0;
    }


    public boolean isEmpty () {
    // Returns true only if the stack is empty.

    return (depth == 0);
    }


    public E peek () {
    // Returns the element on the top od the stack.
    if (depth == 0)
    throw new NoSuchElementException();
    return elems[depth-1];
    }


    public void clear () {
    // Clears the stack.
    for (int i = 0; i < depth; i++) elems[i] = null;
    depth = 0;
    }


    public void push (E x) {
    // Adds x on the top of the stack.
    elems[depth++] = x;
    }


    public E pop () {
    // Removes and returns the element on the top.
    if (depth == 0)
    throw new NoSuchElementException();
    E topmost = elems[--depth];
    elems[depth] = null;
    return topmost;
    }

    }


    public class PostFixEvaluation {


    static int evaluatePostfix(char [] izraz, int n)
    {
    int maxDepth=izraz.length;
    ArrayStack<Character> e = new ArrayStack(n);
    char ch,res;
    int op1,op2,result=0;
    int i=0;
    while(i<n)
    {
    if(Character.isDigit(izraz[i]))
    {
    ch=izraz[i];
    e.push(ch);
    }
    else
    {
    ch=izraz[i];
    op1 =(int)e.pop();
    op2 =(int)e.pop();
    if(ch=='+')
    {
    result=op1+op2;
    }
    if(ch=='-')
    {
    result=op1-op2;
    }
    if(ch=='/')
    {
    result=op1/op2;
    }
    if(ch=='*')
    {
    result=op1*op2;
    }
    res=(char)result;
    e.push(res);

    }
    i++;

    }
    return result;
    }


    public static void main(String[] args) throws Exception{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String expression = br.readLine();
    char exp[] = expression.toCharArray();

    int rez = evaluatePostfix(exp, exp.length);
    System.out.println(rez);

    br.close();

    }

    }



    < Bu mesaj bu kişi tarafından değiştirildi Khwarizm -- 28 Ekim 2014; 0:43:02 >







  • Çözüldü konu kapanabilir.
  • 
Sayfa: 1
- x
Bildirim
mesajınız kopyalandı (ctrl+v) yapıştırmak istediğiniz yere yapıştırabilirsiniz.