1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<stdio.h> void triangel( int num){ int i,j,space=num-1,x; for (i=1 ; i<=num ; i++,space--){ for (x=1;x<=space;x++) printf ( " " ); for (j=1 ; j<=(-1+2*i) ; j++) printf ( "*" ); printf ( "\n" ); } for (i=1;i<=num;i++){ for (x=1;x<=num-1;x++) printf ( " " ); printf ( "*\n" ); } for (i=1;i<=num-1-5;i++){ printf ( " " ); } printf ( "Merry Christmas!" ); } int main( void ){ triangel(10); return 0; } |
2011年12月25日 星期日
100設計一甲_Merry Christmas
Merry Christmas tree.c
2011年12月20日 星期二
100設計一甲_week15
libw15.c
ex1.py
123.c complier出來的dll重新命名為->sum.pyd
ex2.py
w15.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <stdio.h> #include <stdlib.h> int add( int a, int b, int c) { return a+b+c; } double add2( double a, double b, double c) { return a+b+c; } int mul( int a, int b, int c) { return a*b*c; } |
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 | #coding: utf-8 # 本程式透過 ctype 模組, 直接導入 dll 動態連結庫, 並使用其函式 from ctypes import cdll,c_int,c_double lib = "libw15.dll" dll = cdll.LoadLibrary(lib) #add = (lambda x,y: dll.add(c_int(x), c_int(y))) # 以上 lambda 函式定義與下列函式相同 def add(x,y,z): return dll.add(c_int(x), c_int(y), c_int(z)) ''' ctypes 介面僅支援整數, 長整數與字串資料型別, 若使用(雙)浮點數或其他 ctype 所不支援的 資料型別, 則必須要明確告訴 ctypes 如何進行函式傳回值的轉換, 否則無法得到精確資料 ''' def add2(x,y,z): return dll.add2(c_double(x), c_double(y), c_double(z)) # 指定 add2 函式的資料傳回型別為 c_double dll.add2.restype = c_double multiply = ( lambda x,y,z: dll.mul(c_int(x), c_int(y), c_int(z))) 變數 1 = 12 變數 2 = 10 變數 3 = 15 print ( "變數1加上變數2加上變數3, 其值為:" ,add(變數 1 ,變數 2 ,變數 3 )) print ( "變數1乘上變數2乘上變數3, 其值為:" ,multiply(變數 1 ,變數 2 ,變數 3 )) ''' 15*12 = 15*2+15*10 = 150+30 = 180 15*12 = 15*2*6 = 180 ''' 變數 4 = 12.345 變數 5 = 10.6 變數 6 = 66.6 print ( "變數4加上變數5加上變數6, 其值為:" ,add2(變數 4 ,變數 5 ,變數 6 )) |
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 | #include <Python.h> int sum2( int a, int b, int c); int sum2( int a, int b, int c){ return a+b+c; } static PyObject* mod_sum(PyObject *self, PyObject *args) { int a; int b; int c; int s; if (!PyArg_ParseTuple(args, "iii" ,&a,&b,&c)) return NULL; s = sum2(a,b,c); return Py_BuildValue( "i" ,s); } static struct PyMethodDef Mod_Methods[] = { { "sum" , mod_sum, METH_VARARGS, "Description.." }, {NULL,NULL,0,NULL} }; static struct PyModuleDef ModMethods = { PyModuleDef_HEAD_INIT, "SumModule" , "SumModule_doc" , -1, Mod_Methods }; PyMODINIT_FUNC PyInit_sum( void ) { ( void ) PyModule_Create(&ModMethods); } |
1 2 3 4 5 6 7 8 | ''' from sum import * print(sum(10,20)) ''' import sum print ( sum . sum ( 10 , 20 , 30 )) |
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 | #include<stdio.h> #define pi 3.14159 double circlearea( double r){ return r*r*pi; } int plus( int a, int b, int c){ return a*b-c; } float bmi( float heigh, float weight){ return weight/(heigh*heigh); } char *mystring( char str[]){ return str; //printf("%s",str); } int main( void ) { printf ( "%e\n" ,circlearea(1.)); printf ( "%d\n" ,plus(1.5,6,5)); printf ( "%d\n" ,plus(1,6,5)); printf ( "%f\n" ,bmi(1.8,60)); printf ( "%s\n" ,mystring( "asd" )); mystring( "asd" ); return 0; } |
2011年12月6日 星期二
100設計一甲_week13
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 | #include<stdio.h> void fun1(){ int i=0; printf ( "while test1----\n" ); while (i<15){ printf ( "%d\n" ,i); i++; } i=15; printf ( "while test2----\n" ); while (i){ printf ( "%d\n" ,i); i--; } printf ( "for test1----\n" ); for (i=0;i<15;i++){ printf ( "%d\n" ,i); } } int main(){ int num1 = 123; float num2 = 3.14159; double num3 = 3.14159e-3; char name1[6] = { 'h' , 'e' , 'l' , 'l' , 'o' , '\0' }; char name2[6] = "hello" ; /* 字串最後必須加上結束\0字元,代表字串結束 hello 為五個字元 但最後必須加上\0,所以為六個字元。 */ printf ( "%d\n" , num1); printf ( "%f\n" , num2); printf ( "%e\n" , num3); printf ( "%s\n" , name1); printf ( "%s\n" , name2); printf ( "%c\n" , name1[1]); printf ( "%c\n" , name2[1]); fun1(); return 0; } |
2011年11月29日 星期二
100設計一甲_week12 小考答案
exam1.c
exam2.py
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 | #include<stdio.h> void q1(){ int i; int num1=0,num3=0; int total=0; for (i=399;i<=8993;i++){ num1=i%10; num3=(i/100)%10; if ((num1+num3)==11){ total+=i; } } printf ( "%d\n" ,total); } void q2(){ int i; int num1=0,num3=0; int total=0; for (i=359;i<=8691;i++){ if (i%7==0 && i%2==0){ total+=i; } } printf ( "%d\n" ,total); } void q3(){ int i; int num1=0,num3=0; int total=0; for (i=95;i<=8505;i++){ if (i%14!=0){ total+=i; } } printf ( "%d\n" ,total); } void q4(){ int i=0,j=0; int total=0; for (i=473;i<=8393;i++){ int count=0; j=i; while (j!=0 && count==0){ if (j%10==3||j%10==6||j%10==8) count++; j=j/10; } if (count!=0) total+=i; } printf ( "%d\n" ,total); } void q5(){ int i; int num1=0,num3=0; int total=0; for (i=486;i<=8817;i++){ num1=i%10; num3=(i/100)%10; if ((num1*num3)==16) total+=i; } printf ( "%d\n" ,total); } int main(){ q1(); q2(); q3(); q4(); q5(); return 0; } |
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 | def q1(): total = 0 ; for i in range ( 399 , 8993 + 1 ): num1 = i % 10 ; num3 = int (i / 100 ) % 10 ; if ((num1 + num3) = = 11 ): total + = i; print (total) def q2(): total = 0 for i in range ( 359 , 8691 + 1 ): if (i % 7 = = 0 and i % 2 = = 0 ): total + = i print (total) def q3(): total = 0 for i in range ( 95 , 8505 + 1 ): if (i % 14 ! = 0 ): total + = i print (total) def q4(): total = 0 for 索引 in range ( 473 , 8393 + 1 ): if str (索引).find( "3" ) ! = - 1 or str (索引).find( "6" ) ! = - 1 or str (索引).find( "8" ) ! = - 1 : total + = 索引 print (total) def q5(): total = 0 ; for i in range ( 486 , 8817 + 1 ): num1 = i % 10 ; num3 = int (i / 100 ) % 10 ; if ((num1 * num3) = = 16 ): total + = i; print (total) q1() q2() q3() q4() q5() |
100設計一甲_week12
example1.c
Python Module in C 動態教學(Save as...)
Python_module.7z
tcc_gnuplot3.7z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include<stdio.h> int main(){ int num1 = 123; float num2 = 3.14159; double num3 = 3.14159e-3; char name1[6] = "hello" ; char name2[6]={ 'h' , 'e' , 'l' , 'l' , 'o' , '\0' }; /* 字串最後必須加上結束\0字元,代表字串結束 hello 為五個字元 但最後必須加上\0,所以為六個字元。 */ printf ( "%d\n" , num1); printf ( "%f\n" , num2); printf ( "%e\n" , num3); printf ( "%s\n" , name1); printf ( "%s\n" , name2); printf ( "%c\n" , name1[1]); printf ( "%c\n" , name2[1]); return 0; } |
Python Module in C 動態教學(Save as...)
Python_module.7z
tcc_gnuplot3.7z
2011年11月22日 星期二
100設計一甲_week11
example1.c
example2.c
example3.c
1 2 3 4 5 | #include<stdio.h> int main(){ printf ( "hello c!\n" ); return 0; } |
1 2 3 4 5 6 7 8 9 | #include<stdio.h> int main(){ printf ( "hello c!\n" ); int i=0; for (i=0;i<10;i++){ printf ( "%d\n" ,i); } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include<stdio.h> int main(){ int i=0; for (i=0;i<=100;i++){ if (i<=40){ printf ( "C score:%d\n" ,i); } else if (i<=70){ printf ( "B score:%d\n" ,i); } else { printf ( "A score:%d\n" ,i); } } return 0; } /* 班級從1到最後一號 假如號碼是偶數 後面請加even 假如號碼是奇數 後面請加odd 依次序印出 */ |
2011年11月15日 星期二
100設計一甲_week10
tkinter.py
find.py
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 | #coding: utf-8 from tkinter import * # 導入所有的數學函式 from math import * # 導入數學函式後, 圓周率為 pi # deg 為角度轉為徑度的轉換因子 deg = pi / 180. frame = Frame() frame.master.title( "繪圖範例" ) frame.pack() canvas = Canvas(width = 800 , height = 800 , bg = 'white' ) canvas.pack(expand = YES, fill = BOTH) def plotline(): for i in range ( 0 , 500 , 50 ): canvas.create_line( 0 , i, 500 , i) #canvas.create_line(i, 0, i, 500) def polygon(r = 100 ,cx = 150 ,cy = 150 ,offset = 30 ): for i in range ( 270 , 270 + 360 ,offset): x0 = cx + r * cos(i * deg) y0 = cy + r * sin(i * deg) x1 = cx + r * cos((i + offset) * deg) y1 = cy + r * sin((i + offset) * deg) canvas.create_line(x0,y0,x1,y1) def plot(): for i in range ( 0 , 600 , 30 ): canvas.create_line( 400 - i, 350 - i, 500 + i, 350 - i) canvas.create_line( 500 + i, 350 - i, 500 + i, 450 + i) canvas.create_line( 500 + i, 450 + i, 400 - i, 450 + i) canvas.create_line( 400 - i, 450 + i, 400 - i, 350 - i) plotline() polygon() mainloop() |
1 2 3 4 5 6 7 8 9 10 11 12 | s = "applenkajkljkhapplejgaa" pos = 0 count = 0 while ( 1 ): pos = s.find( "apple" ,pos) if (pos = = - 1 ): break pos = pos + 1 count = count + 1 print (count) print (s.count( "apple" )) |
2011年11月8日 星期二
100設計一甲_week7
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 | #Q1 #輸入一字串10字元將字串內容中的英文,小轉大寫,其餘照常印出。 def q1(): string = input ( "輸入一字串10字元:" ) #輸入字串 for i in range ( 0 , len (string)): #len()測量字串長度 #判斷字元是否在需轉換範圍內 if ( ord ( 'A' )< = ord (string[i])< = ord ( 'Z' )): #ord 將字元轉ascii dec print ( chr ( ord (string[i]) + 32 ),end = "") #chr 將整數轉成字元 #其餘正常印出 else : print (string[i],end = "") #Q2 #你拿到一個整數(4位數),卻忍不住想把每個位數都乘在一起。 def q2(): total = 1 num = input ( "輸入數字 n:" ) for i in range ( 0 , len (num)): total * = int (num[i]) #轉成整數 print (total) #Q3 #輸入身高(cm)體重(kg),求BMI值並依照其BMI值印出體重分級與BMI,BMI需印至小數第一位。 def q3(): weight = float ( input ( "輸入體重(kg)" )) heigh = float ( input ( "輸入身高(cm)" )) / 100 #cm轉m 除100 BMI = weight / (heigh * heigh) if (BMI < 18.5 ): #判斷BMI之範圍 print ( "%.1f 體重過輕" % (BMI)) elif ( 18.5 < = BMI < 24 ): print ( "%.1f 正常範圍" % (BMI)) elif ( 24 < = BMI < 27 ): print ( "%.1f 過 重" % (BMI)) elif ( 27 < = BMI < 30 ): print ( "%.1f 輕度肥胖" % (BMI)) elif ( 30 < = BMI < 35 ): print ( "%.1f 中度肥胖" % (BMI)) else : print ( "%.1f 重度肥胖" % (BMI)) #Q4 #由1到100,印出其中只含有3,不含有4,6,9的數值。 def q4(): for 索引 in range ( 1 , 101 ): if str (索引).find( "3" ) ! = - 1 : if str (索引).find( "4" ) = = - 1 and str (索引).find( "6" ) = = - 1 and str (索引).find( "9" ) = = - 1 : #find()找尋字串中是否有符合的字元 如果沒有則為-1 有則為符合字元的位置,底下q4find()函式為測試find的使用例子。 print (索引) #Q5 #平方公尺轉坪數,輸入幾平方公尺,輸出幾坪。 #公式:坪數 = 平方公尺 * 0.3025 (固定) def q5(): 平方公尺 = float ( input ( "輸入幾平方公尺:" )) #輸入可能帶有小數,因此轉為浮點數。 print (平方公尺 * 0.3025 ) def q4find(): for i in [ "taiwan" , "people" ]: print (i.find( "w" )) |
2011年10月29日 星期六
100設計一甲_week6_模擬考答案
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 | #1. def q1(): a = input ( "姓名" ) b = input ( "學號" ) c = input ( "班級" ) print (a,b,c) #2. def q2(): i = 1 while (i< 301 ): print (i) i + = 1 for i in range ( 1 , 301 ): print (i) #3. def q3(): sum = 0 for i in range ( 1 , 301 ): sum = sum + i print ( sum ) #4. # -*- coding: utf-8 -*- def q4(): name = input ( "Please enter a character string: " ) print ( "The string capitalized is " , end = "") for i in range ( 0 , 6 ): dec = int ( ord (name[i])) newstr = chr (dec - 32 ) print (newstr,end = "") print () #5. def q5(R): print (R * R * 3.14159265 ) |
2011年10月25日 星期二
100設計一甲_week6_模擬考
1.使用input()函式輸入三個變數值(姓名 學號 班級)並輸出(Hint : input使用)
2.使用while以及for 各自印出從1到300的各個整數
3.用迴圈,計算1~300的總合(Hint : for while方式不限)
4.輸入6個字元的字串(英文小寫)轉成大寫輸出( Hint : input(),ord(),chr() )
5.使用自訂函式,計算圓面積,輸入R(半徑),R必須為函式之引數,試求面積(Hint : 需使用自訂函式,pi = 3.14159265)
2.使用while以及for 各自印出從1到300的各個整數
3.用迴圈,計算1~300的總合(Hint : for while方式不限)
4.輸入6個字元的字串(英文小寫)轉成大寫輸出( Hint : input(),ord(),chr() )
5.使用自訂函式,計算圓面積,輸入R(半徑),R必須為函式之引數,試求面積(Hint : 需使用自訂函式,pi = 3.14159265)
2011年10月19日 星期三
100設計一甲_week5
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 | #print()<---在python 3裡print是一個函數 所以必須加上括號 與之前python版本print不同 #input()<---輸入,輸入進來的型態為str #str()<---將()內轉換成字串 def ex1(): yourname = input ( "Please input your English name:" ) for i in range ( 0 , 4 ): print (yourname[i] + " ASCII value is " + str ( ord (yourname[i]))) #輸入4字元的字串 #ord()轉換()內字元所對應的ascii瑪 #chr()轉換()內數字所對應的字元<---須為int def ex2(): var = input ( "Please enter a four character string:" ) print ( "The string capitalized is " ,end = "") for i in range ( 0 , 4 ): print ( chr ( ord (var[i]) - 32 ),end = "") print () #mpg mile per gallon #求每一加侖所跑的里程 #輸入miledorve、gallon #miledorve/gallon=每加侖所跑的miles def ex3(): miledrove = float ( input ( "Please enter the miles you drove:" )) gallons = float ( input ("Please enter the gallons \ of gas you put in the tank:")) print ( "You got" , str (miledrove / gallons), "mpg on that tank of gas." ) #美元與外國幣值稅換 #以美國為主所以要查美國相對於各國的匯率 #rate此函式中代表匯率 #結果應該是 所要稅換的錢幣*匯率 def ex4(): dollars = float ( input ("What is the amount of US \ Dollars you wish to convert? ")) rate = float ( input ("What is the current exchange \ rate ( 1 ~US Dollar equals what in the Foreign Currency)? ")) print ( "The amount in the Foreign Currency is $%.2f" % (dollars * rate)) #以list來做9*9乘法表 #必須清楚for迴圈內的動作,變數的變動 #了解如何存取list內的內容 def ex5(): N = list ( range ( 1 , 10 )) #[1,2...9] X = list ( range ( 1 , 10 )) #[1,2...9] for i in N: for y in X: print (i, "*" ,y, "=" ,i * y,end = "\t" ) print () def ex6(): N = list ( range ( 20 , 31 )) #[20,21,22...30] X = list ( range ( 20 , 31 )) #[20,21,22...30] for i in N: for y in X: print ( "%3d * %3d =%4d" % (i,y,i * y),end = "\t" ) print () #店員找錢 #使用while迴圈 應找的錢(change) dollars(各種錢幣) pay(應找的硬幣數) #每一次迴圈 把當時所要找的錢除目前使用的硬幣 得到其商 就是這種硬幣要找的數量 #找完一次,必須要把找的錢與已給的錢(硬幣直*找的硬幣數量)相減 #迴圈直到要找的錢為0時,結束迴圈 def pay(): change = int ( input ( "input how much you should pay:" )) dollars = [ 100 , 50 , 10 , 5 , 1 ] pay = [ 0 , 0 , 0 , 0 , 0 ] i = 0 while (change! = 0 ): coin = change / / dollars[i] pay[i] = coin change = change - coin * dollars[i] i + = 1 print (pay) for num in range ( 0 , 5 ): print ( "%3d : %d 張" % (dollars[num],pay[num])) pay() |
2011年10月11日 星期二
100設計一甲_week4
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #有關進位 #98436145310十進位 #0123456789ABCDEF十六進位 #0101101010二進位 #print(0b01101100) #print(hex(108)) #print(0x6c) #print(bin(108)) #print(bin(108)) def eight_bit(mystr): return ( int (mystr[ 0 ]) * 2 * * 7 + int (mystr[ 1 ]) * 2 * * 6 + int (mystr[ 2 ]) * 2 * * 5 + int (mystr[ 3 ]) * 2 * * 4 + int (mystr[ 4 ]) * 2 * * 3 + int (mystr[ 5 ]) * 2 * * 2 + int (mystr[ 6 ]) * 2 * * 1 + int (mystr[ 7 ]) * 2 * * 0 ) #print(eight_bit("01101100")) #請編寫一個簡單的互動式程式, 程式列印出”請輸入您的姓名”後 #, 等待使用者輸入. 使用者輸入姓名 XXX 後, 程式列印出”XXX, 歡迎進入程式語言的世界”. def ex0(): var = 1010.2011 print ( "%20d %5.1f %s" % (var,var,var)) def ex1(): username = input ( "請輸入您的姓名:" ) print (username, "歡迎進入程式語言的世界." ) #請配合練習 1.14 編寫一個能夠列印從 1 累加到 100 總和的程式. def ex2(): total = 0 for num in range ( 1 , 100 + 1 ): total = total + num print ( "The total is" ,total) def ex2_1(): total = 0 num = 1 while (num< 101 ): total = total + num num = num + 1 print ( "The total is" ,total) #請編寫一個能夠在列印 “請問由 1 累加到哪一個大於 1 的整數?”, 後讀取使用者輸入的整數, #並列印出從 1 累加到”某整數”總和的程式. def ex3(): usernum = int ( input ( "請問由1累加到哪一個大於1的整數:" )) if (usernum< 1 ): print ( "不符合要求" ) else : total = 0 for num in range ( 1 ,usernum + 1 ): total = total + num print ( "1累加到" ,usernum, "總和:" ,total) #請問如何在使用者需要退出累加程式之前, 可以”一直”輸入累加極限值, 並且進行數值的累加運算? def ex1_19_1(): username = input ( "Please enter your name:" ) if (username is not None ): for alphabet in username: print (alphabet, "ASCII value is" , ord (alphabet)) def ex1_19_2(): username = input ( "Please enter your name:" ) print ( "The string capitalized is" ) for alphabet in username: num = ord (alphabet) - 32 print ( chr (num),end = "") print () # Date NTD/USD #2011-10-07 30.493 #example iPhone 4S 16GB 199美元 #匯率稅換 US->$ def usconvert(): dollar = float ( input ( "What is the amount of US Dollars you wish to convert:" )) exchange = float ( input ( "What is the current exchange rate:" )) print ( "The amount in the Foreign Currency is $" ,dollar * exchange) #底下是有關1.8之問題 """ #1.8Q4 print(eight_bit("01101100")) #1.8Q5 print(hex(108)) #1.8Q6 print(bin(-62)) #1.8Q7 print(chr(62)) #print(hex(10)) """ #呼叫匯率稅換函式usconvert() #usconvert() #有問題請在底下留言,謝謝... |
2011年10月4日 星期二
100設計一甲_week3
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 | import random def NumericOperations(num1,num2): print ( "num1=" ,num1, "num2=" ,num2) print ( "num1+num2=" ,num1 + num2) print ( "num1-num2=" ,num1 - num2) print ( "num1*num2=" ,num1 * num2) print ( "num1/num2=" ,num1 / num2) print ( "num1//num2=" ,num1 / / num2) #除法之商 print ( "num1%num2=" ,num1 % num2) #除法之餘數 print ( "num1**num2=" ,num1 * * num2) #num1的num2次方 def 猜數字(): 標準答案 = random.randint( 1 , 100 ) 你猜的數字 = int ( input ( "123:" )) 猜測次數 = 1 while 標準答案 ! = 你猜的數字: if 標準答案 < 你猜的數字: print ( "太大了,再猜一次! 加油" ) else : print ( "太小了,再猜一次!加油" ) 你猜的數字 = int ( input ( "請輸入您所猜的整數:" )) 猜測次數 = 猜測次數 + 1 print ( "猜對了!總共猜了" , 猜測次數, "次" ) def triangle(base,high): #area=base*high/2 return (base * high / 2 ) def tri(base,high): h = (base * base + high * high) * * 0.5 print (h) #return (h) def squre(x,n): ans = 1 while (n> 0 ): ans = ans * x n = n - 1 return (ans) print (squre( 2 , 10 )) #print(2**10) def ex1(x,n): ans = 0 while x< = n: ans = ans + x x = x + 1 return (ans) print (ex1( 1 , 100 )) #1+2+3+...+99+100 #one day one apple def apple(): ans = "apple" guess = "" count = 0 while (ans! = guess and count< 10 ): print ( "一天一??,醫生遠離你,只能猜十次喔" ) ans = input ( "ans:" ) count = count + 1 if (ans! = guess): print ( "十次都沒答對......." ) else : print ( "你答對了,猜了" ,count, "次,答案為一天一" ,ans, "醫生遠離你" ) #apple() print (triangle( 3 , 4 )) print (tri( 3 , 4 )) |
2011年9月27日 星期二
100設計一甲_week2
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #請自行選用函式使用 #def #if else #ex1 num+mde 1~60 43 print None def ex1(): for num in range ( 1 , 60 ): if (num = = 43 ): print ( "None" ) else : print ( "Number:" , 40023100 + num, "Department of Mechanical Design Engineering" ) #ex2 num+girl or boy 43no print def ex2(): for num in range ( 1 , 60 ): if (num! = 43 ): if (num< = 10 ): print ( "Number:" , 40023100 + num, "girl" ) else : print ( "Number:" , 40023100 + num, "boy" ) #ex3 socre A(81~100)B(41~80)C(1~40) def ex3(): for score in range ( 1 , 101 ): if (score< = 40 ): print ( "Score:" ,score, "C" ) elif (score> 40 and score< = 80 ): print ( "Score:" ,score, "B" ) else : print ( "Score:" ,score, "A" ) def myadd(var1, var2): return (var1 + var2) def myfun(): print ( "Hello World!" ) #ex4 funstory input bird(string) def fun(bird): print ( "有個小姐逛街走入一家店時," ) print ( "店門口有一隻" ,bird, "大聲的喊『歡迎光臨』。" ) print ( "她覺得很好奇, 想試試看這隻" ,bird, "是不是真的這麼聰明," ) print ( "就又走了出去" ) print ( "結果這隻" ,bird, "真的對她說『謝謝光臨。』" ) print ( "她覺得很好玩,於是又走了進去,『歡迎光臨』。" ) print ( "又走了出來『謝謝光臨』。" ) print ( "她來來回回走了十幾次," ) print (bird, "也一直說『歡迎光臨』,『謝謝光臨』。" ) print ( "於是她又走進去," ) print ( "這次這隻" ,bird, "不再理她," ) print ( "反而轉過頭去對著裡面大喊:『老闆,有人在玩你的鳥』。" ) #可以直接印出回傳(return)值 print (myadd( 1 , 2 )) """ #也可以將回傳值存在另一變數,之後將之印出 temp=myadd(1,2) print(temp) """ myfun() #fun("鸚鵡") """ 英語小補帖(與課程無關,僅供參考) 1. You had me at hello. 0你一開口就征服我了 1你跟我說哈囉 2. Life will find its own way. 0我可以靠自己找路 1生命會自己找出口 3.Stupid is as stupid does. 0你是個笨蛋 1傻人有傻福 4. Destiny takes a hand. 0命中注定 1有錢能使鬼推磨 5. Am I reaching for the stars here? 0我是在強人所難嗎 1你很難追 6. I'll be back 0我會回來的 1我在你後面 answer:011000 """ |
2011年9月20日 星期二
100設計一甲_week1
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 | #coding: utf8 #--->代表註解,程式會跳過,繼續執行 #print()-->印出 ()內的 #ex1~6 使用,請自行削掉註解符號(#) #ex1 print ( "python!" ) print ( "WOW!~" ) print ( "python!" , "hello" , "WOW!" ,end = "-->this is end!\n" ,sep = "----" ) ###ex2 ##print ("python!",end="") ##print ("WOW!~") ###ex3 ##print ("python!","hello","WOW!",sep="----") ###ex4 ##print ("python!","hello","WOW!",sep="----",end="-->this is end!\n") ###ex5 ##for i in range (1,10): ## print ("hi","hello","python",sep="~") ###ex6 ##for i in range (1,60): ## print ("Number:",40023100+i,"mde") |
2011年9月16日 星期五
建立bat關閉Immunet 3.0服務
1 | net stop "Immunet 3.0" /yes |
放在隨身碟點兩下即關閉該服務,
或是直接複製該串後,按下Windows+R貼上執行。
後記:
電腦教室的防毒軟體實在很拖速度,
要進入控制台>切換到傳統檢視>系統管理工具>服務>汪洋中找到Immunet 3.0
這麼一長串的內容實在是讓人懶得去記,
而且自己電腦通常也不是裝這套防毒軟體,
乾脆做成一個可以點兩下馬上關閉的執行檔不是很快樂嗎=w=
2011年9月14日 星期三
基礎Python
Coding format FYI:http://www.python.org/dev/peps/pep-0263/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # -*- coding: utf-8 -*- olt = [ 83 , 78 , 84 , 81 ] tat = [ 85 , 90 ] fit = [ 85 ] s1 = (olt[ 0 ] + olt[ 1 ] + olt[ 2 ] + olt[ 3 ])\ * 0.1 s2 = (tat[ 0 ] + tat[ 1 ]) * 0.15 s3 = fit[ 0 ] * 0.3 print (s1 + s2 + s3) #if else number = 7 if number < 10 : print ( "ture" ) else : print ( "false" ) #for for i in range ( 0 , 3 ): print ( "Number:%d" % (i)) |
2011年6月21日 星期二
期末考題
一、JAVA Applet 波浪
利用五芒星來畫出波浪,並以number變數控制其波數。
提示:
不需放在CMSimple內,直接執行程式即可,五芒星可以不必旋轉。
參考:
『Java - Wave Applet 範例』、『Java - Draw Star 範例方法』
範例結果如下:
二、CMSimple AJAX 表單輸入
在CMSimple中建立一個form表單包含三個input輸入框,能夠輸入你自己的 班級、姓名、學號 驗證身份是否正確,
若答案正確則在CMSimple中印出身份正確,
若其中一個答案錯誤則印出身份錯誤。
條件:
1. 必須包含AJAX menu選單。
參考:
『CMSimple - my_main_function 範例plugins程式』
提示:
PHP使用if多個條件成立的使用方法如下...
1 2 3 4 5 | if (a== "字串1" && b== "字串2" && c== "字串3" ) { echo "內容1" ; } else { echo "內容2" ; } |
三、CMSimple AJAX 即時顯示
請參考『Java - Draw Applet Gear 範例方法』將其編譯並產生Class檔案,在CMSimple中建立一個form表單包含四個input輸入框,
能夠輸入 座標x、座標y、節圓半徑rp、齒數n,
再使用Applet來顯示齒輪並能夠藉由以上參數來控制,
最後必須能夠由AJAX即時顯示在CMSimple中。
條件:
1. 必須包含AJAX menu選單。
參考:
『CMSimple - my_main_function 範例plugins程式』、『CMSimple - my_main_function 範例plugins子程式』
四、以Java Applet繪製動畫
請繪製一個寬為100的小正方形,讓小正方形能夠持續繞著寬為500的大正方形行走,
參考:
『Java - Draw Anime Star 範例』
提示:
大正方形可以不用畫出來。
範例結果如下:
參考範例目錄
- Java - Wave Applet 範例
- Java - Draw Star 範例方法
- CMSimple - my_main_function 範例plugins程式
- CMSimple - my_main_function 範例plugins子程式
- Java - Draw Gear 範例方法
- Java - Draw Anime Star 範例
Java - Wave Applet 範例
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 | // 在 Eclipse 執行, 以滑鼠右鍵, Run as Applet // 在 NetBeans 執行, 則以滑鼠右鍵, 按下 Run File (系統會建立對應的 html) // 若 Application 與 Applet 同時存在, 在 Eclipse 可以分別以滑鼠右鍵, 選擇執行 // 若 Application 與 Applet 同時存在, 在 NetBeans 按下 Run File, 僅會執行 Application import java.applet.Applet; import java.awt.*; public class HelloWorld extends Applet { Stroke drawingStroke = new BasicStroke( 2 ); // static 變數才可以在各方法中共用 String peakNumber = "" ; int number; public void init() { peakNumber = getParameter( "peakNumber" ); if (peakNumber != null ) { // 轉成整數使用 Integer.parseInt() // 轉為浮點數使用 Float.parseFloat() number = Integer.parseInt(peakNumber); } else { // 若 peakNumber 取不到值, 則內定為 5 number = 5 ; } } public void paint(Graphics g) { int i; Graphics2D graph = (Graphics2D)g; graph.setStroke(drawingStroke); graph.setPaint(Color.black); for (i= 1 ;i<=number;i++) { // 每一行向下增量 20 單位 graph.drawString( "Hello World" , 10 , 10 +i* 20 ); printC(graph,i, 80 , 10 +i* 20 ); } for (i=number- 1 ;i>= 1 ;i--) { // 必須考慮前半部已經增量 number*20 graph.drawString( "Hello World" , 10 , 10 +(number+number-i)* 20 ); printC(graph,i, 80 , 10 +(number+number-i)* 20 ); } } public void stop() { } public void printC(Graphics g, int m, int x, int y) { Graphics2D graph = (Graphics2D)g; int i; for (i= 0 ;i<m;i++) { graph.drawString( "!" , x+i* 10 , y); } } } |
Java - Draw Star 範例方法
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 | //drawstar(graph, x座標, y座標, 半徑, 旋轉角度); public void drawstar(Graphics g, int x, int y, int r, int angle){ Graphics2D graph = (Graphics2D)g; int i,j; double deg = 3.14159 / 180 .; double temp1, temp2, temp3, temp4; double [] ptt_x = new double [ 10 ]; double [] ptt_y = new double [ 10 ]; double [] pt_x = new double [ 10 ]; double [] pt_y = new double [ 10 ]; for (i= 0 ;i< 7 ;i++) { pt_x[i] = x+r*Math.sin( 72 .*deg*i+angle*deg); pt_y[i] = y-r*Math.cos( 72 .*deg*i+angle*deg); temp1 = Math.cos( 54 .*deg- 72 .*deg*i-angle*deg); temp2 = Math.sin( 54 .*deg- 72 .*deg*i-angle*deg); temp3 = Math.sin( 18 .*deg); temp4 = Math.cos( 36 .*deg); ptt_x[i] = x+r*temp3*temp1/temp4; ptt_y[i] = y-r*temp3*temp2/temp4; } for (i= 0 ;i< 5 ;i++) { graph.drawLine(( int )pt_x[i], ( int )pt_y[i], ( int )ptt_x[i], ( int )ptt_y[i]); j=i+ 1 ; graph.drawLine(( int )ptt_x[i], ( int )ptt_y[i], ( int )pt_x[j], ( int )pt_y[j]); } } |
CMSimple - my_main_function 範例plugins程式
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | <?php // 檔名:index.php // 編寫成類別格式的程式樣版 (ready for CMSimple plugin) // CMSimple呼叫格式「#CMSimple $output.=my_main_function();#」 // 程式的主函式 function my_main_function() { $myplugin = new Cmyplugin; return $myplugin ->myplugin_main(); } // 程式類別 class Cmyplugin{ public $common1 = "common1" ; public $common2 = "common2" ; function myplugin_main() { $menu = $_GET [ "menu" ]; $printonce =0; if ( $menu ) { switch ( $menu ) { case "function1" : // could be html form or action scripts $output .= $this ->myplugin_function1(); $output .= $this ->myplugin_printmenu(); break ; case "function2" : // could be html form or action scripts $output .= $this ->myplugin_function2(); $output .= $this ->myplugin_printmenu(); break ; case "function3" : // AJAX 即時顯示範例 $output .= $this ->myplugin_function3(); $output .= $this ->myplugin_printmenu(); break ; case "printmenu" : $output .= $this ->myplugin_printmenu(); break ; default : // use function3 as default function $output .= $this ->myplugin_printmenu(); } } else { // 未指定 menu 則列出表單 $output .= $this ->myplugin_printmenu(); } return $output ; } //範例程式1 - 印出文字測試 function myplugin_function1() { $output = "function 1 and some html " . $this ->common1. " and " . $this ->common2; $output .= " some more html" ; return $output ; } //範例程式2 - 印出文字測試 function myplugin_function2() { $output = "function 2 and some html " . $this ->common1. " and " . $this ->common2; $output .= " some more html" ; return $output ; } //範例程式3 - AJAX 即時顯示結果 function myplugin_function3() { global $sn , $su ; $output .=" <script src=\"./jscript/prototype.js\" language=\"JavaScript\" type=\"text/javascript\"></script> <script language=\"JavaScript\" type=\"text/javascript\"> function submitEntryForm(event) { var updater = new Ajax.Updater({success: 'var_list' ,error: 'error_list' }, './plugins/my_main_function/addaction.php' , { parameters: {num1:$( 'num1' ).value, num2:$( 'num2' ).value, num3:$( 'num3' ).value} }); event.preventDefault(); } function addObservers(event) { $( 'entry' ).observe( 'submit' , submitEntryForm); } Event.observe(window, 'load' , addObservers); </script> <form id= 'entry' action= '' method= 'post' > num1: <input type= 'text' name= 'num1' id= 'num1' value= '10' /><br /> num2: <input type= 'text' name= 'num2' id= 'num2' value= '20' /><br /> num3: <input type= 'text' name= 'num3' id= 'num3' value= '30' /><br /> <input type= 'submit' id= 'submit' value= 'Send' /> </form> <div id= 'var_list' ></div> <div id= 'error_list' ></div> "; return $output ; } // 以下成員函式, 用來測試 AJAX 表單的使用 function myplugin_printmenu() { global $sn , $su ; $output .=" <script src=\"./jscript/prototype.js\" language=\"JavaScript\" type=\"text/javascript\"></script> <script src=\"./jscript/Menu.js\" language=\"JavaScript\" type=\"text/javascript\"></script> <script language=\"JavaScript\" type=\"text/javascript\"> Menu.init(\"menu2\"); </script> <style type=\"text/css\"> #menu2, #menu2 ul { margin: 0; padding: 0; } #menu2 li { list-style-type: none; } #menu2 { font: 12px Verdana, Arial; } #menu2 li, #menu2 a { float: left; } #menu2 a { display: block; padding: 6px; text-decoration: none; background-color: #FFF; color: #2362AF; } #menu2 a:hover, #menu2 a.menu_open { background-color: #2362AF; color: #FFF; } #menu2 ul { visibility: hidden; position: absolute; width: 150px; border: 1px solid #CCC; margin-top: 1px; } #menu2 ul li { width: 150px; } #menu2 ul a { width: 144px; padding: 3px; } * html #menu2 ul a { width: 150px; } #menu2 ul a.submenu { background-image: url(\"./jscript/menu_arrow_right.gif\"); background-repeat: no-repeat; background-position: 138px 6px; } #menu2 ul a.submenu:hover, #menu2 ul a.submenu.menu_open { background-image: url(\"./jscript/menu_arrow_right_mo.gif\"); } #menu2 ul ul { margin-top: -1px; } </style> <ul id=\"menu2\"> <li><a href=\ "" . $sn . "?" . $su ."&menu=function1\">Data 1</a></li> <li><a href=\ "" . $sn . "?" . $su ."&menu=function2\">Data 2</a> <ul> <li><a href=\ "" . $sn . "?" . $su ."&menu=function2\">Data 2-1</a></li> <li><a href=\ "" . $sn . "?" . $su ."&menu=function2\">Data 2-2</a> <ul> <li><a href=\ "" . $sn . "?" . $su ."&menu=function2\">Data 2-2-1</a></li> <li><a href=\ "" . $sn . "?" . $su ."&menu=function2\">Data 2-2-2</a></li> </ul> </li> <li><a href=\ "" . $sn . "?" . $su ."&menu=function2\">Data 2-3</a> </ul> </li> <li><a href=\ "" . $sn . "?" . $su ."&menu=function3\">AJAX即時顯示</a></li> </ul> "; return $output ; } } |
CMSimple - my_main_function 範例plugins子程式
1 2 3 4 5 | <?php //檔名:addaction.php $total = $_POST [ 'num1' ] + $_POST [ 'num2' ] + $_POST [ 'num3' ]; echo $_POST [ 'num1' ]. "+" . $_POST [ 'num2' ]. "+" . $_POST [ 'num3' ]. "=" . $total ; ?> |
Java - Draw Gear 範例方法
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 | //drawgear(graph, x座標, y座標, 節圓半徑, 齒數); public void drawgear(Graphics g, int midx, int midy, int rp, int n){ Graphics2D graph = (Graphics2D)g; int imax= 15 ; int i= 0 ; int j= 0 ; double a= 0 , d= 0 , ra= 0 , rb= 0 , rd= 0 , dr= 0 , num= 0 , sigma= 0 , ang= 0 , ang2= 0 , lxd= 0 , lyd= 0 , r= 0 , theta= 0 , alpha= 0 , xpt= 0 , ypt= 0 , xd= 0 , yd= 0 , lfx= 0 , lfy= 0 , rfx= 0 , rfy= 0 ; double pi = Math.acos(- 1 ); double deg = pi/ 180 .; graph.drawLine(midx, midy, midx, midy-rp); a = 2 *rp/n; d = 2.5 *rp/n; ra = rp+a; rb = rp*Math.cos( 20 *deg); rd = rp-d; dr = (ra-rb)/imax; num = n; sigma = 3.14159 /( 2 *num)+Math.tan( 20 *deg)- 20 *deg; for (j= 0 ;j<n;j++) { ang = - 2 .*j*pi/num+sigma; ang2 = 2 .*j*pi/num+sigma; lxd = midx+rd*Math.sin(ang2- 2 .* 3.14159 /num); lyd = midy-rd*Math.cos(ang2- 2 .* 3.14159 /num); xd = rd*Math.sin(-ang); yd = rd*Math.cos(-ang); for (i= 0 ;i<=imax;i++) { r = rb+i*dr; theta = Math.sqrt((r*r)/(rb*rb)- 1 .); alpha = theta - Math.atan(theta); xpt = r*Math.sin(alpha-ang); ypt = r*Math.cos(alpha-ang); if (i== 0 ) { graph.drawLine(( int )(midx+xpt), ( int )(midy-ypt), ( int )(midx+xd), ( int )(midy-yd)); } graph.drawLine(( int )(midx+xpt), ( int )(midy-ypt), ( int )(midx+xpt), ( int )(midy-ypt)); if (i==imax) { lfx = midx+xpt; lfy = midy-ypt; } } /*the line from last end of dedendum point to the recent end of dedendum point*/ graph.drawLine(( int )lxd, ( int )lyd, ( int )(midx + xd), ( int )(midy - yd)); for (i= 0 ;i<=imax;i++) { r = rb+i*dr; theta = Math.sqrt((r*r)/(rb*rb)- 1 .); alpha = theta-Math.atan(theta); xpt = r*Math.sin(ang2-alpha); ypt = r*Math.cos(ang2-alpha); xd = rd*Math.sin(ang2); yd = rd*Math.cos(ang2); if (i== 0 ) { graph.drawLine(( int )(midx+xpt), ( int )(midy-ypt), ( int )(midx+xd), ( int )(midy-yd)); } graph.drawLine(( int )(midx+xpt), ( int )(midy-ypt), ( int )(midx+xpt), ( int )(midy-ypt)); if (i==imax) { rfx = midx+xpt; rfy = midy-ypt; } } graph.drawLine(( int )lfx, ( int )lfy, ( int )rfx, ( int )rfy); } } |
Java - Draw Anime Star 範例
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import java.applet.Applet; import java.awt.*; public class MovingHelloWorld extends Applet implements Runnable{ Stroke drawingStroke = new BasicStroke( 2 ); String peakNumber = "" ; int number; int frame, delay = 13 ; Thread animator; boolean frozen = false ; Dimension offDimension; Image offImage; Graphics offGraphics; double deg= 3.14159 / 180 .; int pos_x= 0 ; int vel_x= 1 ; int pos_y= 0 ; int vel_y= 1 ; int bound_x= 200 ; int bound_y= 200 ; public void init() { setBackground(Color.white); peakNumber = getParameter( "peakNumber" ); if (peakNumber != null ) { number = Integer.parseInt(peakNumber); } else { number = 3 ; } } public void start() { if (frozen) { } else { if (animator == null ) { animator = new Thread( this ); } animator.start(); } } // 利用繪圖物件變數 g 執行每一影格的繪圖 public void paintFrame(Graphics g) { int i; // 將 g 轉換為 Graphics2D 物件格式, 然後將位址指給 graph 繪圖物件 // 實際執行繪圖的物件為 graph Graphics2D graph = (Graphics2D)g; // 有關繪筆種類 (選擇筆觸格式) Stroke stroke = //new BasicStroke (15.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); new BasicStroke ( 2 .0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER); //graph.setStroke(drawingStroke); graph.setStroke(stroke); graph.setPaint(Color.black); if (pos_x < 0 ) { // 列印位置碰到左邊界時, 位置歸零, 並且改變速度方向 pos_x = 0 ; // 將 x 列印位置歸零 vel_x = -vel_x; // 改變速度方向. } else if (pos_x > bound_x) { // 列印位置碰到右邊界時, 位置設為右邊頂值, 並且改變速度方向 pos_x = bound_x; // 將 x 列印位置停在右邊邊界 vel_x = -vel_x; // 改變速度方向 } // x 列印位置, 以 vel_x 的速度進行增量 pos_x += vel_x; if (pos_y < 0 ) { // 列印位置碰到左邊界時, 位置歸零, 並且改變速度方向 pos_y = 0 ; // 將 y 列印位置歸零 vel_y = -vel_y; // 改變速度方向. } else if (pos_y > bound_y) { // 列印位置碰到右邊界時, 位置設為右邊頂值, 並且改變速度方向 pos_y = bound_y; // 將 y 列印位置停在右邊邊界 vel_y = -vel_y; // 改變速度方向 } // y 列印位置, 以 vel_y 的速度進行增量 pos_y += vel_y; // 畫出旋轉的 drawstar drawstar(graph, 100 +pos_x, 100 +pos_y, 30 , frame); } public void stop() { animator = null ; offImage = null ; offGraphics = null ; } public void run() { long startTime = System.currentTimeMillis(); while (Thread.currentThread() == animator) { frame++; repaint(); try { startTime +=delay; Thread.sleep(Math.max( 0 , startTime-System.currentTimeMillis())); } catch (InterruptedException e) { break ; } } } public boolean mouseDown(Event e, int x, int y) { if (frozen) { frozen = false ; start(); } else { frozen = true ; stop(); } return true ; } // 更新繪圖畫面 public void update( Graphics g ) { Dimension d = getSize(); offImage = createImage(d.width, d.height); offGraphics = offImage.getGraphics(); offGraphics.setColor(getBackground()); offGraphics.fillRect( 0 , 0 , d.width, d.height); offGraphics.setColor(Color.black); paintFrame(offGraphics); g.drawImage(offImage, 0 , 0 , null ); } //drawstar(graph, x座標, y座標, 半徑, 旋轉角度); public void drawstar(Graphics g, int x, int y, int r, int angle){ Graphics2D graph = (Graphics2D)g; int i,j; double deg = 3.14159 / 180 .; double temp1, temp2, temp3, temp4; double [] ptt_x = new double [ 10 ]; double [] ptt_y = new double [ 10 ]; double [] pt_x = new double [ 10 ]; double [] pt_y = new double [ 10 ]; for (i= 0 ;i< 7 ;i++) { pt_x[i] = x+r*Math.sin( 72 .*deg*i+angle*deg); pt_y[i] = y-r*Math.cos( 72 .*deg*i+angle*deg); temp1 = Math.cos( 54 .*deg- 72 .*deg*i-angle*deg); temp2 = Math.sin( 54 .*deg- 72 .*deg*i-angle*deg); temp3 = Math.sin( 18 .*deg); temp4 = Math.cos( 36 .*deg); ptt_x[i] = x+r*temp3*temp1/temp4; ptt_y[i] = y-r*temp3*temp2/temp4; } for (i= 0 ;i< 5 ;i++) { graph.drawLine(( int )pt_x[i], ( int )pt_y[i], ( int )ptt_x[i], ( int )ptt_y[i]); j=i+ 1 ; graph.drawLine(( int )ptt_x[i], ( int )ptt_y[i], ( int )pt_x[j], ( int )pt_y[j]); } } } |
2011年6月7日 星期二
POST & PHP外部輸入值給java
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 | function control() { global $sn,$su; $output= "<form method=\"post\" action=" .$sn. "?" .$su. "&menu=entercontrol>" ; $output.= "x:<input type=\"text\" name=\"x\" value=\"350\"><br/>" ; $output.= "y:<input type=\"text\" name=\"y\" value=\"150\"><br/>" ; $output.= "r:<input type=\"text\" name=\"r\" value=\"30\"><br/>" ; $output.= "angle:<input type=\"text\" name=\"angle\" value=\"0\"><br/>" ; $output.= "<input type=\"submit\" value=\"send\">" ; $output.= "</form>" ; return $output; } function entercontrol(){ $x=$_POST[ "x" ]; $y=$_POST[ "y" ]; $r=$_POST[ "r" ]; $angle=$_POST[ "angle" ]; $output.= "<applet codebase=\"plugins/my_main_function\" code=\"drawstar.\" height=\"450\" width=\"450\">" ; $output.= "<param name=\"peakNumber\" value=x/>" ; //param name 對應給值之變數名稱 value 值 $output.= "<param name=\"origx\" value=\"$x\"/>" ; $output.= "<param name=\"origy\" value=\"$y\"/>" ; $output.= "<param name=\"r\" value=\"$r\"/>" ; $output.= "<param name=\"angle\" value=\"$angle\"/>" ; $output.= "</applet>" ; return $output; } |
2011 c2期中考後的小考-題目
第一題 (30%)
請使用 java+awt套件 繪製出三角形、梯形、菱形,圖形尺寸自訂。
並利用number變數控制展示圖形,如 number=1 時展示三角形,number=2 時展示梯形、number=3 時展示菱形。
參考資料:[url]http://blog.kmol.info/?p=385[/url]
第二題 (30%)
請將下列PHP程式碼由函式形式改為類別形式。
參考資料:[url]http://blog.kmol.info/?p=376[/url]
程式碼:
第三題 (20%)
請將下列 java 程式碼編譯,並改成 CMSimple Plugins 且在 CMSimple 底下執行,可與第四題一起展示。
Hint:注意package drawstar,須放置在drawstar目錄底下,可自由更改。
Hint:注意檔案名稱必須為Main.java。
參考資料:[url]http://blog.kmol.info/?p=437[/url]
程式碼:
第四題 (20%)
利用第三題編譯好之java程式,透過 CMSimple Plugins 控制其參數,並在 CMSimple 底下執行,可與第三題一起展示。
Hint:控制變數之名稱分別為 origx(x軸位置)、origy(y軸位置)、r(半徑)、angle(旋轉角度)。
參考資料:[url]http://blog.kmol.info/?p=437[/url]
請使用 java+awt套件 繪製出三角形、梯形、菱形,圖形尺寸自訂。
並利用number變數控制展示圖形,如 number=1 時展示三角形,number=2 時展示梯形、number=3 時展示菱形。
參考資料:[url]http://blog.kmol.info/?p=385[/url]
第二題 (30%)
請將下列PHP程式碼由函式形式改為類別形式。
參考資料:[url]http://blog.kmol.info/?p=376[/url]
程式碼:
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 | <?php $m =10; //$m=$_GET["m"]; //$color=$_GET["color"]; $color = "red" ; printallH( $m ); for ( $i =0; $i < $m -2; $i +=1) { printheadtailH( $m ); } printallH( $m ); function printallH( $m ) { // 函式目的,由 $m 變數控制列印 H 的個數,全部列印相同的 H for ( $i =1; $i <= $m ; $i +=1) { echo "H" ; } // 印完後,跳行 echo "<br>" ; } function printheadtailH( $m ) { global $color ; // 列印只有頭尾是 H ,其餘是空白 echo "H" ; for ( $i =1; $i <= $m -2; $i +=1) { // 採用 css 控制列印字元顏色,將 orange 改為 white,可以列出白色字元 echo "<span style=\"color:" . $color . "\">H</span>" ; //echo "<span style="color:white">H</span>"; } echo "H" ; echo "<br>" ; } ?> |
第三題 (20%)
請將下列 java 程式碼編譯,並改成 CMSimple Plugins 且在 CMSimple 底下執行,可與第四題一起展示。
Hint:注意package drawstar,須放置在drawstar目錄底下,可自由更改。
Hint:注意檔案名稱必須為Main.java。
參考資料:[url]http://blog.kmol.info/?p=437[/url]
程式碼:
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | package drawstar; import java.applet.Applet; import java.awt.*; // for Frame Exit import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Main extends Applet { Stroke drawingStroke = new BasicStroke( 2 ); // static 變數才可以在各方法中共用 static String setorigx = null ; static String setorigy = null ; static String setr = null ; static String setangle = null ; static float origx,origy,r,angle; public void init() { // getParameter 取得字串值 setorigx = getParameter( "origx" ); setorigy = getParameter( "origy" ); setr = getParameter( "r" ); setangle = getParameter( "angle" ); if (setorigx!= null && setorigy!= null && setr!= null && setangle!= null ) { // 轉成整數使用 Integer.parseInt() // 轉為浮點數使用 Float.parseFloat() origx = Integer.parseInt(setorigx); origy = Integer.parseInt(setorigy); r = Integer.parseInt(setr); angle = Integer.parseInt(setangle); } else { // 若 peakNumber 取不到值, 則內定為 5 origx= 150 ; origy= 150 ; r= 30 ; angle= 180 ; } } public void paint(Graphics g) { int i,j; float x,y; double temp1,temp2,temp3,temp4; double degree= 3.14159 / 180.0 ; double [] ptx= new double [ 6 ]; double [] pty= new double [ 6 ]; double [] pttx= new double [ 6 ]; double [] ptty= new double [ 6 ]; Graphics2D graph = (Graphics2D)g; graph.setStroke(drawingStroke); graph.setPaint(Color.black); for (i= 0 ;i< 6 ;i++) { ptx[i]=origx+r*Math.sin( 72 .*i*degree+angle*degree); pty[i]=origy+r*Math.cos( 72 .*i*degree+angle*degree); temp1= Math.cos( 54 .*degree- 72 .*degree*i-angle*degree); temp2= Math.sin( 54 .*degree- 72 .*degree*i-angle*degree); temp3= Math.sin( 18 .*degree); temp4= Math.cos( 36 .*degree); pttx[i]=origx+r*temp3*temp1/temp4; ptty[i]=origy+r*temp3*temp2/temp4; } for (i= 0 ;i< 5 ;i++) { graph.drawLine(( int )ptx[i], ( int )pty[i], ( int )pttx[i], ( int )ptty[i]); j=i+ 1 ; graph.drawLine(( int )pttx[i], ( int )ptty[i], ( int )ptx[j], ( int )pty[j]); } } public void stop() { } public void printC(Graphics g, int m, int x, int y){ Graphics2D graph = (Graphics2D)g; int i; for (i= 0 ;i<m;i++) { graph.drawString( "!" , x+i* 10 , y); } } public static void main(String[] args) { Applet applet = new Main(); Frame frm = new Frame( "我的主方法(函式)框架" ); frm.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit( 0 ); } }); frm.setSize( 100 , 100 ); frm.setVisible( true ); // 將 applet 加入 Frame 中 frm.add(applet); frm.setSize( 300 , 300 ); frm.setVisible( true ); applet.init(); } } |
第四題 (20%)
利用第三題編譯好之java程式,透過 CMSimple Plugins 控制其參數,並在 CMSimple 底下執行,可與第三題一起展示。
Hint:控制變數之名稱分別為 origx(x軸位置)、origy(y軸位置)、r(半徑)、angle(旋轉角度)。
參考資料:[url]http://blog.kmol.info/?p=437[/url]
2011年5月28日 星期六
兩行reg碼進行系統防寫
2011年5月10日 星期二
MDE電腦教室防隨身碟病毒小工具(已無效)
2011.5.17補充:
電腦教室有更強的病毒,
現在這招已經擋不住啦~
java applet drawstar
畫星星函式
php call java in cmsimple
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 | public void cad_drawstar(Graphics g, int x, int y, int r, int angle) { Graphics2D graph = (Graphics2D)g; double deg,temp1,temp2,temp3,temp4; deg= 3.14159 / 180.0 ; double pt_x[] = new double [ 7 ]; //java 宣告陣列 格式 double pt_y[] = new double [ 7 ]; double ptt_x[] = new double [ 7 ]; double ptt_y[] = new double [ 7 ]; int i,j; for (i= 0 ;i< 7 ;i++){ pt_x[i]=x+r*Math.sin( 72 .*deg*i+angle*deg); //sin cos 三角函數 使用格式 Math. pt_y[i]=y-r*Math.cos( 72 .*deg*i+angle*deg); temp1=Math.cos( 54 .*deg- 72 .*deg*i-angle*deg); temp2=Math.sin( 54 .*deg- 72 .*deg*i-angle*deg); temp3=Math.sin( 18 .*deg); temp4=Math.cos( 36 .*deg); ptt_x[i]=x+r*temp3*temp1/temp4; ptt_y[i]=y-r*temp3*temp2/temp4; } for (i= 0 ;i< 5 ;i++){ graph.drawLine(( int )pt_x[i],( int )pt_y[i],( int )ptt_x[i],( int )ptt_y[i]); //graph.drawLine 內的點必須是 int 因此把double 等變數強制轉換 j=i+ 1 ; graph.drawLine(( int )ptt_x[i],( int )ptt_y[i],( int )pt_x[j],( int )pt_y[j]); } } |
1 2 3 4 5 6 | <?php function HelloWorldApp(){ $output = "<applet codebase=\"plugins/test\" code=\"HelloWorldApp\" height=\"500\" width=\"500\"></applet>" ; return $output ; } ?> |
2011年5月3日 星期二
Java Applet HTML 新舊版嵌入語法
Class 檔案嵌入
舊版 HTML4 :
新版 HTML5 (測試階段):
Jar 檔案嵌入
舊版 HTML4 :
新版 HTML5 (測試階段):
舊版 HTML4 :
1 2 3 | < applet codebase = "Hello/World" code = "HelloWorld.class" height = "500" width = "500" > < param name = "Number" value = "1" /> </ applet > |
新版 HTML5 (測試階段):
1 2 3 | < object type = "application/x-java-applet" codebase = "Hello/World" code = "HelloWorld.class" height = "500" width = "500" > < param name = "Number" value = "9" /> </ object > |
Jar 檔案嵌入
舊版 HTML4 :
1 2 3 | < applet archive = "Hello/World" code = "HelloWorld.jar" height = "500" width = "500" > < param name = "Number" value = "8" /> </ applet > |
新版 HTML5 (測試階段):
1 2 3 | < object type = "application/x-java-applet" archive = "Hello/World" code = "HelloWorld.jar" height = "500" width = "500" > < param name = "Number" value = "7" /> </ object > |
c php applet
c 將波浪寫入txt檔
php 取得值並呼叫c執行檔
java applet由php 取值給入
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 | #include <stdio.h> #include <stdlib.h> void helloworld( int e){ FILE *fp1; fp1= fopen ( "output.txt" , "w" ); int s,point; for (s=1;s<=e;s++){ fprintf (fp1, "Hello World" ); //fprintf主要目的是供你將資料,以格式化方式寫入某檔案內 /*fprintf( )和printf( )兩者唯一的差別是, printf( )會將資料列印在螢幕上,而fprintf( )會將資料列印在某個檔案內。*/ for (point=1;point<=s;point++){ fprintf (fp1, "!" ); } fprintf (fp1, "\n" ); } for (s=e-1;s>0;s--){ fprintf (fp1, "Hello World" ); for (point=1;point<=s;point++){ fprintf (fp1, "!" ); } fprintf (fp1, "\n" ); } fclose (fp1); } int main( int argc, char ** argv) { // int number; if (argc > 1) number = ( int ) atoi (argv[1]); else number = 50; helloworld(number); return 0; } |
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 | <?php /************************************************** File:phpCallexe.php Name:執行exe Explain:呼叫C語言並執行 ****************************************By QQBoxy*/ if (isset( $_GET [ 'n' ])) $num = $_GET [ 'n' ]; else $num = 20; $cmd = exec ( 'c1.exe ' . $num ); execInBackground( $cmd ); echo 'It OK!!' ; //------------------------------------------------------- function execInBackground( $cmd ) { if ( substr (php_uname(), 0, 7) == "Windows" ){ pclose(popen( "start /B " . $cmd . "" , "r" )); } else { exec ( $cmd . " " . $num . " > /dev/null &" ); } } //------------------------------------------------------- ?> |
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | import java.applet.Applet; import java.awt.*; // for Frame Exit import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class HelloWorldApp extends Applet { Stroke drawingStroke = new BasicStroke( 2 ); public void init() { } public void paint(Graphics g) { int i,p; String peakNumber = "" ; peakNumber = getParameter( "peakNumber" ); if (peakNumber != null ) { // 轉成整數使用 Integer.parseInt() // 轉為浮點數使用 Float.parseFloat() p = Integer.parseInt(peakNumber); } else { // 若 peakNumber 取不到值, 則內定為 5 p = 10 ; } Graphics2D graph = (Graphics2D)g; graph.setStroke(drawingStroke); graph.setPaint(Color.black); for (i= 1 ;i<=p;i++) { graph.drawString( "Hello World" , 10 , 10 +i* 20 ); printC(graph,i, 80 , 10 +i* 20 ); } for (i=p- 1 ;i>= 1 ;i--) { //graph.drawString("Hello World", 10, 10+(10+10-i)*20); //printC(graph,i, 80, 10+(10+10-i)*20); graph.drawString( "Hello World" , 10 , 20 + 10 +( 2 *p- 1 )* 20 - 20 *i); printC(graph,i, 80 , 20 + 10 +( 2 *p- 1 )* 20 - 20 *i); } } public void stop() { } public void printC(Graphics g, int m, int x, int y){ Graphics2D graph = (Graphics2D)g; int i; for (i= 0 ;i<m;i++) { graph.drawString( "!" , x+i* 10 , y); } } public static void main(String[] args) { Frame frm = new Frame( "我的主方法(函式)框架" ); Applet applet = new HelloWorldApp(); frm.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit( 0 ); } }); frm.setSize( 100 , 100 ); frm.setVisible( true ); // 將 applet 加入 Frame 中 frm.add(applet); frm.setSize( 300 , 300 ); frm.setVisible( true ); applet.init(); } // 結束 main /* Command Lines int i = 0; for(i=1;i<=10;i++) { System.out.print("Hello World"); printC(i); System.out.print("\n"); } for(i=9;i>=1;i--) { System.out.print("Hello World"); printC(i); System.out.print("\n"); } } static void printC(int m){ int i; for(i=0;i<m;i++) { System.out.print("!"); } } */ } // 結束 HelloWorldApp class |
java applet由php 取值給入
1 2 3 | <applet code= "HelloWorldApp" height= "500" width= "500" > <param name= "peakNumber" value= "10" /> </applet> |
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 | import java.applet.Applet; import java.awt.*; public class HelloWorldApp extends Applet { Stroke drawingStroke = new BasicStroke( 2 ); // static 變數才可以在各方法中共用 String peakNumber = "" ; int number; public void init() { peakNumber = getParameter( "peakNumber" ); if (peakNumber != null ) { // 轉成整數使用 Integer.parseInt() // 轉為浮點數使用 Float.parseFloat() number = Integer.parseInt(peakNumber); } else { // 若 peakNumber 取不到值, 則內定為 5 number = 5 ; } } public void paint(Graphics g) { int i; Graphics2D graph = (Graphics2D)g; graph.setStroke(drawingStroke); graph.setPaint(Color.black); for (i= 1 ;i<=number;i++) { // 每一行向下增量 20 單位 graph.drawString( "Hello World" , 10 , 10 +i* 20 ); printC(graph,i, 80 , 10 +i* 20 ); } for (i=number- 1 ;i>= 1 ;i--) { // 必須考慮前半部已經增量 number*20 graph.drawString( "Hello World" , 10 , 10 +(number+number-i)* 20 ); printC(graph,i, 80 , 10 +(number+number-i)* 20 ); } } public void printC(Graphics g, int m, int x, int y){ Graphics2D graph = (Graphics2D)g; int i; for (i= 0 ;i<m;i++) { graph.drawString( "!" , x+i* 10 , y); } s(graph, 20 , 20 , 15 ); } public void s(Graphics g, int x, int y, int s){ Graphics2D graph = (Graphics2D)g; graph.drawLine(x, y, x+s, y); graph.drawLine(x+s, y, x+s, y+s); graph.drawLine(x+s, y+s, x, y+s); graph.drawLine(x, y+s, x, y); } public void square(Graphics g, int x, int y, int s){ Graphics2D graph = (Graphics2D)g; graph.drawLine(x, y, x+s, y+s); } } |
2011年4月26日 星期二
期中考題
一. 利用C語言寫一程式,並利用1個變數完成一數列級數控制,程式內請使用自訂函式來完成。
數列級數的規則為:奇數相加減,計算到設定變數的數量
例:變數為9,計算的值為1-3+5-7+9-11+13-15+17=9
1~17之間有9個數字做運算,中間的符號為-+-+..-+互相交 替,求出運算結果即可
Q1.c
二. 把上題改成PHP並放上CMS裡,使用GET指令,讓網址上可輸入1個變數
index.php
一. 請利用C語言寫出下列圖形,利用1變數控制其圖形大小,並使用自訂函式做程式。
註:圖中0與1可自行更換。
Q2.c
二. 把上題改成PHP並放上CMS裡,使用GET指令,讓網址上可輸入1個變數
index.php
請使用Java語言撰寫一海嘯程式,
內容至少須呼叫一個方法(類似C語言的副程式),
能夠控制兩個變數,
第1個變數控制波浪大小,
第2個變數為輸出多少個波浪。
輸出3波結果如:
W
WW
WWW
01_tsunami.java
請使用Java語言撰寫一訊號波程式,
內容至少須呼叫一個方法(類似C語言的副程式),
能夠控制三個變數,
第1個變數控制訊號波的大小,
第2個變數控制訊號波的高度,
第3個變數為輸出多少個訊號波。
波形解釋如圖:
輸出2波結果如:
請使用Java語言撰寫一鋸齒程式,
內容至少須呼叫一個方法(類似C語言的副程式),
能夠控制兩個變數,
第1個變數控制鋸齒大小,
第2個變數為輸出多少個鋸齒。
04_sawtooth.java
數列級數的規則為:奇數相加減,計算到設定變數的數量
例:變數為9,計算的值為1-3+5-7+9-11+13-15+17=9
1~17之間有9個數字做運算,中間的符號為-+-+..-+互相交 替,求出運算結果即可
Q1.c
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 | #include<stdio.h> void plus( int end){ //end為數字的數量 int i,y=1,total=0; //y為數字之初始值 total為累加值 for (i=0;i<end;i++){ //控制數字之數量 if (i>0){ //大於一個數字才開始做加減 if (i%2!=0){ //判斷+- 偶數為減 奇數為加 total-=y; //將total值加或減 printf ( "-%d" ,y); } else { total+=y; printf ( "+%d" ,y); } } else { total+=y; printf ( "%d" ,y); } y=y+2; //每次回圈y+2 遞增 } printf ( "=%d" ,total); } main(){ plus(15); //呼叫自訂函式 return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<stdio.h> void plus( int num){ int i,s=-1,sum=0; for (i=1;i<num*2;i+=2) { s *= -1; sum += i*s; printf ( "%+d" ,i*s); } printf ( "=%d\n" ,sum); } int main(){ plus(5); return 0; } |
index.php
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 | <?php function hello(){ $end = $_GET [ "end" ]; $y =1; $total =0; $output = "" ; for ( $i =0; $i < $end ; $i ++) { if ( $i >0){ if ( $i %2!=0) { $total -= $y ; $output .= "-" . $y ; } else { $total += $y ; $output .= "+" . $y ; } } else { $total += $y ; $output .= $y ; } $y = $y +2; } $output .= "=" . $total ; return $output ; } ?> |
一. 請利用C語言寫出下列圖形,利用1變數控制其圖形大小,並使用自訂函式做程式。
註:圖中0與1可自行更換。
Q2.c
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 | #include<stdio.h> void print( int end){ int e,i,j,s,k; for (i=1;i<=end;i++){ //end 控制做幾次 for (s=1;s<i;s++){ //s列印1前面的空格 但第一次時不列印 所以s<i printf ( " " ); } for (e=i;e<=end;e++){ //e 控制列印1的數量(漸減) printf ( "1" ); } printf ( "\n" ); //換行 for (j=i;j<end;j++){ //j 列印0前面的空格 printf ( " " ); } //for(k=end-i;k<end;k++){ for (k=1;k<=i;k++){ //k 控制列印0的數量(漸增) printf ( "0" ); } printf ( "\n" ); } } main(){ print(3); //呼叫自訂函式 return 0; } |
二. 把上題改成PHP並放上CMS裡,使用GET指令,讓網址上可輸入1個變數
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php function pri(){ $end = $_GET [ "end" ]; // 使用get 直接從網址列中輸入變數值 $output = "" ; for ( $i =1; $i <= $end ; $i ++){ for ( $s =1; $s < $i ; $s ++){ $output .= " " ; // 空格問題 無法對齊 請使用全行 } for ( $e = $i ; $e <= $end ; $e ++){ $output .= "1" ; } $output .= "<br />" ; for ( $j = $i ; $j < $end ; $j ++){ $output .= " " ; } for ( $k = $end - $i ; $k < $end ; $k ++){ $output .= "0" ; } $output .= "<br />" ; } return "<pre>" . $output . "</pre>" ; //pre 元素可定義預格式化的文本。被包圍在 pre 元素中的文本通常會保留空格和換行符。而文本也會呈現为等寬字體。 } ?> |
請使用Java語言撰寫一海嘯程式,
內容至少須呼叫一個方法(類似C語言的副程式),
能夠控制兩個變數,
第1個變數控制波浪大小,
第2個變數為輸出多少個波浪。
輸出3波結果如:
W
WW
WWW
01_tsunami.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package tsunami; //package是一種管理容器 管理所定義的名稱避免發生衝突 public class Main { //定義一個類別 static void tsunami(int num){ //建立一個名為海嘯的方法 int i,j; for (i=1;i<=num;i++) { //每波海嘯距離 for (j=0;j<i;j++) //增加海嘯的高度 System.out. print ( "L" ); //print為不換行列印 System.out.println( "" ); //println為換行列印 } } public static void main(String[] args) { //建立一個主要的方法 int w; for (w=0;w<3;w++) tsunami(4); //呼叫海嘯方法 } } |
請使用Java語言撰寫一訊號波程式,
內容至少須呼叫一個方法(類似C語言的副程式),
能夠控制三個變數,
第1個變數控制訊號波的大小,
第2個變數控制訊號波的高度,
第3個變數為輸出多少個訊號波。
波形解釋如圖:
輸出2波結果如:
S S S SSSS SSSS SSSS S S S SSSS SSSS SSSS S S S SSSS SSSS SSSS S S S SSSS SSSS SSSS02_square.java
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 | package square; //package是一種管理容器 管理所定義的名稱避免發生衝突 public class Main { //定義一個類別 static void square(int m, int n){ //建立一個名為square的方法 int i,j; for (i=1;i<=m;i++) //每波訊號波距離 System.out.println( "S" ); for (i=1;i<=m;i++) { for (j=0;j<n;j++) System.out. print ( "S" ); //print為不換行列印 System.out.println( "" ); //println為換行列印 } for (i=1;i<=m;i++) { for (j=1;j<n;j++) System.out. print ( " " ); System.out.println( "S" ); } for (i=1;i<=m;i++) { for (j=0;j<n;j++) System.out. print ( "S" ); System.out.println( "" ); } } public static void main(String[] args) { //建立一個主要的方法 int w; for (w=0;w<2;w++) square(3,4); //呼叫square方法 } } |
內容至少須呼叫一個方法(類似C語言的副程式),
能夠控制兩個變數,
第1個變數控制鋸齒大小,
第2個變數為輸出多少個鋸齒。
04_sawtooth.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package sawtooth; //package是一種管理容器 管理所定義的名稱避免發生衝突 public class Main { //定義一個類別 static void sawtooth(int n){ //建立一個名為鋸齒的方法 int i,j; for (i=1;i<=n;i++) { for (j=1;j<i;j++) System.out. print ( " " ); System.out.println( "P" ); } } public static void main(String[] args) { //建立一個主要的方法 int w; for (w=0;w<3;w++) sawtooth(4); //呼叫鋸齒方法 } } |
2011年4月11日 星期一
Hello World Wave 範例
helloworld.java
完整專案下載:
Eclipse Helloworld Wave、Netbeans Helloworld Wave
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 | package hello_world; //package是一種管理容器 管理所定義的名稱避免發生衝突 public class Main { //定義一個類別 static int i,j,k; //宣告結構變數 static void upwave( int b){ //前波浪 for (i= 0 ;i<=b;i++){ System.out.print( "Hello World" ); //print為不換行列印 for (j= 0 ;j<=i;j++){ System.out.print( "!" ); } System.out.println(i); //println為換行列印 } } static void downwave( int b){ //後波浪 for (i= 0 ;i<=b;i++){ System.out.print( "Hello World" ); for (j= 0 ;j<=b-i;j++){ System.out.print( "!" ); } System.out.println(i); } } static void printC( int a, int b){ //宣告一個方法 for (k= 1 ;k<=a;k++){ upwave(b); downwave(b); } } public static void main(String[] args) { //宣告一個名稱為main的方法 printC( 3 , 5 ); //呼叫printC函式 } } |
訂閱:
文章 (Atom)