the c preprocessor

29
ΗΥ150 – Προγραμματισμός Ξενοφών Ζαμπούλης The C Preprocessor

Upload: cole-freeman

Post on 03-Jan-2016

41 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: The C Preprocessor

ΗΥ150 – Προγραμματισμός Ξενοφών Ζαμπούλης

The C Preprocessor

Page 2: 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.

Page 3: The C Preprocessor

ΗΥ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.

Page 4: The C Preprocessor

ΗΥ150 – Προγραμματισμός

Ξενοφών Ζαμπούλης4

Verbose

#define EBUG

#ifdef EBUGprintf(“Debug info: variable foo has value %d\n”,foo);

#endif

Page 5: The C Preprocessor

ΗΥ150 – Προγραμματισμός

Ξενοφών Ζαμπούλης5

Verbose

#define EBUG

#ifdef EBUGprintf(“Debug info: variable foo has value %d\n”,foo);

#elseifprintf(“No need for debug\n”);

#endif

Page 6: The C Preprocessor

ΗΥ150 – Προγραμματισμός

Ξενοφών Ζαμπούλης6

Verbose

gcc –DEBUG myprogram.c

Page 7: The C Preprocessor

ΗΥ150 – Προγραμματισμός Ξενοφών Ζαμπούλης

Standard I/O

Page 8: The C Preprocessor

ΗΥ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.

Page 9: The C Preprocessor

ΗΥ150 – Προγραμματισμός

Ξενοφών Ζαμπούλης9

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

Page 10: The C Preprocessor

ΗΥ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.

Page 11: The C Preprocessor

ΗΥ150 – Προγραμματισμός Ξενοφών Ζαμπούλης

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

Page 12: The C Preprocessor

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

Page 13: The C Preprocessor

ΗΥ150 – Προγραμματισμός

Ξενοφών Ζαμπούλης13

Οργάνωση κώδικα Σε μεγαλύτερα προγράμματα, οι δηλώσεις των

συναρτήσεων συγκεντρώνονται σε ένα ή περισσότερα header files (κατάληξη .h), τα οποία συμπεριλαμβάνονται κατά την προεπεξεργασία του κώδικα:

#include "name.h" όπου name.h το όνομα του header file και μπορεί να

περιλαμβάνει και το path. Oι headers που ορίζει ο προγραμματιστής περικλείονται

σε “”. Oι headers του συστήματος περικλείονται σε <>. Με την συμπερίληψη των κατάλληλων headers:

– ο compiler γνωρίζει τον τρόπο κλήσης των συναρτήσεων ή εκτελέσιμων βιβλιοθηκών που χρειάζεται ένα τμήμα κώδικα.

– ο προγραμματιστής μπορεί να επισκοπήσει εύκολα τη λειτουργικότητα του κώδικα

Page 14: The C Preprocessor

ΗΥ150 – Προγραμματισμός Ξενοφών Ζαμπούλης

ΗΥ-150Προγραμματισμός

Ασκήσεις

Page 15: The C Preprocessor

ΗΥ150 – Προγραμματισμός

Ξενοφών Ζαμπούλης15

Φωλιασμένοι βρόχοι

/*Πως θα εκτυπώναμε το παρακάτω σχήμα;*/

* *

** **

*** ***

**** ****

***** *****

****** ******

******* *******

****************

Page 16: The C Preprocessor

ΗΥ150 – Προγραμματισμός Ξενοφών Ζαμπούλης

Μηχανές Πεπερασμένων Καταστάσεων

Finite State Machines

Page 17: The C Preprocessor

ΗΥ150 – Προγραμματισμός

Ξενοφών Ζαμπούλης17

Finite State Machines

letterletter | digit I EOF

S A

Page 18: The C Preprocessor

ΗΥ150 – Προγραμματισμός

Ξενοφών Ζαμπούλης18

Finite State Machines viewed as Graphs

• The start state

• A transitiona

• A state

• Accepting/Terminal state

Page 19: The C Preprocessor

ΗΥ150 – Προγραμματισμός

Ξενοφών Ζαμπούλης19

Example: Integer Literals

FSM that accepts integer literals with an optional + or - sign:

+

digit, ‘_’

S

B

A-

digit

digit, ‘_’

Page 20: The C Preprocessor

ΗΥ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.

Page 21: The C Preprocessor

ΗΥ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.

Page 22: The C Preprocessor

ΗΥ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

Page 23: The C Preprocessor

ΗΥ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

}

Page 24: The C Preprocessor

ΗΥ150 – Προγραμματισμός

Ξενοφών Ζαμπούλης24

Παράδειγμα: Robocode Καθορισμός

Καταστάσεων– Attack, Evade, Search,

etc. Υλοποίηση αλλαγής

κατάστασης– Include an error state that

is noticeable. Αποσφαμάτωση

(debugging)– Verbosity levels are a

must.

Page 25: The C Preprocessor

ΗΥ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

Page 26: The C Preprocessor

ΗΥ150 – Προγραμματισμός

Ξενοφών Ζαμπούλης26

Run Length Encoding Run length encoding (RLE) απλούστερη

μέθοδος συμπίεσης. Κωδικοποιεί πληροφορία για την επανάληψη

των χαρακτήρων / στοιχείων εισόδου Είναι ο αλγόριθμος που χρησιμοποιεί το FAX

(τηλεομοιοτυπία)

Page 27: The C Preprocessor

ΗΥ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

Page 28: The C Preprocessor

ΗΥ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

Page 29: The C Preprocessor

ΗΥ150 – Προγραμματισμός

Ξενοφών Ζαμπούλης29

Παράδειγμα

WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW

12W1B12W3B24W1B14W