1 / 29

The C Preprocessor

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;

Download Presentation

The C Preprocessor

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The C Preprocessor

  2. Constants • #define MAXSIZE 256 This will lead to the value 256 being substituted for each occurrence of the word MAXSIZE in the file.

  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.

  4. Verbose #define EBUG #ifdef EBUG printf(“Debug info: variable foo has value %d\n”,foo); #endif

  5. Verbose #define EBUG #ifdef EBUG printf(“Debug info: variable foo has value %d\n”,foo); #elseif printf(“No need for debug\n”); #endif

  6. Verbose gcc –DEBUG myprogram.c

  7. Standard I/O

  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.

  9. Character Input and Output • char c = getchar(); • void putchar(char c);

  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.

  11. Πως ψάχνουμε για εντολές βιβλιοθήκης

  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().

  13. Οργάνωση κώδικα • Σε μεγαλύτερα προγράμματα, οι δηλώσεις των συναρτήσεων συγκεντρώνονται σε ένα ή περισσότερα header files (κατάληξη .h), τα οποία συμπεριλαμβάνονται κατά την προεπεξεργασία του κώδικα: #include "name.h" • όπου name.h το όνομα του headerfile και μπορεί να περιλαμβάνει και το path. • Oι headers που ορίζει ο προγραμματιστής περικλείονται σε “”. • Oι headers του συστήματος περικλείονται σε <>. • Με την συμπερίληψη των κατάλληλων headers: • ο compiler γνωρίζει τον τρόπο κλήσης των συναρτήσεων ή εκτελέσιμων βιβλιοθηκών που χρειάζεται ένα τμήμα κώδικα. • ο προγραμματιστής μπορεί να επισκοπήσει εύκολα τη λειτουργικότητα του κώδικα

  14. ΗΥ-150Προγραμματισμός Ασκήσεις

  15. Φωλιασμένοι βρόχοι /*Πως θα εκτυπώναμε το παρακάτω σχήμα;*/ * * ** ** *** *** **** **** ***** ***** ****** ****** ******* ******* ****************

  16. Μηχανές Πεπερασμένων Καταστάσεων Finite State Machines

  17. Finite State Machines letter | digit I EOF letter S A

  18. Finite State Machines viewed as Graphs a • A state • The start state • A transition • Accepting/Terminal state

  19. Example: Integer Literals • FSM that accepts integer literals with an optional + or - sign: digit, ‘_’ B digit digit, ‘_’ + S A -

  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.

  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.

  22. 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

  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 }

  24. Παράδειγμα: Robocode • Καθορισμός Καταστάσεων • Attack, Evade, Search, etc. • Υλοποίηση αλλαγής κατάστασης • Include an error state that is noticeable. • Αποσφαμάτωση (debugging) • Verbosity levels are a must.

  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

  26. Run Length Encoding • Run length encoding (RLE) απλούστερη μέθοδος συμπίεσης. • Κωδικοποιεί πληροφορία για την επανάληψη των χαρακτήρων / στοιχείων εισόδου • Είναι ο αλγόριθμος που χρησιμοποιεί το FAX (τηλεομοιοτυπία)

  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

  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

  29. Παράδειγμα WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW 12W1B12W3B24W1B14W

More Related