2013年5月21日 星期二

物件導向設計原理期末考(I)


1. 將兩班成績分數透過切割函數,切割成小字串,整數字串轉成整數且存於整數陣列,
(a) 將整數陣列當作score建構方法參數,且產生一score物件。
(b) 再透過score物的成員方法完成題目的要求。
   class score包括:
一個資料成員:
int[] data;
  一個建構方法:
score(int[] dd)    // 設定int[] data初始值
兩個成員方法:
getLevelNum( base)     // 傳回整數 base (含)分數以上人數
getPassAvg() // 傳回及格成績的平均分數(小數後兩位)



class score{
 int[] data;

 score(int[] dd){
  data=dd;
 }
 int getLevelNum(int base){
  int num=0;

  for(int kk:data) 
   if(kk >= base) num++;
  return num;
 }

 double getPassAvg(){
  int sum=0, num=0;
  
  num=getLevelNum(60);
  for(int i:data) {
   if(i>=60) sum+=i; 
  }
  return (int)((double)sum/num*100)/100.0;
 }
}

class prob10507{
        public static void main(String[] args){

 String IMDA="IMDA,75,88,62,77,69,82,79,85,93,44,81,53,66,71";
 String IMDB="IMDB/55/68/72/57/83/77/93/75/66/85/83/47/52/87/96/53/62/89/76/51";
 int gradeA, gradeB, gradeC, gradeD,gradeF; 

        String[] token=IMDA.split("[,]");
 int[] data=new int[token.length-1];

 for(int i=1;i<token.length;i++)
  data[i-1]=Integer.parseInt(token[i]);

 score cls1=new score(data);
                gradeA=cls1.getLevelNum(90);
                gradeB=cls1.getLevelNum(80)-cls1.getLevelNum(90);
                gradeC=cls1.getLevelNum(70)-cls1.getLevelNum(80);
                gradeD=cls1.getLevelNum(60)-cls1.getLevelNum(70);
                gradeF=cls1.getLevelNum(0)-cls1.getLevelNum(60);
 System.out.println(" CLS Name: "+ token[0]);
 System.out.println("     Numbers of students: "+ (token.length-1));
 System.out.println("     Pass Average Number: "+cls1.getPassAvg());
 System.out.println("     grade A Number: "+gradeA);    // score >= 90
 System.out.println("     grade B Number: "+gradeB);    // score >= 80 and score < 90
 System.out.println("     grade C Number: "+gradeC);    // score >= 70 and score < 80
 System.out.println("     grade D Number: "+gradeD);   // score >= 60 and score < 70
 System.out.println("     grade F Number: "+gradeF);    // score < 60

        String[] token2=IMDB.split("[/]");
 int[] data2=new int[token2.length-1];

 for(int i=1;i<token2.length;i++)
  data2[i-1]=Integer.parseInt(token2[i]);

 score cls2=new score(data2);
                gradeA=cls2.getLevelNum(90);
                gradeB=cls2.getLevelNum(80)-cls2.getLevelNum(90);
                gradeC=cls2.getLevelNum(70)-cls2.getLevelNum(80);
                gradeD=cls2.getLevelNum(60)-cls2.getLevelNum(70);
                gradeF=cls2.getLevelNum(0)-cls2.getLevelNum(60);
 System.out.println(" CLS Name: "+ token2[0]);
 System.out.println("     Numbers of students: "+ (token2.length-1));
 System.out.println("     Pass Average Number: "+cls2.getPassAvg());
 System.out.println("     grade A Number: "+gradeA);
 System.out.println("     grade B Number: "+gradeB);
 System.out.println("     grade C Number: "+gradeC);
 System.out.println("     grade D Number: "+gradeD);
 System.out.println("     grade F Number: "+gradeF);
        }
}



2. (a) class degree 包括:
  一個建構方法:
degree( char, double)    // 設定degreeC, degreeF 初始值
兩個資料成員:
private: double degreeC, degreeF;
  四個成員方法:
private:  CtoF()    // degreeC  degreeF
private:  FtoC()    // degreeF  degreeC
public: getDegC()    // 傳回degreeC
public: getDegF()    // 傳回degreeF
(b) class check 繼承degree
  一個建構方法:
check(char, double)  //呼叫父類別之建構方法,設定degreeC, degreeF 初始值
有兩個成員方法:
public:  degChk()          // 傳回體溫狀態代碼(整數)
public:  toStatus(byte)    // 傳回體溫狀態(字串)
(c) 透過相關的成員方法完成題目要求(產業輸出內容)。

體溫、代碼與狀態對照表
~ 36.4c (97.5f)     -1 (Hypothermia) (體溫過低)
36.4c (97.5f) ~ 37.8c (100f)   0 (Normal) (正常)
37.8c (100.f) ~ 39.4c (103.0f)   1 (Fever) (發燒)
39.4c (103.0f) ~ 40.3c (104.5f)   2 (High Fever) (高燒)
40.3c (104.5f) ~               3 (Serious) (嚴重)



class degree{
 private double degC=0, degF=32;

 degree(char CF, double dd){
  if(CF=='C'){
   degC=dd;
   CtoF();
  }else if(CF=='F'){
   degF=dd;
   FtoC();
  }
 }
 private void CtoF(){
  degF=degC*5.0/9.0+32;
 }
 private void FtoC(){
  degC=(degF-32)/9.0*5.0;
 }
 double getDegC(){
  return degC;
 }
 double getDegF(){
  return degF;
 }
}

class check extends degree{
 check(char cc, double dd){
  super(cc,dd);
 }
  
 byte degChk(){               // if-else statement 
                double degC=getDegC();            // call getDegC to get degC
  if(degC < 36.4) return -1;
                else if(degC < 37.8) return 0;
                else if(degC < 39.4) return 1;
                else if(degC < 40.3) return 2;
                else return 3;

 }
 String toStatus(byte n){     // switch case statement
  switch(n){
  case -1:
   return "Hypothermia";
  case 0:
   return "Normal";
  case 1:
   return "Fever";
  case 2:
   return "High Fever";
  default:
   return "Serious";
  }
 } 
}
 
class prob20507{
        public static void main(String[] args){

  check c1 =new check('C',38.3);

 System.out.println(" degree C "+c1.getDegF());
 System.out.println(" degree F "+c1.getDegC());
 System.out.println("   Status code: "+ c1.degChk());
 System.out.println("   Degree Status: "+c1.toStatus(c1.degChk()));

 check c2 = new check('F',103.5);
 System.out.println(" degree C "+c2.getDegF());
 System.out.println(" degree F "+c2.getDegC());
 System.out.println("   Status code: "+ c2.degChk());
 System.out.println("   Degree Status: "+c2.toStatus(c2.degChk())); 
        }
}

第22次ITSA答案參考

題目連結: https://db.tt/tg3FeNrb (右鍵開新視窗)
1
#include<stdio.h>
void minheap(int *n[1000],int size){
int i;
while(1){
    int flag=1;
    for(i=1;i<=size;i++){
        if(n[i]>n[i*2+1]){int t; t=n[i],n[i]=n[i*2+1],n[i*2+1]=t; flag=0;}
        if(n[i]>n[i*2]){int t; t=n[i],n[i]=n[i*2],n[i*2]=t; flag=0;}
    }
    if(flag) break;
}

}

int main(){
int n[1000],size=0,num,i;
for(i=0;i<1000;i++) n[i]=500;
char order;
while(scanf("%c",&order)!=EOF){
    if(order=='a'){
        scanf("%d",&num);
        n[++size]=num;
        minheap(n,size);
        printf("The min-heap is of size %d and the current minimum is %d a\n",size,n[1]);
    }else if(order=='b'){
        n[1]=n[size];
        n[size]=500;
        --size;
        minheap(n,size);
        printf("The min-heap is of size %d and the current minimum is %d b\n",size,n[1]);
    }else if(order=='c'){
        for(i=1;i<=size;i++) {
            if(i!=1) printf(" ");
            printf("%d",n[i]);
        }
    }else if(order=='d'){
        break;
    }
}

return 0;
}

2
#include<stdio.h>
int n[90000],top=0,min=1e9;
int search(int s,int sum){
int ns,nsum,i;
if(s==top-1||s==top-2) { /* printf("*%d\n",sum)*/; if(sum<=min) min=sum; return 0;}

for(i=1;i<=3;i++){
    if((s+i)<top){
        //printf("i=%d s=%d sum=%d\n",i,s,sum);
        ns=s+i,nsum=sum+n[s+i];
        //printf("          n s=%d sum=%d,top=%d\n",ns,nsum,top);
        search(ns,nsum);

    }
}
    return 0;
}

int main(){
    int tmp;
    while(scanf("%d",&tmp)&&tmp) n[top++]=tmp;
    //while(scanf("%d",&tmp)!=EOF) n[top++]=tmp;
    search(0,n[0]);
    search(1,n[1]);
    printf("%d\n",min);
    return 0;
}

3
#include<stdio.h>
#include<string.h>
int n,map[5000][5000]={0},time[5000]={0},top=0;
int max=0;
void search(int mask,int sum,int size){
    if(size>=top) return;
    if(sum>max) max=sum;
    //printf("mask=%d sum %d size %d\n",mask,sum,size);
    int i,j;
    int max=0;
    for(i=0;i<top;i++){
        int ch=-1,max=0;
        for(j=0;j<top;j++){
            if(map[i][j]!=0&&(mask&(1<<j))&&(mask&(1<<i))){
                if(time[j]>max) max=time[j],ch=j;
            }
        }
        if(ch!=-1){
            mask-=1<<i;
            mask-=1<<ch;
            //printf(" (%d,%d) ",i,ch);
            search(mask,time[ch]+time[i],++size);
        }
    }




    return ;
}

void task (char t[100]){
    //printf("task\n");
    int c=0,tt=0,tn=0;
    while(t[++c]!=','){
        tt*=10;
        tt+=t[c]-'0';
    }

    while(t[++c]){
        tn*=10;
        tn+=t[c]-'0';
    }

    time[tt-1]=tn;
    top++;
    //printf("**task=%d time=%d\n",tt,time);

}

void prec (char p[100]){
    //printf("prec\n");
    int c=0,tb=0,ta=0;
    while(p[++c]!=','){
        tb*=10;
        tb+=p[c]-'0';
    }
    c++;
    while(p[++c]){
        ta*=10;
        ta+=p[c]-'0';
    }

    map[ta-1][tb-1]=1;
    //printf("  pp %d %d\n",tb,ta);
}

int main(){
    char input[100];
    int i,j;

    while(gets(input)){
        if(strcmp(input,"end")==0) break;
        char left[100],right[100]={0};
        int n1=0,n2=0;
        while(input[n1]!=':') left[n1]=input[n1],n1++;
        left[n1++]='\0';
        while(input[n1]) right[n2++]=input[n1++];
        right[n2]='\0';

        if(strcmp(left,"Process")==0){
            int sum=0,n3=0;
            while(right[n3]) sum*=10,sum+=right[n3++]-'0';
            n=sum;
            //printf("sum=%d\n",sum);
        }else if(strcmp(left,"Task")==0){
            task(right);
        }else if(strcmp(left,"Prec")==0){
            prec(right);
        }
    }

    /*for(i=0;i<top;i++) printf("%d ",time[i]);
    printf("  top=%d\n",top);

    for(i=0;i<top;i++){
        for(j=0;j<top;j++)
            printf("%d ",map[i][j]);
        puts("");
    }*/

    int mask=~(~0<<top);
    search(mask,0,0);
    printf("%d\n",max);
    return 0;
}

4
#include<stdio.h>
char ans[100][100];
int top=0;
int map[100][100],n,min=1e9;
int star,end;

int search(int a[n],int p,int size,int mask){
    int i,c;
    mask|=1<<p;
    a[size++]=p;

    if(size>=n) return 1;
/*
printf("p=%d size=%d mark=%d\n",p,size+1,mask);
for(i=0;i<size;i++) printf("%d ",a[i]);
puts("");*/
    if(p==end) {
        if(size<min){
            min=size;
            top=0;
            c=0;
            for(i=0;i<size;i++){
                if(i!=0) ans[top][c++]=',';
                ans[top][c++]=a[i]+'0';
            }
            ans[top++][c]='\0';
        }else if(size==min){
            c=0;
            for(i=0;i<size;i++){
                if(i!=0) ans[top][c++]=',';
                ans[top][c++]=a[i]+'0';
            }
            ans[top++][c]='\0';
        }
            /*printf("ans min=%d ",min);
            for(i=0;i<size;i++) printf("%d ",a[i]);
            puts("");*/

        return 0;
    }

    for(i=0;i<n;i++){
        if(map[p][i]==1&&(mask&(1<<i))==0)
            search(a,i,size,mask);
    }

}


int main(){
    scanf("%d,%d,%d",&n,&star,&end);
    int i,j;

    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            scanf("%d",&map[i][j]);

    int a[n];
    search(a,star,0,0);

    for(i=0;i<top;i++)
        printf("%s\n",ans[i]);

    return 0;
}

5
#include<stdio.h>
int main(){
while(1){
int n[100],top=0,num;
while(scanf("%d",&num)==1){
if(num==-999999) break;
n[top++]=num;
}
int i,j,max=n[0];
for(i=0;i<top;i++){
    int r=n[i];
    for(j=i+1;j<top;j++){
        r*=n[j];
        if(r>=max) max=r;
    }
}
printf("%d\n",max);
}
return 0;
}

第21次ITSA答案參考

題目連結:http://db.tt/dbfYqNFW (右鍵開新視窗)
1
#include<stdio.h>
int main(){
int n,sum=0,i,v=0,tmp;
scanf("%d",&n);
for(i=0;i<n;i++){
    scanf("%d",&tmp);
    if(tmp>=v) v=tmp;
    else {
        sum+=v-tmp;
    }
}
printf("%d\n",sum);
return 0;
}

2
#include<stdio.h>
int main(){
int m,n,k;
scanf("%d",&m);
while(m--){
    scanf("%d %d",&n,&k);
    int mark[n],i;
    for(i=0;i<n;i++) mark[i]=1;
    int sum=n-1,p=0,tmp=k;
    while(sum){
        if(mark[p]==1){

            p++;
            if(p>=n) p-=n;

            tmp--;
        }
        else{

            p++;
            if(p>=n) p-=n;

        }
        if(tmp==0){
            if(p-1>=0)
            mark[p-1]=0;
            else mark[p-1+n]=0;
            sum--;
            tmp=k;
        }

    }
    int ans;
    for(i=0;i<n;i++) if(mark[i]!=0) ans=i;
    char name[50];
    for(i=0;i<n;i++){
        scanf("%s",name);
        if(i==ans) printf("%s\n",name);
    }

}
return 0;
}

3
#include<stdio.h>
int main(){
int x1=0,b1,b2;
char input[100];
scanf("%s",input);
scanf("%d",&b1);
scanf("%d",&b2);
int i,flag=0,no;
for(i=0;input[i];i++){
    if(input[i]>='0'&&input[i]<='9') no=input[i]-'0';
    else if(input[i]>='A'&&input[i]<='F') no=input[i]-'A'+10;
    if(no>=b1) flag=1;
    x1*=b1;
    x1+=no;
}

char ans[100];
int n=0;
while(x1){
if(x1%b2>=10) ans[n]=x1%b2-10+'A';
else ans[n]=x1%b2+'0';
n++;
x1/=b2;
}

for(i=n-1;i>=0;i--) printf("%c",ans[i]);
puts("");

return 0;
}

4
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int top=1;
while(n-->1) top*=10;
int i;
for(i=1;i<top;i++){
    int t=i,sum=0,p=1;
    while(t){
    int tmp=t%10;
    t/=10;
    sum+=tmp;
    p*=tmp;
    }
    if(sum==p) printf("%d\n",i);
}
puts("");
return 0;
}

5
#include<stdio.h>
#include<math.h>
double dist (double x,double y,double a,double b){
return sqrt((x-a)*(x-a)+(y-b)*(y-b));
}

int main(){
int n;
int  x[100],y[100];
scanf("%d",&n);
int i,j;
double min=1e9;
for(i=0;i<n;i++) scanf("%d %d",&x[i],&y[i]);



for(i=0;i<n;i++)
    for(j=i+1;j<n;j++){
        //printf("(%d,%d)-(%d,%d) %f\n",x[i],y[i],x[j],y[j],dist(x[i],y[i],x[j],y[j]));
        if(dist(x[i],y[i],x[j],y[j])<=min&&i!=j) min=dist(x[i],y[i],x[j],y[j]);
    }

printf("%.4f\n",min);


return 0;
}

2013年4月13日 星期六

物件導向期中考-所有小考程式詳解

ex0226a
class ex0226a{
	public static void main(String[] args)
	{
		String fName="John", lName="Chen";
		int weight=56;               // weight: Kg
		int height=175;              // height: cm
		int age=20;
		boolean gender=true;

		// 1cm = 0.0328 英呎
		// 1kg  = 2.21 磅

		System.out.println(" Name: "+fName+" "+ lName);
		System.out.println(" Gender: "+gen(gender));
		System.out.println(" weight: "+wei(weight)+" 磅 ");
		System.out.println(" height: "+hei(height)+" 英呎 ");
	}
	public static String gen(boolean gen)
	{
        if(gen=true) return "Man";
        else return "Female";
	}
	public static Double wei(int wei)
	{
        return wei*2.21;
	}
	public static Double hei(int hei)
	{
        return hei*0.0328;
	}



}

ex0305a
import java.io.*;

class ex0305a{
	public static void main(String[] args) throws IOException{
//
//              input a string & transfer into a integer
//   
                System.out.print(" Input a number :");
	BufferedReader br =new 
		BufferedReader(new InputStreamReader(System.in));
	String str=br.readLine();
	int fee=Integer.parseInt(str);
//
                System.out.println(" NT1000 :" +fee/1000 );
		fee%=1000;
                System.out.println(" NT500 :" + fee/500);
		fee%=500;
                System.out.println(" NT100 :" + fee/100);
		fee%=100;
                System.out.println(" NT50 :" + fee/50);
		fee%=50;
                System.out.println(" NT10 :" + fee/10);
		fee%=10;
                System.out.println(" NT5 :" + fee/5);
		fee%=5;
                System.out.println(" NT1 :" + fee/1);

	}
}
ex0312a
class ex0312a{
        public static void main(String[] args){
	int data[]={81,95,66,80,77,51,69,33,-1,46,90,87,93,72,63,55,-1,65,66,71,50,88,47,49,83,61};

//       
//     Statistic A, B, C, D, Fail & absent students number
//
//    Accumulate total score and total number (except absent students)
//
	int na=0,nb=0,nc=0,nd=0,fn=0,an=0,sum=0;
	for(int i:data){
		if(i>=0){
			sum+=i;
			if(i>=90) na++;
			else if(i>=80) nb++;
			else if(i>=70) nc++;
			else if(i>=60) nd++;
			else fn++;

		}
		else
		an++;
	}

		System.out.println(" Number of A :"+na);
		System.out.println(" Number of B :"+nb);
		System.out.println(" Number of C :"+nc);
		System.out.println(" Number of D :"+nd);
		System.out.println(" Fail Number :"+fn);
		System.out.println(" Absent Number  :"+ an);
		System.out.println(" *** Average :"+avg(sum,data.length-an));
        }
//
// sum: total score (except absent students)
//
// num: total number (except absent students)
//
	public static double avg(int sum, int num){
		double tmp;
		int tmp1;

		tmp=(double)sum/num;
		tmp1=(int)(tmp*10);
		return tmp1/10.0;
	}
}

ex0319b
class ex0319b{
	public static void main(String[] args) 
	{
		int[][] matA={{1,2,3},{4,5,6},{7,8,9}};
		int[][] matB={{1,7,6,4,3},{2,1,5,7,9},{4,3,5,1,7}};
		int[][] matC=new int[3][5];
		// Declate and allocate two dimension int array ==> matC
		int sum=0;

                                  mtrPtr(matA);
		System.out.println("=====================================");
                                  mtrPtr(matB);
                for(int i=0;i<matC.length;i++){
			for(int j=0;j<matC[i].length;j++){
				sum=0;
				for(int k=0;k<matC.length;k++)
					sum=sum+matA[i][k]*matB[k][j];
				matC[i][j]=sum;
			}
		}
		System.out.println("=====================================");
                                  mtrPtr(matC);
	}
	public static void mtrPtr(int[][] mat){
        for(int i=0;i<mat.length;i++){
			for(int j=0;j<mat[i].length;j++)
				System.out.print("  "+mat[i][j]+"\t");
			System.out.println();
		}
	}
}

ex0326b
class student{
	String name;
	int age;
	boolean gender;

	student(){};
	void setName(String nn){
		name=nn;
	}
	String getName(){
		return name;
	}

	void setAge(int nn){
		age=nn;
	}
	int getAge(){
		return age;
	}

	void setGender(boolean nn){
		gender=nn;
	}
	boolean getGender(){
		return gender;
	}
}

class ex0326b{
	public static void main(String[] args) 
	{
		student std01=new student();
		std01.setName("Mary Chen");
		std01.setAge(20);
		std01.setGender(true);
		System.out.println(" Std name: "+std01.getName());
		System.out.println(" Std age: "+std01.getAge());
		System.out.println(" Std gender: "+std01.getGender());
	}
}

ex0326d
class score{
	int[] data;
	int base=60;

	score(int[] dd){ data=dd;}
	int getHigh(){
        	int high=0;
		for(int i:data) if(i>high) high=i;
		return high;
	}
	int passNo(){
		int count=0;
		for(int i:data) if(i>=base) count++;
		return count;
	}
	float getAvg(){
		float sum=0f;
		for(int i:data) sum+=i;
		return sum/(float) data.length;
	}
}

class ex0326d{
	public static void main(String[] args) 
	{
		int[] dataA={99,88,77,66,55,44,92,56};
		int[] dataB={63,57,89,73,61,72,78,47,76,85,63,90,51,68,39};
		int[] dataC={79,78,47,96,85,54,80,66,73,52,84,73,97,60,55,70,65};
		score itmA=new score(dataA);
		score itmB=new score(dataB);
		score itmC=new score(dataC);

		System.out.println(" Highest score of itmA: "+itmA.getHigh());
		System.out.println(" Pass No of itmA: "+itmA.passNo());
		System.out.println(" Average of itmA: "+itmA.getAvg());
		System.out.println(" Highest score of itmB: "+itmB.getHigh());
		System.out.println(" Pass No of itmB: "+itmB.passNo());
		System.out.println(" Average of itmB: "+itmB.getAvg());
		System.out.println(" Highest score of itmC: "+itmC.getHigh());
		System.out.println(" Pass No of itmC: "+itmC.passNo());
		System.out.println(" Average of itmC: "+itmC.getAvg());
		System.out.println(" *Highest score of all: "+getMax(itmA.getHigh(),itmB.getHigh(),itmC.getHigh()));
		System.out.println(" *Pass No of all: "+(itmA.passNo()+itmB.passNo()+itmC.passNo()));
		System.out.println(" *Average of all: "+(itmA.getAvg()+itmB.getAvg()+itmC.getAvg())/3);
	}
	public static int getMax(int a, int b, int c){
		if(a > b)
			if( a > c) return a;
			else return c;
		else
			if(b > c) return b;
			else return c;
	}
}

ex0326x
class ex0326x{
	public static void main(String[] args) 
	{
		int[][] fee={{45,40,50,30,45},{100,95,80,90,110},
                                                     {90,85,120,110,150}};
		int[] sumDay=new int[5];
		int[] sumBld=new int[3];
		int sum=0;

		for(int i=0;i<3;i++){
			sum=0;
			for(int j=0;j<5;j++){
				sum=sum+fee[i][j];
			}
			sumBld[i]=sum;
		}
		for(int j=0;j<5;j++){
			sum=0;
			for(int i=0;i<3;i++){
				sum=sum+fee[i][j];
			}
			sumDay[j]=sum;
		}	
                                  mtrPtr(fee);
		System.out.println("  Total fee of Breakfast, Lunch and dinner: "+
                                                             sumBld[0]+"  "+sumBld[1]+"  "+sumBld[2]);
		System.out.print("  Total fee of each day: ");
		for(int df: sumDay) System.out.print("  "+df);
		System.out.println();
	}
	public static void mtrPtr(int[][] mat){
                                  for(int i=0;i<mat.length;i++){
			for(int j=0;j<mat[0].length;j++)
				System.out.print("  "+mat[i][j]+"\t");
			System.out.println();
		}
	}
}

ex0409b
class point{
	int x, y;

	point(){ 
		x=0;
		y=0;
	}
	
	point(int x, int y){
		this.x=x;
		this.y=y;
	}

	double distance(){   // Distance from point to origin 
		return (int) (Math.sqrt(x*x+y*y)*100)/100.0;	
	}
	int location(){
		if(x==0 && y==0) return 0;       // on origin
		if(x>0 && y>0) return 1;           // on 1st quadrant
		if(x<0 && y>0)  return 2;          // on 2nd quadrant
		if(x<0 && y<0)  return 3;          // on 3rd quadrant
		if(x>0 && y<0) return 4;           // on 4thquadrant
		if(y==0) return 5;                     // on X axle
		return 6;                                  // on Y axle
	}
	int disX(){     // Distance from point to X axle 
		return Math.abs(y);	
	}
	int disY(){    // Distance from point to Y axle
		return Math.abs(x);	
	}
}

class ex0409b{
	public static void main(String[] args) 
	{
		// create 3 points object;
                               // point1 ==> (0, 0), point2 ==> (4, 7), point3 ==> (-5 ,3)

		point p1 = new point();
		point p2 = new point(4,7);
		point p3 = new point(-5,3);

		System.out.println(" Location of point1: "+p1.location());
		System.out.println(" Location of ponit2: "+p2.location() );
		System.out.println(" Location of point3: "+p3.location() );

		System.out.println(" Distance of point1: "+p1.distance() );
		System.out.println(" Distance of point2: "+p2.distance() );
		System.out.println(" Distance of point3: "+p3.distance() );

		System.out.println(" Distance point2 to X : "+p2.disX() );
		System.out.println(" Distance point2 to Y : "+p2.disY() );
		System.out.println(" Distance point3 to X : "+p3.disX() );
		System.out.println(" Distance point3 to Y : "+p3.disY() );
	}
}

test1
class test1{
	public static void main(String[] args) 
	{
		int aa=-1,bb,cc,dd;

		bb=aa>>1;
		cc=aa>>>1;
		dd=aa<<1;
		System.out.println("   aa >> 1 "+ bb);
		System.out.println("   aa >>> 1 "+ cc);
		System.out.println("   aa << 1 "+ dd);
	}
}

2013年3月1日 星期五

Ma apologizes for 228



President Ma Ying-jeou apologized on behalf of the nation for the 228
Massacre yesterday, on the 66th anniversary of the tragic event.

Ma pledged at the ceremony that his administration will uphold rule of law,
protect human rights and promote cross-strait peace to prevent such a tragedy
from ever happening again.

Historians estimate that between 10-thousand and 30-thousand Taiwanese people
- many of them intellectuals - were killed by the Kuomintang in the brutal
crackdown on anti-government uprisings that began on February 28th, 1947,
shortly after the end of Japanese colonial rule.

The incident marked the beginning of the Kuomintang's White Terror period in
Taiwan, in which thousands more inhabitants vanished, died, or were
imprisoned.

馬道歉228
馬英九總統道歉,代表國家的228
大屠殺昨天,第66週年悲慘事件。

馬在儀式上承諾,他的政府堅持法治,
保護人權和促進兩岸和平,防止這樣的悲劇
再次發生。

歷史學家估計,在10萬和30萬台灣人
- 他們許多的知識分子 - 國民黨在殘酷的殺害
鎮壓反政府起義,於1947年2月28日開始,
日本殖民統治結束後不久。

該事件標誌著國民黨的白色恐怖時期
台灣,數千居民消失了,死了,或
監禁

2013年1月1日 星期二

多媒體製作

動腦遊戲3
import flash.events.MouseEvent;
import flash.ui.Mouse;


stop();
result_txt.visible=false;
/*
answerA_mc.addEventListener(MouseEvent.CLICK ,pressA);
function pressA(event:MouseEvent) {
 answerA_mc.visible=false;
 answerB_mc.visible=false;
 answerC_mc.visible=false;
 //trace("A");
}
*/

answerA_mc.addEventListener(MouseEvent.CLICK, pressA);
function pressA(me:MouseEvent){
 result_txt.visible=true;
 result_txt.text="答對!";
}

answerB_mc.addEventListener(MouseEvent.CLICK, pressB);
function pressB(me:MouseEvent){
 result_txt.text="答錯!";
 result_txt.visible=true;
}
answerC_mc.addEventListener(MouseEvent.CLICK, pressC);
function pressC(me:MouseEvent){
 result_txt.text="答錯!";
 result_txt.visible=true;
}
遙控直升機遊戲
/*right_btn.addEventListener(MouseEvent.MOUSE_OVER, plane_right);
function plane_right(me:MouseEvent){
 plane_mc.x += 5 ;
}

left_btn.addEventListener(MouseEvent.CLICK, plane_left);
function plane_left(me:MouseEvent){
 plane_mc.x -= 5 ;
}
up_btn.addEventListener(MouseEvent.CLICK, plane_up);
function plane_up(me:MouseEvent){
 plane_mc.y -= 5 ;
}

down_btn.addEventListener(MouseEvent.CLICK, plane_down);
function plane_down(me:MouseEvent){
 plane_mc.y += 5 ;
}
*/

//定義四個方向速度
var left:Number=0;
var right:Number=0;
var up:Number=0;
var down:Number=0;

//定義速度speed
var speed:Number=10;


up_btn.addEventListener(MouseEvent.MOUSE_OVER, upOver);
function upOver(me:MouseEvent){
 up = -speed;
}
up_btn.addEventListener(MouseEvent.MOUSE_OUT, upOUT);
function upOUT(me:MouseEvent){
 up = 0;
}
down_btn.addEventListener(MouseEvent.MOUSE_OVER, downOver);
function downOver(me:MouseEvent){
 down = speed;
}
down_btn.addEventListener(MouseEvent.MOUSE_OUT, downOUT);
function downOUT(me:MouseEvent){
 down = 0;
}
left_btn.addEventListener(MouseEvent.MOUSE_OVER, leftOver);
function leftOver(me:MouseEvent){
 left = -speed;
 plane_mc.rotation = -10;
}
left_btn.addEventListener(MouseEvent.MOUSE_OUT, leftOUT);
function leftOUT(me:MouseEvent){
 left = 0;
 plane_mc.rotation = 0;
}
right_btn.addEventListener(MouseEvent.MOUSE_OVER, rightOver);
function rightOver(me:MouseEvent){
 right = speed;
 plane_mc.rotation = 10;
}
right_btn.addEventListener(MouseEvent.MOUSE_OUT, rightOUT);
function rightOUT(me:MouseEvent){
 right = 0;
 plane_mc.rotation = 0;
}

//不停更新飛機的四個方向速度
this.addEventListener(Event.ENTER_FRAME, EnterFrame);
function EnterFrame(me:Event){
 plane_mc.x += right;
 plane_mc.x += left;
 plane_mc.y += up;
 plane_mc.y += down;
 
 //邊界處理
if (plane_mc.x > stage.stageWidth){
 plane_mc.x = 0;
}
if (plane_mc.x < 0){
 plane_mc.x = stage.stageWidth;
}

if (plane_mc.y > stage.stageHeight){
 plane_mc.y = 0;
}
if (plane_mc.y < 0){
 plane_mc.y = stage.stageHeight;
}
 
}

3.3水果選單
chk_btn.addEventListener(MouseEvent.CLICK, chk);
function chk(me:MouseEvent){
 //trace(input_txt.text);
 switch (input_txt.text.toLowerCase()) {
  case "a":
    result_txt.text = "您選Apple";
    break;
  case "b":
    result_txt.text = "您選Banana";
    break;
  case "c":
    result_txt.text = "您選Coconut";
    break;
  default:
    result_txt.text = "您選的不在選單內";
 }
}
3.3判斷奇偶數
check_btn.addEventListener(MouseEvent.CLICK , chk);
function chk(me:MouseEvent){
 if (int(input_txt.text) % 2 == 0) {
  result_txt.text = "偶數";
 }else{
  result_txt.text = "奇數";  
 }
}
4.1播放停止
this.stop();

// For Scene
play_btn.addEventListener(MouseEvent.CLICK, man_move);
function man_move(me:MouseEvent){
 this.play();
}

stop_btn.addEventListener(MouseEvent.CLICK, man_stop);
function man_stop(me:MouseEvent){
 this.stop();
}

// For man_mc
play2_btn.addEventListener(MouseEvent.CLICK, man_move2);
function man_move2(me:MouseEvent){
 man_mc.play();
}

stop2_btn.addEventListener(MouseEvent.CLICK, man_stop2);
function man_stop2(me:MouseEvent){
 man_mc.stop();
}
4.3地球停止
globe_mc.stop();

globe_mc.addEventListener(MouseEvent.MOUSE_OVER, mouse_in);

function mouse_in(me:MouseEvent){
 globe_mc.play();
}

globe_mc.addEventListener(MouseEvent.MOUSE_OUT, mouse_out);

function mouse_out(me:MouseEvent){
 globe_mc.stop();
}
4.3利用按鈕切換
stop();

right_btn.addEventListener(MouseEvent.CLICK, nextF);
function nextF(me:MouseEvent){
 if(this.currentFrame >= 5){
  this.gotoAndStop(1);
 }else{
  this.nextFrame();
 }
}

left_btn.addEventListener(MouseEvent.CLICK, prevF);
function prevF(me:MouseEvent){
 if(this.currentFrame <= 1){
  this.gotoAndStop(5);
 }else{
  this.prevFrame();
 }
}
4.3影格切換
stop();
a_mc.stop();

next_btn.addEventListener(MouseEvent.CLICK, nextF);
function nextF(me:MouseEvent){
 if (a_mc.currentFrame < 5){
  a_mc.nextFrame();
 } else{
  a_mc.gotoAndStop(1);
 }
}

prev_btn.addEventListener(MouseEvent.CLICK, prevF);
function prevF(me:MouseEvent){
 if(a_mc.currentFrame > 1){
  a_mc.prevFrame();
 }else{
  a_mc.gotoAndStop(5);
 }
}

4.4跳場景停止
this.stop();

a_btn.addEventListener(MouseEvent.CLICK, wrong);
function wrong(me:MouseEvent){
 this.gotoAndStop(2);
}

b_btn.addEventListener(MouseEvent.CLICK, right);
function right(me:MouseEvent){
 this.gotoAndStop(3);
}
4.5切換5照片
//Scene 1
stop();

next_btn.addEventListener(MouseEvent.CLICK, next_scene);

function next_scene(me:MouseEvent){
 nextScene();
}

function back_scene(me:MouseEvent){
 prevScene();
}

//Scene 2-4
stop();

next_btn.addEventListener(MouseEvent.CLICK, next_scene);
back_btn.addEventListener(MouseEvent.CLICK, back_scene);

//Scene 5
stop();

back_btn.addEventListener(MouseEvent.CLICK, back_scene);
4.5選擇播放
stop(); //影格15及35也須停止

a_btn.addEventListener(MouseEvent.CLICK, playA);
function playA(me:MouseEvent){
 this.gotoAndPlay("聖誕老人");
}

b_btn.addEventListener(MouseEvent.CLICK, playB);
function playB(me:MouseEvent){
 this.gotoAndPlay("聖誕樹");
}
4.7延遲播放
stop();

var timer=setTimeout(startPlay, 3000);
function startPlay(){
 play();
 clearTimeout(timer);  
}
4.8控制影片播放速度
wheel_mc.stop();

var actMode;
var Rot=0;
var playStep=0;
var Pn=1;

drag_btn.addEventListener(MouseEvent.MOUSE_DOWN, chgYes);
function chgYes(me:MouseEvent){
 actMode = "yes";
}


drag_btn.addEventListener(MouseEvent.MOUSE_UP, chgNo);
function chgNo(me:MouseEvent){
 actMode = "no";
}

jog_mc.addEventListener(MouseEvent.MOUSE_OVER, chkRot);
function chkRot(me:MouseEvent){
 if(actMode == "yes"){
  Rot = Math.atan2(jog_mc.mouseX, -jog_mc.mouseY) * 180 / Math.PI;
  drag_btn.rotation = Rot;
 }
}

this.addEventListener(Event.ENTER_FRAME, cirPlay);
function cirPlay(me:Event){
 playStep = Math.floor(Math.abs(Rot)/30);
 if (playStep>5){
  playStep = 5;
 }
 
 Pn = 1;
 if (Rot < 0){
  Pn = -1;
 }
 
 for (var i=1; i<= playStep; i++) {
  if (Pn > 0) {
   wheel_mc.nextFrame();
   if (wheel_mc.currentFrame == wheel_mc.totalFrames){
    wheel_mc.gotoAndPlay(1);
   }
  }else{
   wheel_mc.prevFrame();
   if (wheel_mc.currentFrame == 1){
    wheel_mc.gotoAndPlay(wheel_mc.totalFrames);
   }
  }
 } 

 fps_txt.text = String(playStep * 12 * Pn);

}



4.9動態控制SWF
fullscreen_btn.addEventListener(MouseEvent.CLICK, fullscreen);
function fullscreen(me:MouseEvent){
 fscommand("fullscreen","true");
}

quit_btn.addEventListener(MouseEvent.CLICK, quit);
function quit(me:MouseEvent){
 fscommand("quit");
}

menu_btn.addEventListener(MouseEvent.CLICK, menu);
function menu(me:MouseEvent){
 //stage.showDefaultContextMenu = false;
 stage.showDefaultContextMenu = !stage.showDefaultContextMenu;
}

5.1動態加入
stage.addEventListener(MouseEvent.CLICK, copyMC);

function copyMC(me:MouseEvent) {
 var tigerCopy_mc:tiger = new tiger();
 stage.addChild(tigerCopy_mc);
 tigerCopy_mc.x=this.mouseX;
 tigerCopy_mc.y=this.mouseY;

}
5.2動態移除
var addNum:int = 10; //斑馬數量
var basicIndex: Number = this.numChildren -1;

sh_mc.startDrag(true);

//建立舞台上斑馬
for(var i:int=0; i < addNum; i++){
 var horseCopy:horse = new horse();
 horseCopy.x = 500 * Math.random();
 horseCopy.y = 350 * Math.random(); 
 horseCopy.scaleX = horseCopy.scaleY = Math.floor(Math.random() * 10) / 10; 
 this.addChild(horseCopy);
}

//依滑鼠點選移除斑馬
stage.addEventListener(MouseEvent.CLICK, delMc);
function delMc(me:MouseEvent){
 if( basicIndex < this.getChildIndex(me.target)){
  this.removeChild(me.target);
 }
}
5.3反彈球
var speed:Number = 10;
var speed_x:Number = speed;
var speed_y:Number = speed;

stage.addEventListener(Event.ENTER_FRAME, gogo);
function gogo(me:Event){
 with (ball_mc){
  x += speed_x;
  y += speed_y;
  
  if (x>stage.stageWidth){
   x = stage.stageWidth;
   speed_x = -speed;
  }else if ( x < 0){
   x= 0;
   speed_x = speed;
  }

  if (y>stage.stageHeight){
   y = stage.stageHeight;
   speed_y = -speed;
  }else if ( y < 0){
   y= 0;
   speed_y = speed;
  }

  
 }
}


5.5顏色轉換
var color_arr:Array = [0xFF0000, 0xFFFF00, 0x00FFFF, 0x0000FF];

var listNum:int = color_arr.length;

var trans:Transform = new Transform(color_mc);
var my_color:ColorTransform = new ColorTransform();

setInterval(chgcolor, 2000);

function chgcolor(){
 var num:int = Math.floor(Math.random() * listNum);
 my_color.color = color_arr[num];
 trans.colorTransform = my_color;
 
}

5.6衣服變色
var color_array=[0xFF0000,0x00FF00,0x0000FF,0x00FFFF,0xFFFF00,0xFF00FF,0x9900FF,0xCCCCFF,0xFF9900,0xCC9900,0x3399CC];

a_btn.addEventListener(MouseEvent.CLICK, chgcolor);
b_btn.addEventListener(MouseEvent.CLICK, chgcolor);
c_btn.addEventListener(MouseEvent.CLICK, chgcolor);
d_btn.addEventListener(MouseEvent.CLICK, chgcolor);
e_btn.addEventListener(MouseEvent.CLICK, chgcolor);
f_btn.addEventListener(MouseEvent.CLICK, chgcolor);
g_btn.addEventListener(MouseEvent.CLICK, chgcolor);
h_btn.addEventListener(MouseEvent.CLICK, chgcolor);
i_btn.addEventListener(MouseEvent.CLICK, chgcolor);
j_btn.addEventListener(MouseEvent.CLICK, chgcolor);
k_btn.addEventListener(MouseEvent.CLICK, chgcolor);


function chgcolor(me:MouseEvent){
 var my_color:ColorTransform = new ColorTransform();
 
 switch (me.target.name) {
  case "a_btn":
   my_color.color = color_array[0];
   break;
  case "b_btn":
   my_color.color = color_array[1];
   break;
  case "c_btn":
   my_color.color = color_array[2];
   break;
  case "d_btn":
   my_color.color = color_array[3];
   break;
  case "e_btn":
   my_color.color = color_array[4];
   break;
  case "f_btn":
   my_color.color = color_array[5];
   break;
  case "g_btn":
   my_color.color = color_array[6];
   break;
  case "h_btn":
   my_color.color = color_array[7];
   break;
  case "i_btn":
   my_color.color = color_array[8];
   break;
  case "j_btn":
   my_color.color = color_array[9];
   break;
  case "k_btn":
   my_color.color = color_array[10];
   break;
 }
  
 cloth_mc.transform.colorTransform = my_color;
 
}


5.7 廣告位移
stop();
var speed:Number = 0;

stage.addEventListener(Event.ENTER_FRAME, go);
function go(me:Event){
 speed = (stage.stageWidth/2 - this.mouseX) / 10; 
 movie_mc.x += speed;
 
 if(movie_mc.x < 0 || movie_mc.x > 850){
  movie_mc.x = 850;
 }
 
}
5.8顫慄的男孩
var strong:Number = 10;

var initX:Number = realboy_mc.x;
var initY:Number = realboy_mc.y;

shakeboy_mc.x = initX;
shakeboy_mc.y = initY;

stage.addEventListener (Event.ENTER_FRAME, goshake);

function goshake(me:Event){
 shakeboy_mc.x = initX + shake(); 
 shakeboy_mc.y = initY + shake(); 
}

function shake(){
 return (Math.floor(Math.random()*strong) - strong/2 ); 
}
5.9 雪花飄
// 場景1的影格1
var snowNum:Number = 30;

for (var i:int=0; i<snowNum; i++){
 var mysnow:snow = new snow();
 this.addChild(mysnow);
}

// 雪花元件的影格1
this.x = Math.random() * stage.stageWidth;
this.y = Math.random() * stage.stageHeight;

// 雪花元件的影格2
this.y += Math.random() * 5 + 1; 
this.x += Math.random() * 5 - 2.5; 

// 雪花元件的影格3
if (this.y> stage.stageHeight){
 //this.y = 0;
 gotoAndPlay(1);
} else{
 gotoAndPlay(2);
}

5.10 遮罩開場
basic_mc.visible = false;
mask_mc.visible = false;
mask_mc.stop();

stage.addEventListener(MouseEvent.CLICK, smask);

function smask(me:MouseEvent){
 start_txt.visible =false;
 basic_mc.visible = true;
 mask_mc.visible = true;
 basic_mc.mask = mask_mc;
 mask_mc.play();
}

2013編譯娘

21
#include<stdio.h>
#include "stoprun.c"
int dx(int x, int a[], int n){
int sum=0,i,j,r=1;
for(i=n-2,j=1;i>=0;i--,j++){
sum+=a[i]*j*r;
r*=x;
}
return sum;
}
main(int argc, char *argv[]){
 stoprun();
 int x, n;
 for(;;){
  if(scanf("%d%d", &x, &n)<0) break;
  int a[n],i;
for(i=0;i<n;i++) scanf("%d",&a[i]);
  printf("%d\n", dx(x,a,n));
 }
}
22
#include<stdio.h>
#include "stoprun.c"
main(int argc, char *argv[]){
 stoprun();
int n,i,sum=0,score;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&score);
sum+=score;
}
printf("Total score is %d\n",sum);
printf("Float average is %f\n",sum*1.0/n);
printf("Integer average is %d\n",sum/n);
sum=(sum*1.0/n*10+5)/10;
printf("Rounded Integer average is %d\n",sum);
}
23
#include<stdio.h>
#include "stoprun.c"
int getprime(int,int[]);
main(int argc, char *argv[]){
 stoprun();
 int n;
 int prime[200],x;
 scanf("%d", &n);
 int no=getprime(n,prime);
 for(x=0;x<no;++x) {
  printf("prime[%d]=%d\n",x,prime[x]);
 }
}
int getprime(int num, int p[]) {
int i,j,m,nc=0;
for(i=2;i<=num;i++){
for(j=2,m=1;j<i;j++)
if(i%j==0) m=0;
if(m) p[nc++]=i;
}
return nc;
}
24
#include<stdio.h>
#include "stoprun.c"
void int2decimal(int,char[]);
main(int argc, char *argv[]){
 stoprun();
 int number;
 char decimal[32];
 for(;;) {
  if(scanf("%d", &number)<0) break;
  int2decimal(number,decimal);
  printf("Decimal(%d)=\"%s\"\n",number,decimal);
 }
}
void int2decimal(int n,char d[]){
int i=0,j;
if(n==0) d[i++]='0';
while(n){
if(i%4==3)d[i++]=',';
d[i]=n%10+'0',n/=10;
i++;
}
d[i]='\0';

for(--i,j=0;j<i;j++,i--){
int t;
t=d[i],d[i]=d[j],d[j]=t;
}
}
25
#include<stdio.h>
#include "stoprun.c"
void calendar(int fday,int days)
{
printf("| S  M  T  W  T  F  S|\n");
printf("|--------------------|\n|");
int i;
for(i=0;i<fday+days;i++){
if(i%7==0&&i!=0) printf("|\n|");
else if(i!=0) printf(" ");
if(i<fday) printf("  ");
else printf("%2d",i-fday+1);
}
while(i%7!=0) printf("   "),i++;
printf("|\n|--------------------|\n");
}
main(int argc, char *argv[]){
 stoprun();
 int year, month, week;
 scanf("%d%d%d", &year, &month, &week);
int m[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(year%4==0&&year%100!=0||year%400==0) m[1]=29;
int days=m[month-1];
year-=1911;
 printf("|*******%03d-%02d*******|\n", year, month);
 calendar(week, days);
}