You are on page 1of 11

Practical C++ Programming (O'Reilly & Associates

If you want know the easy way to do something, ask a fat man

The code used throughout this book has been tailored to clearly illustrate the topic
covered in each recipe.‫اعد وضع‬
‫ اخطاء مطبعية‬errata
sketch highlights the quick and easy way to have an idea realized.‫يسلط الضوء‬
omitted ‫يحدف‬
The last element is one less than the size
‫ خطا برمجي‬bug symptoms‫ االعراض‬woodpecker‫نقار الخشب‬
more prone to problems‫ اكثر عرضة للمشاكل‬Indentation‫المسافة الفارغة‬
works smoothly‫يعمل بسالسة‬ leak. ‫ يتسرب‬didn't it bother you‫يزعجك‬
novice programmer‫ مبرمج مبتدىء‬Each of these styles has its own following or cult
track down ‫تعقب‬ cracks‫الشقوق‬ mimic ‫تحاكي‬
starts misbehaving]‫البدا في سوء التصرف‬
by virtue of using‫ استنادا على استعمال‬append‫الحق‬
bottom -up programming.‫من االسفل الى االعلى‬
top-down‫من االعلى الى االسفل‬
You must crawl before you walk‫ يزحف يحبي‬nested‫متداخلة‬
The while statement is outermost.‫=االبعد‬/ innermost
The function scanf is notorious‫سيئة السمعة‬
that you will know how to spot and correct them‫تكتشف] و تصحح‬
crowd have labeled this practice as sexist.‫وصف هده الممارسة بالمتحيزة‬
it be firmly strapped on.‫ يكون مربوط] بحزم‬boss=leader
If you are going to construct a building, you need two things: the bricks and a
blueprint that tells you how to put them together.‫ مخطط‬2 ‫الطوب‬
Don't let yourself be seduced by the idea that ]‫ال تكن مغوي] او مغرور‬
how horrified‫ مروع‬it merely sends‫مجرد يرسل‬
One way around this problem is to have const.h‫تتغلب‬
Include files may be nested, and this feature can cause problems.‫متداخل‬
Example 10-2 is a new version of Example 10-1.
Aside from being a lot of work, multiple changes can lead to errors.‫بغض النظر‬
We must get rid‫يجب ان نتخلص‬
rotten style ‫اسلوب فاسد‬
fascinated me]‫ فتنتني‬I could utter‫انطق‬
becomes mature ‫ ناضج‬rigorous testing‫صارم‬
However, good programming practices dictate
that you should limit header files to types and
definitions only.
‫تملي امالء‬

An apprentice builder does not start out building the Empire State Building, but
rather starts on a one -room house.‫بناء المتدرب‬
they must undergo several transformations.‫يخضع‬
Take your wheelbarrow and go back and forth between the truck and the building
site‫ الشاحنة‬2 ‫البرويتة‬
the result is truncated (any fractional part is discarded)‫ يقتطع‬warehouse.‫مستودع‬
how local variables can be declared elsewhere‫مكان اخر‬
printed verbatim‫يطبع حرفيا‬
wipes out‫يمسح اكرازي‬ a deposit‫الوديعة االمانة‬
For example, suppose we want to record the time a runner completes each lap of a
four-lap race.‫لفة دورة‬
struct time {
int hour; /* hour (24 hour clock ) */
int minute; /* 0-59 */
int second; /* 0-59 */
};
const int MAX_LAPS = 4; /* we will have only 4 laps */
/* the time of day for each lap*/
struct time lap[MAX_LAPS];
We can use this structure as follows:
/*
* Runner just passed the timing point
*/
lap[count].hour = hour;
lap[count].minute = minute;
lap[count].second = second;
++count;
examples show how to misuse the pointer operations‫سوء االستعمال‬
mailing list.‫ القائمة البريدية‬blurs ‫ يطمس‬C blurs the d istinction between pointers and
arrays by treating them in the same manner in many cases.
C++ has fulfilled most of the hopes]‫ استوفى‬its most pervasive uses]‫انتشارا‬
The hope is that this book will help the reader gain new insights‫يربح رؤية‬
This book demonstrates key techniques that make C++ effective
allow many techniques to be applied more directly than was possible before
the book would undoubtedly have been much improved‫ال شك فيه‬
Fortunately you don't have to run the compiler
C++ owes its success to the fact that it allows the programmer to organize
Printouts‫ المطبوعات‬ampersand (&) To understand each of these statements, you
must carefully dissect each expression to discover its hidden meaning.‫تشريح انااليز‬
we need to know how to use the basic programming tools.
Some of the diehards stayed with the two -character sequence. stripped out‫جردته‬
In order to limit this problem
A working but uncommented program is a time bomb waiting to explode.
Spend the time to figure out why they don't work.
In order to write a program, you must have a clear idea of what you are going to
do.
but I could not determine what units were being used in the program. Finally, I
gave up and put the following comment in the program:

the null character (the value 0) to indicate the end of the string.
Use strlen (short for string length) to determine the number of characters before the
terminating null:
int length = strlen(string); // return the number of characters in the string
- Use strcpy (short for string copy) to copy one string to another:
strcpy(destination, source); // copy string source to destination

/ Use strcat (short for string concatenate) to append one string to the end of
another:
// append source string to the end of the destination string
strcat(destination, source);
char buffer[12];
value = 123456789;
ltoa(value, buffer, 10);

Serial.setTimeout(1000 * 60); // wait up to one minute


parseInt
The null at the end of a character string is not the same as the character
0. The null has an ASCII value of 0, whereas 0 has an ASCII value of 48.

text1.concat(text2);
The do...while loop is similar to the while loop, but the instructions in the code
block are executed before the condition is checked.
Use this form when you must have the
code executed at least once, even if the expression is false:
do
{
blink(); // call a function to turn an LED on and off
}
while (analogRead(sensorPin) > 100);

The format string "Twice %d is %d\n" tells printf to display Twice followed by a
space, the value of the first expression, then a space followed byis and a space, the
value of the second expression, finishing with an end -of-line (indicated by \n).

When a floating-point number using printf is written , the %f conversion is used.


To print the expression 1.0/3.0, we use this statement:
printf("The answer is %f\n", 1.0/3.0);
Characters use the printf conversion %c.
printf("%c%c%c reversed is %c%c%c\n", char1, char2, char3, char3, char2,
char1);
When executed, this program prints:
ABC reversed is CBA

Instead we must use the standard library function strcpy to copy the string
constant into the variable. (strcpy copies the whole string, including the
end-of-string character.) To initialize the variable name to Sam, we would write:

#include <string.h>
char name[4];
int main()
{
strcpy(name, "Sam"); /* Legal */
return (0);
}
strcpy(name, "Sam"); /* Initialize the name */
printf("The name is %s\n", name);

The standard function fgets can be used to read a string from the keyboard. The
general form of an fgets call is:
fgets(name, sizeof(name), stdin);

/* trim off last character */


first[strlen(first)-1] = '\0';

Reading Numbers
The function scanf works like printf, except that scanf reads
numbers instead of writing them.
we usefgets to read a line of input andsscanf to convert the
text into numbers. (The name sscanf stands for "stringscanf". sscanf is like
scanf, but works on strings instead of the standard input.)

fgets(line, sizeof(line), stdin);


sscanf(line, "%d", &value);

The function strcmp compares two strings

if (strcmp(string1, string2) == 0)*/ be careful we must declared it


printf("Strings equal\n");
else
printf("Strings not equal\n");

if (balance_owed = 0)
is equivalent to:
balance_owed = 0;
if (balanced_owed != 0)
The statement should be written:
if (balance_owed == 0)
So a continue is used inside the switch to start the loop at the beginning.

The one rule you should follow in programming is "Use what works best."
The class of a variable may be either permanent or temporary. Global variables are
always permanent.
Local variables are temporary unless they are declared static.p160
The variable temporary is initialized each time it is created (at the beginning of the
for statement block). The variable permanent is initialized only once, at startup
time.
‫حساب طول الكلمة‬
int length(char string[])
{
int index; /* index into the string */
/*
* Loop until we reach the end-of-string character
*/
for (index = 0; string[index] != '\0'; ++index)
continue; /* do nothing */
return (index);
}

One way around this problem is to have const.h check to see if it has already been
included and does not define any symbol that has already been defined. The
directive #ifndef symbol is true if the symbol is not defined. The directive is the
reverse of #ifdef. 184‫صفحة‬
#define SQR(x) ((x) * (x))‫ مهمة االقواس‬/* Square a number */

The printf format for hexadecimal is %x; for octal the format is %o.
In the bitwise and (&), if the corresponding bits of both the operands are true
(ones), then the corresponding bits of the results are true (ones).

You can use the bitwise and operator ]‫( )من االحسن تفادي استعمالها] وا ستعمال الموديلو‬to test
if a number is even or odd. In base 2, the last digit of all even numbers is zero and
the last digit of all odd numbers is one.
The following function uses the bitwise and to pick off this last digit. If it is zero
(an even number), the result of the function is true.
int even (const int value)
{
return ((value & 1) == 0);
}

The Ones Complement Operator (Not)(~)

i = j << 3; /* Multiple j by 8 (2**3) */


is faster than:
i = j * 8;
typedef

typedef int count;


defines a new type count that is the same as an integer.
So the declaration:
count flag;
is the same as:
int flag;

Opérateur de cast
Il est possible de forcer la conversion d’une expression quelconque dans un type de
son choix,
grâce à l’opérateur dit de « cast ». Par exemple, si n et p sont des variables entières,
l’expression :
(double) n / p // ou : static_cast<double> (n/p)
aura comme valeur celle de l’expression
entière n/p convertie en double.

You might also like