Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Cource_work_theory.doc
Скачиваний:
2
Добавлен:
31.07.2019
Размер:
90.11 Кб
Скачать

Theory

Description of directives

#define

The #define directive defines a macro. Macros provide a mechanism for token replacement with or without a set of formal, function-like parameters.

Each occurrence of macro identifier in your source code following this control line will be replaced in the original position with the possibly empty token sequence (there are some exceptions, which are noted later). Such replacements are known as macro expansions. The token sequence is sometimes called the body of the macro.

An empty token sequence results in the removal of each affected macro identifier from the source code.

After each individual macro expansion, a further scan is made of the newly expanded text. This allows for the possibility of nested macros: The expanded text can contain macro identifiers that are subject to replacement. However, if the macro expands into what looks like a preprocessing directive, such a directive will not be recognized by the preprocessor. There are these restrictions to macro expansion:

Any occurrences of the macro identifier found within literal strings, character constants, or comments in the source code are not expanded.

A macro won't be expanded during its own expansion (so #define AA won't expand indefinitely).

#Include

The #include directive pulls in other named files, known as include files, header files, or headers, into the source code. The syntax has three versions:

The first and second versions imply that no macro expansion will be attempted; in other words, header name is never scanned for macro identifiers. Header name must be a valid file name with an extension (traditionally .h for header) and optional path name and path delimiters.

The third version assumes that neither < nor " appears as the first non-whitespace character following #include; further, it assumes a macro definition exists that will expand the macro identifier into a valid delimited header name with either of the <header name> or "header name" formats.

The preprocessor removes the #include line and conceptually replaces it with the entire text of the header file at that point in the source code. The source code itself is not changed, but the compiler "sees" the enlarged text. The placement of the #include can therefore influence the scope and duration of any identifiers in the included file.

If you place an explicit path in the header name, only that directory will be searched.

The difference between the <header name> and "header name" formats lies in the searching algorithm employed in trying to locate the include file.

Constants

Constants are tokens representing fixed numeric or character values. C++Builder supports four classes of constants: integer, floating point, character (including strings), and enumeration.

Internal representation of numerical types shows how these types are represented internally.

Variables and type of data

We can work with various kinds of information, such as numbers, characters, strings, set of numbers and so on. We can use integer numbers or numbers with floating point. Different types of information demand different amount of memory space and allow doing with them different operations. For example, we can add and multiply numbers, but we cannot do the same with strings.

All data with which the program works, are during its execution in the RAM. There the information is stored in variables.

Variable is a piece of memory in which we store information during the program's work. Each variable has its name. It may consist of English characters and digits. For example: a, b3, sum, x Note, that A and a are different names.

We access to data which are stored in a variable, on its name. Variables must be declared before they can be used:

Type of the variable name of the variable;

A data type provides two valuable pieces of information to the compiler:

  • what amount of space the variable will use,

  • what kind of information will be allowed in that space.

After declaring a variable, the compiler reserves a space in memory for that variable. Let's consider the basic numeric types.

int - integer numbers from approximately -1 mlrd. up to 1 mlrd.

long - integer numbers, which absolute value is greater than 1 mlrd.

float - floating point numbers

double - very big floating point numbers or numbers with very many digits after point (a double-r precision float)

Examples of declarations:

int a;

intal, c;

float b,x;

It is possible to initialize a variable value at the declaration:

inta=l;

float b=8.3;

To set or change a value of a variable, it must be assigned.

If the user enters a value in the Edit box, it can be assigned to a variable. By default, the content of a text control, such as an edit box, is a string. If you want the value or content of such a control to participate in a mathematical operation, you must first convert a string value to a valid data type.

Converting a String to an Integer:

StrToInt(const AnsiString S);

This function takes as an argument, the string that you are trying to convert. Imagine that there are three edit boxes on our form. In the following example, the strings of two edit boxes are converted to integers and an addition is performed on their values:

int Nl = StrToInt(Editl->Text);

int N2 = StrToInt(Edit2->Text);

int A = Nl+N2;

Edit3->Text = A;

Converting an Integer to String :

IntToStr(int Value);

This function takes an integer as the argument and returns a string.

Converting a String to Float:

StrToFloat(const AnsiString S);

This function takes one argument which is the string to convert. The function returns a long double-precision value. Here is an example:

double VI = StrToFloat(Editl->Text);

double V2 = StrToFloat(Edit2->Text);

double V3 = VI+V2; Edit3->Text = V3;

Converting a Float to String:

FloatToStr (const AnsiString S);

FormatFloat("0.00",x);

Arrays

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

That means that, for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier.

Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:

type name [elements];

where type is a valid type (like int, float...), name is a valid identifier and the elements field (which is always enclosed in square brackets []), specifies how many of these elements the array has to contain.

Therefore, in order to declare an array called billy as the one shown in the above diagram it is as simple as:

int billy [5];

An array stores many values in memory using one name, and individual values are identified by number. Unlike math, arrays must be declared and a fixed amount of memory must be allocated for them.

Individual elements are identified by integer subscripts which are enclosed in square brackets [] following the array variable. xi in mathematics is written as x[i] in C++ and most other programming languages.

Arrays are the basic underlying mechanism for storing multiple values, and many additional ways to store values are built on them, especially in the Standard Template Library (STL). A solid understanding of arrays is essential, even though you will want to use some of STL library data structures.

In general, you should use arrays only for storing a fixed number of elements, and vectors when storing a variable number of elements.

Conditions

From our point of view, condition is a question which demands an answer YES (true) or NO (false). In C++ condition is any expression which gives a numeric result. Any non-zero number means that condition is true, zero means that condition is false.

Commonly we can formulate the conditions as:

"If condition is true, we do some actions, else we do other actions".

A behavior of computer often depends on different conditions. Almost all the programs contain conditions.

C++ provides six relational operators for comparing numeric quantities. Relational operators evaluate to 1(representing the true outcome) or 0 (representing the false outcome)

Priority of relational operations is lower than arithmetical operations.

Often we use compound conditions, which consist of two or more conditions. There are three logical operations which are used to combine conditions. They are:

Logical negation is a unary operator, which negates the logical value of its single operand. If its operand is nonzero it produce 0, and if it is 0 it produces 1.

Logical outproduces 0 if one or both of its operands evaluate to 0. Otherwise, it produces 1. Logical or produces 0 if both of its operands evaluate to 0. Otherwise, it produces1.

Note that here we talk of zero and nonzero operands (not zero and 1). In general, any nonzero value can be used to represent the logical true, whereas only zero represents the logical false. The following are, there­fore, all valid logical expressions:

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]