Προγραµµατισµός Επιπλέον στοιχεία της C Προγραµµατισµός
Πρόγραµµα σε ιαφορετικά Αρχεία Οργανώνουµε καλύτερα τον κώδικα Χρήσιµα σε µεγάλα προγράµµατα Παράδειγµα (human tracking) pgmio.c alloc.c pointstracking.c groups.c pgmio.h alloc.h pointstracking.h linker header files #include myheader.h extern extern void myfunction(); Προγραµµατισµός 2
header files Παράδειγµα - myalloc.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); Προγραµµατισµός 3
Ορίσµατα στη γραµµή εντολής int main(int argc, char *argv[]) argc: # Ορισµάτων argv: Ορίσµατα Παράδειγµα > a.out 10 20 test.txt //argc = 4 //argv[0] = a.out //argv[1] = 10 //argv[2] = 20 //argv[3] = test.txt int main(int argc, char *argv[]) { int number; if (argc!= 2) { printf( Usage:%s number\n,argv[0]); exit(0); } number = atoi(argv[1]); } Προγραµµατισµός 4
Χειρισµός Bits (1101) 2 = 1 x 2 3 + 1 x 2 2 + 0 x 2 1 + 1 x 2 0 = (13) 10 (A3F) 16 = 10 x 16 2 + 3 x 16 1 + 15 x 16 0 = (2623) 10 Προγραµµατισµός 5
Χειρισµός Bits Οι τελεστές των bits εφαρµόζονται σε κάθε bit των ακεραίων τιµών και είναι: ~ (ΣΥΜΠΛΗΡΩΜΑ) ~1 = 0 ~0 = 1 ~(10011010) = (01100101) & (AND) 0 & 1 = 0 1 & 1 = 1 (10010011) & (00111101) = (00010001) (OR) 0 1 = 1 0 0 = 0 (10010011) (00111101) = (10111111) ^ (XOR) 1 ^ 1 = 0 1 ^ 0 = 1 (10010011) ^ (00111101) = (10101110) Προγραµµατισµός 6
Χειρισµός Bits Τελεστές ολίσθησης << και >> x << y ολισθαίνει το αριστερά κατά y θέσεις bit Τα νέα bits είναι 0 (10001010) << 2 = (00101000) int a = 1; int b; b = a << 2; /* assigns 4 to b */ a <<= 2; /* changes a to 4 */ Προγραµµατισµός 7
Χειρισµός Bits Τελεστές ολίσθησης << και >> x >> y ολισθαίνει το δεξιά κατά y θέσεις bit Αν το x είναι unsigned, τα νέα bits είναι 0 Αλλιώς, τα νέα bits διατηρούν το πρόσηµο (ανάλογα µε την αρχιτεκτονική της µηχανής) (10001010) >> 2 = (00100010) (unsigned) (10001010) >> 2 = (11100010) (signed sign extension) Προγραµµατισµός 8
Χειρισµός Bits - Παραδείγµατα Πόσα bits του x είναι 1; int getbitsequaltoone(int x) { int n; for (n = 0; x!= 0; ++n) x &= x-1; } return n; Προγραµµατισµός 9
Χειρισµός Bits - Παραδείγµατα Μετατροπή σε δυαδικό string του ακεραίου n, και εκτύπωση του. void printbinary(int n) { int i; int size = 8 * sizeof(int); char *ps = (char *)malloc(size+1); for (i = size - 1; i >= 0; i--, n >>= 1) ps[i] = (01 & n) + '0'; ps[size] = '\0'; } printf( %s\n,ps); free(ps); Προγραµµατισµός 10
είκτες σε συναρτήσεις είκτης σε συνάρτηση Περιέχει τη διεύθυνση του κώδικα της συνάρτησης Όπως ένας δείκτης δείχνει στο 1 ο στοιχείο ενός πίνακα Γιατί; Πολλές φορές δεν ξέρουµε τη συνάρτηση θέλουµε να καλέσουµε, µέχρι να τρέξουµε το πρόγραµµα (π.χ., αν η συνάρτηση που πρέπει να καλέσουµε καθορίζεται από την είσοδο). Πολλές φορές προγραµµατίζουµε ή χρησιµοποιούµε συναρτήσεις βιβλιοθήκης οι οποίες χρησιµοποιούν άλλες υποσυναρτήσεις. Οι τελευταίες µπορεί να προµηθεύονται από τον χρήστη της βιβλιοθήκης, ώστε να αλλάζουν την συµπεριφορά της συνάρτησης, χωρίς να χρειάζεται να την ξαναµεταγλωττίσουµε. Απαραίτητες πολλές φορές για κατανοητό, ευανάγνωστο ή γρήγορο κώδικα. Αποφεύγουµε τις πολλές if/switch Προγραµµατισµός 11
είκτες σε συναρτήσεις Οι δείκτες σε συναρτήσεις Τα περνάµε σαν ορίσµατα σε συναρτήσεις Αποθηκεύονται σε πίνακες Εκχωρούνται σε άλλους δείκτες ιαφορά 1 από τους υπόλοιπους δείκτες: «πρόσβαση» στα περιεχόµενά τους: *functionptr (*functionptr)(argument1, argument2) Σε αυτήν την περίπτωση καλείται η συνάρτηση της οποίας η διεύθυνση είναι αποθηκευµένη στον δείκτη ιαφορά 2 από τους υπόλοιπους δείκτες:ανάθεση διεύθυνσης µιας συνάρτησης και όχι µεταβλητής functionptr = &afunction; Προγραµµατισµός 12
ήλωση εικτών σε Συναρτήσεις int (*funptr)(int, int) Ο τύπος της µεταβλητής funptr είναι «δείκτης σε συνάρτηση που παίρνει δύο ορίσµατα τύπου int, και επιστρέφει int. int *funptr(int, int) Αυτό θα σήµαινε, ότι το funptr είναι τύπου «συνάρτηση που παίρνει δύο ορίσµατα τύπου int, και επιστρέφει δείκτη σε int) int (*funptrarray[10])(int, int)) Τυπος του funptrarray: πίνακας από 10 δείκτες σε συναρτήσεις που παίρνουν δύο ορίσµατα τύπου int και επιστρέφουν int Προγραµµατισµός 13
Παραδείγµατα 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; } ήλωση του δείκτη σε συνάρτηση Ανάθεση της διεύθυνσης µιας συνάρτησης Κλήση της συνάρτησης που είναι αποθηκευµένη στον δείκτη Προγραµµατισµός 14
Παράδειγµα Θυµηθείτε ότι η BubbleSort ταξινοµεί ακεραίους βασιζόµενη σε συγκρίσεις µεταξύ τους. Αλλάξτε την BubbleSort ώστε να παίρνει ως όρισµα την συνάρτηση σύγκρισης δύο ακεραίων και να την χρησιµοποιεί για ταξινόµηση. Καλέστε την BubbleSort έτσι ώστε να ταξινοµεί ακεραίους και κατά αύξοντα σειρά και κατά φθίνουσα Προγραµµατισµός 15
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 Προγραµµατισµός
Binary trees O(log N) < O(N) Binary search trees: O(log N) search and insert struct tree_node { tree_node *left; tree_node *right; int data; }; Προγραµµατισµός 19
Print binary search tree void print_inorder(tree_node *p) { if (p!= NULL) { print_inorder(p->left); printf( %d,p->data); print_inorder(p->right); } } Προγραµµατισµός 20
Delete tree void deleteword(struct wordtree **node) { struct wordtree *temp = NULL; if (node!= NULL) { if(*node!= '\0') { if((*node)->right!= NULL) { temp = *node; deleteword(&temp->right); } if((*node)->left!= NULL) { temp = *node; deleteword(&temp->left); } if((*node)->word!= NULL) free((*node)->word); if((*node)->firstline!= NULL) deletelist((*node)->firstline); free(*node); *node = NULL; } } } Προγραµµατισµός 21
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. Προγραµµατισµός 22
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; }; Προγραµµατισµός 23
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. Προγραµµατισµός 24
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); } Προγραµµατισµός 25
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''); } Προγραµµατισµός 26
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); } Προγραµµατισµός 27
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; Προγραµµατισµός 28
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 */ Προγραµµατισµός 29
Preprocessor Turning debugging code off and on #define DEBUG... #ifdef DEBUG... // debugging output #endif > gcc DDEBUG myfile.c Προγραµµατισµός 30