วันอาทิตย์ที่ 24 สิงหาคม พ.ศ. 2557

POSTFIX

public class Postfix {
    public static void main( String args[] ) {
        String Math = "2 2 2 * * 3 % 1 +";
        String[] a = Math.split( " " );
        LinkedListStack<Float> test = new LinkedListStack<Float>(a.length);
        for(int i = 0; i<a.length; i++){

            if(a[i].equals("-")==true){
               
                float p=test.top();
                float b=test.pop();
                float n=b-p;
                test.pop();
                test.push(n);
               
            }else
                if(a[i].equals("+")==true){
               
                float p=test.top();
                float b=test.pop();
                float n=b+p;
                test.pop();
                test.push(n);
               
            }else
            if(a[i].equals("*")==true){
               
                float p=test.top();
                float b=test.pop();
                float n=b*p;
                test.pop();
                test.push(n);
               
            }else
            if(a[i].equals("/")==true){
               
                float p=test.top();
                float b=test.pop();
                float n=b/p;
                test.pop();
                test.push(n);
               
            }else
            if(a[i].equals("%")==true){
               
                float p=test.top();
                float b=test.pop();
                float n=b%p;
                test.pop();
                test.push(n);
               
            }else{test.push( Float.parseFloat(a[i]) );}
       
        }
     
       System.out.println( "Answer is "+test.top());
     
            }
       
    }

LED

void setup() {
  size(screen.width, screen.height);
}
void draw() {
  frameRate(10);
  LED f=new LED(5, 5, 10);
  f.display();
}

class LED {
  int a;
  int b;
  int size1;
  LED(int x, int y, int s) {
    this.a=x;
    this.b=y;
    this.size1=s;
  }
  void colour() {
    float c =random(0, 2);
    if (c>1) {
      fill(0);
    }
    else {
      fill(255);
    }
  }
  void display() {
    ellipse(this.a, this.b, this.size1, this.size1);
    colour();
  }
}