Arama butonu
Bu konudaki kullanıcılar: 1 misafir
1
Cevap
750
Tıklama
0
Öne Çıkarma
Postfix Notation Yardim
K
12 yıl (103 mesaj)
Çavuş
Konu Sahibi

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 >

K
12 yıl (103 mesaj)
Çavuş
Konu Sahibi

Çözüldü konu kapanabilir.



DH Mobil uygulaması ile devam edin. Mobil tarayıcınız ile mümkün olanların yanı sıra, birçok yeni ve faydalı özelliğe erişin. Gizle ve güncelleme çıkana kadar tekrar gösterme.