The VEX Visual Studio Code Extension has replaced VEXcode Pro V5, which is now end-of-life.
VEXcode Blocks and VEXcode Text remain actively developed and supported for all VEX platforms.
Ví dụ: Hàm ước chung lớn nhất (ƯCLN) đơn giản (sử dụng thuật toán Euclid)
Mẫu sau đây cũng minh họa cách nó được sử dụng trong cấu trúc vòng lặp đơn giản.
Không sử dụng hàm do người dùng định nghĩa:
int main() {
int remainder = 1;
int a1, b1, a2, b2;
vexcodeInit();
a1 = a2 = 20;
b1 = b2 = 64;
while(remainder > 0){
remainder = a1 % b1;
a1 = b1;
b1 = remainder;
}
Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d", a2, b2, a1);
a1 = a2 = 60;
b1 = b2 = 200;
while(remainder > 0){
remainder = a1 % b1;
a1 = b1;
b1 = remainder;
}
Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d", a2, b2, a1);
}
Sử dụng hàm do người dùng định nghĩa:
int getGCD(int a, int b){
int remainder = 1;
while(remainder>0){
remainder = a % b;
a = b;
b = remainder;
}
return a;
}
int main() {
vexcodeInit();
Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d",getGCD(20, 64));
Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d",getGCD(60, 100));
}
Video dưới đây minh họa trình tự logic diễn ra từ hàm gọi main():
Ví dụ: Tính trung bình cộng của một tập hợp các số.
float doAverage(int n1, int n2, int n3){
float avg = (n1 + n2 + n3)/3;
return avg;
}
int main() {
int n1, n2, n3;
Brain.Screen.print("average(%d, %d, %d) = %d", 10, 20, 30);
doAverage(10, 20, 30);
n1=10; n2=20; n3=40;
Brain.Screen.print("average(%d, %d, %d) = %d", n1, n2, n3);
doAverage(n1, n2, n3);
}
Ví dụ: Cho phép robot tiến về phía trước bằng cách sử dụng giá trị bộ mã hóa biến đổi.
void reportMotionValues(){
Brain.Screen.printAt(5,100, "%8.2lf%8.2lf%8.2f",
LeftMotor.position(rev),
LeftMotor.position(deg));
}
void goStraight(float totalEnc, float vel){
LeftMotor.resetPosition();
LeftMotor.setVelocity(50.0, percent);
LeftMotor.spinToPosition(totalEnc, deg, true);
return;
}
int main() {
int enc=1000; // >0: tiến, <0: lùi
vexcodeInit();
for(int i=0; i<10; i++){
enc *= -1;
goStraight(enc, 100);
Brain.Screen.printAt(5,60, "Done");
reportMotionValues();
// Độ trễ này chỉ để bạn xem các hành động này.
Bạn không cần thêm độ trễ ở đây.
chờ(2000, msec);
}
}Như bạn thấy, gần như không thể cho phép robot tiến và lùi với số lần thay đổi; vì nó chỉ có thể được xác định trong thời gian chạy.