วันอังคารที่ 20 ตุลาคม พ.ศ. 2558

v1.0

/*vending machine value*/
int insert=0; //1->Select Product & Input money | 2->coin[0]>Buy & Can Buy now | 3->Press 'c' to clear
int buy=0; // total buy
int[] coin={0, 100, 100, 100, 0, 0, 0, 0, 0, 0} ;//0 total input value |1-3 remain | 4-6 input | 7-9 change
int[] n= {30, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //0-4 remain | 5-9 get | 10-14 sold
int[] price={12, 20, 7, 15, 10}; //pepi orage watr tea mlk
/*animated*/
int posY=90, dy=80, c=0; //control line
int xin=0; //animated skewline
float show=-1, dx=0;//animated coin
int s=0; //coin Y position
float light_opacity=50, plusl=0.25;
/*admin value*/
String trans=""; // HISTORY detail
String timeBF=""; //checking time if same then do not add time
int count=0;
boolean admin=true;
int action=0; //'1' select action '2'insert or remove
String info="Select action by button";
int frameclick, frameZ=0, reset=0; //animated 1 second button & Z
int allzero=0;
void setup() {
  size(350, 520);
  textSize(15);
}
void draw() {
  if (n[0]==0 && n[1]==0 && n[2]==0 && n[3]==0 && n[4]==0 && insert==0)
    allzero=1;
  else
    allzero=0;
  if (admin==true) {
    background(51);
    admin_panel();
  } else {
    ven_img();
    coin[0]=coin[4]+coin[5]*5+coin[6]*10; //calc money u have
    if (allzero==0) {
      if (insert==1 || insert==2) {
        /*TEXT ZONE*/
        String textcoin="Coin 1(1):"+coin[4]+" \nCoin 5(2):"+coin[5]+" \nCoin 10(3):"+coin[6]+" \n= "+coin[0]+" Baht";
        if (reset==0) {
          show-=0.05;
          dx+=0.5;
          if (show<=-1) {
            show=-1;
            dx=0;
          }
          if (show>-1) {
            draw_coin(100+dx, 428+s, map(show, 1, -1, 255, 0));
          }
          fill(255);
          text("ENTER COIN", 20, 410);
          fill(#78BC80);
          text(textcoin, 20, 428);
          // show text total buy
          if (buy>0) { //buy > 0
            fill(#C6D842);
            text("Total : "+buy+".-", 130, 410);
            fill(#FF5858);
            text("Press Z to reset coin and item", 130, 450);
            insert=2;
            //================show "YOU CAN BUY" ++ ================
            if (coin[0]>=buy) {
              fill(#F6FF08);
              text("You can BUY by press ENTER", 130, 430);
            }
          }
        }
        if (reset==1) {
          fill(#f02e6e);
          text("Your Change : ", 20, 410);
          text(textcoin, 20, 428);
          if (frameCount-frameZ==60) {
            reset_CBGET();
            reset=0;
            insert=1;
          }
        }
      } else if (insert==3) { //calc Change and show what you get
        fill(#929292);
        text("Total : "+buy+".-", 90, 430);
        fill(#7c852b);
        text("Your money : "+coin[0]+".-", 40, 410);
        fill(#F6FF08);
        text("Change : "+(coin[0]-buy)+".- "+calc_coin(coin[0]-buy), 199, 410);
        fill(#C6D842);
        text("Press 'C' to clear", 119, 512);
      }
    } else if (allzero==1) {
      fill(#FC666B);
      text("PLEASE CONTACT OWNER - OUT OF STOCK", 20, 450);
    }
  }
}

//==================================================INTERACTIVE AND INPUT======================================================================//
void mousePressed() {
  if (mouseButton==LEFT) {
    if (mouseX>=220 && mouseX<=340 && mouseY>=368 && mouseY<=388 && insert==0) { //can enter admin only insert==0
      if (keyPressed && (key=='X' || key=='x')) {
        admin=true;
      }
    }
    //admin click quit & action//
    if (admin==true) {
      if (mouseX>=20 && mouseX<=70 && mouseY>=20 && mouseY<=40 && action==0) {
        admin=false;
      }
      if (mouseX>=280 && mouseX<=340 && mouseY>=20 && mouseY<=40 && action==0) {
        action=1;
      }
      if (mouseX>=20 && mouseX<=70 && mouseY>=20 && mouseY<=40 && action==1) {
        action=0;
      }
      if (mouseX>=50 && mouseX<=300 && mouseY>=170 && mouseY<=250 && action==1) { //RESET MACHINE
        info="Reset Machine Completed !";
        frameclick=frameCount;
        reset();
      }
      if (mouseX>=50 && mouseX<=300 && mouseY>=270 && mouseY<=350 && action==1) { //RESET SIMULATOR
        info="Reset Simulator Completed !";
        frameclick=frameCount;
        reset();
        count=0;
        for (int i=10; i<=14; i++) {
          n[i]=0;
        }
        trans="";
      }
      if (mouseX>=50 && mouseX<=300 && mouseY>=70 && mouseY<=150 && action==1) { //GO TO action2 = INSERT/REMOVE COINS PRODUCTS
        action=2;
      }
      if (action==2) {
        if (mouseX>=20 && mouseX<=70 && mouseY>=20 && mouseY<=40) { //back to action1
          action=1;
        }
        for (int i=0; i<5; i++) { //add
          if (mouseX>=42+60*i && mouseX<=57+60*i && mouseY>=59 && mouseY<=74) {
            if (n[i]<30)
              n[i]+=1;
          }
        }
        for (int i=0; i<5; i++) { //minus
          if (mouseX>=42+60*i && mouseX<=57+60*i && mouseY>=123 && mouseY<=136) {
            if (n[i]>0)
              n[i]-=1;
          }
        }
      }
    }
    //buying zone//
    if ((insert==0 || insert==1) && allzero==0) {
      if (mouseX>=10 && mouseX<=130 && mouseY>=368 && mouseY<=388) {
        if (insert==0) {
          insert=1;
        } else if (insert==1) {
          insert=0;
          show=-1; //resetcoin ani
          reset_CBGET(); // reset coin
        }
      }
    }
    if (insert==1 || insert==2) {
      for (int i=0; i<5; i++) {
        if (mouseX>=315 && mouseX<=335 && mouseY>=60+60*i && mouseY<=80+60*i && n[i]>0) {
          buy+=price[i];
          n[i+5]+=1;
          n[i]--;
        }
      }
    }
    //end buying zone//
  }
}
void mouseDragged() {
  if (mouseX>=169 && mouseX<=183 && mouseY>=71 && mouseY<=229) {
    dy=mouseY;
    if (dy>=230)dy=230;
    if (dy<=80)dy=80;
    posY=int(map(dy, 80, 230, 90, -c));
  }
}
void keyPressed() {
  if (insert==1 || insert==2 && reset==0) {
    if (key=='1') {
      coin[4]+=1;
      show*=-1;
      s=0;
      dx=0;
    }
    if (key=='2') {
      coin[5]+=1;
      show*=-1;
      s=20;
      dx=0;
    }
    if (key=='3') {
      coin[6]+=1;
      show*=-1;
      s=40;
      dx=0;
    }
  }
  if (insert==2) {
    if (key=='Z' || key=='z') {
      for (int i=0; i<=4; i++) { //reset number of pieces [by add nget to nremain]
        n[i]+=n[i+5];
      }
      frameZ=frameCount;
      reset=1;
    }
    if (keyCode==ENTER && coin[0]>=buy) {
      for (int i=5; i<=9; i++) {      //add sold product
        n[i+5]+=n[i];
      }
      //add coin to remain coin
      coin[1]+=coin[4];
      coin[2]+=coin[5];
      coin[3]+=coin[6];
      count++;
      String timenow=nf(hour(), 2)+":"+nf(minute(), 2); //check timebefore
      if (timenow.equals(timeBF)==false) {
        trans+="Time : "+timenow+"\n"  ;
        timeBF=timenow;
      }
      c+=25;// scrolling line
      println(c);
      trans+="+";
      //check and add to history
      if (n[5]>0)trans+="Px"+n[5]+" ";
      if (n[6]>0)trans+="Ox"+n[6]+" ";
      if (n[7]>0)trans+="Wx"+n[7]+" ";
      if (n[8]>0)trans+="Tx"+n[8]+" ";
      if (n[9]>0)trans+="Mx"+n[9]+" ";
      trans+="\n";
      insert=3;
      show=-1;
    }
  }
  if (insert==3) {
    if (key=='c' || key=='C') {
      //dec coin from changing
      for (int i=1; i<=3; i++) {
        coin[i]-=coin[i+6];
        coin[i+6]=0;
      }
      reset_CBGET();
      insert=0;
    }
  }
  if (action==2 && admin==true) {
    if (key=='+') {
      if (mouseX>=61 && mouseX<=121 && mouseY>=207 && mouseY<=267) {
        if (coin[3]<1000)
          coin[3]++;
      }
      if (mouseX>=146 && mouseX<=197 && mouseY>=212 && mouseY<=263) {
        if (coin[2]<1000)
          coin[2]++;
      }
      if (mouseX>=215 && mouseX<=258 && mouseY>=218 && mouseY<=258) {
        if (coin[1]<1000)
          coin[1]++;
      }
    } else if (key=='-') {
      if (mouseX>=61 && mouseX<=121 && mouseY>=207 && mouseY<=267) {
        if (coin[3]>0)
          coin[3]--;
      }
      if (mouseX>=146 && mouseX<=197 && mouseY>=212 && mouseY<=263) {
        if (coin[2]>0)
          coin[2]--;
      }
      if (mouseX>=215 && mouseX<=258 && mouseY>=218 && mouseY<=258) {
        if (coin[1]>0)
          coin[1]--;
      }
    }
  }
}
//==================================================END INTERACTIVE AND INPUT======================================================================//
// draw vending machine and product
void ven_img() {
  fill(31);
  rect(0, 0, width, height);
  fill(51);
  rect(0, 0, width-20, height-170);//inner black
  /*==== button ====*/
  fill(0);
  rect(13, 372, 120, 20);
  rect(223, 372, 120, 20);
  fill(#C18585);
  rect(10, 368, 120, 20); //pink rect [end =130]
  fill(#FF812C);
  rect(220, 368, 120, 20); //yellow rect [end =340]
  fill(255);
  text("BUY", 20, 385);
  text("ADMIN", 230, 385);
  /*====draw goods zone====*/
  for (int x=0; x<n[0]; x++) {
    fill(0);
    ellipse(20+x*40, 65, 35, 35);
    pepsi(20+x*40, 60, 35, 35); //pepsi
  }
  for (int x=0; x<n[1]; x++) {
    fill(0);
    ellipse(20+x*40, 125, 35, 35);
    orange(20+x*40, 120, 35, 35); //orange
  }
  for (int x=0; x<n[2]; x++) {
    fill(0);
    ellipse(20+x*40, 190, 35, 35);
    water(20+x*40, 180, 35, 35); //water
  }
  for (int x=0; x<n[3]; x++) {
    fill(0);
    ellipse(20+x*40, 250, 35, 35);
    tea(20+x*40, 240, 35, 35); //tea
  }
  for (int x=0; x<n[4]; x++) {
    fill(0);
    ellipse(20+x*40, 310, 35, 35);
    milk(20+x*40, 300, 35, 35); //milk
  }
  /*end draw goods*/
  if (insert>=1) { //light animated
    if (light_opacity==25 || light_opacity==50)plusl*=-1;
    light_opacity+=plusl;
    for (int i=16; i<46; i++) { //light
      stroke(#39A52B, map(i, 16, 45, light_opacity, 0)); //<change_lightcolor>
      line(0, i, width, i);
      line(0, 364-i, width, 364-i);
    }
  }
  fill(20);
  rect(300, 0, 50, height-170);//right tab
  /*======== Draw button ==========*/
  for (int i=1; i<=5; i++) {
    fill(#F2315E);
    rect(315, 60*i, 20, 20);
  }
  /*=========Show prices and show green color============*/
  for (int i=0; i<=4; i++) {
    if (n[i]>0) {
      fill(#81FF97);
      rect(315, 60*(i+1), 20, 20);
      if (n[i]>0)
        text(price[i]+".-", 315, 50+60*i);
    }
  }
  fill(33);
  rect(0, 0, width, 33);// Title Vending Machine & Animated
  skew_line(xin, 30, 20, 30, 50, 1);
  textSize(25);
  fill(0);
  text("Drinking Water", 93, 25);
  fill(#47EAE9);
  text("Drinking Water", 90, 22);
  textSize(15);
}
// ======================================= Drawing Function =====================================//
void pepsi(int x, int y, int w, int h) {
  noStroke();
  fill(#F01D44);
  ellipse(x, y, w, h);
  fill(#1A73FF);
  arc(x, y, w, h, radians(-20), radians(160));
  stroke(255);
  noFill();
  strokeWeight(3);
  bezier(x-w*0.47, y, x-w*0.4, y+h*0.5, x+w*0.4, y-h*0.5, x+w*0.47, y);
  noStroke();
}

void orange(int x, int y, int w, int h) {
  fill(#FCC80A);
  ellipse(x, y, w, h);
  fill(255);
  ellipse(x, y, w-5, h-5);
  fill(#FCC80A);
  ellipse(x, y, w-10, h-10);
}

void water(int x, int y, int w, int h) {
  fill(#47EAE9);
  noStroke();
  ellipse(x, y+h*0.159, w, h);
  triangle(x, y-h*0.5, x+w*0.49, y, x-w*0.49, y);
}

void tea(int x, int y, int w, int h) {
  String tea="tea";
  fill(#2AB434);
  noStroke();
  ellipse(x, y+h*0.159, w, h);
  triangle(x, y-h*0.5, x+w*0.49, y, x-w*0.49, y);
  fill(255);
  text(tea, x-textWidth(tea)/2, y+h*0.3);
}
void milk(int x, int y, int w, int h) {
  String tea="milk";
  fill(255);
  noStroke();
  ellipse(x, y+h*0.159, w, h);
  triangle(x, y-h*0.5, x+w*0.49, y, x-w*0.49, y);
  fill(#5067D6);
  text(tea, x-textWidth(tea)/2, y+h*0.3);
}
void plus(float x, float y, int sizebox) {
  noStroke();
  fill(#9CFC82);
  rect(x, y, sizebox*3, sizebox*0.8); //x
  rect(x+sizebox*1.1, y-sizebox*1.1, sizebox*0.8, sizebox*3); //y
}

void minus(float x, float y, int sizebox) {
  noStroke();
  fill(255, 0, 0);
  rect(x, y, sizebox*3, sizebox*0.8);
}
void skew_line(int x, int y, int w, int h, int n, int dx) {
  noStroke();
  fill(#F52A67, 50);
  xin+=dx;
  if (xin>=20)xin=-100;
  for (int i=0; i<n; i++) {
    quad(x+(i*w*2), y, x+w+(i*w*2), y, x+w*2+(i*w*2), y-h, x+w+(i*w*2), y-h);
  }
}
// ======================================= END Drawing Function =====================================//
String calc_coin(int n) { //show change in coins and - coins [coin7-9 for change only]
  String coins="";
  if (coin[3]>0) {
    coin[9]=int(n/10);
    if ( coin[9]>coin[3]) coin[9]=coin[3];
    n=n-( coin[9]*10);
  }
  if (coin[2]>0) {
    coin[8]=int(n/5);
    if (coin[8]>coin[2])coin[8]=coin[2];
    n=n-(coin[8]*5);
  }
  if (coin[1]>0) {
    coin[7]=n;
    if (coin[7]>coin[1])coin[7]=coin[1];
  }
  if (coin[9]>0)    coins+="\nCoin10="+coin[9]+" ";
  if (coin[8]>0)   coins+="\nCoin5="+coin[8]+" ";
  if (coin[7]>0)    coins+="\nCoin1="+coin[7]+" " ;
  if (coin[7]==0 && coin[8]==0 && coin[9]==0) {  // no coin and no change //coin1 5 10 for change
    if (coin[0]!=buy)
      coins="\nSorry no coin left.";
    else
      coins="\nNo Change";
  }
  return coins;
}

// ============================================================= ADMIN ======================================================//
void admin_panel() {
  if (action!=0) {
    fill(33);
    rect(0, 15, width, 33);// Animated skew line
    skew_line(xin, 45, 20, 30, 50, 1);
  }
  if (action==0) {
    fill(30);
    rect(185, 55, 155, 450); //box right
    rect(0,73,182,444); // box left
    /* =================== remaining ============== */
    fill(#FA0A42);
    text("Remaining Product", 190, 360);
    String remain="Pepsi(P) : "+n[0]+"\nOrange Juice(O) : "+n[1]+"\nWater(W) : "+n[2]+"\nTea(T) : "+n[3]+"\nMilk(M) : "+n[4];
    fill(#94FCB4);
    text(remain, 190, 380);
    /* ===================== sold ================= */
    fill(#FF9100);
    text("Sold Product", 190, 190);
    String sold="Pepsi : "+n[10]+"["+(n[10]*12)+"]"+
      "\nOrange Juice : "+n[11]+"["+(n[11]*20)+"]"+
      "\nWater : "+n[12]+"["+(n[12]*7)+"]"+
      "\nTea : "+n[13]+"["+(n[13]*15)+"]"+
      "\nMilk : "+n[14]+"["+(n[14]*10)+"]";
    fill(#94FCB4);
    text(sold, 190, 210);
    /* ===================== coin ================= */
    fill(#35B45C);
    text("Remaining Coins", 190, 70);
    String coins="One : "+coin[1]+"\nFive : "+coin[2]+"\nTen : "+coin[3];
    fill(#94FCB4);
    text(coins, 190, 90);
    fill(#FFC16F);
    text("Value : "+(coin[1]+coin[2]*5+coin[3]*10)+" Baht", 190, 155);
    text("Sold : "+(n[10]*12+n[11]*20+n[12]*7+n[13]*15+n[14]*10)+" Baht", 190, 325); //sold value
    /* ===================== history ================= */
    fill(120);
    text(trans, 5, posY);
    fill(51);
    rect(0, 0, 183, 70);
    fill(#FCAB40);
    text("History "+day()+"/"+month()+"/"+year()+"["+count+"]", 5, 70);
    ctrl_line(175, 80, 0, 255);
    /*==============header====================*/
    fill(33);
    rect(0, 15, width, 33);// Animated skew line
    skew_line(xin, 45, 20, 30, 50, 1);
    fill(#6EF798);
    text("ADMIN", 150, 35);
    fill(30);
    rect(20, 20, 50, 20, 8); //button
    rect(280, 20, 60, 20, 8);
    fill(#FFF708);
    text("Quit", 30, 35);
    text("Action", 287, 35);
  } else if (action==1) {
    fill(#ACA8EA);
    text("ADMIN ACTION", 120, 35);
    for (int i=0; i<3; i++) {
      fill(35);
      rect(50, 70+100*i, 250, 80, 8);
    }
    fill(30);
    rect(20, 20, 50, 20, 8);
    fill(#FFF708);
    text("Back", 30, 35);
    fill(#64F77D);
    text("Insert/Remove Product & Coins", 60, 110);
    fill(#FFDA03);
    text("RESET MACHINES", 110, 210);
    text("RESET SIMULATOR", 110, 310);
    fill(#00B9FF);
    if (frameCount-frameclick==30)info="Select action by button";
    text("INFO : "+info, 71, 400);
  } else if (action==2) { // INSERT OR REMOVE PRODUCT & INSERT OR REMOVE COIN
    fill(#ACA8EA);
    text("ADMIN ACTION", 120, 35);
    fill(30);
    rect(20, 20, 50, 20, 8);//back button
    fill(#FFF708);
    text("Back", 30, 35);//back
    fill(100);
    rect(0, 80, width, 40);
    /*====draw goods && +,- buttonin admin zone====*/
    pepsi(50, 100, 35, 35); //pepsi
    orange(110, 100, 35, 35); //orange
    water(170, 95, 35, 35); //water
    tea(230, 95, 35, 35); //tea
    milk(290, 95, 35, 35); //milk
    for (int i=0; i<5; i++) {
      plus(42+i*60, 65, 5);
      minus(42+i*60, 130, 5);
      fill(0);
      rect(35+i*60, 140, 30, 20);
    }
    fill(#F23350);
    for (int i=0; i<5; i++) {
      text(nf(n[i], 2), 40+60*i, 155);
    }
    /*end draw goods*/
    fill(#795A5F);
    ellipse(92, 238, 60, 60);
    ellipse(173, 238, 50, 50);
    ellipse(237, 238, 40, 40);
    fill(255);
    text("10", 82, 243);
    text("5", 168, 243);
    text("1", 232, 243);
    text(nf(coin[3], 3), 78, 295);
    text(nf(coin[2], 3), 159, 295);
    text(nf(coin[1], 3), 222, 295);
    text("How to +/- coin", 88, 354);
    fill(#E395A1);
    text("1.place mouse on coin", 92, 374);
    text("2.press + or - ", 92, 394);
  }
}
void reset() { //reset machine
  allzero=0;
  for (int i=0; i<=4; i++) {
    n[i]=30;
  }
  coin[1]=100;
  coin[2]=100;
  coin[3]=100;
}
void reset_CBGET() { //reset coin & nget & buy 'z'
  coin[4]=0; //coin input reset
  coin[5]=0;
  coin[6]=0;
  buy=0;
  for (int i=5; i<=9; i++) { //reset number you get
    n[i]=0;
  }
}
void draw_coin(float x, int y, float z) {
  noStroke();
  fill(#FC2626, z);
  ellipse(x, y, 15, 15);
  text("+1", x+10, y);
}
void ctrl_line(int x, int y, int index, int col) {
  strokeWeight(3);
  stroke(255, 50);
  line(x, y+150, x, y);
  fill(col);
  ellipse(x, dy, 10, 10);
  noStroke();
}

Lab06 : Parallel array

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def setup():
    name=["John","Henry","Michael"]
    id=[1,2,3]
    age=[17,16,19]
    weight=[45,90,65]
    height=[156,170,175]
    bmi=[]
    print("---------- DISPLAY ZONE ----------")
    i=0
    while i<len(weight):
        print(" [id",id[i],"][ Name",name[i],"][ age",age[i],"][ weight",weight[i],"][ height",height[i],"]")
        i+=1
    i=0
# ---------- bmi zone ----------
    print("---------- BMI ZONE ----------------")
    count=0 # bmi > 25
    bmi=BMI(weight,height)
    while i<len(bmi): # ---------- print bmi>25
        if bmi[i]>25:
            count+=1
            print("[ id",id[i],"][ Name",name[i],"][ age",age[i],"][ weight",weight[i],"][ height",height[i],"][ BMI","{:.2f}".format(bmi[i]),"]")
        i+=1
    print("count BMI > 25 :",count) # number of student bmi>25
# ---------- average age zone ----------
    print("---------- Age ZONE ----------------")
    print("Average age of students : ","{:.2f}".format(avg(age)))
# ---------- count age < 30 ----------
    c=0 # age<30
    i=0
    while i<len(age):
        if age[i]<30:
            c+=1
        i+=1
    print("count age < 30 : ",c)
# ---------- minimum weight of student ----------
    print("---------- Weight ZONE ----------------")
    print("minimum weight of students :",mini(weight))
    c1=0
    i=0
    while i<len(weight):
        if weight[i]<50:
            c1+=1
            print("[ id",id[i],"][ Name",name[i],"][ age",age[i],"][ weight",weight[i],"][ height",height[i],"]")
        i+=1
    print("count number of students with weight < 50 : ",c1)
def BMI(w,h):
    bmi=[]
    i=0
    while i<len(w):
        m=h[i]/100
        bmi.append(w[i]/(m*m))
        i+=1
    return bmi
def avg(a):
    i=0
    sum=0
    while i<len(a):
        sum+=a[i]
        i+=1
    sum=sum/len(a)
    return sum
def mini(a):
    min=a[0]
    i=0
    while i<len(a):
        if a[i]<min:
            min=a[i]
        i+=1
    return min

setup()

Lab06 : Using 2D array as matrix

def setup():
    row1=[20,30,70]
    row2=[10,25,15]
    row3=[30,15,35]
    matrix1=[row1,row2,row3]
    arow1=[40,20,23]
    arow2=[10,15,70]
    arow3=[23,15,27]
    matrix2=[arow1,arow2,arow3]
    opear="+"
    display_2D(matrix1)
    print(opear)
    display_2D(matrix2)
    print("=")
    result=do_two_2D(matrix1,matrix2,opear)
    display_2D(result)
    transpose(result)
    print("after transpose")
    display_2D(result)
def transpose(aa):
    i=0
    j=0
    while i<len(aa):
        j = i+1
        while j<len(aa[0]):
            temp = aa[i][j]
            aa[i][j]=aa[j][i]
            aa[j][i]=temp
            j+=1
        i+=1

def do_two_2D(aa,bb,mode): #2D
    matrix=[]
    i=0
    while i<len(aa):
        matrix.append([]) #append row
        k=0
        while k<len(aa[0]):
            matrix[i].append(opearation(aa[i][k],bb[i][k],mode))
            k+=1
        i+=1
    return matrix
def opearation(a,b,mode):
    if mode=="+":
        return a+b
    if mode=="-":
        return a-b
    if mode=="*":
        return a*b

def display_2D(aa):
    i=0
    while i<len(aa):
        display_1D(aa[i])
        print()
        i+=1
def display_1D(a):
    i=0
    while i<len(a):
        print(a[i],end=' ')
        i+=1

  
setup()

Lab06 : Building


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def setup():
    floor1=[10,10,10,15,30]
    floor2=[20,40,10,75,15]
    floor3=[35,20,75,25,30]
    building=[floor1,floor2,floor3]
    display_2D(building)
    print ("Total chairs:",sum_2D(building))
    print("index of floor with maximum chair in building: ",end='')
    index_max(building)
    print("index of room with max chair in building:",end=' ')
    index_max_2D(building)
# ========================= 2D =========================
def index_max_2D(b):
    max=find_max(b[0],"value")
    i=1
    while i<len(b):
        if max<find_max(b[i],"value"):
            max=find_max(b[i],"value") # max of 2D
        i+=1
    i=0
    while i<len(b):
        if find_max(b[i],"value")==max:
            print("[",i,find_max(b[i],"index"),"]",end='')
        i+=1
    print()
def index_max(b):
    i=1
    max=0
    while i<len(b):
        if sum_1D(b[max])<sum_1D(b[i]):
            max=i
        i+=1
    print(max,"[ total:",sum_1D(b[max]),"]")
def display_2D(aa):
    i=0
    while i<len(aa):
        print("floor ",i,end='-> ')
        display_1D(aa[i])
        print()
        i+=1
def sum_2D(b):
    i=0
    sum=0
    while i<len(b):
        sum+=sum_1D(b[i])
        i+=1
    return sum
# ========================= 1D =========================
def sum_1D(a):
    i=0
    sum=0
    while i<len(a):
        sum+=a[i]
        i+=1
    return sum
def display_1D(a):
    i=0
    while i<len(a):
        print(a[i],end=' ')
        i+=1
def find_max(a,mode):
    max=a[0]
    max_index=0
    i=0
    while i<len(a):
        if a[i]>max:
            max=a[i]
            max_index=i
        i+=1
    if mode=="value":
        return max
    elif mode=="index":
        return max_index

setup()
http://pastebin.com/YLNf2sng

Lab06 : Multiply Matirx

def setup():
    row1=[1,2,3]
    row2=[4,5,4]
    row3=[5,9,10]
    matrix1=[row1,row2,row3]
    arow1=[4,2,5]
    arow2=[1,1,2]
    arow3=[2,1,3]
    matrix2=[arow1,arow2,arow3]
    display_2D(multiply_matrix(matrix1,matrix2))

def multiply_matrix(aa,bb):
    i=0
    matrix=[]
    while i<len(aa):
        col=0
        matrix.append([])
        while col<len(bb[0]):
            row=0
            temp=0  
            while row<len(bb):
                temp+=aa[i][row]*bb[row][col]
                row+=1
            matrix[i].append(temp)
            col+=1
        i+=1
    return matrix


# =========== Display function ===========
def display_2D(aa):
    i=0
    while i<len(aa):
        display_1D(aa[i])
        print()
        i+=1
def display_1D(a):
    i=0
    while i<len(a):
        print(a[i],end=' ')
        i+=1
setup()

วันจันทร์ที่ 19 ตุลาคม พ.ศ. 2558

Lab06 : insertion sort

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def setup():
    name=["John","Henry","Michael","Morgan","Hestia","Lux","Lulu","EX","L"]
    id=[1,2,3,4,5,6,7,8,9]
    age=[17,16,19,15,20,12,17,13,14]
    weight=[70,90,65,55,50,78,56,57,67]
    height=[156,170,175,172,173,174,159,160,165]
    insertion_sort(age,name,id,weight,height) #sort by age
    i=0
    while i<len(age):
        print(" [id",id[i],"][ Name",name[i],"][ age",age[i],"][ weight",weight[i],"][ height",height[i],"]")
        i+=1
def insertion_sort(a,b,c,d,e): # sort by a
    i=1
    j=0
    while i<len(a):
        j=i
        while j>0 and a[j]<a[j-1]:
            swap(j,j-1,a)
            swap(j,j-1,b)
            swap(j,j-1,c)
            swap(j,j-1,d)
            swap(j,j-1,e)
            j-=1
        i+=1
def swap(i,k,a): # i,k are indices a=array
    temp=a[i]
    a[i]=a[k]
    a[k]=temp
setup()

Lab06 : String Text

def setup():
    text=input("input text:")
    display_string(text)
def display_string(a):
    a1=" ##   "
    a2="#  #  "
    a3="####  "
    a4="#  #  "
    a5="#  #  "
    A=[a1,a2,a3,a4,a5]
    e1="####  "
    e2="#     "
    e3="####  "
    e4="#     "
    e5="####  "
    E=[ e1, e2, e3, e4, e5]
    c1="####  "
    c2="#     "
    c3="#     "
    c4="#     "
    c5="####  "
    C=[ c1, c2, c3, c4, c5]
    s1="####  "
    s2="#     "
    s3="####  "
    s4="   #  "
    s5="####  "
    S=[ s1, s2, s3, s4, s5]
    j=0
    while j<5:
        i=0
        while i<len(a):
            if a[i]=="A":
                print(A[j],end='')
            if a[i]=="E":
                print(E[j],end='')
            if a[i]=="C":
                print(C[j],end='')
            if a[i]=="S":
                print(S[j],end='')
            i+=1
        print()
        j+=1
setup()

commit เพิ่มเติม : เพื่ม userstory, bookmark

>>  add userstory ทำการเพิ่ม userstory ใน functional tests >>  add bookmark for user เพิ่มระบบ Bookmark โดยการสร้าง model ขึ...