Προσαρμοσμένη μορφοποίηση συμβολοσειρών#
Εισαγωγή#
String formatting in C++ for VEX IQ (1st 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.
%d
Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους.
Return ValuesΜορφοποιεί την ακέραια τιμή ως ακέραιο αριθμό στη συμβολοσειρά.
Examplesint 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.
%.xf
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Ο αριθμός των δεκαδικών ψηφίων που θα εμφανιστούν. |
Μορφοποιεί την τιμή κινητής υποδιαστολής με τον καθορισμένο αριθμό δεκαδικών ψηφίων.
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).
1 — Μορφοποιεί τους αριθμούς ως πεζούς δεκαεξαδικούς αριθμούς.
%x
Parameters2 — Μορφοποιεί τους αριθμούς ως κεφαλαία δεκαεξαδικά.
%X
Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους.
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.
%c
Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους.
Return ValuesΜορφοποιεί την τιμή του χαρακτήρα ως έναν μόνο χαρακτήρα.
Exampleschar 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.
%s
Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους.
Return ValuesΜορφοποιεί την τιμή συμβολοσειράς ως κείμενο.
Exampleschar message[] = "IQ";
// Display a string variable
Brain.Screen.print("Hello, %s", message);
Συνδυασμός χορδών#
Combines two strings together using the strcat function.
char* strcat(char* string1, const char* string2);
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Η συμβολοσειρά στην οποία θα προστεθεί. Πρέπει να υπάρχει χώρος για το νέο κείμενο. |
|
|
Η συμβολοσειρά που θέλετε να προσθέσετε. |
Returns a pointer to the resulting string (string1).
Βεβαιωθείτε ότι η συμβολοσειρά έχει αρκετό χώρο για τους χαρακτήρες που προστίθενται. Η προσθήκη χαρακτήρων σε μια συμβολοσειρά που είναι πολύ μικρή μπορεί να αντικαταστήσει άλλες μεταβλητές και να προκαλέσει μη αναμενόμενη συμπεριφορά.
// 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.
char* strstr(const char* str, const char* substring);
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Ένας δείκτης προς τη συμβολοσειρά που αναζητείται. |
|
|
Η συμβολοσειρά που θα αναζητηθεί. |
Returns a pointer to the first occurrence of the substring, or NULL if the string is not found.
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.
\n
Αυτή η ακολουθία διαφυγής δεν έχει παραμέτρους.
Return ValuesΕισάγει έναν χαρακτήρα νέας γραμμής στην έξοδο.
NotesΑυτό λειτουργεί μόνο στην κονσόλα. Το κείμενο δεν θα αποστέλλεται στην κονσόλα μέχρι να ξεκινήσει μια νέα γραμμή.
// Display text on two lines
printf("First line\nSecond line\n");