140 likes | 293 Views
HY150. Ε π ι π λέον στοιχεία της C. Πρόγραμμα σε Διαφορετικά Αρχεία. Οργανώνουμε καλύτερα τον κώδικα Χρήσιμα σε μεγάλα π ρογράμματα Παράδειγμα ( human tracking) Linker, - Ι header files #include “myheader.h” extern extern void myfunction();.
E N D
HY150 ΕπιπλέονστοιχείατηςC
ΠρόγραμμασεΔιαφορετικάΑρχεία • Οργανώνουμεκαλύτερατονκώδικα • Χρήσιμασεμεγάλα προγράμματα • Παράδειγμα (human tracking) • Linker, -Ι • header files • #include “myheader.h” • extern • extern void myfunction();
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);
Ορίσματαστηγραμμήεντολής int main(int argc, char *argv[]) { int number; if (argc != 2) { printf(“Usage:%s number\n”,argv[0]); exit(0); } number = atoi(argv[1]); … } 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[], char *env[]) { int count = 0; for (count = 0; env[count] != 0; count++ ) printf("%s\n",env[count]); } NULL terminated array
env[0] : ALLUSERSPROFILE=C:\Documents and Settings\All Usersenv[1] : APPDATA=C:\Documents and Settings\founder\Application Dataenv[2] : CLASS_HOME=D:\Program Files\Java\jdk1.6.0_02\lib\dt.jar;.;D:\Program Files\Java\jdk1.6.0_02\libenv[3] : CommonProgramFiles=C:\Program Files\Common Filesenv[4] : COMPUTERNAME=LUOSIYONGenv[5] : ComSpec=C:\WINDOWS\system32\cmd.exeenv[6] : FP_NO_HOST_CHECK=NOenv[7] : HOMEDRIVE=C:env[8] : HOMEPATH=\Documents and Settings\founderenv[9] : JAVA_HOME=D:\Program Files\Java\jdk1.6.0_02env[10] : LOGONSERVER=\\LUOSIYONGenv[11] : NUMBER_OF_PROCESSORS=1env[12] : OS=Windows_NTenv[13] : Path=C:\windows\system32;D:\Program Files\Java\jdk1.6.0_02\bin;D:\xampp\mysql\binenv[14] : PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSHenv[15] : PROCESSOR_ARCHITECTURE=x86env[16] : PROCESSOR_IDENTIFIER=x86 Family 15 Model 47 Stepping 2, AuthenticAMDenv[17] : PROCESSOR_LEVEL=15env[18] : PROCESSOR_REVISION=2f02env[19] : ProgramFiles=C:\Program Filesenv[20] : PROMPT=$P$Genv[21] : SESSIONNAME=Consoleenv[22] : SystemDrive=C:env[23] : SystemRoot=C:\WINDOWSenv[24] : TEMP=C:\DOCUME~1\founder\LOCALS~1\Tempenv[25] : TMP=C:\DOCUME~1\founder\LOCALS~1\Tempenv[26] : USERDOMAIN=LUOSIYONGenv[27] : USERNAME=founderenv[28] : USERPROFILE=C:\Documents and Settings\founderenv[29] : VS90COMNTOOLS=D:\Program Files\VS2008\Common7\Tools\env[30] : windir=C:\WINDOWS
Χειρισμός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)
Χειρισμός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 */
ΧειρισμόςBits • Τελεστέςολίσθησης << και >> • x >> y ολισθαίνειτοδεξιάκατάy θέσειςbit • Αντοx είναιunsigned, τανέαbits είναι 0 • Αλλιώς, τανέαbits διατηρούντο πρόσημο (ανάλογαμετηναρχιτεκτονικήτηςμηχανής) • (10001010) >> 2 = (00100010) (unsigned) • (10001010) >> 2 = (11100010) (signed – sign extension)
#define ANIM_LOOP 1 // (0000 0001) • #define ANIM_ONCE 2 // (0000 0010) • #define ANIM_MAXSPEED 4 // (0000 0100) • #define ANIM_MINSPEED 8 // (0000 1000) • #define ANIM_CUSTSPEED 16 // (0001 0000) • #define ANIM_LINK 32 // (0010 0000) • #define ANIM_LINKALL 64 // (0100 0000)
Testing a bit Problem: given int i, is bit n set (equal to 1)? 0000 0000 0000 0000 0000 0000 0000 0010 How can we test if this bit is 1? We can use & operator with a "mask" variable: mask 0000 0000 0000 0000 0010 0000 0000 0000 if (i & mask) printf ("yes"); else printf ("no");
Toggling a bit and leaving all other bits unchanged x = x ^ mask; (or shorthand x ^= mask;) Bits that are set to 1 in the mask will be toggled in x. Bits that are set to 0 in the mask will be unchanged in x. Toggling means that if the bit is 1, it is set to 0, and if the bit is 0, it is set to 1. XOR truth table 0 ^ 0 = 0 1 ^ 0 = 1 0 ^ 1 = 1 1 ^ 1 = 0
Setting a bit to zero and leaving all other bits unchanged x = x & mask; (or x &= mask;) Bits that are set to 1 in the mask will be unchanged in x. Bits that are set to 0 in the mask will be set to zero in x. AND truth table 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1
Setting a bit to one and leaving all other bits unchanged x = x | mask; (or x |= mask;) Bits that are set to 1 in the mask will be set to one in x. Bits that are set to 0 in the mask will be unchanged in x. OR truth table 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1 1 | 1 = 1