290 likes | 447 Views
The C Preprocessor. Constants. #define MAXSIZE 256 This will lead to the value 256 being substituted for each occurrence of the word MAXSIZE in the file. Macros. Here is an example of a macro which won't work . #define DOUBLE(x) x+x For statement: a = DOUBLE(b) * c;
E N D
Constants • #define MAXSIZE 256 This will lead to the value 256 being substituted for each occurrence of the word MAXSIZE in the file.
Macros • Here is an example of a macro which won't work. #define DOUBLE(x) x+x • For statement: a = DOUBLE(b) * c; • will be expanded to a = b+b * c; • Since * has a higher priority than +, the compiler will treat it as: a = b + (b * c); • But: #define DOUBLE(x) (x+x) • Here the brackets around the definition force the expression to be evaluated before any surrounding operators are applied.
Verbose #define EBUG #ifdef EBUG printf(“Debug info: variable foo has value %d\n”,foo); #endif
Verbose #define EBUG #ifdef EBUG printf(“Debug info: variable foo has value %d\n”,foo); #elseif printf(“No need for debug\n”); #endif
Verbose gcc –DEBUG myprogram.c
UNIX File Redirection • To run prog1 but read data from file infile instead of the keyboard, you would type prog1 < infile • To run prog1 and write data to outfile instead of the screen, you would type prog1 > outfile • Both can also be combined as in prog1 < infile > outfile • Redirection is simple, and allows a single program to read or write data to or from files or the screen and keyboard.
Character Input and Output • char c = getchar(); • void putchar(char c);
EOF, The End of File Marker • EOF is a character which indicates the end of a file. It is returned by read commands of the getc and scanf families when they try to read beyond the end of a file / input.
Πως ψάχνουμε για εντολές βιβλιοθήκης
Εντολή συστήματος “man” • If you already know the name of the function you want, you can read the page by typing (to find about strcat). > man 3 strcat • If you don't know the name of the function, a full list is included in the introductory page for section 3 of the manual. To read this, type > man 3 intro • There are approximately 700 functions described here. This number tends to increase with each upgrade of the system. On any manual page, the SYNOPSIS section will include information on the use of the function. For example #include <time.h> char *ctime(time_t *clock) • This means that you must have #include <time.h> in your file before you call ctime. And that function ctime takes a pointer to type time_t as an argument, and returns a string (char *). time_t will probably be defined in the same manual page. The DESCRIPTION section will then give a short description of what the function does. For example ctime() converts a long integer, pointed to by clock, to a 26-character string of the form produced by asctime().
Οργάνωση κώδικα • Σε μεγαλύτερα προγράμματα, οι δηλώσεις των συναρτήσεων συγκεντρώνονται σε ένα ή περισσότερα header files (κατάληξη .h), τα οποία συμπεριλαμβάνονται κατά την προεπεξεργασία του κώδικα: #include "name.h" • όπου name.h το όνομα του headerfile και μπορεί να περιλαμβάνει και το path. • Oι headers που ορίζει ο προγραμματιστής περικλείονται σε “”. • Oι headers του συστήματος περικλείονται σε <>. • Με την συμπερίληψη των κατάλληλων headers: • ο compiler γνωρίζει τον τρόπο κλήσης των συναρτήσεων ή εκτελέσιμων βιβλιοθηκών που χρειάζεται ένα τμήμα κώδικα. • ο προγραμματιστής μπορεί να επισκοπήσει εύκολα τη λειτουργικότητα του κώδικα
ΗΥ-150Προγραμματισμός Ασκήσεις
Φωλιασμένοι βρόχοι /*Πως θα εκτυπώναμε το παρακάτω σχήμα;*/ * * ** ** *** *** **** **** ***** ***** ****** ****** ******* ******* ****************
Μηχανές Πεπερασμένων Καταστάσεων Finite State Machines
Finite State Machines letter | digit I EOF letter S A
Finite State Machines viewed as Graphs a • A state • The start state • A transition • Accepting/Terminal state
Example: Integer Literals • FSM that accepts integer literals with an optional + or - sign: digit, ‘_’ B digit digit, ‘_’ + S A -
FSM’s in Theory • Simple theoretical construct • Set of states (S) • Input vocabulary (I) • Transitional function T(s,i) • A way of denoting how an program can change its state over time.
FSM’s in Practice • Each state represents some desired behavior. • The transition function T resides across all states. • Each state “knows” how to transition to other states. • Accepting states (those that require more input) are considered to be the end of execution for an FSM. • Input to the FSM continues as long as the game continues.
FSM’s in Games • Character AI can be modeled as a sequence of mental states. • “World” events can force a change in state. Monster In Sight Gather Treasure Flee No Monster Fight Monster Dead Cornered
FSM Implementation - Code • Simplest method • After an action, the state might change. void RunLogic( int state ) { switch( state ) { case 0: //Wander Wander(); if( SeeEnemy() ) state = 1; if( Dead() ) state = 2; break; case 1: //Attack Attack(); state = 0; if( Dead() ) state = 2; break; case 3: //Dead DeleteAvatar(); break; } return statel }
Παράδειγμα: Robocode • Καθορισμός Καταστάσεων • Attack, Evade, Search, etc. • Υλοποίηση αλλαγής κατάστασης • Include an error state that is noticeable. • Αποσφαμάτωση (debugging) • Verbosity levels are a must.
More FSM Extensions • Fuzzy State Machines • Degrees of truth allow multiple FSM’s to contribute to character actions. • Multiple FSM’s • High level FSM coordinates several smaller FSM’s. • Polymorphic FSM’s • Allows common behavior to be shared. • Soldier -> German -> Machine Gunner
Run Length Encoding • Run length encoding (RLE) απλούστερη μέθοδος συμπίεσης. • Κωδικοποιεί πληροφορία για την επανάληψη των χαρακτήρων / στοιχείων εισόδου • Είναι ο αλγόριθμος που χρησιμοποιεί το FAX (τηλεομοιοτυπία)
RLE • As a basic example, consider the following string of numbers: 5 5 5 5 8 8 8 2 2 2 2 2 • There is a fair amount of redundancy there. In RLE notation, this same string could be expressed as: 4 5 3 8 5 2
RLE 1 2 3 4 5 6 • Apply the same RLE compression scheme as before: 1 1 1 2 1 3 1 4 1 5 1 6
Παράδειγμα WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW 12W1B12W3B24W1B14W