[C language exploration journey] Part 1 Lesson 5: computing that point, journey Part 1
Introduction
1. Course outline
2. Lesson 5: Computing
3. Lesson 6: conditional expressions
Course outline
Our courses are divided into four parts. Each part has exercise questions after completion and answers will be published. Three games will also be written in C language.
Basic knowledge of C Programming
What is programming?
To do their best, you must first sharpen your tools.
Your first program
World of Variables
Computing
Conditional expressions
Loop statement
Practice: The first C games
Function
Exercise questions
Exercise: Perfect the first C games
C Advanced Technology
Modular programming
Attack pointer, C trump card
Array
String
Preprocessing
Create your own variable type
File read/write
Dynamic Allocation
Practice: "hanging villain" games
Secure text input
Exercise questions
Exercise: Explain the pointer in your own language
Develop 2D games with C language-based SDL Library
Install SDL
Create a window and canvas
Show images
Event Processing
Practice: "Super Mary pushes boxes" games
Time usage
Use SDL_ttf to edit text
Use FMOD to control sound
Practice: Visual Sound line
Exercise questions
Data Structure
Linked List
Heap, stack, and queue
Hash table
Exercise questions
Part 1 Lesson 5: Computing
The "World of variables" lesson is divided into three chapters. Because a person has to go to work again, there is not much time.
However, over the weekend, you can spend a lot of time editing your article.
Today, I will study the computation in C Language (similar to most programming languages.
In previous lessons, we have already said that a computer is a "stupid" machine that only performs computation. Whether you use a computer to listen to music, or a computer to watch movies or play games, the computer is only doing computation. Otherwise, how is it called a "computer?
In this lesson, we will learn the majority of computing operations that computers can achieve. We will continue to use the variable knowledge we learned in the previous lesson. In fact, it is to add, subtract, multiply, divide, modulo, and other operations on variables.
This lesson is essential even if you are not interested in mathematics. Stick it to your scalp.
Basic operations
Speaking of basic operations, nothing more
Add
Subtraction
Multiplication
Division
Modulo (if this is your first hearing, don't worry, we will explain it later)
In fact, your computer only knows how to perform these basic operations. If you want it to perform more complex operations (square, multiplication, logarithm, etc.), you need to program it, that is to say, you need to explain how to do it to the computer. However, we are lucky to see that the C language has already been designed with a math library. (For more information about the library, see the previous course. Simply put, it is a set of variables and functions that have been compiled and can be called by your program. You only need to use the content in the library defined by experts. You do not need to repeat the wheel ".
First, let's start with addition.
In order to add in C language, we need to use the plus sign (+), which is simple.
You need to put the result of addition into a variable. Create an integer variable named "result ).
IntResult = 0;
Result = 4 + 6;
You don't have to be a professional mathematician. You can also guess that the result value will change to 10 after the program runs. We use the printf function to output the result:
Printf ("4 + 6 = % d", result );
Run the program:
4 + 6 = 10
Let's see, addition is so simple and not willful at all.
The same principle applies to other computing types. Only the operators are different. See the following table:
Operation |
Symbol |
Addition |
+
|
Subtraction |
-
|
Multiplication |
*
|
Division |
/
|
Modulo |
%
|
If you have used a calculator on your computer, you must know these symbols. Except the last two (division and modulo), others should be familiar with it. Let's talk about the last two symbols.
Division
When there is no remainder, Division works well. For example, if 6/3 is equal to 2, your computer gives you the correct answer. No problem till now.
However, if we let the computer do 5/2, the result should be 2.5, but let's look at our program:
Int result = 0;
Result = 5/2;
Printf ("5/2 = % d", result );
Run the program:
5/2 = 2
We asked the computer to calculate 5/2. The expected result is 2.5, but the computer actually gave 2. This is not a very good thing... dear computer, how can you treat me like this.
In fact, there is something strange inside. Is our computer so stupid at this point?
In fact, when the computer sees numbers 5 and 2, it will divide an integer (also called Euclidean Division). That is to say, it truncates the result and leaves only the integer part (here is 2 ).
You may say: Ah, I know, it's because the result variable is an integer. If the result is declared as a floating point number of the double type, it should be able to store the number with the decimal point.
Actually not. This is not the reason. If you declare result as double type, do
Result = 5/2. You will still get 2. In fact, this is because the numbers on both sides of the operator are integers, so the computer performs division between integers.
Of course, the computer can also output what you want. What should I do? Check the following program:
Double result = 0;
Result = 5.0/2.0;
Printf ("5/2 = % f", result );
Run, display:
5/2 = 2.500000
If you want your computer to display the correct results, you also need to write the numbers on both sides of the operator as 5.0 and 2.0 (also 5 and 2, but the computer thinks these two are floating-point numbers, so it is used to divide floating-point numbers?
This integer division feature is very important. So remember, for a computer:
A little surprising, isn't it? However, this is how computers perform integer operations.
If you want to get the result of a floating point number, you need to calculate the number itself as a floating point number. (to mention it, you do not need to use both of them as a floating point number. One is a floating point number, the computer will automatically calculate another floating point ):
5.0/2.0 = 2.5
10/3 .0 = 3.33333
4.0/5 = 0.8
In fact, during Integer Division, for example, 5/2, your computer will answer the following question: "How many 2 are there in 5 ?", The answer is two. Similarly, "How many three are there in 10" has three answers.
Then you will ask, how can we get the remainder of the division?
This is the turn of the modulo operation.
Modulo
A modulo operation is a mathematical operation that obtains the remainder of a division. Compared with the four basic operations of addition, subtraction, multiplication, and division, it is not so well-known, but for computers, modulo operation is one of the basic operations. It is likely to solve the problem of Integer Division mentioned above.
As shown in the table above, the modulo notation is %.
The following are examples of Modulo operations:
5% 2 = 1
14% 3 = 2
4% 2 = 0
Modulo operation 5% 2 is the remainder of division operation 5/2, so it is 1. The computer calculates 5 = 2*2 + 1, so the modulo operation returns 1 as the result.
Similarly, 14 = 3*4 + 2, so the remainder is 2.
4 = 2*2, so the remainder is 0.
Now, I am announcing the good news: we have learned all the basic operations, and the mathematics class is over.
Calculation between variables
Ah, how come another professor of mathematics just arrived when the math teacher left? No way. Computers, also known as computers, must deal with mathematics. Fortunately, it is not "called a beast ".
Now that we have learned five basic operations in the previous section, let's take a look at the operations between variables.
In fact, the calculation between variables is the same.
Result = number1 + number2;
The code above performs addition operations on the number1 and number2 variables and stores the results in the result variable.
Now our learning is getting more and more interesting. In fact, you can now implement the function of a mini calculator. Don't doubt yourself, you can.
Imagine a program that asks the user to input two numbers, which you store in the variable.
Then, you add the two variables and store the results in another variable. Next, you just need to display the computation results on the screen, so that users can see the computer skills, and many people do not do the addition so quickly!
Try to write the above program by yourself. It is very simple and you can also practice it for you.
Of course, we should write the code:
# Include <stdio. h>
Int main (int argc, char * argv [])
{
Int result = 0, number1 = 0, number2 = 0;
// Request the user to enter the values of number1 and number2:
Printf ("Enter the number 1 :");
Scanf ("% d", & number1 );
Printf ("Enter the number 2 :");
Scanf ("% d", & number2 );
// Perform operations:
Result = number1 + number2;
// Display the operation result on the screen:
Printf ("% d + % d = % d \ n", number1, number2, result );
Return 0;
}
Run, display:
Enter the number 1: 289
Enter the number 2: 376
289 + 376 = 665
Maybe you haven't realized: This is our first interesting program. Our program requests the user to input two numbers, then add them, and then output the results to the screen. That's great!
Please try to use the other four basic operators to write the program and see how it works.
Abbreviations
We have previously guaranteed that there will be no new forms of operation. Indeed, we already know all the basic operations: addition, subtraction, multiplication, division, and modulo. With these basic operations, we can do everything. No other operations are required. I know this is hard to believe. Would you say that a complicated 3D game is ultimately composed of addition, subtraction, multiplication, division, and so on? Yes, indeed.
Even so, we can perform computation in C language.
Why is it abbreviated? Because most of the operations we perform are repeated. Here you will see the benefits of abbreviations.
Auto-increment operation
You will find that you often need to perform the + 1 operation on a variable in programming.
Suppose your variable name is number. Do you know how to perform the + 1 operation on it? Yes:
Number = number + 1;
What did the above statement do?
First, we perform the number + 1 operation, and then we store the calculation result in the variable number (its own.
Therefore, if the value of our variable is 4, it will be 5 after calculation, and if its value is 8, it will be 9, and so on.
This operation is repeated. You know, computer scientists are very lazy people. They don't want to enter numbers twice (it's really tiring ).
So they invented an abbreviated form called auto-increment. The result is the same as that of the + 1 operation:
Number ++;
This line of code uses the auto-increment operator ++. Is it simpler than the sentence number = number + 1 just now? It means "+ 1 operation on number ".
A keen reader may think that the ++ symbol in the C ++ programming language is actually the meaning of the auto-incrementing operator. I didn't quite understand why C ++ is not C ++, but C ++. It turns out that computer scientists made a very interesting joke: C ++ means to perform + 1 operations on C. Of course, in fact, C ++ is only programming in different ways. It doesn't mean that C ++ is better than C, but it's just different.
Auto-Subtraction
Knowing the principle of the auto-increment operation, it is not difficult to understand the self-subtraction operation: It is to perform the-1 operation on the variable.
Number --;
Other abbreviations
Likewise, there are several other forms of computation abbreviations. For example, number = number * 2; number * = 2;
See the following code:
Int number = 2;
Number + = 4; // number is changed to 6
Number-= 3; // The number is changed to 3.
Number * = 5; // The number is changed to 15.
Number/= 3; // number is changed to 5
Number % = 3; // number is changed to 2 (because 5 = 1*3 + 2)
Math Library
In C language, we have something that is called a "standard library", which is a very practical library. We usually use those basic libraries frequently.
Let's review: the Library refers to the set of defined functions and variables. These functions are written by our predecessors to avoid "repeating the wheel ".
We have used the printf and scanf functions in the stdio. h library.
In fact, there are many other useful libraries, including math. h, which contains mathematical functions.
In fact, simply adding, subtracting, multiplication, division, and modulo are not enough. Although these five operations are performed at the underlying layer, we often need to call the database or write the function by ourselves to perform complex operations. Because the computer cannot understand the operators except +,-, *,/, and %. For example, if you want a computer as a multiplication party, enter 5 ^ 2, unless you call a function defined in the math library as a multiplier.
It's easy to call the math library,
# Include <math. h>
With this line of code, your program can use all the functions defined in it.
Let's introduce some of the most common ones.
Fabs
This function returns the absolute value:
If you pass this function-53, it returns 53
If you pass this function 53, it returns 53
Double absolut = 0, number =-29;
Absolut = fabs (number); // The value of absolut is changed to 29.
Ceil
This function returns the integer next to the given floating point number. This is a rounding method. The ceil function always rounds the nearest integer greater than the parameter.
Double above = 0, number = 34.81;
Above = ceil (number); // The value of above is 35.
Floor
This function is opposite to ceil and returns the integer immediately following the given floating point number.
Double below = 0, number = 45.63;
Below = floor (number); // The value of below is changed to 45.
Pow
This function calculates the multiplication of numbers. You need to give it two parameters: base number and index.
Double result = 0, number = 2;
Result = pow (number, 4); // The value of result is 16 (2 ^ 4 = 16)
Sqrt
This function returns the square root of the parameter. The returned value is of the double type.
Double result = 0, number = 100;
Result = sqrt (number); // The value of result is changed to 10.
Sin, cos, tan
These three functions calculate the sine, cosine, and tangent values.
Asin, acos, atan
These three functions calculate the arcsin, arccosine, and arc tangent values.
Exp
This function is a special form of multiplication. It returns the base number of an exponential Operation Based on e (natural logarithm base number, approximately equal to 2.7182 ).
Log
This function returns the base-e logarithm value (ln is also used for mathematics)
Log10
This function returns a base-10 logarithm.
Summary
The computer only knows about computing
The computer performs basic operations: addition, subtraction, multiplication, division, and modulo (Modulo is the rest of Division)
Auto-increment is the operation of adding one variable to variable ++.
Auto-Subtraction is the operation to subtract one variable and write it as variable --
To increase the computing mode that computers can know, You need to load the math Library (# include <math. h>)
There are advanced functions in the Math Library, such as multiplication, square root, rounding, exponent, logarithm, and so on.
Part 1 Lesson 6 notice: conditional expressions
Today's class is here to cheer up.
In the next lesson, we will take the Sixth lesson to learn about conditional expressions.
Programmer Alliance public account* If you think this article is good, click "share to friends" or "send to friends" or "add to Favorites" in the upper-right corner of the screen"
* New friends should follow the "Programmer alliance" Search Public AccountProgrammerleleague
Little Editor: frogoscar
Mini-editor's QQ number: 379641629
Small mailbox: enmingx@gmail.com
(Most common with mailbox)
PS: A friend reported that reading articles on the mobile phone is too tired. In fact, they can be viewed on a browser webpage.
Method 1. click the "·" button in the upper-right corner of the screen, select "Copy link", paste the link to your browser, or send it to yourself by email, you can open it in your computer's browser.
Method 2. toutiao.comWww.toutiao.com, Search my self-Media "Programmer alliance", which has an article, you can also directly into this link: http://www.toutiao.com/m3750422747/
How do new friends view all articles:
Click "view public account" and then "view historical messages"
The "Programmer alliance" public account is designed for programmers and App designers. You love programming and sharing to push a wide range of programming-related knowledge, excellent software recommendations, and industry trends. Search for ProgrammerLeague and follow-up ~
Continuous attentionProgrammer AlliancePublic Account, more interesting, expected, and content with highlights are waiting for you!