34
Hochschule Fakultät Elektrotechnik und Informatik Data Processing Ravensburg-Weingarten Mechatronik Elementary C/C++ Programming DPME_Appendix.doc (r.siol) Seite 1 von 34 12.01.10 10:13 Appendix to the lectures in data processing Index APPENDIX TO THE LECTURES IN DATA PROCESSING ............................................................................................. 1 A GRAMMER ....................................................................................................................................................................... 2 A.1 INTRODUCTION........................................................................................................................................................... 2 A.2 KEYWORDS ................................................................................................................................................................ 3 A.2.1 The C++ keywords are: ....................................................................................................................................... 3 A.2.2 Standard C++ Keywords, Description and an Example .................................................................................... 4 A.3 STANDARD C++ OPERATORS .................................................................................................................................... 6 A.3.1 Operators for elementary data types ................................................................................................................... 8 A.3.1.1 Binary operators ........................................................................................................................................................... 8 A.3.1.2 Unary Operators ........................................................................................................................................................... 8 A.3.1.3 Postfix- and Prefix-Operators ...................................................................................................................................... 8 A.3.1.4 Assignment operators ................................................................................................................................................... 9 A.3.1.5 Conditional and Comma............................................................................................................................................... 9 A.4 CONTROL FLOW STATEMENTS ................................................................................................................................ 10 A.4.1 Conpound statement or block ............................................................................................................................ 10 A.4.2 The if statement................................................................................................................................................... 10 A.4.3 The if/else statement ........................................................................................................................................... 11 A.4.4 The switch expression......................................................................................................................................... 11 A.4.5 The while expression .......................................................................................................................................... 12 A.4.6 The do / while expression ................................................................................................................................... 12 A.4.7 The for - statement.............................................................................................................................................. 13 A.4.8 The break statement............................................................................................................................................ 13 A.4.9 The continue statement ....................................................................................................................................... 14 A.5 DATA TYPES............................................................................................................................................................. 15 A.5.1 Fundamental and derived types ......................................................................................................................... 15 A.5.1.1 Fundamental Types .................................................................................................................................................... 15 A.5.1.2 Derived Types............................................................................................................................................................. 16 A.5.1.3 Storage Class Specifiers and const or volatile Qualifiers ......................................................................................... 18 A.6 FORMATS OF NUMERIC DATA TYPES........................................................................................................................ 19 A.6.1 Floatingpoint numbers (IEEE-Standard) .......................................................................................................... 19 A.6.1.1 Format ......................................................................................................................................................................... 19 B BOOLEAN ALGEBRA AND LOGIC ALGEBRA .................................................................................................... 20 B.1 TRUTH TABLES ......................................................................................................................................................... 20 B.2 BASIC THEOREMS OF BOOLEAN ALGEBRA............................................................................................................... 21 C BIBLIOGRAPHY ........................................................................................................................................................... 22 D CHARACTER SETS ...................................................................................................................................................... 23 D.1 CHARACTER AND DESCRIPTION .............................................................................................................................. 23 D.2 THE ISO-7-BIT CODE............................................................................................................................................... 26 E THE CPP I/O-SYSTEM................................................................................................................................................. 27 E.1 THE C++ STREAM CLASSES .................................................................................................................................... 27 E.2 FORMATTED I/O....................................................................................................................................................... 28 E.2.1 The set of format flags ........................................................................................................................................ 28 E.2.2 Setting and Clearing Format Flags ................................................................................................................... 29 E.2.3 Using width(), precision(), and fill().................................................................................................................. 30 E.3 USING C++ I/O MANIPULATORS ............................................................................................................................. 31 E.3.1 The C++ Standard Manipulators ...................................................................................................................... 31 E.3.2 Examples that uses some manipilators .............................................................................................................. 32 E.4 OVERLOADING << AND >> ...................................................................................................................................... 33 E.4.1 Creating your own inserters .............................................................................................................................. 33

Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 1 von 34 12.01.10 10:13

Appendix to the lectures in data processingIndex

APPENDIX TO THE LECTURES IN DATA PROCESSING............................................................................................. 1

A GRAMMER ....................................................................................................................................................................... 2

A.1 INTRODUCTION........................................................................................................................................................... 2A.2 KEYWORDS ................................................................................................................................................................ 3

A.2.1 The C++ keywords are: ....................................................................................................................................... 3A.2.2 Standard C++ Keywords, Description and an Example.................................................................................... 4

A.3 STANDARD C++ OPERATORS .................................................................................................................................... 6A.3.1 Operators for elementary data types ................................................................................................................... 8

A.3.1.1 Binary operators ........................................................................................................................................................... 8A.3.1.2 Unary Operators ........................................................................................................................................................... 8A.3.1.3 Postfix- and Prefix-Operators ...................................................................................................................................... 8A.3.1.4 Assignment operators ................................................................................................................................................... 9A.3.1.5 Conditional and Comma............................................................................................................................................... 9

A.4 CONTROL FLOW STATEMENTS ................................................................................................................................ 10A.4.1 Conpound statement or block ............................................................................................................................ 10A.4.2 The if statement................................................................................................................................................... 10A.4.3 The if/else statement ........................................................................................................................................... 11A.4.4 The switch expression......................................................................................................................................... 11A.4.5 The while expression .......................................................................................................................................... 12A.4.6 The do / while expression................................................................................................................................... 12A.4.7 The for - statement.............................................................................................................................................. 13A.4.8 The break statement............................................................................................................................................ 13A.4.9 The continue statement....................................................................................................................................... 14

A.5 DATA TYPES............................................................................................................................................................. 15A.5.1 Fundamental and derived types......................................................................................................................... 15

A.5.1.1 Fundamental Types .................................................................................................................................................... 15A.5.1.2 Derived Types............................................................................................................................................................. 16A.5.1.3 Storage Class Specifiers and const or volatile Qualifiers ......................................................................................... 18

A.6 FORMATS OF NUMERIC DATA TYPES........................................................................................................................ 19A.6.1 Floatingpoint numbers (IEEE-Standard) .......................................................................................................... 19

A.6.1.1 Format ......................................................................................................................................................................... 19

B BOOLEAN ALGEBRA AND LOGIC ALGEBRA.................................................................................................... 20

B.1 TRUTH TABLES......................................................................................................................................................... 20B.2 BASIC THEOREMS OF BOOLEAN ALGEBRA............................................................................................................... 21

C BIBLIOGRAPHY ........................................................................................................................................................... 22

D CHARACTER SETS ...................................................................................................................................................... 23

D.1 CHARACTER AND DESCRIPTION .............................................................................................................................. 23D.2 THE ISO-7-BIT CODE............................................................................................................................................... 26

E THE CPP I/O-SYSTEM................................................................................................................................................. 27

E.1 THE C++ STREAM CLASSES .................................................................................................................................... 27E.2 FORMATTED I/O....................................................................................................................................................... 28

E.2.1 The set of format flags........................................................................................................................................ 28E.2.2 Setting and Clearing Format Flags................................................................................................................... 29E.2.3 Using width(), precision(), and fill().................................................................................................................. 30

E.3 USING C++ I/O MANIPULATORS............................................................................................................................. 31E.3.1 The C++ Standard Manipulators...................................................................................................................... 31E.3.2 Examples that uses some manipilators.............................................................................................................. 32

E.4 OVERLOADING << AND >>...................................................................................................................................... 33E.4.1 Creating your own inserters .............................................................................................................................. 33

Page 2: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13

A Grammer

A.1 Introduction

This summary of the C++ syntax is intended to be an aid to comprehension. It is not an exactstatement of the language. In particular, the grammar described here accepts a superset of valid C++constructs. Disambiguation rules must be applied to distinguish expressions from declarations.Moreover, access control, ambiguity, and type rules must be used to weed out syntactically valid butmeaningless constructs.

The C and C++ standard grammars express very minor distinctions syntactically rather thanthrough constraints. That gives precision, but it doesn't always improve readability.

Page 3: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 3 von 34 12.01.10 10:13

A.2 KeywordsNew context-dependent keywords are introduced into a program by typedef , namespace, class

enumeration, and template declarations.

typedef-name:identifier

namespace-name:original-namespace-namenamespace-alias

original-namespace-name: ident(fier

namespace-alias: identifier

class-name:identifiertemplate-id

enum-name:identifier

template-name:identifier

Note that a typedef-name naming a class is also a class-name.Unless an identifier is explicitly declared to name a type, it is assumed to name samething that is nota type.

A.2.1 The C++ keywords are:

and and_eq asm auto bitand bitorbool break case catch char dasscompl const const_cast continue default deletedo double dynamic_cast else enum explicitexport extern false float for friendgoto if inline int long mutablenamespace new not not_eq operator oror_eq private protected public register reinterpret _castreturn short signed sizeof static static_caststruct switch template this throw truetry typedef typ eid typename union unsignedusing virtual void volatile wchar_t whilexor xor_eq

Page 4: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 4 von 34 12.01.10 10:13

A.2.2 Standard C++ Keywords, Description and an Example

Keyword Descriptionand A synonym for the AND operator &&and_eq A synonym for the bitwise AND assignment operntor &=asm Allows information to be passed to the assembler directlyauto Storage class for objects that exist only within their own blockbitand A synonym for the bitwise AND operator &bitor A synonym for the bitwise OR operator |bool A boolean typebreak Terminates a loop or a switch statementcase Used in a switch statement to specify control expressioncatch Specifies actions to take when an exception occurschar An integer typeclass Specifies a class declarationcompl A synonym for the bitwise NOT operator ~const Specifies a constant definitionconst cast Used to change objects from within immutable member functionscontinue Jumps to beginning of next iteration in a loopdefault The "otherwise" case in a switch statementdelete Deallocates memory allocated by a new statementdo Specifies a do … while loopdouble A real number typedynamic_cast Returns a T* pointer for a given pointerelse Specifies alternative in an if statementenum Used to declare an enumeration typeexplicit Used to prevent a constructor from being invoked implicitlyexport Allows access from another compilation unitextern Storage class for objects declared outside the local blockfalse One of the two literals for the bool typefloat Areal number typefor Specifies a for loopfriend Specifies a friend function in a classgoto Causes execution to jump to a labeled statementif Specifies an if statementinline Declares a function whose text is to be substituted for its callint An integer typelong Used to define integer and real typesmutable Allows immutable functions to change the fieldnamespace Allows the identification of scope blocksnew Allocates memorynot A synonym for the NOT operator !not_eq A synonym for the inequality operator !=operator Used to declare an operator overloador A synonym for the OR operator ||or_eq A synonym for the bitwise OR assignment operatorprivate Specifies private declarations in a class

Page 5: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 5 von 34 12.01.10 10:13

protected Specifies protected declarations in a classpublic Specifies public declarations in a classregister Storage class specifier for objects stored in registersreinterpret_cast Returns an object with given value and typereturn Statement that terminates a function and returns a valueshort An integer typesigned Used to define integer typessizeof Operator that returns the number of bytes used to store an objectstatic Storage class of objects that exists for the duration of a programstatic_cast Returns a T* pointer for a given pointerstruct Specifies a structure definitionswitch Specifies a switch statementtemplate Specifies a template classthis Pointer that points to the current objectthrow Used to generate an exceptiontrue One of the two literals for the bool typetry Specifies a block that contains exception handlerstypedef Declares a synonym for an existing typetypeid Returns an object that represents an expression’s typetypename A synonym for the keyword classusing Directive that allows omission of namespace prefixunion Specifies a structure whose elements occupy the same storageunsigned Used to define integer typesvirtual Declares a member function that is defined in a subclassvoid Designates the absence of a typevolatile Declares objects that can be modified outside of program controlwchar_t Wide (16-bit) character typewhile Specifies a while loopxor A synonym for the bitwise exclusive OR operator ^xor_eq A synonym for the bitwise exclusive OR assignment operator ^=

Page 6: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 6 von 34 12.01.10 10:13

A.3 Standard C++ Operators

This table lists all the operators in C++, grouping them by order of precedence. The higher-levelprecedence operators are evaluated before the lower-level precedence operators. For example, in theexpression (a - b*c), the * operator will be evaluated first and the - operator second, because * hasprecedence level 13 which is higher than the level 12 precedence of -. The column labeled "Assoc." tellswhether an operator is right associative or left associative. For example, the expression (a - b - c) isevaluated as (( a - b) - c) because - is left associative. The column labeled "Arity" tells whether an operatoroperates on one, two, or three operands (unary, binary, or ternary). The column labeled "Ovrldbl." tellswhether an operator is overloadable.

Operator Name Prec. Assoc. Arity Ovrldbl. Example:: Global scope resolution 17 Right Unary No ::x:: Class scope resolution 17 Left Binary No X::x. Direct member selection 16 Left Binary No s.len-> Indirect member selection 16 Left Binary Yes p->len[] Subscript 16 Left Binary Yes a[i]() Function call 16 Left n/a Yes rand()() Type construction 16 Left n/a Yes int (ch)++ Post-increment 16 Right Unary Yes n++-- Post-decrement 16 Right Unary Yes n--sizeof Size of object or type 15 Right Unary No sizeof(a)++ Pre-increment 15 Right Unary Yes ++n-- Pre-decrement 15 Right Unary Yes --n~ Bitwise complement 15 Right Unary Yes ~s! Logical NOT 15 Right Unary Yes !p+ Unary plus 15 Right Unary Yes +n- Unary minus 15 Right Unary Yes -n* Dereference 15 Right Unary Yes *p& Address 15 Right Unary Yes &xnew Allocation 15 Right Unary Yes new pdelete Deallocation 15 Right Unary Yes delete p() Type conversion 15 Right Binary Yes int (ch).* Direct member selection 14 Left Binary No x.*q->* Indirect member selection 14 Left Binary Yes p->q* Multiplication 13 Left Binary Yes m*n/ Division 13 Left Binary Yes m/n% Remainder 13 Left Binary Yes m%n+ Addition 12 Left Binary Yes m+n- Subtraction 12 Left Binary Yes m-n<< Bit shift left 11 Left Binary Yes cout << n>> Bit shift right 11 Left Binary Yes cin >> n< Less than 10 Left Binary Yes x < y<= Less than or equal to 10 Left Binary Yes x <= y> Greater than 10 Left Binary Yes x > y>= Greater than or equal to 10 Left Binary Yes x >= y== Equal to 9 Left Binary Yes x == y!= Not equal to 9 Left Binary Yes x != y

Page 7: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 7 von 34 12.01.10 10:13

& Bitwise AND 8 Left Binary Yes s & t^ Bitwise XOR 7 Left Binary Yes s ^ t| Bitwise OR 6 Left Binary Yes s | t&& Logical AND 5 Left Binary Yes u && v|| Logical OR 4 Left Binary Yes u || v?: Conditional expression 3 Left Ternary No u ? x : y= Assignment 2 Right Binary Yes n = 22+= Addition assignment 2 Right Binary Yes n += 8-= Subtraction assignment 2 Right Binary Yes n -= 4*= Multiplication assignment 2 Right Binary Yes n *= -1/= Dvision assignment 2 Right Binary Yes n /= 10%= Remainder assignment 2 Right Binary Yes n %= 10&= Bitwise AND assignment 2 Right Binary Yes s &= mask^= Bitwise XOR assignment 2 Right Binary Yes s ^= mask|= Bitwise OR assignment 2 Right Binary Yes s |= mask<<= Bit shift left assignment 2 Right Binary Yes s <<= 1>>= Bit shift right assignment 2 Right Binary Yes s >>= 1, Comma 0 Left Binary Yes ++m, --n

Page 8: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 8 von 34 12.01.10 10:13

A.3.1 Operators for elementary data types

A.3.1.1 Binary operators

Operator Name Type Applicable to+ Addition- Subtraction* Multiplication Arithmetic Numbers1

/ Division% Remainder Integers< Less than<= Less or equal== Equal Comparison Numbers!= Not equal>= Greater or equal> Greater& bitw. AND- linking| bitw. OR- linking^ bitw. EXCL.-OR linking Bitoperation Integers<< bitw. shift left>> bitw. shift right&& log. AND-link in expressions Logical Boolean|| log. OR- link in expressions linking Values

A.3.1.2 Unary Operators

Operator Name Type Applicable to& Address Reference all types* Dereference Dereference Addresses+ Unary plus Arithmetic Numbers- Unary minus~ Bitwise complement Operations on

bitsIntegers (Integral types)

! Logical NOT Logic Boolean values(type) Type conversion Types all typessizeof sizeof expr.: storage requirements Expressionssizeof sizeof (type): storage requirements Types

A.3.1.3 Postfix- and Prefix-Operators

Operator Name Type Applicable to++ Incrementing Arithmetic Integer and-- Decrementing pointer

1 Some arithmetic operators are applicable in combination with addresses and pointers; but there are some restrictions.

Page 9: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 9 von 34 12.01.10 10:13

A.3.1.4 Assignment operators

Operator Name Type Applicable to= Assignment Assignment Types+= Addition-= Subtraction*= Multiplication Arithmetic Numbers2

/= Division and%= Remainder assignment only integers&= Bitwise AND assignment|= Bitwise OR assignment^= Bitwise XOR assignment Oerations on Bits Integral types<<= Bit shift left assignment with assignment>>= Bit shift right assignment

A.3.1.5 Conditional and Comma

Operator Name Type Applicable to?: Conditional expression Expressions, Comma

2Darüber hinaus sind einige arithmetische Operationen – mit Einschränkungen – auch in Verbindung mit Adressen undZeigern verwendbar.

Page 10: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 10 von 34 12.01.10 10:13

A.4 Control Flow Statements

A.4.1 Conpound statement or block

(r.siol) 21.10.2006 FH_RV-Weingarten DV2 WS_0506 4

Compound statement or block{declaration-listopt statement-listopt}

Example:

{float buffer;buffer = x;x = y;y = buffer;

}

V Block

V Block

A.4.2 The if statement

(r.siol) 21.10.2006 FH_RV-Weingarten DV2 WS_0506 5

The if statementif (expression) statement

Example:if (x < y)

{float buffer;buffer = x;x = y;y = buffer;}

V

conditionalprocessing

ja ( x < y)

V

Page 11: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 11 von 34 12.01.10 10:13

A.4.3 The if/else statement

(r.siol) 21.10.2006 FH_RV-Weingarten DV2 WS_0506 6

The if / else - statementif (expression) statement else statement

Example:if (i == 0)

{y1 = x;y2 = x * x;}

else{y1 = x + 1;y2 = x * x * x;}

V1

simplealternative

ja (i == 0)

V2

nein

V1

V2

A.4.4 The switch expression

(r.siol) 20.09.2009 Hochschule Ravensburg-WeingartenTechnik | Wirtschaft | Sozialwesen

80

switch - Anweisungswitch (expression) statement

Übliche Verwendung:switch (expression){

case constant expression: statements... case constant expression: statementsdefault: statements

}

Beispiel:switch (art){

case 0 : ... ; ... ; break;case 1 : ... ;case 2 : ... ; break ;case 3 : break;case 4 : case 9 : ... ; break;default: cout << ‘‘falscher Wert !\n“;

}

expression(bzw. Ausdruck)

case 0

case 1

case ...

case n

...

default

statement...statementbreak; statement

... ;

statementstatementbreak;

statementstatementbreak;

statement...statement

Page 12: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 12 von 34 12.01.10 10:13

A.4.5 The while expression

(r.siol) 21.10.2006 FH_RV-Weingarten DV2 WS_0506 10

The while - statementwhile (expression) statement

Example:

sum = 0;count = 1;while (count <= 100)

{sum += count;count++;}

V

Pre-tested iterationB

V1

V2

Some blocks.

A.4.6 The do / while expression

(r.siol) 21.10.2006 FH_RV-Weingarten DV2 WS_0506 11

The do / while - statementdo statement while (expression)

Example:

sum = 0;count = 1;

do{sum += count;count++;}

while (count <= 100)

V

Post-tested iteration

B

V1

V2

Some blocks

Page 13: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 13 von 34 12.01.10 10:13

A.4.7 The for - statement

(r.siol) 22.10.2006 FH_RV-Weingarten DV2 WS_0506 12

The for - statementfor (expression1opt; expression2opt; expression3opt) statement

Example 1:sum = 0;for (count = 1; count <= 100; count++)

sum += count;

Example 2:sum = 0;for (i = 1, j = 100;i <= 50, j > 50; i++, j--)

sum = sum + i + j; V

while expression2

expression1

expression3

count = 1;

count <= 1;

count++

sum += count;

Example 1

i = 1, j = 100;

i <= 100, j > 50;

i++, j--

sum = sum + i + j;

Example 2

A.4.8 The break statement

(r.siol) 22.10.2006 FH_RV-Weingarten DV2 WS_0506 14

The break - statementbreak

a)

while (z < 50){

...

... break;

...}/* Next statement */

b)

for (z=0; z<50; z++ ){

...

... break;

...}/* Next statement */

c)

do{

...

... break;

...

...

...} while (z < 50);/* Next statement */

while - loop for - loop do while - loop

Page 14: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 14 von 34 12.01.10 10:13

A.4.9 The continue statement

(r.siol) 22.10.2006 FH_RV-Weingarten DV2 WS_0506 13

The continue - statementcontinue

a)

while (z < 50){

...

... continue;

...}

b)

for (z=0; z<50; z++ ){

...

... continue;

...}

c)

do{

...

... continue;

...

...

...} while (z < 50);

while - loop for - loop do while - loop

Page 15: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 15 von 34 12.01.10 10:13

A.5 Data Types

A.5.1 Fundamental and derived types

(r.siol) 28.11.2009 Hochschule Ravensburg-WeingartenTechnik | Wirtschaft | Sozialwesen

34

Data TypesTypes

Derived TypesFundamental(elementary, basic) types

Array-Typen (Feld - Typen)- int array[]

Pointer-Type- int *int-pointer

Structur-Type- struct

Union-Type- union

Integer

Floating Point

Integer types Enumeration type-enum-char

-short-int-long

-float-double-long double

Reference-Types (only C++)- int &int-referenz

Incomplete Types-void-void f ( void )-int array [ ]

Boolean Types-bool

Function Types-double formel(double)

A.5.1.1 Fundamental Types

(r.siol) 28.11.2009 Hochschule Ravensburg-WeingartenTechnik | Wirtschaft | Sozialwesen

46

Integer and floating type ranges• Typical ranges in 32 Bit Systems

3.4*10-4932 .. 1.1*10493296long double

2.2*10-308 .. 1.8*1030864double

1.2*10-38 .. 3.4*103832float

0 .. 18 446 744 073 709 551 61564unsigned long long, unsigned long long int

-9 223 372 036 854 775 808 .. 9 223 372 036 854 775 80764long long, signed long long, long long int, signed long long int

-2 147 483 648 .. 2 147 483 64732long, signed long, long int, signed long int

0 .. 4 294 967 29532unsigned, unsigned int

-2 147 483 648 .. 2 147 483 64732int, signed, signed int

0 .. 65 53516unsigned short, unsigned short int

-32 768 .. 32 76716short, signed short, short int, signed short int

0 .. 2558unsigned char

-128 .. 1278char, signed char

rangebitType

Page 16: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 16 von 34 12.01.10 10:13

A.5.1.2 Derived Types

(r.siol) 28.11.2009 Hochschule Ravensburg-WeingartenTechnik | Wirtschaft | Sozialwesen

131

Array, Index and Pointer

0 1 2 3 4 5 6 7 8 9

int a[10]

a == &a[0]

a [0] == *a

a + 5 == &a[5]

a [5] == *(a + 5) Content

Adress (Pointer)

Used Operators:[] Subscript& Address* Dereference() Type construction

(r.siol) 28.11.2009 Hochschule Ravensburg-WeingartenTechnik | Wirtschaft | Sozialwesen

135

Structure declaration (general form)

struct struct_type_name {

type_1 element_1;type_2 element_2;type_3 element_3;...type_n element_n;

};

struct is the keyword that begins a structure declarationstruct_type_name is user defined

Page 17: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 17 von 34 12.01.10 10:13

(r.siol) 28.11.2009 Hochschule Ravensburg-WeingartenTechnik | Wirtschaft | Sozialwesen

139

Unions• The union and struct type

union name { short number;char character;double floating_pt;} example;

struct name { short number;char character;double floating_pt;} example;

numbercharacter

floating_pt

0

2

4

6

relative Address(offset)

floating_pt

character

number0

2

4

6

relative Address(offset)

8

10

Page 18: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 18 von 34 12.01.10 10:13

A.5.1.3 Storage Class Specifiers and const or volatile Qualifiers

(r.siol) 28.11.2009 Hochschule Ravensburg-WeingartenTechnik | Wirtschaft | Sozialwesen

214

Storage Class Specifiers

– extern• Global variables might be defined after main()

– static• Local variables inside a function don‘t loose their value; the function can be called

again and use the value.• As global it is only known in the file in which you declared it.

– register• Requests the compiler to store a variable in a register.• Global register variables are not allowed.

– auto• Could be used to declare local variables; however that is the default case therefore the

keyword is never used.

These specifiers tell the compiler how to store the subsequent variable.

(r.siol) 28.11.2009 Hochschule Ravensburg-WeingartenTechnik | Wirtschaft | Sozialwesen

215

The const and volatile Qualifiers

– const• A const variable will receive it‘s value either from an

explicit initialization or by some hardware dependentmeans.

• const int N = 100;– volatile

• A variables value may be changed in ways not explicitlyspecified by the program.

• const volatile char *port = (const volatile char *) 0x30where port only can be changed by external conditions.

They control how variables may be accessed or modified. They must precede the typemodifiers and the type names that they qualify.

These qualifiers are formally referred to as the cv-qualifiers.

Page 19: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 19 von 34 12.01.10 10:13

A.6 Formats of numeric data typesThe formats shown are pictorial representations and may not reflect the actual byte order in

any particular implementation.Each of the diagrams on the following pages is followed by a table that gives the rules for

evaluating the number. In each field of the diagram, the leftmost bit is the msb (most significant bit)and the rightmost is the lsb (least significant bit).

Symbols used in format diagrams

Symbol Descriptionv Value numbers sign bite Biased exponenti Explicit one’s bitf Fractiond Nonsign bits (comp type only)

A.6.1 Floatingpoint numbers (IEEE3-Standard)

A.6.1.1 FormatThe format as it has to be used today is declared by the IEEE-standard since 1985. The format

is shown in the picture below.

The based exponent e is calculated from the real exponent E by addition of a proper selectedconstant K. The selection shall assure to get always positive values. The format of the number is:

v = −1( )s ∗2E ∗1.b1b2Kbt−1e = E + K

With:

emin =1emax = 254

; e = 0 and e = 255 are reserved.

3 IEEE = Institute of Electrical and Electronics Engineers

Based exponent e (Characteristic)

Sign s of the fraction (mantissa)

Fraction f (Mantissa)

Page 20: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 20 von 34 12.01.10 10:13

B Boolean algebra and logic algebra

B.1 Truth tablesIn the tables below A and B are variables with values which are either TRUE or FALSE.

Negation

A

¬A (Corresponding to !A in C )

F WW F

Konjunction AND

A B

A∧B (Corresponding to A && B in C )

F F FF W FW F FW W W

Disjunction OR latine: (vel)

A B

A∨B (Corresponding to A || B in C)

F F FF W WW F WW W W

Inequivalence OR latine: (aut)

A B

A ≠ B (Corresponding to A != B in C)

F F FF W WW F WW W F

Equivalence

A B

A = B (Corresponding to A = = B in C)

F F WF W FW F FW W W

Page 21: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 21 von 34 12.01.10 10:13

B.2 Basic theorems of boolean algebra

1. Associative law

A∧B( )∧C = A∧ B∧C( )

A∨B( )∨C = A∨ B∨C( )

2. Commutative law

A∧B = B∧ A

A∨B = B∨ A

3. Distributive law

A∨B( )∧C = A∧C∨B∧C

A∧B∨C = A∨C( )∧ B∨C( )

4. Absorption law

A∧ A∨B( ) = A

A∨ A∧B = A

5. Idempotent law

A∧ A = A

A∨ A = A

6. Complement

A∧ A = F

A∨ A =W

7. DE Morgan’s law

A∧B = A∨B

A∨B = A∧B

8. Laws about expressions with TRUE and FALSE

A∧W = A

A∨F = A

A∧F = F

A∨W =W

W = F

F =W

9. Double Complement

A = A

Page 22: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 22 von 34 12.01.10 10:13

C Bibliography

John R. Hubbard Programming with C++Second EditionISBN 0-07-135346-1

1 (2000) McGraw-Hill

John R. Hubbard Data Structures with C++ISBN 0-07-135345-3

1 (2000) McGraw-Hill

Margaret A. EllisBjarne Stroustrup

The Annotated C++ Reference ManualISBN 0-201-51459-1

4 (2001) Addison Wesley

Nicolai M. Josuttis The C++ Standard LibraryA Tutorial and ReferenceISBN 0-201-37926-0

2 (2004) Addison Wesley

Bjarne Stroustrup The C++ Programming LanguageSpecial EditionISBN 0-201-70073-5

1 (2000) Addison Wesley

Brian W. Kernighan,Dennis M. Ritchie

The C Programming Language,

2nd Edition, ANSI C

1 (1990) Prentice HallInternational

ANSI C A Lexical GuideISBN 0-13-037814-3

1 (1988) Mark WilliamsCompany

Jessica PerryHekman

LINUX in a nutshellA Desktop Quick ReferenceISBN 1-56592-167-4

1 (1997) O’Reilly

SaumyendraSenguptaPaul Edwards

Data structures in ANSI CISBN 0-12-636655-1

1 (1991) Academic Press

Byron S. Gottfried Schaum's Outline of Programming withC

ISBN 0-07-024035-3

2 (1996) McGraw-Hill

Greg M. Perry Absolute Beginner's Guide to C

ISBN 0-672-30510-0

2 (1994) SAMS Publishing

Peter A. Darnell,Philip E. Margolis

C , A Software Engineering Approach (ANSI C )

ISBN 0-387-94675-6

3 (1996) Springer Verlag NewYork

Kyle Loudon Mastering Algorithms with C

ISBN 1565924533

1 (1999) O'Reilly UK

Herbert Schildt C++ from the GROUND UPISBN 0-07-222897-0

McGraw-Hill/Osborne 2003

Herbert Schildt C++ A Beginner’s GuideISBN 0-07-223215-3

McGraw-Hill/Osborne 2004

Margaret A. EllisBjarne Stroustrup

The Annotated C++ ReferenceManualISBN 0-201-514591

Addison Wesley 2001

Nicolai M. Josuttis The C++ Standard LibraryISBN 0-201-379260

Addison Wesley 2004

Robert Sedgewick Algorithms in C++ Addison Wesley 2002

Page 23: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 23 von 34 12.01.10 10:13

D Character Sets

D.1 Character and Description

Character Description Decimal Octal Hex Binary

Ctrl-@ Null, end of string 0 000 0x0 0Ctrl-A Start of heading 1 001 0x1 1Ctrl-B Start of text 2 002 0x2 10Ctrl-C End of text 3 003 0x3 11Ctrl-D End of transmission, end of file 4 004 0x4 100Ctrl-E Enquiry 5 005 0x5 101Ctrl-F Acknowledge 6 006 0x6 110\a Bell, alert, system beep 7 007 0x7 111\b Backspace 8 010 0x8 1000\t Horizontal tab 9 011 0x9 1001\n Line feed, new line 10 012 0xa 1010\v Vertical tab 11 013 0xb 1011\f Form feed, new page 12 014 0xc 1100\r Carriage return 13 015 0xd 1101Ctrl-N Shift out 14 016 0xe 1110Ctrl-O Shift in 15 017 0xf 1111Ctrl-P Data link escape 16 020 0x10 10000Ctrl-Q Device contral 1, resume scroll 17 021 0x11 10001Ctrl-R Device contral 2 18 022 0x12 10010Ctrl-S Device contral 3, stop scroll 19 023 0x13 10011Ctrl-T Device control 4 20 024 0x14 10100Ctrl-U Negative acknowledgment 21 025 0x15 10101Ctrl-V Synchronous idle 22 026 0x16 10110Ctrl-W End transmission block 23 027 0x17 10111Ctrl-X Cancel 24 030 0x18 11000Ctrl-Y End of message, interrupt 25 031 0x19 11001Ctrl-Z Substitute, exit 26 032 0x1a 11010Ctrl-[ Escape 27 033 0x1b 11011Ctrl-/ File separator 28 034 0x1c 11100Ctrl-] Group separator 29 035 0x1d 11101Ctrl-^ Record separator 30 036 0x1e 11110Ctrl-_ Unit separator 31 037 0x1f 11111

Blank, space 32 040 0x20 100000! Exclamation point 33 041 0x21 100001‘‘ Quotation mark, double quote 34 042 0x22 100010# Hash mark, number sign 35 043 0x23 100011$ Dollar sign 36 044 0x24 100100% Percent sign 37 045 0x25 100101& Ampersand 38 046 0x26 100110‘ Apostrophe, single quote 39 047 0x27 100111( Left parenthesis 40 050 0x28 101000) Right parenthesis 41 051 0x29 101001* Asterisk, star, times 42 052 0x2a 101010+ Plus 43 053 0x2b 101011

Page 24: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 24 von 34 12.01.10 10:13

, Comma 44 054 0x2c 101100- Dash, minus 45 055 0x2d 101101. Dot, period, decimal point 46 056 0x2e 101110/ Slash 47 057 0x2f 1011110 Digit zero 48 060 0x30 1100001 Digit one 49 061 0x31 1100012 Digit two 50 062 0x32 1100103 Digit three 51 063 0x33 1100114 Digit four 52 064 0x34 1101005 Digit five 53 065 0x35 1101016 Digit six 54 066 0x36 1101107 Digit seven 55 067 0x37 1101118 Digit eight 56 070 0x38 1110009 Digit nine 57 071 0x39 111001: Colon 58 072 0x3a 111010; Semicolon 59 073 0x3s 111011< Less than 60 074 0x3c 111100= Equal to 61 075 0x3d 111101> Greater than 62 076 0x3e 111110? Question mark 63 077 0x3f 111111@ Commercial at sign 64 0100 0x40 1000000A Letter capital A 65 0101 0x41 1000001B Letter capital B 66 0102 0x42 1000010C Letter capital C 67 0103 0x43 1000011D Letter capital D 68 0104 0x44 1000100E Letter capital E 69 0105 0x45 1000101F Letter capital F 70 0106 0x46 1000110G Letter capital G 71 0107 0x47 1000111H Letter capital H 72 0110 0x48 1001000I Letter capital I 73 0111 0x49 1001001J Letter capital J 74 0112 0x4a 1001010K Letter capital K 75 0113 0x4b 1001011L Letter capital L 76 0114 04xc 1001100M Letter capital M 77 0115 0x4d 1001101N Letter capital N 78 0116 0x4e 10011100 Letter capital O 79 0117 0x4f 1001111P Letter capital P 80 0120 0x50 1010000Q Letter capital Q 81 0121 0x51 1010001R Letter capital R 82 0122 0x52 1010010S Letter capital S 83 0123 0x53 1010011T Letter capital T 84 0124 0x54 1010100U Letter capital U 85 0125 0x55 1010101V Letter capital V 86 0126 0x56 1010110W Letter capital W 87 0127 0x57 1010111X Letter capital X 88 0130 0x58 1011000y Letter capital Y 89 0131 0x59 1011001Z Letter capital Z 90 0132 0x5a 1011010[ Left bracket 91 0133 0x5b 1011011\ Backslash 92 0134 0x5c 1011100

Page 25: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 25 von 34 12.01.10 10:13

] Right bracket 93 0135 0x5d 1011101^ Caret 94 0136 0x5e 1011110_ Underscore 95 0137 0x5f 1011111` Accent grave 96 0140 0x60 1100000a Letter lowercase A 97 0141 0x61 1100001b Letter lowercase B 98 0142 0x62 1100010c Letter lowercase C 99 0143 0x63 1100011d Letter lowercase D 100 0144 0x64 1100100e Letter lowercase E 101 0145 0x65 1100101f Letter lowercase F 102 0146 0x66 1100110g Letter lowercase G 103 0147 0x67 1100111h Letter lowercase H 104 0150 0x68 1101000i Letter lowercase I 105 0151 0x69 1101001j Letter lowercase J 106 0152 0x6A 1101010k Letter lowercase K 107 0153 0x6B 11010111 Letter lowercase L 108 0154 0x6C 1101100m Letter lowercase M 109 0155 0x6D 1101101n Letter lowercase N 110 0156 0x6E 1101110o Letter lowercase O 111 0157 0x6F 1101111p Letter lowercase P 112 0160 0x70 1110000q Letter lowercase Q 113 0161 0x71 1110001r Letter lowercase R 114 0162 0x72 1110010s Letter lowercase S 115 0163 0x73 1110011t Letter lowercase T 116 0164 0x74 1110100u Letter lowercase U 117 0165 0x75 1110101v Letter lowercase V 118 0166 0x76 1110110w Letter lowercase W 119 0167 0x77 1110111x Letter lowercase X 120 0170 0x78 1111000y Letter lowercase Y 121 0171 0x79 0111001z Letter lowercase Z 122 0172 0x7a 1111010{ Left brace 123 0173 0x7b 1111011I Pipe 124 0174 0x7c 1111100} Right brace 125 0175 0x7d 1111101~ Tilde 126 0176 0x7e 1111110Delete Delete, rub out 127 0177 0x7f 1111111

Page 26: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 26 von 34 12.01.10 10:13

D.2 The ISO-7-Bit Code

(r.siol) 23.09.2009 Hochschule Ravensburg-WeingartenTechnik | Wirtschaft | Sozialwesen

12

ISO-7-Bit-Code

DELo_O?/IS1SI151111

~ (ß)n^N>.IS2SO140111

} (ü)m] (Ü)M=-IS3FE5 (CR)131011

| (ö)l\ (Ö)L<,IS4FE4 (FF)120011

{ (ä)k[ (Ä)K;+ESCFE3 (VT)111101

zjZJ:*SUBFE2 (LF)100101

yiYI9)EMFE1 (HT)91001

xhXH8(CANFE0 (BS)80001

wgWG7‚TC10 (ETB)BE71110

vfVF6&TC9 (SYN)TC6 (ACK)60110

ueUE5%TC8 (NAK)TC5 (ENQ)51010

tdTD4$DC4TC4 (EOT)40010

scSC3#DC3TC3 (ETX)31100

rbRB2„DC2TC2 (STX)20100

qaQA1!DC1TC1 (SOH)11000

p`P@0SPTC7 (DLE)NUL00000

76543210ColumnRow

b1b2b3b4b5b6b7Bit

10101010

11001100

11110000

German character set: DIN 66003 with the mutated vowels (Ä, Ö, Ü, ä, ö, ü, ß)

ISO Code on thetransmission line:

b1 is the firs bitalong the line.

b8 is used asParity Bit

b1b2b3b4b5b6b7P

Page 27: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 27 von 34 12.01.10 10:13

E The Cpp I/O-System

E.1 The C++ Stream Classes

Stream Meaning Default Devicecin Standard input Keyboardcout Standard output Screencerr Standard error output Screenclog Buffered version of cerr Screen

Page 28: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 28 von 34 12.01.10 10:13

E.2 Formatted I/O

E.2.1 The set of format flagsFormatting using the ios members. The set of format flags control the way information is

formatted. The ios class declares a bitmask enumeration called fmtflags in which the followingvaluesw are defined.

adjustfield floatfield right skipwsbasefield hex scientific unitbufboolalpha internal showbase uppercasedec left showpointfixed oct showpos

/* Setting the Format FlagsTo set a flag, use the setf() function.Its most common form is shown here:

fmtflags setf (fmtflags flags );

*/

#include <iostream>using namespace std;

int main(){cout.setf (ios :: showpoint);cout.setf (ios :: showpos);

cout << 100.0;

return 0;}

+100.000

/* Instead of multiple calls to setf(),you can simply OR together the values of the flags you want to set.

*/

#include <iostream>using namespace std;

int main(){cout.setf (ios :: showpoint | ios :: showpos);

cout << 127.345;

return 0;}

Page 29: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 29 von 34 12.01.10 10:13

E.2.2 Setting and Clearing Format Flags

To set a flag, use the setf() function; it is a member of ios.

fmtflags setf ( fmtflags flags ); // General formstream.setf ( ios :: showpos );

To unset a flag, use the unsetf() function; it is a member of ios.

fmtflags unsetf ( fmtflags flags ); // General formstream.unsetf ( ios :: showpos );

All together in an example:

/* Demonstrate setting and unsetting of format flags */

#include <iostream>using namespace std;

int main(){cout.setf (ios :: uppercase | ios :: scientific); cout << "Show a Floating Point Number\t" << 127.345 << endl;cout.unsetf (ios :: uppercase); cout << "Show a Floating Point Number\t" << 127.345 << endl;return 0;}

Show a Floating Point Number 1.273450E+02Show a Floating Point Number 1.273450e+02

An overloaded Form of setf() (That is the two parameter form of setf()).

fmtflags unsetf ( fmtflags flags1, fmtflags flags2); // General form

/* Demonstrate setting and unsetting of format flags */

#include <iostream>using namespace std;

int main(){cout.setf (ios :: hex, ios :: basefield);

cout << 100 << endl; // this displays 64 which is 0x64return 0;}

Page 30: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 30 von 34 12.01.10 10:13

E.2.3 Using width(), precision(), and fill()

The general form of those three membeer functions deffined by the ios is:

streamsize width (streamsize w);streamsize precision (streamsize p);char fill (char ch);

Here is a program that illustrates these functions:

/* Demonstrate width(), precision() and fill()together with setting of a format flag */

#include <iostream>using namespace std;

int main(){cout.precision(4);cout.width(10);cout << 10.12345 << "\n"; // displays 10.12

cout.fill('*');cout.width(10);cout << 10.12345 << "\n"; // displays 10.12

// field width applies to strings, toocout.width(10);cout << "Hi!" << "\n"; // displays *******Hi!

cout.width(10);cout.setf (ios :: left); // left justifycout << 10.12345 << "\n"; // displays 10.12*****

return 0;}

10.12*****10.12*******Hi!10.12*****

Page 31: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 31 von 34 12.01.10 10:13

E.3 Using C++ I/O Manipulators

E.3.1 The C++ Standard Manipulators

Manipulator Purpose Input/Outputboolalpha Turns on boolapha flag. Input/Outputdec Turns on dec flag. Input/Outputendl Output a newline character and flush the stream. Outputends Output a null. Outputfixed Turns on fixed flag. Outputflush Flush a stream. Outputhex Turns on hex flag. Input/Outputinternal Turns on internal flag. Outputleft Turns on left flag. Outputnobooalpha Turns off boolalpha flag. Input/Outputnoshowbase Turns off showbase flag. Outputnoshowpoint Turns off showpoint flag. Outputnoshowpos Turns off showpos flag. Outputnoskipws Turns off skipws flag. Inputnounitbuf Turns off unitbuf flag. Outputnouppercase Turns off uppercase flag. Outputoct Turns on oct flag. Input/Outputresetiosflags(fmtflags f) Turns off the flags specified in f Input/Outputright Turns on right flag. Outputscientific Turns on scientific flag. Outputsetbase(int base) Sets the number base to base. Input/OutputsetfilI(int eh) Sets the filI character to eh. Outputsetiosflags(fmtflags f) Turns on the flags specified in f Input/Outputsetprecision(int p) Sets the number of digits of precision. Outputsetw(int w) Sets the fieId width to w. Outputshowbase Turns on showbase flag. Outputshowpoint Turns on showpoint flag. Outputshowpos Turns on showpos flag. Outputskipws Turns on skipws flag. Inputunitbuf Turns on unitbuf flag. Outputuppercase Turns on uppercase flag. Outputws Skips leading white space. Input

To access manipulators that take parameters (such as setw()), you must include <iomanip> inyour program.

Page 32: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 32 von 34 12.01.10 10:13

E.3.2 Examples that uses some manipilators

/* Demonstrate the use of manipulators */

#include <iostream>#include <iomanip>using namespace std;

int main(){int k;cout << "Type an integer value: k = "; cin >> k;

cout << "k = " << k << endl;cout << hex << "k = " << k << endl;cout << showbase << hex << "k = " << k << endl;cout << setw (20) << showbase << hex << "k = " << k << endl;cout << "k = " << setfill ('_') << setw (20) << showbase << hex << k << endl;cout << "k = " << left << setfill ('_') << setw (20) << showbase << hex << k << endl;cout << "k = " << left << setfill ('_') << setw (20) << showbase << oct << k << endl;

return 0;}

Type an integer value: k = 1023k = 1023k = 3ffk = 0x3ff k = 0x3ffk = _______________0x3ffk = 0x3ff_______________k = 01777_______________

Page 33: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 33 von 34 12.01.10 10:13

E.4 Overloading << and >>

In the language C++

o the << output operator is referred to as the insertion operator because it insertscharacters into a stream.

o the >> input operator is called the extraction operator because it extracts charactersfrom a stream.

o the functions that overload the insertion and extraction operators are generally calledinserters and extractors, respectively.

E.4.1 Creating your own insertersInserter functions, for a class that you create, have this general form

ostream &operator << (ostream &stream, class_type obj){// body of inserterreturn stream;}

/* Demonstrate how to create an inserter function */

#include <iostream>using namespace std;

struct vektor{double x,y,z;};

// Display the element values of vektorostream &operator << (ostream &stream, vektor v)

{stream << v.x << "\t" << v.y << "\t" << v.z << "\n";return stream; // must return stream}

int main(){vektor v;v.x = 5;v.y = 17;v.z = -2;cout << v;

return 0;}

5 17 -2

Page 34: Appendix to the lectures in data processingsiol/Examples_WS0910/MM1_Appendix.pdf · DPME_Appendix.doc (r.siol) Seite 2 von 34 12.01.10 10:13 A Grammer A.1 Introduction This summary

Hochschule Fakultät Elektrotechnik und Informatik Data ProcessingRavensburg-Weingarten Mechatronik Elementary C/C++ Programming

DPME_Appendix.doc (r.siol) Seite 34 von 34 12.01.10 10:13

/* Demonstrate how to create an inserter function */

#include <iostream>#include <cstring>using namespace std;

class phonebook{public:

char name[80];int areacode;int prefix;int num;phonebook (char *n, int a, int p, int nm)

{strcpy (name, n);areacode = a;prefix = p;num = nm;}

};

// Display name and phone numberostream &operator << (ostream &stream, phonebook o)

{stream << o.name << "\t";stream << "(" << o.areacode << ")\t";stream << o.prefix << "_" << o.num << "\n";

return stream; // must return stream}

int main(){phonebook a("Ted", 111, 555, 1234);phonebook b("Alice", 300, 405, 2009);phonebook c("Tom", 700, 399, 1250);

cout << a << b << c;

return 0;}

Ted (111) 555_1234Alice (300) 405_2009Tom (700) 399_1250