Advertisement
Guest User

Untitled

a guest
Mar 29th, 2016
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. private double oblicz() {
  2.  
  3.  
  4. Stoss stos = new Stoss();
  5.  
  6. StringTokenizer st = new StringTokenizer(postfix, " ");
  7.  
  8.  
  9. while(st.hasMoreTokens()) {
  10.  
  11.  
  12. String s = st.nextToken();
  13.  
  14.  
  15. if (!s.equals("+") && !s.equals("*") && !s.equals("-") && !s.equals("/") && !s.equals("%") ) {
  16.  
  17. double wartosc = Double.parseDouble(s);
  18.  
  19. stos.push(wartosc);
  20. }
  21. else {
  22.  
  23. double wartosc1 = stos.pop();
  24. double wartosc2 = stos.pop();
  25.  
  26. switch(s.charAt(0)) {
  27. case '*': {stos.push(wartosc2 * wartosc1); break;}
  28. case '+': {stos.push(wartosc2 + wartosc1); break;}
  29. case '-': {stos.push(wartosc2 - wartosc1); break;}
  30. case '/': {if(wartosc1==0) {
  31. System.out.println("Nie dzielimy przez 0");break; // tu bym chcial zeby program przerwal sie wykonywac i zostal wyswietlony tylko ten komunikat
  32. }else{
  33. stos.push(wartosc2 / wartosc1); break;}
  34. }
  35. }
  36. }
  37. }
  38. // zwracamy końcowy wynik
  39. return stos.pop();
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement