Hello, C + + (16) express our design intent with an expression--4.1 operation of the data with an operator

Source: Internet
Author: User
Tags arithmetic operators bitwise

The 4th Chapter weaves the statement into a program

Having learned the various data types in C + +, you know how to use various data types to define variables to describe things in the real world. Now, we can write a payroll statistics program roughly like this:

//Payroll Statistics Procedureint main () {// constant num const int num = 100000// save an array of all wages int Arrsalary[num]; // variable to save average wage float Fsalaryaver = 0.0// pay for the payroll ... return 0    

However, we now only know how to define variables with data types to represent real-world data, and we do not have a clue as to how to handle the data to solve the problem. We do not know how to conveniently enter these 100,000 payroll data, and we do not know how to calculate the average wage of these 100,000 payroll data. Two tasks of the program-describing the data and processing the data. We have now completed our first task of describing data in the real world with various types of variables. So next, let's look at how to do the second task in C + +-process the data to get the resulting data and finally solve the problem.

4.1 Using operators to perform operations on data

The most common thing to do with data is to manipulate the data to get the results of an operation. Just like in the real world, we add operations to 1 and 2, we can get the result of the Operation 3, in C + +, we can also use two int variables A and b to represent 1 and 2, then how to add to a and B to get the result data 3?

4.1.1 Expression of design intent with an expression

In the calculation of mathematical problems, if you want to know the sum of two numbers, always first use the addition operation symbol "+" to connect two addend to list the addition formula, and then calculate the entire calculation to get the final sum. For example:

3

Also in C + +, if you want to process the data to get the results of the operation, it is necessary to use a formula to describe the processing of the data, because this expression expresses our intention to deal with the data, so this equation is also called an expression. While the program performs the calculation of an expression, it calculates the data according to the operation procedure described in the expression, and finally obtains the result of the whole expression.

In C + +, an expression consists of three parts: an operator, an operand, and a punctuation mark (which must be in English), which is a function of describing a process of data manipulation. The core of an expression is the operators and operands. The operand is the data to be involved in the operation, it can be the data represented by the variable, or it can be a direct representation of the constant. The various symbols that connect these operands to express the operational intent are operators, such as "+", which represents the intent of the addition operation, "*" that represents the intent of the multiplication operation. The operand is the processing object of the operator, and the operator expresses how the connected operand is handled. For example, an addition operation expression "A + 5", the variable "a" and the constant "5" is the operand of this expression, and the connection of the two operands is the addition operator "+", indicating that it is connected to the two operands "a" and "5" for the addition and operation to obtain the result. If the expression is part of a more complex expression, the result will continue to participate in the operation as the value of the expression until the value of the entire expression is finally reached. For example:

 // Define the variables to be used int< Span style= "color: #000000;" > A,b,c; // A, 5 is the operand, = is the operator, it describes an assignment operation: assigns a constant 5 to the variable AA = 5// b, A, 5 is the operand, =, + are operators 5; // A, B, C is the operand, =, *,-is the operator, () is a punctuation mark ////               

Figure 4-1 The calculation of an expression

The operands in an expression determine the number of operands in the entire expression. Most operators require two operands, such as the addition operator "+", which requires a addend and a addend. This operator, which requires two operands, is called a two-element operator, such as a common add, subtract, multiply, and divide operator. Other operators require one or three operands, which are referred to as unary operators and ternary operators, respectively. C + + provides a rich operator for expressing complex operational relationships between data, such as arithmetic operators that represent addition, subtraction, multiplication, and addition, as well as relational operators that represent size relationships. Here's a look at how to use these operators to implement data processing.

4.1.2 Arithmetic operators

In development practice, the most used is the arithmetic operators for mathematical calculations. There are several arithmetic operators available in C + +.

L + (PLUS): Calculates the and of two numbers.

L "-" (minus): Calculates the difference of two numbers.

L "*": (multiply): Calculates the product of two numbers.

L/(except): Calculates a quotient of two numbers.

L% (remainder): Calculates the remainder of two numbers.

The above arithmetic operators are the same as the corresponding operators in mathematics, and the meanings are consistent. These arithmetic operators can be used to express the arithmetic operations of the data conveniently. For example:

2;    // for the addition of the two operands, 1 and 2, the value of the expression is 3// for multiplication of the two operands of 4 and 5, the value of the expression is +/- 10 divided by 7, the expression has a value of 3  

In addition, C + + programs to add 1 or minus 1 of the value of the operation is very common, in order to improve the coding efficiency, C + + provides a quick to complete the operation of the "+ + (self-increment)" and "-(auto-minus)" operator. They are unary operators that can be placed before or after a single operand (respectively, referred to as the predecessor operator or the post operator), performing a 1 or minus 1 operation on the operand. For example, we typically use it to implement some increment and decrement operations:

0;  ..... // NIndex self plus 1, its value is incremented to 1, equivalent to NIndex = NIndex + 1++nindex;    

Best practice: Use the pre-increment operator instead of the post-increment operator

Although the meaning of the pre-and post-increment operators is the same, it adds 1 to the operand, but when the results of the two operators continue to be used to participate in the operation, their effect is different. Observe the following code:

1;    // define integer variable A and assign an initial value of 1cout<<++a;    ///// using the post-increment operator for a plus 1, the output is still 2, but a has a value of 3    

The output of the second statement is 2, because when a pre-increment operator is used, a starts with a self-increment operation, its value becomes 2, and then the value of a is 2. But everyone will be on the third sentence output also is 2 wonder, why a also performed the self-increment operation, output or 2? This is because the post-increment operator is used, and the output statement first outputs the current value of a of 2, and a then does the self-increment operation, and its value becomes 3. The predecessor operator is the first to calculate the output, the post-operator is the output after the calculation, the order of the calculation is different to lead to such a strange result.

Since they are so easy to confuse people, then, in the actual application, in the end how to choose? You can remember the programming experience of using a pre-increment operator instead of a post-increment operator. This can provide the following benefits:

1. The efficiency of the predecessor operator is better than the post operator

At the bottom of C + +, the post operator is implemented by the predecessor operator, which in essence uses the predecessor operator eventually, and adds additional conversion consumption. Therefore, using the predecessor operator can improve the execution efficiency of the code to some extent.

2. The pre-operators are not easy to confuse and increase the readability of the Code

The post operator sometimes makes people confused. For example:

1;  is the value of variable B exactly 2 or 3? 

After this code execution is complete, the value of B is 2, not 3. This is because the post-increment operator is used here, which first calculates the current value of a of 1 for the result 2, and then assigns it to B and increments by 1 to 2. The result is simple, but it's hard to make people "see it at a glance" and sometimes it makes people mistakenly think the result is 3. And if you use the predecessor operator, write:

the self-increment operation of A is changed to 2, then the plus 1 operation is performed, and the result is 3.

At one glance, you can see that the value of B is 3, and the entire code results at a glance.

The pre-increment operator can improve the execution efficiency of the code, while increasing the readability of the code, it is naturally preferred to use the pre-increment operator, the predecessor of the self-subtraction operator is the same reason.

4.1.3 Assignment operator

With the arithmetic operator, we can get the arithmetic result of each data. But with the results of the operation, the results need to be saved for later use. The assignment operator is used to save the result data to the variable. In C + +, the simplest assignment operator is "=". It is a two-dollar operator whose right side is usually a constant or expression, and the left side is a variable. The function is to save the data by saving the value on the right (if it is an expression, the value of the expression evaluated first) to the variable on the left. For example:

int A, b, C;  1// continuous assignment, first execute "c = 1" expression, assign C to 1,// then "c = 1" The value of this expression is 1, continue to assign to B,//1// calculate "B + 1" first The value of an expression is 2,/  /           

In addition, the variables on the left side of the assignment operator are sometimes involved in the operation of the right-hand expression. At this point, the current value of the left variable participates in the operation of the right-hand expression before assigning the result of the operation to the left variable. For example:

first with the current value of a 2 for the "a+10" calculation, to obtain the results of a//ten;  The right expression is calculated with the current value of a of 12, and the result is1.       

It is common in C + + for assignment operations such as this variable to both calculate and save the result, and in order to simplify the code, C + + combines the operators of these expressions with the assignment operators to form a compound assignment operator with computational functions. Includes: combination of arithmetic operators and assignment operators such as + =,-=, *=,/=,%=, bitwise operation operators and assignment operators, such as <<=, >>=, &=, ^=, |=.

These compound assignment operators are also two-element operators, which first calculate the operands on both sides according to the arithmetic or bitwise operator in the composite operator, at which point the current values of the variables are calculated, and then the operands are assigned to the left by the result of the calculation, thus completing the assignment at the same time. The combination of computation and assignment is realized. With the compound assignment operator, the above code can be simplified to:

Ten;            //1;         // equivalent to an expression "a = A * (b + 1)"   

Original address: http://www.cnblogs.com/nihaoCPP/p/4059213.html

Hello, C + + (16) express our design intent with an expression--4.1 operation of the data with an operator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.