Προγραμματισμός Επιπλέον στοιχεία της C++ Προγραμματισμός
Πρόγραμμα σε Διαφορετικά Αρχεία Οργανώνουμε καλύτερα τον κώδικα Χρήσιμα σε μεγάλα προγράμματα Παράδειγμα (human tracking) linker header files pgmio.cpp #include pgmio.h extern extern void myfunction(); Προγραμματισμός 2
header files Παράδειγμα - myalloc.h #ifndef MYHEADER_H #define MYHEADER_H... #include <stdio.h> #define Pi 3.1415927 //Orismos Domwn typedef struct Level { int level; struct List *head; struct List *tail; struct Level *next; } Level; typedef struct List { float **image; int l,c; struct List *next; } List; //Orismos global metablhtwn int globalvariable; //prototypes sunarthsewn void **Alloc2D(int dimx, int dimx_bytes, int dimy, int dimy_bytes); int **FreeInt(int **I, int dimx); Level *AllocTree(int depth, int l, int c); Level *FreeTree(Level *root); #endif /* MYHEADER_H */ Προγραμματισμός 3
Preprocessor Prevent multiple definitions in header files #ifndef MYHEADER_H #define MYHEADER_H... // This will be seen by the compiler only once #endif /* MYHEADER_H */ Προγραμματισμός 4
Preprocessor Turning debugging code off and on #define EBUG... //#define EBUG... #ifdef EBUG... // debugging output cout << Hello\n ; #endif > g++ myfile.cpp > g++ DEBUG myfile.cpp Προγραμματισμός 5
Ορίσματα στη γραμμή εντολής int main(int argc, char *argv[]) argc: # Ορισμάτων argv: Ορίσματα int main(int argc, char *argv[]) { int i; for (i=0;i<argc;i++) cout << i << << argv[i]; } Παράδειγμα > a.out 10 20 test.txt //argc = 4 //argv[0] = a.out //argv[1] = 10 //argv[2] = 20 //argv[3] = test.txt Προγραμματισμός 6
int main(int argc, char *argv[]) { int number; } if (argc!= 2) { printf( Usage:%s number\n,argv[0]); exit(0); } number = atoi(argv[1]); Προγραμματισμός 7
Δείκτες σε συναρτήσεις Δείκτης σε συνάρτηση Περιέχει τη διεύθυνση του κώδικα της συνάρτησης Όπως ένας δείκτης δείχνει στο 1 ο στοιχείο ενός πίνακα Γιατί; Πολλές φορές δεν ξέρουμε τη συνάρτηση θέλουμε να καλέσουμε, μέχρι να τρέξουμε το πρόγραμμα (π.χ., αν η συνάρτηση που πρέπει να καλέσουμε καθορίζεται από την είσοδο). Πολλές φορές προγραμματίζουμε ή χρησιμοποιούμε συναρτήσεις βιβλιοθήκης οι οποίες χρησιμοποιούν άλλες υποσυναρτήσεις. Οι τελευταίες μπορεί να προμηθεύονται από τον χρήστη της βιβλιοθήκης, ώστε να αλλάζουν την συμπεριφορά της συνάρτησης, χωρίς να χρειάζεται να την ξαναμεταγλωττίσουμε. Απαραίτητες πολλές φορές για κατανοητό, ευανάγνωστο ή γρήγορο κώδικα. Αποφεύγουμε τις πολλές if/switch Προγραμματισμός 8
Δείκτες σε συναρτήσεις Οι δείκτες σε συναρτήσεις Τα περνάμε σαν ορίσματα σε συναρτήσεις Αποθηκεύονται σε πίνακες Εκχωρούνται σε άλλους δείκτες Διαφορά 1 από τους υπόλοιπους δείκτες: «πρόσβαση» στα περιεχόμενά τους: *functionptr (*functionptr)(argument1, argument2) Σε αυτήν την περίπτωση καλείται η συνάρτηση της οποίας η διεύθυνση είναι αποθηκευμένη στον δείκτη Διαφορά 2 από τους υπόλοιπους δείκτες: ανάθεση διεύθυνσης μιας συνάρτησης και όχι μεταβλητής functionptr = &afunction; Προγραμματισμός 9
Δήλωση Δεικτών σε Συναρτήσεις int (*funptr)(int, int) Ο τύπος της μεταβλητής funptr είναι «δείκτης σε συνάρτηση που παίρνει δύο ορίσματα τύπου int, και επιστρέφει int. int *funptr(int, int) Αυτό θα σήμαινε, ότι το funptr είναι τύπου «συνάρτηση που παίρνει δύο ορίσματα τύπου int, και επιστρέφει δείκτη σε int) int (*funptrarray[10])(int, int)) Τυπος του funptrarray: πίνακας από 10 δείκτες σε συναρτήσεις που παίρνουν δύο ορίσματα τύπου int και επιστρέφουν int Προγραμματισμός 10
Παραδείγματα int function(int a, int b) { return a*b; } int main() { int (*funptr)(int, int); funptr = &function; printf( Calling the function returns %d\n, (*funptr)(4, 5)); return 0; } Δήλωση του δείκτη σε συνάρτηση Ανάθεση της διεύθυνσης μιας συνάρτησης Κλήση της συνάρτησης που είναι αποθηκευμένη στον δείκτη Προγραμματισμός 11
Παράδειγμα Θυμηθείτε ότι η BubbleSort ταξινομεί ακεραίους βασιζόμενη σε συγκρίσεις μεταξύ τους. Αλλάξτε την BubbleSort ώστε να παίρνει ως όρισμα την συνάρτηση σύγκρισης δύο ακεραίων και να την χρησιμοποιεί για ταξινόμηση. Καλέστε την BubbleSort έτσι ώστε να ταξινομεί ακεραίους και κατά αύξοντα σειρά και κατά φθίνουσα Προγραμματισμός 12
1 2 3 #include <stdio.h> 4 #define SIZE 10 5 void bubble( int [], const int, int (*)( int, int ) ); 6 int ascending( int, int ); 7 int descending( int, int ); 8 9 int main() 10 { 11 12 int order, 13 counter, 14 a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; 15 16 printf( "Enter 1 to sort in ascending order,\n" 17 "Enter 2 to sort in descending order: " ); 18 scanf( "%d", &order ); 19 printf( "\ndata items in original order\n" ); 20 21 for ( counter = 0; counter < SIZE; counter++ ) 22 printf( "%5d", a[ counter ] ); 23 24 if ( order == 1 ) { 25 bubble( a, SIZE, &ascending ); 26 printf( "\ndata items in ascending order\n" ); 27 } 28 else { 29 bubble( a, SIZE, &descending ); 30 printf( "\ndata items in descending order\n" ); 31 } Προγραμματισμός 32 Notice the function pointer parameter.
33 for ( counter = 0; counter < SIZE; counter++ ) 34 printf( "%5d", a[ counter ] ); 35 36 printf( "\n" ); 37 38 return 0; 39 } 40 41 void bubble( int work[], const int size, 42 int (*compare)( int, int ) ) 43 { 44 int pass, count; 45 46 void swap( int *, int * ); 47 48 for ( pass = 1; pass < size; pass++ ) 49 50 for ( count = 0; count < size - 1; count++ ) 51 52 if ( (*compare)( work[ count ], work[ count + 1 ] ) ) 53 swap( &work[ count ], &work[ count + 1 ] ); 54 } 55 56 void swap( int *element1ptr, int *element2ptr ) 57 { 58 int temp; 59 60 temp = *element1ptr; 61 *element1ptr = *element2ptr; 62 *element2ptr = temp; 63 } Προγραμματισμός 64 ascending and descending return true or false. bubble calls swap if the function call returns true. Notice how function pointers are called using the dereferencing operator. The * is not required, but emphasizes that compare is a function pointer and not a function.
65 int ascending( int a, int b ) 66 { 67 return b < a; /* swap if b is less than a */ 68 } 69 70 int descending( int a, int b ) 71 { 72 return b > a; /* swap if b is greater than a */ 73 } 73 } Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 1 Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 68 89 Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 2 Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in descending order 89 68 45 37 12 10 8 6 4 2 Προγραμματισμός
Time time_t time(time_t *tloc) returns the time since 00:00:00 GMT, Jan. 1, 1970, measured in seconds. time() returns the value of time on success. If tloc is not NULL, the return value is also stored in the location to which tloc points. On failure, it returns (time_t) -1. time_t is typedefed to a long (int) in <sys/types.h> and <sys/time.h> header files. Προγραμματισμός 16
Time int ftime(struct timeb *tp) fills in a structure pointed to by tp, as defined in <sys/timeb.h> On success, ftime() returns no useful value. On failure, it returns -1. struct timeb { time_t time; unsigned short millitm; short timezone; short dstflag; }; Προγραμματισμός 17
Time char *ctime(time_t *clock), char *asctime(struct tm *tm) ctime() converts a long integer, pointed to by clock, to a 26-character string of the form produced by asctime(). It first breaks down clock to a tm structure by calling localtime(), and then calls asctime() to convert that tm structure to a string. asctime() converts a time value contained in a tm structure to a 26-character string of the form: Sun Sep 16 01:03:52 1973 asctime() returns a pointer to the string. Προγραμματισμός 18
Timing example #include <time.h> main() { int i; time_t t1,t2; time(&t1); int sum = 0; for (i=1;i<=300;++i) sum++; //printf(``%d %d %d n'',i, i*i, i*i*i); time(&t2); printf(`` n Time to do 300 squares and cubes= %d seconds n'', (int) t2-t1); } Προγραμματισμός 19
Running system commands from C int system(char *string) system returns the exit status of the shell. system is prototyped in <stdlib.h> main() { printf( Files in Directory are: n''); system( ls -l''); } Προγραμματισμός 20
Running system commands from C execl stands for execute and leave which means that a process will get executed and then terminated by execl. execl(char *path, char *arg0,...,char *argn, 0); The last parameter must always be 0. It is a NULL terminator. Since the argument list is variable we must have some way of telling C when it is to end. The NULL terminator does this job. execl( /bin/ls'', ls'', -l'',0); system( /myhome/mya.out.exe 10 20 test.txt ); Προγραμματισμός 21
Random numbers Random positive integers - rand() #include <ctime> // For time() #include <cstdlib> // For srand() and rand() srand(time(0)); // Initialize random number generator. r = (rand() % 10) + 1; Προγραμματισμός 22