Created
March 11, 2010 15:28
-
-
Save mmandersheid/329238 to your computer and use it in GitHub Desktop.
A bracket checker
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
import java.awt.*; | |
import java.awt.event.*; | |
public class BracketChecker extends EventPanel implements ActionListener, TextListener { | |
private TextField field; | |
private Label answerLabel, title, instructions;; | |
public BracketChecker(){ | |
setBackground(Color.white); | |
setLayout(null); | |
field = new TextField(""); | |
field.setSize(380,25); | |
field.setLocation(10,150); | |
field.setBackground(Color.white); | |
field.setForeground(Color.black); | |
add(field); | |
answerLabel = new Label(""); | |
answerLabel.setSize(400,30); | |
answerLabel.setLocation(0,210); | |
answerLabel.setAlignment(Label.CENTER); | |
answerLabel.setFont(new Font("Serif",Font.BOLD,16)); | |
answerLabel.setForeground(Color.red); | |
add(answerLabel); | |
title = new Label ("Bracket Checking Program"); | |
title.setLocation(0,20); | |
title.setSize(400,30); | |
title.setFont(new Font("Serif" ,Font.BOLD,20)); | |
title.setAlignment(Label.CENTER); | |
title.setForeground(Color.red); | |
add(title); | |
instructions = new Label("Press enter to check the expression"); | |
instructions.setSize(400,30); | |
instructions.setLocation(0,120); | |
instructions.setAlignment(Label.CENTER); | |
instructions.setForeground(Color.red); | |
add(instructions); | |
field.addActionListener(this); | |
field.addTextListener(this); | |
} | |
public void textValueChanged(TextEvent t ){ | |
answerLabel.setText(""); | |
} | |
public void actionPerformed(ActionEvent a){ | |
String s = field.getText(); | |
CharStack stack = new CharStack(); | |
IntStack stack2 = new IntStack(); | |
for(int i =0; i<s.length(); i++){ | |
char ch = s.charAt(i); | |
if("({[<".indexOf(ch)>=0) { | |
stack.push(ch); | |
stack2.push(i); | |
} | |
else if (")}]>".indexOf(ch)>=0){ | |
if(stack.empty()){ | |
answerLabel.setText("Unmatched Bracket"); | |
field.setSelectionStart(0); | |
field.setSelectionEnd(i+1); | |
return; | |
} | |
char previous = stack.pop(); | |
int where = stack2.pop(); | |
if("]})>".indexOf(ch)!="[{(<".indexOf(previous)){ | |
answerLabel.setText("Brackets don't Match"); | |
field.setSelectionStart(where); | |
field.setSelectionEnd(i+1); | |
return; | |
} | |
} | |
} | |
if (!stack.empty()){ | |
//int where = stack2.pop() | |
answerLabel.setText("Unmatched Bracket"); | |
field.setSelectionStart(stack2.pop()); | |
field.setSelectionEnd(s.length()); | |
} | |
else | |
answerLabel.setText("Brackets are good"); | |
} | |
} | |
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
public class CharStack { | |
private char data[]; | |
private int top; | |
public CharStack(){ | |
data = new char[100]; | |
top = -1; | |
} | |
public void push(char c){ | |
data[++top] = c; | |
} | |
public char pop(){ | |
if(top<0) | |
return'('; | |
return data[top--]; | |
} | |
public boolean empty(){ | |
if(top==-1) | |
return true; | |
return false; | |
} | |
public char peek(){ | |
if(top<0) | |
return '('; | |
return data[top]; | |
} | |
} |
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
public class DoubleStack { | |
private double data[]; | |
private int top; | |
public DoubleStack(){ | |
data = new double[100]; | |
top = -1; | |
} | |
public void push(double c){ | |
data[++top] = c; | |
} | |
public double pop(){ | |
return -9999.99; | |
return data[top--]; | |
} | |
public boolean empty(){ | |
if(top==-1) | |
return true; | |
return 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
/* | |
applet that runs a panel | |
*/ | |
import java.awt.*; | |
import java.awt.event.*; | |
import java.applet.Applet; | |
public class PanelApplet extends Applet | |
{ | |
BracketCheckerpanel; | |
public void init() | |
{ | |
setLayout(null); | |
panel = new BracketChecker(); | |
add(panel); | |
panel.setSize(getSize().width, getSize().height); | |
panel.setLocation(0,0); | |
panel.setVisible(true); | |
panel.requestFocus(); | |
if (panel instanceof KeyListener) | |
addKeyListener((KeyListener)panel); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment