C + + from scratch (2) - What is expression

  C + + from scratch (2) 

  – What is expression 

  This is the beginning of this series, learn English, the first time to learn the alphabet, is the basis of English.    Similarly, in C + +, all of the code through identifiers (Identifier), expressions (Expression) and the statement (Statement) and the necessary symbols (such as braces, etc.), the first in what is meant by this identifier. 

  Id 

  Identifier is a sequence of letters, upper and lower case letters, numbers and underscores for the logo.    Logo is marked and identified, which is the name.    Which can be referred to as the back of variables or function or category, such as the name, that is used to identify a particular variable or function or class in C + + and other elements. 
  For example: abc is a legitimate identifiers, which can be used as abc variables, and other elements of the function name, but it does not mean abc is a variable or function name, and so-called legitimate that any one identifier must be in digital beginning, including the case only in English letters, underlined and figures to not have other symbols, such as,! ^, and C + + can not be with the same keywords.    This is what we in for a variable or a function name, a name must be seen as an identifier, and thus must meet the above requirements.    12 ab_C if it is not a legitimate identifier, we can not function to a variable or within 12 ab_C such names ab_12C identifier is legitimate, it can be used as variables or function name. 
  Keyword mentioned earlier in the follow-up statements and statements Xiuchifu presentation will be found, C + + provides a number of special identifiers as a statement of the name, logo for a particular phrase, such as if, while, and so on; or provide Xiuchifu for some modified variables, functions and other elements in order to achieve semantic or to the compiler and connectors to provide some specific information for optimization, Chai Xi, and other operations, such as extern, such as static.    Therefore, in naming variables or functions or other elements, if not use, such as extern C + + keywords such as names, otherwise this will lead compiler is unable to confirm a variable (or function or other elements of C + +) or a statement, which could not compile . 
  If you want a specific identifier is variable or function or class name, it is necessary to use a statement in the follow-up article to specify. 

  Digital 

  C + + programming language as a computer, the computer is digital, so in C + + what is the basis of figures.    C + + offers two figures: integer and floating-point, integer, and that is a small number.    However, the actual computer is not in the figures of the imagination (For details, see "C + + from scratch (c)," the type a), the number of cosmetic surgery are also divided into symbols and unsigned integer, floating-point, from the difference between precision and divided into single-precision and double-precision floating point, integer number of the same points are based on the length of growing cosmetic and short integer. 
  In the C + + code that a number of figures can be written directly, such as: 123,34.23, and other -34.34.    The digital computer is not based on the result of the classification of the preceding figures, in order to in the code shown, C + + provided a series of suffix to that, as follows: 
  U or U said that figure is unsigned integer, such as: u 123, but does not indicate a long or short integer or integer l L said that figure is a long integer, such as: 123 l, and 123 ul of whole is unsigned Type; 34.4 l is the length and double-precision floating-point, double-precision floating-point equivalent to i64 or I64 that figure is a long integer, it is the 64-bit operating system for the definition of length than long long integer.    Such as: 43 F i64 f or that the single precision floating point numbers, such as: 12.3 f e or E said that the number of power, such as: 34.4 e-2 is 0.344; 0.2544 e3f said a single-precision floating point, when the value of 254.4 What Suffix not write, and according to the median whether a decimal point to determine its specific type, such as: 123 said that the number of cosmetic surgery is a symbol, and symbols 12341434 is a long integer, and that double-precision floating-point 34.43 . 
  Why should so much out, but also at what a symbol like no symbols?    This is because the computer is not based on the figures, but based on the state, the details will be detailed in the next. 
  As a scientific basis, may often come into the use of non-decimal numbers, such as 16 hexadecimal, 8-band, C + + also provided some support for prefix. 
  Digital added to the front or 0 x 0 X said that the figure is 16 hexadecimal, such as: 0 xF3Fa, 0×11cF.    In the front and a 0 means that this figure is eight hexadecimal, such as: 0347, into a decimal number for the 231.    But 16 hex and 8 band that float are not available, only that integer, 0 x34.343 is wrong. 

  String 

  C + + In addition to providing this most basic figures that way, and also provided a character string.    This is just to facilitate the preparation for the procedure, as a C + + computer language, there is no need to provide string.    However, because people on the basic requirements of the computer was to show results, characters and strings are due to the accessibility of the people is being used to display symbols result, C + + provides a specialized string support. 
  Said earlier, the computer only recognized figures, and text characters is the symbol is a graphic symbol.    In order to make the computer will be able to handle symbols, a way to pass a number of symbols in this computer through symbols and between the establishment of a digital mapping to achieve, which is a table.    There are two forms, one that we wish to show the graphic symbols, and the other is a figure out, through such a table in the graphic symbol can be established between mapping and digital.    Now define a standard form called the ASCII code table, almost all computer hardware support to the conversion table will be a number of symbols to show results. 
  With the above table, when to illustrate the results of the "A", the ASCII code table on the investigation, "A" this graphic symbols corresponding figures are 65 and then tell the computer output for 65 of the characters in the serial number, the final screen Showing "A." 
  This was obviously an extremely complicated, for C + + and provides a character string.    When we want to a graphic symbol of the ASCII code table number, only by single quotes that will be enclose the characters, such as: 'A', the effect and 65 is the same.    When using more than one character, then using double quotes to enclose a number of characters, is the so-called string, such as: "ABC."    Therefore string is linked up with more than just characters.    But according to the previous note easily found, the string into digital mapping needs, but it's not like characters on the map can be as simple as a look-up table to get to this, in the follow-up article in the array been introduced after the . 

  Operators 

  Basic computer figures, then all computer operations are changing figures, the normal C + + provides a number of operations to some of the basic operations, as operator (Operator), such as: + - * /, etc..    Any operator must return a number of operators as the return value, it is the operator and returned to the operating figures of the number of symbols.    As a general classification, according to operators at the same time the number of the figures into one yuan, binary and ternary operator. 
  1 yuan operators are: 
  + Subsequent to figures simply return to the figures.    Such as: +4.4 f the return value is 4.4; f + -9.3 is the return value of -9.3.    Completely out of the needs of semantics, such as that of this number are few.    — Subsequent to figures will be followed by the figures from the anti-symbol.    Such as:-34.4f the return value is -34.4 ;-( -54), the return value is 54.    For that negative.    ! Subsequent to figures from the anti-logic followed by the figures.    Logic value is the "true" or "false", in order to use digital logic that the value in C + +, which is non-zero value of real logic, and logic for zero false.    Therefore 3,43.4, 'A' is true that logic, and that logic 0 leave.    The logical follow-up was applied to the judge and in the loop.    The logic is to take anti-judgement "!" Followed by the number of logic is true or false logic and then take the corresponding value against.    Such as: 
  ! 5 is the return value of 0, 5 nonzero first really is a logical sense, and then take an anti-false logic, the final return 0. 
  ! 345.4 is the return value of 1, the first non-zero logic for 345.4 truth, anti-admission after a false logic, a logic to take anti-really.    Although logic is nonzero if true, but as a compiler to return to the logic of truth, its use will be a true representative of logic.    ~ Then to figures from the anti-followed by the figures.    Take anti-logic is defined in the operation can not be used for statistics.    In order to figures from the anti-operated applications, the computer will use the binary number, and then every one to figures from the anti-operation (because each one of the binary are only 1 or 0, in line with the true and false logic ).    If the return value of ~ 123 to the -124.    First 123 to a binary number 01111011, and then you take an anti-10000100, in the final -124. 
  The question here is why is eight rather than 16-bit binary number.    123 because less than 128, is positioned for the char type, and are thus 8 (char on what will be introduced next).    If it is ~ 123 ul, the return value is 4294967172. 
  Why do we need the figures from the anti-operation?    Because the CPU to provide such instruction.    And its also a very good and very important application will be presented behind.    On the other one yuan operators in the follow-up article will be referred to (but not necessarily all mentioned). 
  Binary operators are: 
+
  – 
*
/
  % Before and after its access to a number of figures and the return of the two, poor plot,, the balance.    Such as: 
  34 +4.4 f the return value is 38.4; 3 + -9.3 f is the return value of -6.3. 
  34-4 is the return value of 30; 5-234 is the return value of -229. 
  3 * 2 is the return value of 6; 10 / 3 is the return value of 3. 
  10% 3 is the return value of a 20% 7 is the return value of 6.    & & 
  | | Before and after its access a logic value, the return value of the two logic "and the" logic of computing and "or" logic of computing.    Such as: 
  'A' & & 34.3f the return value is the logic of truth, for one; 34 & 0 is the return value of false logic, 0. 
  0 | | 'B' is the return value of true logic, 1; 0 | | 0 is the return value of false logic, 0.    & 
|
  ^ Before and after its access a digital back to the two figures "and" computing, "or" computing, "XOR" computing value.    As previously mentioned, the first figures on both sides of the binary conversion, and then you carry out, or, XOR operation.    Such as: 
  4 & 6 is the return value of 4,4 to 00000100,6 to you phase and a 00000110, 00000100, 4. 
  4 | 6 is the return value of 6,4 to 00000100,6 to you with, or in 00000110, 00000110, 6. 
  4 ^ 6 is the return value of 2,4 to 00000100,6 to you different, or in 00000110, 00000010, 2.    > 
<
==
  > = 
<=
  ! = Before and after its access a figure, according to the two figures are greater than, less than, equal to, greater than equal, and less than or equal to the corresponding does not mean returning to the logic of value.    Such as: 
  34> 34 is the return value of 0, false logic; 32 <345 for the return value of 1, the logic really. 
  23> = 23 and 23> = 14 is the return value of 1, the logic of truth; 54 <= 4 for the return value of 0, false logic. 
  56 == 6 is the return value of 0, false logic; 45 == 45 is the return value of 1, the logic really. 
  5! = 5 is the return value of 0, false logic; 5! = 35 is the return value of true, true logic.    >> 
  <<Before and after its access to a number of figures will be shifted to right or left side of the left median designated right figures.    ~,&,| In front of such an operation, is to provide the left, shifted to right CPU operation mainly because of the provision of these instructions, mainly used for a number of algorithms based on the binary. 
  <<Left of the figures will be transformed into binary, and then shift to the left to the right side of the median values, such as: 4, to 00000100, the left two, a 00.01 million, a 16. 
  >> And <<like, but is just the right move.    Such as: 6, to 00000110, shifted to right one, into 00000011, a 3.    If the shift two, there are an excess, will be cut off, 6>> 2 is the return value of 00000001, 1. 
  The left and shifted to right what is the use?    For some based on the binary algorithm, but also the way as a simple means of optimization.    Consider a decimal number 3524, it will be the left two, a 352,400 than the original, has expanded its 100 times, that should be accurate to expanding the 10-th power of 2 times.    If 3524 will be shifted to right two, a 35, equivalent to the original 100 divided by the number of business. 
  Similarly, in front of 4>> 2, equivalent to 4 / 4 to 32>> 32 or 3 / 8, equivalent to 32 divided by 2 3 Power business.    And 4 <<2 equivalent to 4 * 4, the equivalent of four times the 2 2D.    So the left and shifted to right equivalent to multiplying and dividing, but only by the corresponding hexadecimal or in addition to the number of 2D only, but its speed was so much higher than multiplication and division, so that it is a simple Optimization means.    , Before and after its access a number of simple right to return to their figures.    Such as: 
  34.45f, 54, the return value is 54; -324,4545 f 4545 is the return value of f. 
  That it in the end what is the use?    Will be used for a number of figures as a whole and figures in the "C + + from scratch (d)" will further explanation.    On the other binary operator in the follow-up article will be referred to (but not necessarily all mentioned). 
  Only a ternary operator for?: Its format: <figure 1>? <Digital 2>: <figure 3>.    It is the return value: <figure 1> logic is true, the return <Digital 2>, or else return <the number 3>.    Such as: 
  34? 4:2 is the return value of 4, as 34 non-zero, logic true, the return of 4.    And 0? 4:2 is the return value of 2, because 0 is a false logic, to return to 2. 

  Expressions 

  You should find that absurd, in front of the - 12> 435 return value is 0, then why not write a 0 shoring also feed a write a 12> 435 in that?    This is the meaning of the expression. 
  Said earlier ">" before and after to a figure, but the number of operators is the operation and return to the symbolic figure, as it returned to the figure, it can be said on any of the above requirements to a number of places, it formed a so-called expression Type.    Such as: 23 * 54/45> 34 is the return value of 0, 23 * 54 because of the return value for 1242 and then again as the 1242 "/" Left to figures by the return value of the new 27.6; Finally, as a 27.6 " > "then be left to figure the return value 0, false logic. 
  Therefore expression is a series of figures to return to the operator and things combination of the section of the code, since it is formed by the operator, and it will return values.    The front said that the "return figures things" could be another expression, or a variable, or a return value is a function of the number or type of operator override the object of the class, since as long as they are able to return to a Digital things.    If the definition of variables, functions, etc. are unfamiliar with these terms, do not need to control them, in the subsequent articles will be them out. 
  So 34 is an expression, the return value is 34, is not merely the expression of only operators (in the back will be that 34 is a kind of operator).    Therefore, the concept of expression is very wide, and as long as there are things on the return value can be called a formula. 
  Because there are many expressions operator, the implementation of operators dependent on the order of operation at the top level, and the same in mathematics, *, / priority than +, -, and +, - is greater than>, <such as Logical Operators.    Remember not to deliberately operate at the top level, when the operator can not determine the implementation of the order, we can use small brackets to be appointed.    Such as: 
  ((1 + 2) * 3) + 3) / 4 for the return value of 3, and 1 + 2 * 3 + 3 / 4 as the return value of 7.    Note 3 / 4 to 0, 3 / 4 to zero.    When hope to float division or multiplication, only to operate in a certain number for the float can, such as: 3/4.0 the return value of 0.75. 

  & | ^ ~ Such as Application 

  Mentioned earlier "&&","||","!" logic operators such as that logic, provided by C + + is not surprising.    But why have a digital binary conversion, and then all of the binary logic operations to a class of operators so?    First, CPU provides the instructions, and its also has Below this very meaningful application. 
  Consider a crossroads, and each intersection there are three traffic lights were specified can turn left, turn right and go straight.    A total of 12, it should now prepare a control procedures, no matter how the function of this process, first of all, the traffic lights will be needed for the digital transformation of the state, because the computer only know that the figures.    So with three figures were that some traffic lights at the junction of three, each of the traffic lights from the state to a figure that on the assumption that the red light is 0, 1 green (not yellow or other consideration). 
  Later suddenly discovered, but actually it can be said that one of a number of three traffic lights at the junction of the state, such as 110 that left turn green, green and straight right turn red.    Above 110 is a decimal number, it could be that every practical 0 ~ 10 September figures, but there is only applied to two: 0 and 1, I feel very wasteful.    They choose to express binary, or 110, but is a binary number, a decimal number to six, even when the conversion for the 111, only seven decimal number than in front of the decimal number 110 more than the small, saving… …?    ?    What?    ? 
  We write in the paper to write the number 235425234 than this figure to 134 more occupied paper (assuming that the characters are as big).    Therefore records of a large number than a small number of records to spend more resources.    Simply ridiculous!    Whether 100 or 1000, only one figure, why the figures recorded for more resources?    Because the computer is not the digital computer, but a computer, which is based on state and is not based on the figures, which will explain in detail in the next.    Said a computer must be used to represent a digital way, and that way and binary like that, but it is not binary, it recorded a large number of smaller more consumption of resources, which is why the above integer to What long integer points for the short integer. 
  Below continue with the thinking.    The use of binary 110 three traffic lights to indicate the status, now need to know this figure represents 110 of the traffic lights turn left what state.    To figure that the third turn left, but the computer does not know this, as follows: 110 & 100.    This expression is the return value of 100, non-zero, logic really.    Assumptions at the junction of state for a 010, the same 010 & 100, the return value of 0, false logic.    Therefore the use of "&" operator can be in a binary one or several of the state extracted.    Therefore, we have to understand a figure representative of the state of the traffic lights turn left into the green traffic lights, and 100 phase and just let it be. 
  Now other traffic lights to keep the status quo, just as the green light to turn left traffic lights, such as the current status of the 010, in order to make the left turn traffic lights green, value should be 110, which can be adopted by 010 | 100 do.    If the current status of the 001, 001 | 100 to 101, the right - straight and turn right into the state of the traffic lights were not changed.    Therefore the use of "|" operator can give a binary number in a certain one or several settings, but only set to 1 if you want to set to 0, such as 101, turn left to turn off the green, 101 & ~ 100, 001 for the return value. 
  Mentioned above has been the status of the traffic lights at junctions can be prepared by the actual use of a variable that, and above 100 can be used to express an identifier, such as state & TS_LEFT, check variables that can be expressed by the state of the state of the traffic lights turn left state. 
  The above method was to use a large number, such as the creation of a window, a window style may be 20 to 30 months, through the above methods, we can only use a 32-bit binary long said that the figures on the window style, not Qulong 20-30 with a figure to represent each of the style. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags:

Releated Articles


0 Comments to “C + + from scratch (2) - What is expression”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.