Μεταβλητές#

Εισαγωγή#

Οι μεταβλητές αποθηκεύουν δεδομένα και σας επιτρέπουν να τα επαναχρησιμοποιείτε και να τα χειρίζεστε σε όλο το πρόγραμμά σας. Η C++ είναι μια γλώσσα στατικής τυποποίησης, που σημαίνει ότι πρέπει να δηλώσετε ρητά τον τύπο μιας μεταβλητής όταν τη δημιουργείτε. Ο τύπος δεν μπορεί να αλλάξει μετά τη δήλωσή της. Για παράδειγμα:

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++ είναι επίσης έντονα τυποποιημένη, πράγμα που σημαίνει ότι δεν μπορείτε να εκτελέσετε λειτουργίες σε ασύμβατους τύπους χωρίς ρητή μετατροπή. Για παράδειγμα:

int blocks = 2;              // blocks is an integer
char rings[] = "4";          // rings is a string
int result = blocks + rings; // Creates a compilation error

Αυτό το API εξηγεί τους κοινούς τύπους μεταβλητών στην C++. Αν και δεν είναι μια εξαντλητική λίστα, καλύπτει τους τύπους που είναι πιο πιθανό να χρησιμοποιήσετε στην πράξη.

  • Τοπικές μεταβλητές — Δηλώνονται μέσα σε μια συνάρτηση και χρησιμοποιούνται μόνο εντός αυτού του πεδίου εφαρμογής. Ιδανικό για προσωρινές ή απομονωμένες τιμές.

  • Καθολικές μεταβλητές — Δηλώνεται εκτός οποιασδήποτε συνάρτησης και χρησιμοποιείται σε όλο το έργο. Κατάλληλο για την κοινή χρήση δεδομένων μεταξύ συναρτήσεων.

  • Ακέραιος — Ακέραιοι αριθμοί που χρησιμοποιούνται για μέτρηση, αποστάσεις ή οτιδήποτε άλλο χωρίς δεκαδικά ψηφία.

  • Διπλό — Δεκαδικοί αριθμοί υψηλής ακρίβειας για μαθηματικούς υπολογισμούς που απαιτούν ακρίβεια.

  • Κυμαινόμενος αριθμός — Δεκαδικοί αριθμοί τυπικής ακρίβειας, χρήσιμοι για μετρήσεις ή υπολογισμούς.

  • Συμβολοσειρά τύπου C — Τιμές κειμένου, που χρησιμοποιούνται για μηνύματα, ετικέτες ή για την εμφάνιση αναγνώσιμου αποτελέσματος.

  • Boolean (bool)true or false values for logic and decision-making.

  • Πίνακες — Συλλογές στοιχείων σταθερού μεγέθους του ίδιου τύπου.

  • Δισδιάστατοι Πίνακες — Πίνακες από πίνακες. Ιδανικοί για την αναπαράσταση γραμμών, πλεγμάτων ή δεδομένων τύπου πίνακα.

  • Const — A variable declared with const cannot be modified.

Δήλωση και ανάθεση μεταβλητής#

To create a variable in C++, you must declare its type followed by the variable name, then optionally assign a value using the = operator:

int distance = 100;

Κατά την ονομασία μιας μεταβλητής, πρέπει να τηρούνται οι ακόλουθοι κανόνες:

  • Το όνομα δεν μπορεί να περιέχει ειδικούς χαρακτήρες (π.χ., θαυμαστικό).

  • Το όνομα δεν μπορεί να ξεκινά με αριθμό.

  • Το όνομα δεν μπορεί να χρησιμοποιήσει κενά.

  • The name cannot conflict with predefined VEXcode objects (for example, Drivetrain).

  • The name cannot be a C++ reserved keyword (e.g., int, for, etc.).

  • Variable names are case-sensitive (e.g., Distance and distance are different variables).

Local Variables#

Οι τοπικές μεταβλητές ορίζονται μέσα σε μια συνάρτηση ή ένα μπλοκ κώδικα. Είναι προσβάσιμες μόνο εντός του πεδίου εφαρμογής αυτής της συνάρτησης ή του μπλοκ και δεν είναι ορατές εκτός αυτού.

Define the function (outside of int main())

void show_local() {
  // This variable only exists inside this function
  int angle = 90;
  Brain.Screen.print("%d", angle);
}

Call the function inside 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#

Οι καθολικές μεταβλητές ορίζονται εκτός οποιασδήποτε συνάρτησης ή μπλοκ. Μπορούν να προσπελαστούν και να διαβαστούν οπουδήποτε στο πρόγραμμα, συμπεριλαμβανομένων και των συναρτήσεων που βρίσκονται μέσα σε αυτές.

Σημείωση: Οι καθολικές μεταβλητές είναι προσβάσιμες από οπουδήποτε στο πρόγραμμα, γεγονός που τις καθιστά εύκολες για την κοινή χρήση δεδομένων μεταξύ συναρτήσεων. Ωστόσο, η μεγάλη εξάρτηση από καθολικές μεταβλητές μπορεί να οδηγήσει σε ακούσιες παρενέργειες, καθώς οι αλλαγές στη μεταβλητή σε ένα μέρος του προγράμματος ενδέχεται να επηρεάσουν άλλα μέρη απρόβλεπτα. Για αυτόν τον λόγο, οι τοπικές μεταβλητές προτιμώνται γενικά όταν είναι δυνατόν, καθώς περιορίζουν το πεδίο εφαρμογής μιας μεταβλητής στη συγκεκριμένη συνάρτηση όπου ορίζεται. Αυτό μειώνει την πιθανότητα διενέξεων και διευκολύνει την αποσφαλμάτωση.

Define the function (outside of 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();
}

Call the function inside 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);
}

In C++, global variables can be directly accessed and modified from within functions without any special keywords. However, if you have a local variable with the same name, you can use the scope resolution operator :: to access the global variable.

Define the function (outside of 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();
}

Call the function inside 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#

An integer (int) is a whole number.

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#

Ένα διπλό είναι ένας δεκαδικός αριθμός με υψηλή ακρίβεια (συνήθως 64-bit).

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#

Ένας float αποθηκεύει δεκαδικούς αριθμούς. Στα περισσότερα έργα VEX, οι double προτιμώνται από τους float, εκτός εάν η χρήση μνήμης αποτελεί πρόβλημα.

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#

A C-style string (char variable_name[]) is a sequence of characters stored in a character array. C-style strings must be large enough to store the text and a null terminator.

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#

Ένας δισδιάστατος πίνακας χρησιμοποιείται συνήθως για την αναπαράσταση πλεγμάτων, πινάκων ή μητρών. Κάθε γραμμή αντιπροσωπεύει μια συγκεκριμένη ομαδοποίηση δεδομένων.

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();
  }
}

Μπορείτε να τροποποιήσετε συγκεκριμένα στοιχεία μέσα σε έναν δισδιάστατο πίνακα:

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#

A variable declared with const cannot be modified after it is initialized.

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);
}