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.
ตัวอย่าง: ฟังก์ชันตัวหารร่วมมากอย่างง่าย (GCD) (พร้อมอัลกอริทึม Euclid)
ตัวอย่างต่อไปนี้ยังเป็นตัวอย่างวิธีการใช้ในโครงสร้างการวนซ้ำอย่างง่าย
โดยไม่ต้องใช้ฟังก์ชันที่ผู้ใช้กำหนด:
int main() {
ส่วนที่เหลือ int = 1;
int a1, b1, a2, b2;
vexcodeInit();
a1 = a2 = 20;
บี1 = บี2 = 64;
ในขณะที่(ส่วนที่เหลือ > 0){
ส่วนที่เหลือ = a1 % b1;
ก1 = ข1;
b1 = เศษ;
}
Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d", a2, b2, a1);
a1 = a2 = 60;
บี1 = บี2 = 200;
ในขณะที่(ส่วนที่เหลือ > 0){ ส่วนที่เหลือ
= a1 % b1;
ก1 = ข1;
b1 = เศษ;
}
Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d", a2, b2, a1);
}
การใช้ฟังก์ชันที่ผู้ใช้กำหนด:
int getGCD(int a, int b){
ส่วนที่เหลือ int = 1;
ในขณะที่(ส่วนที่เหลือ>0){
ส่วนที่เหลือ = a % b;
ก = ข;
b = เศษ;
}
กลับ;
}
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));
}
ที่นี่จะแสดงลำดับของการไหลของลอจิกจากฟังก์ชันผู้เรียก main():
ตัวอย่าง: การหาค่าเฉลี่ยชุดตัวเลข
float doAverage(int n1, int n2, int n3){
float avg = (n1 + n2 + n3)/3;
ผลตอบแทนเฉลี่ย;
}
int main() {
int n1, n2, n3;
Brain.Screen.print("เฉลี่ย(%d, %d, %d) = %d", 10, 20, 30);
ค่าเฉลี่ย(10, 20, 30);
น1=10; n2=20; n3=40;
Brain.Screen.print("เฉลี่ย(%d, %d, %d) = %d", n1, n2, n3);
ทำค่าเฉลี่ย(n1, n2, n3);
}
ตัวอย่าง: เปิดใช้งานหุ่นยนต์เพื่อไปข้างหน้าด้วยค่าตัวเข้ารหัสแบบแปรผัน
โมฆะ reportMotionValues () {
Brain.Screen.printAt (5,100, "%8.2lf%8.2lf%8.2f",
LeftMotor.position (rev),
LeftMotor.position (องศา));
}
โมฆะ goStraight (ลอย TotalEnc, ลอย vel) {
LeftMotor.resetPosition ();
LeftMotor.setVelocity (50.0, เปอร์เซ็นต์);
LeftMotor.spinToPosition(totalEnc, องศา, จริง);
กลับ;
}
int main() {
int enc=1000; // >0: ไปข้างหน้า, <0: ย้อนกลับ
vexcodeInit();
สำหรับ(int i=0; i<10; i++){
enc *= -1;
ไปตรง(enc, 100);
Brain.Screen.printAt (5,60, "เสร็จสิ้น");
reportMotionValues();
// ความล่าช้านี้มีให้คุณดูการกระทำเหล่านี้เท่านั้น
//คุณไม่จำเป็นต้องล่าช้าที่นี่
รอ (2000, มิลลิวินาที);
}
}
อย่างที่คุณเห็น แทบจะเป็นไปไม่ได้เลยที่หุ่นยนต์จะเดินหน้าและถอยหลังด้วยจำนวนครั้งที่แตกต่างกัน เนื่องจากสามารถกำหนดได้เฉพาะที่รันไทม์เท่านั้น