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)
// 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));
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)
DH forumlarında vakit geçirmekten keyif alıyor gibisin ancak giriş yapmadığını görüyoruz.
Üye Ol Şimdi DeğilÜye olduğunda özel mesaj gönderebilir, beğendiğin konuları favorilerine ekleyip takibe alabilir ve daha önce gezdiğin konulara hızlıca erişebilirsin.
< Bu mesaj bu kişi tarafından değiştirildi Khwarizm -- 28 Ekim 2014; 0:43:02 >