sir sepulveda is cool.
adprog is the coolest. hehehe.
sir anton makes everything look so easy. roar. he can make the brace pairing thingy in less thank thirty minutes samantalang kelangan pa naming ng ilang comsci periods para magawa yon. hayyy.
the brace pairing stuff was exceptional. gwahahahaha. . . corny ko. :P
got this from rara. :P
CharStack
public interface CharStack {
public void push( char c ) throws StackFullException;
public char pop() throws StackEmptyException;
public boolean isEmpty();
}
StackEmptyException
public class StackEmptyException extends Exception {}
StackFullException
public class StackFullException extends Exception {}
MyStack
public class MyStack implements CharStack {
int ptr, MAX;
char[] array;
public MyStack() {
ptr = -1;
MAX = 255;
array = new char[MAX];
}
public boolean isEmpty() {
return (ptr < 0);
}
public void push( char c ) throws StackFullException {
if( ptr == MAX-1 ) throw new StackFullException();
// erratum: dapat MAX-1 kasi 'yun ang index ng last element. :)
// MAX kasi nalagay ko during class, hehe. :)
else {
ptr++;
array[ptr] = c;
}
}
public char pop() throws StackEmptyException {
char tmp = '\0'; // null character
if( isEmpty() ) throw new StackEmptyException();
else {
tmp = array[ptr];
array[ptr] = '\0';
ptr--;
}
return tmp;
}
}
Checker
import javax.swing.*;
public class Checker {
public static boolean isOpen( char c ) {
return ( c == '(' || c == '[' || c == '{' );
}
public static boolean isClose( char c ) {
return ( c == ')' || c == ']' || c == '}' );
}
public static boolean notMatch( char close, char open ) {
if( close == ')' && open != '(' ) return true;
if( close == ']' && open != '[' ) return true;
if( close == '}' && open != '{' ) return true;
return false;
}
public static void exit( String msg ) {
JOptionPane.showMessageDialog(null,msg);
System.exit(0);
}
public static void main( String arg[] ) {
// variables
MyStack stack = new MyStack();
//get input
String input = JOptionPane.showInputDialog("Enter Equation:");
int len = input.length();
if( len == 0 ) exit( "Ngye!" );
JOptionPane.showMessageDialog(null,"\""+input+"\"? Are you sure?");
//loop through each character
for( int x = 0; x < len; x++ ) {
char current = input.charAt(x);
// if opening symbol --> push
if( isOpen(current) ) {
try {
stack.push(current);
}
catch (StackFullException sfe) {
exit( "Stack Full. Too many symbols." );
}
}
// else if closing
else if( isClose(current) ) {
try {
char pair = stack.pop();
// check pairing --> if wrong ,error
if( notMatch(current,pair) ) {
exit( "Pairing error." );
}
}
catch (StackEmptyException see) {
// pop --> if empty stack, error
exit("Pairing error.");
}
}
} // end loop
// check for empty stack --> if not, error
if( stack.isEmpty() ) exit ("KEREEEKT!");
else exit( "Pairing error." );
}
}
wokei... throw - catch lalala.
i don't know what confuses me. harharhar. gets ko naman ah. :]
okayyyy. i will prepare for the quiz on wednesday. hehe. buti na lang minove siya kasi super puro STR lang nasa utak ko kanina. hahahahha. owel. :D
annndd. QUEUE. barbequeue. hahahhahahahhahahaha. kyot. :)) PILA. LILO. wheeee. funnnnnn. :D