ตัวแปร#
การแนะนำ#
ตัวแปรใช้สำหรับจัดเก็บข้อมูลและช่วยให้คุณสามารถนำข้อมูลนั้นมาใช้ซ้ำและจัดการได้ตลอดทั้งโปรแกรมของคุณ ภาษา C++ เป็นภาษาที่กำหนดชนิดข้อมูลแบบคงที่ (statically typed) ซึ่งหมายความว่าคุณต้องประกาศชนิดของตัวแปรอย่างชัดเจนเมื่อคุณสร้างมันขึ้นมา ชนิดของตัวแปรจะไม่สามารถเปลี่ยนแปลงได้หลังจากประกาศแล้ว ตัวอย่างเช่น:
int angle = 90; // angle is an integer
char dist[] = "Distance"; // dist is an array of characters
double steps = 2.5; // steps is a double
C++ เป็นภาษาที่มีการกำหนดประเภทข้อมูลอย่างเข้มงวด (strongly typed) ซึ่งหมายความว่าคุณไม่สามารถดำเนินการกับประเภทข้อมูลที่ไม่เข้ากันได้โดยไม่ต้องแปลงประเภทข้อมูลอย่างชัดเจน ตัวอย่างเช่น:
int blocks = 2; // blocks is an integer
char rings[] = "4"; // rings is a string
int result = blocks + rings; // Creates a compilation error
API นี้อธิบายประเภทตัวแปรทั่วไปในภาษา C++ แม้จะไม่ใช่รายการที่ครบถ้วนสมบูรณ์ แต่ก็ครอบคลุมประเภทที่คุณน่าจะใช้บ่อยที่สุดในทางปฏิบัติ
ตัวแปรโลคอล — ประกาศภายในฟังก์ชันและใช้เฉพาะภายในขอบเขตนั้นเท่านั้น เหมาะสำหรับค่าชั่วคราวหรือค่าที่แยกออกมา
ตัวแปรส่วนกลาง — ประกาศไว้นอกฟังก์ชันใดๆ และใช้งานตลอดทั้งโปรเจกต์ เหมาะสำหรับการแชร์ข้อมูลระหว่างฟังก์ชัน
จำนวนเต็ม — จำนวนเต็มที่ใช้สำหรับการนับ ระยะทาง หรือสิ่งใดก็ตามที่ไม่มีทศนิยม
Double — ตัวเลขทศนิยมความแม่นยำสูงสำหรับการคำนวณทางคณิตศาสตร์ที่ต้องการความถูกต้อง
Float — ตัวเลขทศนิยมความแม่นยำมาตรฐาน มีประโยชน์สำหรับการวัดหรือการคำนวณ
สตริงแบบ C — ค่าข้อความ ใช้สำหรับข้อความ ป้ายกำกับ หรือแสดงผลลัพธ์ที่อ่านได้
บูลีน (bool) —
trueหรือfalseค่าสำหรับตรรกะและการตัดสินใจอาร์เรย์ — คอลเลกชันขององค์ประกอบที่มีขนาดคงที่และมีประเภทเดียวกัน
อาร์เรย์ 2 มิติ — อาร์เรย์ของอาร์เรย์ เหมาะสำหรับการแสดงข้อมูลเป็นแถว ตาราง หรือข้อมูลแบบตาราง
Const — ตัวแปรที่ประกาศด้วย
constไม่สามารถแก้ไขได้
การประกาศและการกำหนดค่าตัวแปร#
ในการสร้างตัวแปรในภาษา C++ คุณต้องประกาศชนิดของตัวแปร ตามด้วยชื่อตัวแปร จากนั้นจึงกำหนดค่าให้กับตัวแปรโดยใช้ตัวดำเนินการ = (ถ้าต้องการ)
int distance = 100;
ในการตั้งชื่อตัวแปร ต้องปฏิบัติตามกฎต่อไปนี้:
ชื่อต้องไม่มีอักขระพิเศษ (เช่น เครื่องหมายอัศเจรีย์)
ชื่อต้องไม่ขึ้นต้นด้วยตัวเลข
ห้ามเว้นวรรคในชื่อ
ชื่อต้องไม่ซ้ำกับอ็อบเจ็กต์ VEXcode ที่กำหนดไว้ล่วงหน้า (ตัวอย่างเช่น
Drivetrain)ชื่อดังกล่าวต้องไม่ใช่คำสงวนของภาษา C++ (เช่น
int,forเป็นต้น)ชื่อตัวแปรนั้นต้องตรงตามตัวพิมพ์ใหญ่เล็ก (เช่น
Distanceและdistanceเป็นตัวแปรที่แตกต่างกัน)
Local Variables#
ตัวแปรโลคอล คือตัวแปรที่ถูกกำหนดขึ้น ภายใน ฟังก์ชันหรือบล็อกโค้ด สามารถเข้าถึงได้เฉพาะภายในขอบเขตของฟังก์ชันหรือบล็อกนั้นเท่านั้น และไม่สามารถมองเห็นได้จากภายนอก
กำหนดฟังก์ชัน (นอก
int main())void show_local() { // This variable only exists inside this function int angle = 90; Brain.Screen.print("%d", angle); }เรียกฟังก์ชันภายใน
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Begin project code show_local(); }
ตัวแปรโลคอลมักใช้สำหรับเก็บค่าชั่วคราวที่เกี่ยวข้องเฉพาะภายในฟังก์ชันหรือส่วนใดส่วนหนึ่งของโปรแกรมเท่านั้น
Global Variables#
ตัวแปรส่วนกลาง คือตัวแปรที่ถูกกำหนดขึ้น นอก ฟังก์ชันหรือบล็อกใดๆ สามารถเข้าถึงและอ่านค่าได้ ทุกที่ ในโปรแกรม รวมถึงภายในฟังก์ชันด้วย
หมายเหตุ: ตัวแปรส่วนกลาง สามารถเข้าถึงได้จากทุกที่ในโปรแกรม ซึ่งอาจทำให้สะดวกในการแบ่งปันข้อมูลระหว่างฟังก์ชันต่างๆ อย่างไรก็ตาม การพึ่งพา ตัวแปรส่วนกลาง มากเกินไปอาจนำไปสู่ผลข้างเคียงที่ไม่พึงประสงค์ได้ เนื่องจาก1การเปลี่ยนแปลงตัวแปรในส่วนหนึ่งของโปรแกรมอาจส่งผลกระทบต่อส่วนอื่นๆ อย่างไม่คาดคิด ด้วยเหตุนี้ ตัวแปรเฉพาะที่ จึงเป็นที่นิยมมากกว่าเมื่อเป็นไปได้ เนื่องจากจำกัดขอบเขตของตัวแปรให้อยู่เฉพาะในฟังก์ชันที่กำหนดค่าไว้เท่านั้น ซึ่งจะช่วยลดโอกาสเกิดข้อขัดแย้งและทำให้การแก้ไขข้อผิดพลาดง่ายขึ้น
กำหนดฟังก์ชัน (นอก
int main())// The variable is defined outside a function int angle = 90; void show_global() { // You can access 'angle' inside a function Brain.Screen.print("%d", angle); Brain.Screen.newLine(); }เรียกฟังก์ชันภายใน
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Begin project code show_global(); Brain.Screen.print("%d", angle); }
ในภาษา C++ ตัวแปรส่วนกลางสามารถเข้าถึงและแก้ไขได้โดยตรงจากภายในฟังก์ชันโดยไม่ต้องใช้คีย์เวิร์ดพิเศษใดๆ อย่างไรก็ตาม หากคุณมีตัวแปรส่วนท้องถิ่นที่มีชื่อเดียวกัน คุณสามารถใช้ตัวดำเนินการกำหนดขอบเขต :: เพื่อเข้าถึงตัวแปรส่วนกลางได้
กำหนดฟังก์ชัน (นอก
int main())// Define the global variable int count = 0; void increase_count() { // Global variables can be accessed directly count = count + 1; Brain.Screen.print("Count: %d", count); Brain.Screen.newLine(); }เรียกฟังก์ชันภายใน
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Begin project code increase_count(); increase_count(); }
ประเภทของข้อมูล#
ภาษา C++ รองรับข้อมูลหลายประเภทที่สามารถเก็บไว้ในตัวแปรได้ บางประเภทสามารถกำหนดค่าใหม่ได้ ในขณะที่บางประเภทอนุญาตให้แก้ไขค่าแต่ละส่วนได้ ด้านล่างนี้คือประเภทข้อมูลที่ใช้กันทั่วไปมากที่สุด:
Integer#
จำนวนเต็ม (int) คือจำนวนเต็มบวกหรือลบ
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
int distance = 100;
// Move the robot forward for the variable value in mm
Drivetrain.driveFor(forward, distance, mm);
// Add to the variable and move forward the new value,
// for 200mm total
wait(1, seconds);
distance = distance + 100;
Drivetrain.driveFor(forward, distance, mm);
}
Double#
double คือตัวเลขทศนิยมที่มีความแม่นยำสูง (โดยทั่วไปคือ 64 บิต)
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Store a value with decimal points
double raw_value = 0.88;
// Print the decimal value as a percentage
Brain.Screen.print("%.1f%%", raw_value * 100);
}
Float#
A float stores decimal numbers. In most VEX projects, doubles are preferred over floats unless memory usage is a concern.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Store a value with decimal points
float raw_value = 0.88;
// Print the decimal value as a percentage
Brain.Screen.print("%.1f%%", raw_value * 100);
}
String#
สตริงแบบ C (char variable_name[]) คือลำดับของอักขระที่เก็บไว้ในอาร์เรย์อักขระ สตริงแบบ C ต้องมีขนาดใหญ่พอที่จะเก็บข้อความและตัวจบสตริงที่เป็นค่าว่างได้
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Set the variable to a string then print the string
char message[] = "Ready!";
Brain.Screen.print(message);
}
Boolean#
A Boolean (bool) represents true or false values.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Set the state of the variable
bool delivered = false;
// Print different messages depending on the Boolean.
if (delivered) {
Brain.Screen.print("Package delivered!");
} else {
Brain.Screen.print("Delivering...");
}
}
ค่าบูลีนสามารถเปลี่ยนแปลงได้ทุกจุดในโครงการ
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Print the value of the delivered variable
bool delivered = false;
// Print different messages depending on the Boolean.
if (delivered) {
Brain.Screen.print("Package delivered!");
} else {
Brain.Screen.print("Delivering...");
}
wait(2, seconds);
// Clear the screen and print the value of the variable again
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1,1);
delivered = true;
// Print different messages depending on the Boolean.
if (delivered) {
Brain.Screen.print("Package delivered!");
} else {
Brain.Screen.print("Delivering...");
}
}
Arrays#
อาร์เรย์ คือกลุ่มขององค์ประกอบที่มีขนาดคงที่และเป็นชนิดเดียวกัน โดยเริ่มต้นที่ดัชนี 0 ควรทราบความยาวของอาร์เรย์และประกาศไว้ขณะสร้าง
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Declare an array of 5 integers
int distances[5] = {100, 200, 150, 300, 250};
// Drive and turn 4 times to move the distance of the first four distances of the array
for (int i = 0; i < 4; i++) {
Drivetrain.driveFor(forward, distances[i], mm);
Drivetrain.turnFor(right, 90, degrees);
}
}
2D Arrays#
อาร์เรย์สองมิติ (2D array) มักใช้ในการแสดงตารางหรือเมทริกซ์ โดยแต่ละแถวจะแสดงถึงกลุ่มข้อมูลเฉพาะกลุ่มหนึ่ง
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Assign the values in the matrix 2D array
int matrix[3][3] = {
{ 0, 1, 2 },
{ 3, 4, 5 },
{ 6, 7, 8 }
};
// Loop through each row
for (int i = 0; i < 3; i++) {
// Loop through each column in the row
for (int j = 0; j < 3; j++) {
Brain.Screen.print("%d, ", matrix[i][j]);
}
Brain.Screen.newLine();
}
}
คุณสามารถแก้ไของค์ประกอบเฉพาะภายในอาร์เรย์ 2 มิติได้:
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Assign the values in the matrix 2D array
int matrix[3][3] = {
{ 0, 1, 2 },
{ 3, 4, 5 },
{ 6, 7, 8 }
};
// Modify the color (in column 2) in row 0
matrix[0][1] = 200;
// Print the modified row from the matrix 2D array
for (int j = 0; j < 3; j++) {
Brain.Screen.print("%d, ", matrix[0][j]);
}
}
Constant Variables#
ตัวแปรที่ประกาศด้วย const จะไม่สามารถแก้ไขได้หลังจากที่ได้กำหนดค่าเริ่มต้นแล้ว
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
const int maxSpeed = 100;
// maxSpeed can be used like a normal variable
Drivetrain.setDriveVelocity(maxSpeed, percent);
}