Last active
October 21, 2015 18:39
-
-
Save ad-m/c7c86638a1651ff06581 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package First; | |
| public class ArrayStack implements IStos{ | |
| private int length = 0; | |
| private int[] tab; | |
| public ArrayStack(int size) { | |
| this.tab = new int[size]; | |
| } | |
| private void extend(){ | |
| int[] tmp = new int[tab.length*2]; | |
| System.arraycopy(tab, 0, tmp, 0, tab.length); | |
| tab = tmp; | |
| } | |
| @Override | |
| public void push(int i) { | |
| if(length == tab.length){ | |
| this.extend(); | |
| } | |
| tab[length++]=i; | |
| } | |
| @Override | |
| public int pop() { | |
| return tab[--length]; | |
| } | |
| @Override | |
| public int peek() { | |
| return tab[length]; | |
| } | |
| @Override | |
| public boolean isEmpty() { | |
| return (length==0); | |
| } | |
| @Override | |
| public void print() { | |
| for(int i=0; i<length; i++){ | |
| System.out.print(i+"\n"); | |
| } | |
| } | |
| @Override | |
| public void clear() { | |
| length=0; | |
| } | |
| public static void main(String[] args) { | |
| IStos a = new ArrayStack(25); | |
| assert(a.isEmpty() == true); | |
| a.push(5); | |
| a.push(10); | |
| assert(a.isEmpty() == false); | |
| assert(a.pop() == 10); | |
| assert(a.pop() == 5); | |
| assert(a.isEmpty() == true); | |
| a.push(5); | |
| assert(a.isEmpty() == false); | |
| a.clear(); | |
| assert(a.isEmpty() == true); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package First; | |
| public class Element{ | |
| public int dane; | |
| public Element next; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package First; | |
| public class Equation { | |
| private String text; | |
| public Equation(String text) { | |
| super(); | |
| this.text = text; | |
| } | |
| public boolean isValid(){ | |
| Stos stack = new Stos(); | |
| for(int i=0; i<text.length(); i++){ | |
| if(text.charAt(i) == '('){ | |
| stack.push(1); | |
| }else if(text.charAt(i) == ')'){ | |
| if(stack.isEmpty()){ | |
| return false; | |
| } | |
| stack.pop(); | |
| } | |
| } | |
| return stack.isEmpty(); | |
| } | |
| public static void main(String[] args) { | |
| assert(new Equation("(()(()))").isValid() == true); | |
| assert(new Equation(")(").isValid() == false); | |
| assert(new Equation("(()").isValid() == false); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package First; | |
| import java.lang.reflect.Array; | |
| public class GenericArrayStack<T>{ | |
| private int length = 0; | |
| private T[] tab; | |
| @SuppressWarnings("unchecked") | |
| public GenericArrayStack(Class<T> c, int size) { | |
| final T[] tab = (T[]) Array.newInstance(c, size); | |
| this.tab = tab; | |
| } | |
| public void push(T i) { | |
| tab[length++]=i; | |
| } | |
| public T pop() { | |
| return tab[--length]; | |
| } | |
| public T peek() { | |
| return tab[length]; | |
| } | |
| public boolean isEmpty() { | |
| return (length==0); | |
| } | |
| public void print() { | |
| for(int i=0; i<length; i++){ | |
| System.out.print(i+"\n"); | |
| } | |
| } | |
| public void clear() { | |
| length=0; | |
| } | |
| public static void main(String[] args) { | |
| GenericArrayStack<Integer> a = new GenericArrayStack<Integer>(Integer.class, 25); | |
| assert(a.isEmpty() == true); | |
| a.push(new Integer(5)); | |
| a.push(new Integer(10)); | |
| assert(a.isEmpty() == false); | |
| assert(a.pop().equals(new Integer(10))); | |
| assert(a.pop().equals(new Integer(5 ))); | |
| assert(a.isEmpty() == true); | |
| a.push(5); | |
| assert(a.isEmpty() == false); | |
| a.clear(); | |
| assert(a.isEmpty() == true); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package First; | |
| public interface IStos { | |
| public void push(int i); | |
| public int pop(); | |
| public int peek(); | |
| public boolean isEmpty(); | |
| public void print(); | |
| public void clear(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package First; | |
| /* | |
| Dla wszystkich symboli z wyrażenia ONP wykonuj: | |
| jeśli i-ty symbol jest liczbą, to odłóż go na stos, | |
| jeśli i-ty symbol jest operatorem to: | |
| zdejmij ze stosu jeden element (ozn. a), | |
| zdejmij ze stosu kolejny element (ozn. b), | |
| odłóż na stos wartość b operator a. | |
| jeśli i-ty symbol jest funkcją to: | |
| zdejmij ze stosu oczekiwaną liczbę parametrów funkcji(ozn. a1...an) | |
| odłóż na stos wynik funkcji dla parametrów a1...an | |
| Zdejmij ze stosu wynik. | |
| */ | |
| public class ONPEquation { | |
| private String text; | |
| public ONPEquation(String text) { | |
| this.text = text; | |
| } | |
| private static boolean isNumber(int i){ | |
| return (i >= 48 && i <= 57); | |
| } | |
| public int getValue(){ | |
| Stos stack = new Stos(); | |
| for(int i=0; i<this.text.length(); i++){ | |
| int ch = this.text.charAt(i); | |
| if(isNumber(ch)){ | |
| int liczba = ch-48; | |
| if(i>0 && isNumber(this.text.charAt(i-1))){ | |
| liczba = stack.pop()*10+liczba; | |
| }; | |
| stack.push(liczba); | |
| }else if(ch == '+'){ | |
| stack.push(stack.pop() + stack.pop()); | |
| }else if(ch == '/'){ | |
| int arg = stack.pop(); | |
| stack.push(stack.pop() / arg); | |
| }else if(ch == '*'){ | |
| int arg = stack.pop(); | |
| stack.push(stack.pop() * arg); | |
| } else if(ch == '-'){ | |
| int arg = stack.pop(); | |
| stack.push(stack.pop() - arg); | |
| }; | |
| } | |
| return stack.pop(); | |
| } | |
| public static void main(String[] args) { | |
| assert(ONPEquation.isNumber('0') == true); | |
| assert(ONPEquation.isNumber('1') == true); | |
| assert(ONPEquation.isNumber('5') == true); | |
| assert(ONPEquation.isNumber('a') == false); | |
| assert(ONPEquation.isNumber('z') == false); | |
| assert(new ONPEquation("2").getValue() == 2); | |
| assert(new ONPEquation("2 2 +").getValue() == 4); | |
| assert(new ONPEquation("3 2 -").getValue() == 1); | |
| assert(new ONPEquation("222 2 +").getValue() == 224); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package First; | |
| public class Stos implements IStos { | |
| private Element top; | |
| @Override | |
| public void push(int i){ | |
| Element tmp = new Element(); | |
| tmp.dane = i; | |
| tmp.next = this.top; | |
| this.top = tmp; | |
| } | |
| @Override | |
| public int pop() { | |
| if(isEmpty()) throw new IllegalStateException("Stack is empty!"); | |
| int tmp = this.top.dane; | |
| this.top = this.top.next; | |
| return tmp; | |
| } | |
| @Override | |
| public int peek() { | |
| if(isEmpty()) throw new IllegalStateException("Stack is empty!"); | |
| return this.top.dane; | |
| } | |
| @Override | |
| public boolean isEmpty() { | |
| return this.top == null; | |
| } | |
| @Override | |
| public void print() { | |
| Element tmp = this.top; | |
| while(tmp !=null){ | |
| System.out.print(tmp.dane+"\n"); | |
| tmp = tmp.next; | |
| } | |
| } | |
| @Override | |
| public void clear() { | |
| this.top = null; | |
| } | |
| public static void main(String[] args) { | |
| Stos a = new Stos(); | |
| assert(a.isEmpty() == true); | |
| a.push(5); | |
| a.push(10); | |
| assert(a.isEmpty() == false); | |
| assert(a.pop() == 10); | |
| assert(a.pop() == 5); | |
| assert(a.isEmpty() == true); | |
| a.push(5); | |
| assert(a.isEmpty() == false); | |
| a.clear(); | |
| assert(a.isEmpty() == true); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package First; | |
| public class Word { | |
| private String text; | |
| public Word(String text) { | |
| this.text = text; | |
| } | |
| public boolean isPalindrome(){ | |
| Stos stack = new Stos(); | |
| for(int i=0; i<this.text.length();i++){ | |
| stack.push(this.text.charAt(i)); | |
| } | |
| for(int i=0; i<this.text.length(); i++){ | |
| if(this.text.charAt(i) != stack.pop()){ | |
| return false; | |
| }; | |
| } | |
| return true; | |
| } | |
| public static void main(String[] args) { | |
| assert(new Word("aa").isPalindrome() == true); | |
| assert(new Word("aba").isPalindrome() == true); | |
| assert(new Word("abba").isPalindrome() == true); | |
| assert(new Word("kobyłamamałybok").isPalindrome() == true); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment