2020年5月7日 星期四

week11 程式設計

泡泡排序
int []a=new int[10];//Java's Array
//int []a={6,3,5,9,1,0,4,2,7,8};
//int []a={1,2,3,5,0,4,6,7,8,9};
//int []a={9,8,7,6,5,4,3,2,1,0};
void setup(){
  size(500,800);
  for(int i=0;i<10;i++){//random choose number
    a[i]=int(random(10));//亂數決定數字
  }
  textSize(36);
  textAlign(LEFT,TOP);
  showArray(y);
}
int y=0;
void showArray(int y){
  for(int i=0;i<10;i++){
    fill(255);  rect(i*50, y, 50,50);
    fill(0);         text(a[i], i*50, y);
  } 
}
void draw(){
 
}
void mousePressed(){
  for(int i=0; i<10-1;i++){
    if(a[i] > a[i+1]){
      int temp=a[i];
      a[i]=a[i+1];
      a[i+1]=temp;
      fill(255,0,0,128); rect(i*50, y, 100,50);
    }
  }
  y+=50;
  showArray(y);
}



size(200,200);
rect(50,50,50,50);
textSize(40); textAlign(LEFT,TOP);
fill(#ff0000); text("A", 50,50);



大小寫轉換

#include <stdio.h>
int main(){
char a[100];
scanf("%s", a);
for(int i=0;a[i]!=0;i++){
///char c=a[i];
if(a[i]>='A' && a[i]<='Z'){
a[i]=a[i]-'A'+'a';///減掉大寫加小寫
}
else if(a[i]>='a' && a[i]<='z'){
a[i]=a[i]-'a'+'A';
}
}
printf("%s\n", a);
}
A的B次方函數 

#include <stdio.h>
int MYPOWER(int a,int b){///呼叫
int ans=1;
for(int i=0;i<b;i++){
ans*=a;///a乘b次
}
return ans;
}
int main(void)
{
int a,b;
scanf("%d%d",&a,&b);
printf("[%d]",MYPOWER(a,b));
return 0;
}



void setup(){
    size(300,500);
    colorMode(HSB);
}
int ansX=2, ansY=3;
int N=5;///5X5
int H=255,S=255,B=255;///色調,色相,飽和
void draw(){
    background(0);
    int R=300/N, w=R/2;
    for(int x=0;x<N;x++){
        for(int y=0;y<N;y++){
            if(x==ansX && y==ansY) fill(H+10, S-35, B-45);///更改圓顏色
            else fill(H, S, B);
            ellipse(0+w+x*R, 200+w+y*R, R,R);
        }
    }
}
void mousePressed(){
    ansX = int(random(N)); ansY = int(random(N));///隨機換圓
    H+=16;
    if(H>255)H=0;
}

沒有留言:

張貼留言