Προσαρμοσμένη μορφοποίηση συμβολοσειρών#

Εισαγωγή#

String formatting in C++ for VEX IQ (2nd gen) uses printf-style format specifiers to create strings that include variables, expressions, or values. This method uses format specifiers like %d, %f, %s, etc., to insert values into strings.

Μορφοποίηση αριθμών — Εισαγωγή τιμών και παραστάσεων σε μια συμβολοσειρά.

  • %d — Formats integers.

  • %.xf — Sets the number of decimal places to show for floats.

  • %x — Formats a number as hexadecimal.

  • %c — Formats single characters.

  • %s — Formats C-style strings.

Συνδυασμός συμβολοσειρών — Συνδυάστε κείμενο και τιμές.

  • strcat — Concatenate C-style strings manually.

Έλεγχος για υποσυμβολοσειρές — Έλεγχος για παρουσία ή θέση κειμένου.

  • strstr — Finds a substring within a string.

Ακολουθίες διαφυγής — Μορφοποίηση εξόδου με ειδικούς χαρακτήρες.

  • \n — Adds a line break (new line).

Μορφοποίηση αριθμών#

Η C++ χρησιμοποιεί προσδιοριστές μορφοποίησης για να ελέγχει τον τρόπο με τον οποίο εμφανίζονται οι αριθμοί και άλλοι τύποι δεδομένων σε συμβολοσειρές.

Integers#

Formats integer values using the %d format specifier.

Format specifiers
%d

Parameters

Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους.

Return Values

Μορφοποιεί την ακέραια τιμή ως ακέραιο αριθμό στη συμβολοσειρά.

Examples
int count = 42;

// Display a single variable 
Brain.Screen.print("Count: %d", count);

int x = 1;
int y = 5;

// Display multiple variables
Brain.Screen.print("Position: (%d, %d)", x, y);

// Display a calculation
Brain.Screen.print("Sum: %d", 5 + 3);

// Display the robot's battery capacity
Brain.Screen.print("Battery: %d%%", Brain.Battery.capacity());

Fixed Decimal Places#

Controls how many decimal places a floating-point number is displayed with using the %.xf format specifier.

Format specifiers
%.xf

Parameters

Παράμετρος

Τύπος

Περιγραφή

x

int

Ο αριθμός των δεκαδικών ψηφίων που θα εμφανιστούν.

Return Values

Μορφοποιεί την τιμή κινητής υποδιαστολής με τον καθορισμένο αριθμό δεκαδικών ψηφίων.

Examples
// Display pi with 2 decimal places
double pi = 3.1415926535;
Brain.Screen.print("Pi: %.2f", pi);

Hexadecimal#

Converts numbers to hexadecimal representation using %x (lowercase) or %X (uppercase).

Format specifiers

1 Μορφοποιεί τους αριθμούς ως πεζούς δεκαεξαδικούς αριθμούς.

%x

2 Μορφοποιεί τους αριθμούς ως κεφαλαία δεκαεξαδικά.

%X

Parameters

Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους.

Return Values

Μορφοποιεί την ακέραια τιμή ως δεκαεξαδική συμβολοσειρά.

Examples
// Convert 255 to hexadecimal
int number = 255;

// Display hexadecimals 
Brain.Screen.print("Hex: %x", number);

Brain.Screen.print("Hex: 0x%X", number);

Characters#

Formats single characters using the %c format specifier.

Format specifiers
%c

Parameters

Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους.

Return Values

Μορφοποιεί την τιμή του χαρακτήρα ως έναν μόνο χαρακτήρα.

Examples
char letter = 'A';

// Display a single character variable
Brain.Screen.print("Letter: %c", letter);

Strings#

Formats C-style strings (character arrays) using the %s format specifier.

Format specifiers
%s

Parameters

Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους.

Return Values

Μορφοποιεί την τιμή συμβολοσειράς ως κείμενο.

Examples
char message[] = "IQ";

// Display a string variable
Brain.Screen.print("Hello, %s", message);

Συνδυασμός χορδών#

Combines two strings together using the strcat function.

Available Functions
char* strcat(char* string1, const char* string2);

Parameters

Παράμετρος

Τύπος

Περιγραφή

string1

char*

Η συμβολοσειρά στην οποία θα προστεθεί. Πρέπει να υπάρχει χώρος για το νέο κείμενο.

string2

const char*

Η συμβολοσειρά που θέλετε να προσθέσετε.

Return Values

Returns a pointer to the resulting string (string1).

Notes
  • Βεβαιωθείτε ότι η συμβολοσειρά έχει αρκετό χώρο για τους χαρακτήρες που προστίθενται. Η προσθήκη χαρακτήρων σε μια συμβολοσειρά που είναι πολύ μικρή μπορεί να αντικαταστήσει άλλες μεταβλητές και να προκαλέσει μη αναμενόμενη συμπεριφορά.

Examples
// Make sure the variable has enough space
// for all characters (set it to 20)
char message[20] = "Hello,";
strcat(message, " IQ");

// Display the combined strings
Brain.Screen.print("%s", message);

Έλεγχος για υποσυμβολοσειρές#

Returns a pointer to the position of the first occurrence of a C-style string in another string using the strstr function.

Available Functions
char* strstr(const char* str, const char* substring);

Parameters

Παράμετρος

Τύπος

Περιγραφή

str

const char*

Ένας δείκτης προς τη συμβολοσειρά που αναζητείται.

substring

const char*

Η συμβολοσειρά που θα αναζητηθεί.

Return Values

Returns a pointer to the first occurrence of the substring, or NULL if the string is not found.

Examples
char message[] = "Hey everyone!";

// Display if "Hey" is in the variable
if (strstr(message, "Hey") != NULL) {
    Brain.Screen.print("Hello!");
}

Ακολουθίες διαφυγής#

Οι ακολουθίες διαφυγής είναι ειδικοί χαρακτήρες που χρησιμοποιούνται μέσα σε συμβολοσειρές για τη μορφοποίηση του κειμένου εξόδου.

New Line#

Moves text to a new line when printing using the \n escape sequence.

Format specifiers
\n

Parameters

Αυτή η ακολουθία διαφυγής δεν έχει παραμέτρους.

Return Values

Εισάγει έναν χαρακτήρα νέας γραμμής στην έξοδο.

Notes
  • Αυτό λειτουργεί μόνο στην κονσόλα. Το κείμενο δεν θα αποστέλλεται στην κονσόλα μέχρι να ξεκινήσει μια νέα γραμμή.

Examples
// Display text on two lines
printf("First line\nSecond line\n");