Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte...

25
INFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare) Kontrollstrukturen Präprozessoranweisungen Libraries Funktionen und Klassen

Transcript of Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte...

Page 1: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.108.05.2011

Dr.-Ing. habil. Jörg Wollnack

Inhalte

Informatik IInformatik I

Einführung

Algorithmus, Programmiersprache, Compiler und Linker

Datentypen (elementare)

Kontrollstrukturen

Präprozessoranweisungen

Libraries

Funktionen und Klassen

Page 2: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.208.05.2011

Dr.-Ing. habil. Jörg Wollnack

Type Name Bytes Other Names

int *signed, signed int

System dependent

unsigned int * unsigned System dependent

__int8 1char, signed char

–128 to 127

__int16 2short,

short int, signed short int

–32.768 to 32.767

__int32 4signed, signed int

–2.147.483.648 to 2.147.483.647

__int64 8 None–9.223.372.036.854.775.808 to 9.223.372.036.854.775.807

char 1 signed char –128 to 127

unsigned char 1 None 0 to 255

short 2short int, signed short int

–32.768 to 32.767

unsigned short 2 unsigned short int 0 to 65.535

long 4long int, signed long int

–2.147.483.648 to 2.147.483.647

unsigned long 4 unsigned long int 0 to 4.294.967.295

enum * none Same as int

float 4 none 3.4E +/- 38 (7 digits)

double 8 none 1.7E +/- 308 (15 digits)

long double 10 none 1.2E +/- 4932 (19 digits)

Elementare Datentypen

Page 3: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.308.05.2011

Dr.-Ing. habil. Jörg Wollnack

Name kleinster Wert größter Wert Größe in Bit

bool (false,true) = (0, !=0) 8

(signed) char -128 127 8

unsigned char 0 255 8

enum -32.768 32.767 16

(signed) int -32.768 32.767 16

unsigned int 0 65.535 16

short int -32.768 32.767 16

(signed) long -2.147.483.648 2.147.483.647 32

unsigned long 0 4.294.967.295 32

float 3,4E-38 3,4E+38 32

double 1,7E-308 1,7E+308 64

long double 3,4E-4932 1,1E+4932 80

Elementare Datentypen 16 Bit-Systeme

Page 4: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.408.05.2011

Dr.-Ing. habil. Jörg Wollnack

Name kleinster Wert größter Wert Größe in Bit

bool (false, true) = (0,!=0) 8

(signed) char -128 127 8

unsigned char 0 255 8

enum -2.147.483.648 2.147.483.647 32

(signed) int -2.147.483.648 2.147.483.647 32

unsigned int 0 4.294.967.295 32

short int -32.768 32.767 16

(signed) long -2.147.483.648 2.147.483.647 32

unsigned long 0 4.294.967.295 32

float 3,4E-38 3,4E+38 32

double 1,7E-308 1,7E+308 64

long double 3,4E-4932 1,1E+4932 80

Elementare Datentypen 32 Bit-Systeme

Page 5: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.508.05.2011

Dr.-Ing. habil. Jörg Wollnack

Name Äquivalent Bemerkung

TRUE true, 1

FALSE false, 0

NULL 0

UINT unsigned int 16bit bzw. 32bit

BYTE unsigned char 8bit

WORD unsigned short 16bit

DWORD unsigned long 32bit

LONG long 32bit

VOID void

LPSTR char far* long-Zeiger auf einen String

LPCSTR const char far* long-Zeiger auf einen konstanten String

HANDLE void* allg. Handle für div. Windows-Elemente

HWND (nicht anwendbar) Handle zu einem Fenster

PASCAL pascal

WPARAM unsigned int 16bit bzw. 32bit

LPARAM long 32bit

LRESULT long 32bit

HINSTANCE (nicht anwendbar) Handle der Instanz (oder Kopie)

Windows-Defines

Page 6: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.608.05.2011

Dr.-Ing. habil. Jörg Wollnack

02 -1n

2 /2-1n

2 -2n

-2 /2n

1-1

-22

31 2 2 1 0 {0,1}n

n nx x x x x− − ∈�

höchstes Bit dient alsVorzeichen der Zahl

Dualzahlendarstellung

Page 7: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.708.05.2011

Dr.-Ing. habil. Jörg Wollnack

Escape Sequence Represents

\a Bell (alert)

\b Backspace

\f Formfeed

\n New line

\r Carriage return

\t Horizontal tab

\v Vertical tab

\' Single quotation mark

\" Double quotation mark

\\ Backslash

\? Literal question mark

\ooo ASCII character in octal notation

\xhhh ASCII character in hexadecimal notation

Text und Escape-Sequenzen

„Hallo\n“ Hallo\n\0

SpeicheranordnungVektor vom Typ char

Page 8: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.808.05.2011

Dr.-Ing. habil. Jörg Wollnack

����

������

�� ��

��������� � � � � � �� ��

��� � � � � � �

Speicherbild und Index von Texten

1 Byte / 8 Bit pro Zeichen <=> 256 Zeichen codierbar

“Hallo\n“

Page 9: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.908.05.2011

Dr.-Ing. habil. Jörg Wollnack

[ ] := optionale Wiederholungtag := engl. Namensschild, Name, Bezeichner declarator := engl. ErklärungTyp := DatenobjektVarname := Name des Datenobjekts

Auszug Notation Syntax-Beschreibungen

Page 10: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.1008.05.2011

Dr.-Ing. habil. Jörg Wollnack

Präfix Datentyp

Daten-typ Bemerkung

by BYTE unsigned char

c char

n number short integer

i int

x, y int für x, y Koordinatensysteme

cx, cy int für Längen in x,y Koordinatensystemen (c steht für count)

pt POINT zweidimensionale Punktkoordinate

b BOOL true oder false, bzw. 0 oder 1 (nicht 0)

f Flag

w WORD unsigned short

l LONG long integer

dw DWORD unsigned long

fn Funktion

s string

str Cstring Klasse für Zeichenketten

sz string mit \0 als Ende Zeichen

h handle

p pointer

lp long pointer

lpfn long pointer auf eine Funktion

Auszug Präfix-Konventionen

Ungarische Notation

Page 11: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.1108.05.2011

Dr.-Ing. habil. Jörg Wollnack

Syntax:Typ Name1 [,Name2];

Beispiele:int iAnzahlStudenten;float fPreis, fGewinnSpanne;double dFlaeche;unsigned char ucZeichen;int iHexWert;

Definition/Deklaration von Variablen

Page 12: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.1208.05.2011

Dr.-Ing. habil. Jörg Wollnack

Syntax:Typ Name1 [ = Ausdruck]

[ , Name2 [ = Ausdruck]];Typ Name1 [ ( Ausdruck ) ] // nur C++

[ , Name2 [ ( Ausdruck ) ]];

Beispiele:int iStudenten(15); // implizite Initialisierung nur in C++float fKosten = 12.659; /* explizite Initialisierung */float fErtrag = FKosten * 0.75;unsigned char ucKlasse = 'D';int iHexWert = 0xAFF3;

Initialisierung von Variablen I

Page 13: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.1308.05.2011

Dr.-Ing. habil. Jörg Wollnack

// dPi wird als symbolische Konstante definiert.// Symbolische Konstanten müssen bei der Definition // initialisiert werden. // Der Compiler lässt keine Änderungen zu.

const double dPi = 3.1459.....;

dPi = 0.0; // Fehlermeldung durch Compiler !!!!!!

Initialisierung von Variablen II

Page 14: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.1408.05.2011

Dr.-Ing. habil. Jörg Wollnack

Initialisierung von Variablen III

// Schreibzugriffe können Aktionen starten, die zu// Veränderungen der Daten führen (A/D-Wandler // starten). Der Compiler nimmt hier keine Code-// Optimierungen vor.volatile Typ TName;

// Eine Veränderung im Programm ist nicht zulässig,// jedoch kann eine Veränderung von außen z.B. von// einem Treiber erfolgen.const volatile Typ TName = TypValue;

Page 15: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.1508.05.2011

Dr.-Ing. habil. Jörg Wollnack

/* // for definition of enumerated type enum [tag] [: type] {enum-list} [declarator]; // for declaration of variable of type tag enum tag declarator; */ enum WochenTage { Montag, Dienstag, Mittwoch, Donnerstag, Freitag, Sonnabend, Sonntag } TagWoche; // Initialisierung TagWoche = Dienstag;

#define Montag 1 #define Dienstag (Montag+1) #define Mittwoch (Montag+2) #define Donnerstag (Montag+3) #define Freitag (Montag+4) #define Sonnabend (Montag+5) #define Sonntage (Montag+6)

Enums I

Enums codieren endliche Mengen von Bezeichnern mit integer-Zahlen. Dies erhöht die Lesbarkeit und Flexibilität der Programme gegenüber der Verwendung von #define -Vereinbarungen.

Page 16: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.1608.05.2011

Dr.-Ing. habil. Jörg Wollnack

Enums II

enum EnumName{Name1 [= 0],Name2 [= 1],[Name [= eindeutige Integer-Zahl]]

}Varname;

Bei Bedarf können die Enums mit definierten Werten belegt werden.

Page 17: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.1708.05.2011

Dr.-Ing. habil. Jörg Wollnack

/* struct tag { member-list } [declarators]; */ struct Window { int x; int y; int hx; int hy; } MyWindow; MyWindow.x = 1; MyWindow.y = 2; MyWindow.hx = 100; MyWindow.hy = 200;

Strukturen

Strukturen fassen mehrere primitive oder komplexe Datenobjekte zu einer logischen Einheit zusammen.

Die Variablen können unterschiedliche Datentypen aufweisen.

Die Variablen der Struktur werden als Komponenten (engl. members) bezeichnet.

Page 18: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.1808.05.2011

Dr.-Ing. habil. Jörg Wollnack

Unions I

union UnionName{Typ Varname;Typ Varname;[Typ Varname;]

} Varname;

Unions sind verwandt mit Strukturen. Ihre Elemente benutzen jedoch den gleichen Speicherplatz, weshalb das größte Element den Speicherplatzbedarf bestimmt.

Unions können beim Konvertieren von Daten benutzt werden. Die Interpretation der Daten wird durch die in der union definierten Typen festgelegt.Beispiel:

union Vector3D{struct { double x, y, z; } vec1;struct { double alpha, beta, gamma; } vec2;double vec3[3];

};

Page 19: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.1908.05.2011

Dr.-Ing. habil. Jörg Wollnack

Unions II

union NumericType{

int iValue; // int value 4 Bytelong lValue; // long value 4 Bytedouble dValue; // double value 8 Byte

};

������ ������

����

����

����

Page 20: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.2008.05.2011

Dr.-Ing. habil. Jörg Wollnack

Deklaration von Arrays I (Vektoren und Matrizen)

Syntax:Typ TName [ konst. Ausdruck1 ] [[konst. Ausdruck2 ]];

Bemerkung:Keine dynamische Speicherverwaltung für Arrays auf diese Weise möglich. Weshalb dieser Ansatz in der Praxis nicht brauchbar ist.

Beispiele:int iVek[3]; // Integer-Vektor iVek mit 3

// Elementenint ia, ib; // Integer Variablen

double dJacobiMatrix[3][2]; // 2-dimensionales Array mit 6 // Elementen

Page 21: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.2108.05.2011

Dr.-Ing. habil. Jörg Wollnack

Beispiele:int iVek[3]; // Integer-Vektor iVek mit 3

// Elementenint ia, ib; // Integer Variablen

double dJacobiMatrix[3][2]; // 2-dimensionales Array mit 6 // Elementen

��� ������� ������� ����� �� ��

���� � � �

Deklaration von Arrays II (Vektoren und Matrizen)

Page 22: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.2208.05.2011

Dr.-Ing. habil. Jörg Wollnack

Syntax:

TName [ Ausdruck1 ] [[Ausdruck2 ]]

Beispiele:

int iVek[3]; // Integer-Vektor mit 4 ElementeniVek[0] = 1; // Achtung Zählung beginnt bei 0 !!!!iVek[1] = 2; iVek[2] = 3; // letztes Element von iVek

iVek[4] = 5; // Keine Fehlermeldung! Überschreibt Speicher-// inhalt nach dem Ende des Arrays!!!!!!

Zugriff auf Array-Elemente

Page 23: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.2308.05.2011

Dr.-Ing. habil. Jörg Wollnack

Literalkonstanten I

Literalkonstanten definieren einen sich nicht ändernden konstanten Wert.

Ganzzahlige Konstanten (Integer Konstanten)bool false true 0, ungleich null; nur C++dezimal 5 19 32L beginnen nicht mit 0oktal 05 023 040 beginnen mit 0hexadezimal 0x5 0X13 0xAFFE beginnen mit 0x oder 0X

Suffix/Zusatz l oder L kennzeichnet eine long- Konstante Suffix/Zusatz u oder U kennzeichnet eine unsigned-Konstante

ohne Vorzeichen sind die Konstanten vom Typ int.

Page 24: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.2408.05.2011

Dr.-Ing. habil. Jörg Wollnack

Gleitpunktkonstanten (Fließkomma-Konstanten)

3.14 .5L 3.14159F 1.4e10 1.2e-15 1.2E-15 -1.4E+10

Suffix l oder L kennzeichnet long double Konstante Suffix f oder F kennzeichnet float Konstanteohne Zusatz sind die Konstanten vom Typ double

Zeichenkonstanten (Character-Konstanten)

direkt ' a' 'b' '1' L'a' oktal ' \141' '\142' '\61' '\12' hexadezimal ' \x61' '\x62' '\x31' '\x0A' Steuerzeichen ' \n'

Präfix L kennzeichnet eine (Unicode) Zeichenkonstante vom Typ wchar_t

Literalkonstanten II

Page 25: Informatik I - TUHH · PDF fileINFDT.1 08.05.2011 Dr.-Ing. habil. Jörg Wollnack Inhalte Informatik I Einführung Algorithmus, Programmiersprache, Compiler und Linker Datentypen (elementare)

INFDT.2508.05.2011

Dr.-Ing. habil. Jörg Wollnack

Literalkonstanten III

Konstante Zeichenketten(Zeichenfolgen-Konstanten, Stringliterale, Text-Konstanten)

"Das ist ein Text" "Das ist ein Text \ mit 2 Zeilen im Quellprogramm“"Das ist ein Text \n mit 2 Zeilen im Ausdruck““Hallo\n“

����

������

�� ��

��������� � � � � � �� ��

��� � � � � � �