the c preprocessor
DESCRIPTION
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; - PowerPoint PPT PresentationTRANSCRIPT
ΗΥ150 – Προγραμματισμός Ξενοφών Ζαμπούλης
The C Preprocessor
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης2
Constants #define MAXSIZE 256 This will lead to the value 256 being substituted
for each occurrence of the word MAXSIZE in the file.
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης3
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.
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης4
Verbose
#define EBUG
#ifdef EBUGprintf(“Debug info: variable foo has value %d\n”,foo);
#endif
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης5
Verbose
#define EBUG
#ifdef EBUGprintf(“Debug info: variable foo has value %d\n”,foo);
#elseifprintf(“No need for debug\n”);
#endif
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης6
Verbose
gcc –DEBUG myprogram.c
ΗΥ150 – Προγραμματισμός Ξενοφών Ζαμπούλης
Standard I/O
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης8
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.
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης9
Character Input and Output char c = getchar(); void putchar(char c);
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης10
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.
ΗΥ150 – Προγραμματισμός Ξενοφών Ζαμπούλης
Πως ψάχνουμε για εντολές βιβλιοθήκης
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης12
Εντολή συστήματος “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().
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης13
Οργάνωση κώδικα Σε μεγαλύτερα προγράμματα, οι δηλώσεις των
συναρτήσεων συγκεντρώνονται σε ένα ή περισσότερα header files (κατάληξη .h), τα οποία συμπεριλαμβάνονται κατά την προεπεξεργασία του κώδικα:
#include "name.h" όπου name.h το όνομα του header file και μπορεί να
περιλαμβάνει και το path. Oι headers που ορίζει ο προγραμματιστής περικλείονται
σε “”. Oι headers του συστήματος περικλείονται σε <>. Με την συμπερίληψη των κατάλληλων headers:
– ο compiler γνωρίζει τον τρόπο κλήσης των συναρτήσεων ή εκτελέσιμων βιβλιοθηκών που χρειάζεται ένα τμήμα κώδικα.
– ο προγραμματιστής μπορεί να επισκοπήσει εύκολα τη λειτουργικότητα του κώδικα
ΗΥ150 – Προγραμματισμός Ξενοφών Ζαμπούλης
ΗΥ-150Προγραμματισμός
Ασκήσεις
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης15
Φωλιασμένοι βρόχοι
/*Πως θα εκτυπώναμε το παρακάτω σχήμα;*/
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
ΗΥ150 – Προγραμματισμός Ξενοφών Ζαμπούλης
Μηχανές Πεπερασμένων Καταστάσεων
Finite State Machines
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης17
Finite State Machines
letterletter | digit I EOF
S A
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης18
Finite State Machines viewed as Graphs
• The start state
• A transitiona
• A state
• Accepting/Terminal state
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης19
Example: Integer Literals
FSM that accepts integer literals with an optional + or - sign:
+
digit, ‘_’
S
B
A-
digit
digit, ‘_’
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης20
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.
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης21
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.
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης22
FSM’s in Games Character AI can be
modeled as a sequence of mental states.
“World” events can force a change in state.
Gather Treasure
Flee
Fight
Monster In Sight
No Monster
Monster Dead Cornered
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης23
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
}
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης24
Παράδειγμα: Robocode Καθορισμός
Καταστάσεων– Attack, Evade, Search,
etc. Υλοποίηση αλλαγής
κατάστασης– Include an error state that
is noticeable. Αποσφαμάτωση
(debugging)– Verbosity levels are a
must.
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης25
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
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης26
Run Length Encoding Run length encoding (RLE) απλούστερη
μέθοδος συμπίεσης. Κωδικοποιεί πληροφορία για την επανάληψη
των χαρακτήρων / στοιχείων εισόδου Είναι ο αλγόριθμος που χρησιμοποιεί το FAX
(τηλεομοιοτυπία)
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης27
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
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης28
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
ΗΥ150 – Προγραμματισμός
Ξενοφών Ζαμπούλης29
Παράδειγμα
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
12W1B12W3B24W1B14W