積木語法makecode官網說明:https://makecode.com/defining-blocks
程式雲端硬碟:GiHub (請自行註冊)
//% color="#AA278D" weight=200 icon="\uf0f9"
namespace hello {
// 函數中大寫字體會產生空格+小寫
//% block
export function helloWorld() { }
//% block
//% A.defl=100
export function myPhase1(A:number) { }
//% block="division A = $A B = $B"
//% A.defl=60 B.defl=30
export function division(A:number,B:number) :number { return A/B; }
//% block="greatestCommonFactor A = $A B = $B"
//% A.defl=60 B.defl=40
export function greatestCommonFactor(A:number,B:number) :number {
let a = A; let b = B;
let value = 1; let counter = 2;
while(counter <= a && counter <= b) {
while(a % counter == 0 && b % counter == 0) {
value *= counter;
a /= counter; b /= counter;
}
counter += 1;
}
return value;
}
}
定義積木前需要先//%定義積木群組名稱,color =#XXXXX定義群組顏色(顏色代碼查詢),weight=??定義群組排位,數字愈大愈上面。icon="\u????" 代表群組圖示符號,4位數代碼可在網站http://fontawesome.io/icons/ 找到, 積木程式都放在namespace 。
namespace中使用 //% block 開啟積木塊,export function顯示出積木,
函數(A:number,B:number)中為傳入A,B值,():number為該函數傳回值,也可使用void或string
定義變數用let,運算符號比照C程式。
請照左邊求最大公因數函數,自行寫出求最小公倍數函數
// HEX2DEC function
// FB link : https://www.facebook.com/mason.chen.1420
//% weight=12 color=#ff3333 icon="\uf2d6" block="HEX2DEC"
namespace HEX2DEC {
let hex_array = "0123456789ABCDEF"
let hex_array_2 = "0123456789abcdef"
let dec_out = 0
//% blockId="hex2dec_func" block="convert HEX %hex_in | to DEC"
//% weight=90
export function hex2dec_func(hex_in: string): number {
dec_out = 0
for (let bit = 0; bit <= hex_in.length - 1; bit++) {
let char = hex_in.charAt(hex_in.length - 1 - bit)
for (let ii = 0; ii < 16; ii++) {
if ((char.compare(hex_array.charAt(ii)) == 0) || (char.compare(hex_array_2.charAt(ii)) == 0)) {
dec_out = dec_out + ii * Math.pow(16, bit)
}
}
}
return dec_out;
}
}