Created
April 14, 2020 07:41
-
-
Save 71m024/b937b9b1d4b79f1d3c6d20b82b6e4d3f to your computer and use it in GitHub Desktop.
This file contains 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
import java.util.Arrays; | |
public class Main { | |
private char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; | |
private char[] operators = {'+', '*'}; | |
private char[] basement = {'$', '_', '_', '_', '_', '_', '_', '_'}; | |
private int basementPointer = 0; | |
private char basementBottom = '$'; | |
private char basementPlaceholder = '_'; | |
private char noInput = '_'; | |
private char[] word = {'3', '+', '4'}; | |
public static void main(String[] args) throws Exception { | |
new Main(); | |
} | |
public Main() throws Exception { | |
char basementTop = popFromBasement(); | |
char input = read(); | |
if (isDigit(input) && basementTop == this.basementBottom) { | |
System.out.println("transition from state-0 to state-1"); | |
char[] result = {'$', input}; | |
putToBasement(result); | |
System.out.println("Results in input: " + Arrays.toString(this.word)); | |
System.out.println("Results in basement: " + Arrays.toString(this.basement)); | |
} else { | |
throw new Exception("this word is not acceptable"); | |
} | |
basementTop = popFromBasement(); | |
input = read(); | |
while (isDigit(basementTop) && (isDigit(input) || isOperator(input))) { | |
System.out.println("transition from state-1 to state-1"); | |
if (isDigit(input)) { | |
char[] result = {basementTop, input}; | |
putToBasement(result); | |
System.out.println("Results in input: " + Arrays.toString(this.word)); | |
System.out.println("Results in basement: " + Arrays.toString(this.basement)); | |
} else if (isOperator(input)) { | |
char[] result = {}; | |
putToBasement(result); | |
System.out.println("Results in input: " + Arrays.toString(this.word)); | |
System.out.println("Results in basement: " + Arrays.toString(this.basement)); | |
} | |
basementTop = popFromBasement(); | |
input = read(); | |
} | |
if (input == noInput && isDigit(basementTop)) { | |
System.out.println("transition from state-1 to state-2"); | |
// nothing to do | |
System.out.println("Results in input: " + Arrays.toString(this.word)); | |
System.out.println("Results in basement: " + Arrays.toString(this.basement)); | |
} else { | |
throw new Exception("this word is not acceptable"); | |
} | |
basementTop = popFromBasement(); | |
input = read(); | |
if (input == noInput && basementTop == basementBottom) { | |
System.out.println("transition from state-2 to state-3"); | |
char[] result = {basementBottom}; | |
putToBasement(result); | |
System.out.println("Results in input: " + Arrays.toString(this.word)); | |
System.out.println("Results in basement: " + Arrays.toString(this.basement)); | |
} else { | |
throw new Exception("this word is not acceptable"); | |
} | |
System.out.println("The word is OK!"); | |
} | |
private boolean isDigit(char character) { | |
return charsContain(character, this.digits); | |
} | |
private boolean isOperator(char character) { | |
return charsContain(character, this.operators); | |
} | |
private boolean charsContain(char character, char[] characterList) { | |
for (char digit : characterList) { | |
if (character == digit) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private char popFromBasement() { | |
char value = this.basement[this.basementPointer]; | |
this.basement[this.basementPointer] = '_'; | |
this.basementPointer--; | |
return value; | |
} | |
private void putToBasement(char[] word) { | |
for (char character : word) { | |
putToBasement(character); | |
} | |
} | |
private void putToBasement(char character) { | |
this.basementPointer++; | |
this.basement[this.basementPointer] = character; | |
} | |
/** | |
* Read the first character of the word and remove it. | |
* | |
* @return the next character from the left | |
*/ | |
private char read() { | |
if (this.word.length > 0) { | |
char character = this.word[0]; | |
char[] newWord = new char[this.word.length - 1]; | |
System.arraycopy(this.word, 1, newWord, 0, newWord.length); | |
this.word = newWord; | |
return character; | |
} else { | |
return noInput; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment