From zero single row "C + + Primer"
--(1) a simple C + + program
This Learning Harvest
0. Write in front
Dearths had previously had a C + + course, but did not take it seriously, and could not use C + + for project development.
This time again to learn C + +, the first will read C++prime English version of the fifth edition, hope to be able to lay a solid foundation for future learning.
1. Procedure
A simple C + + program (P17) that helps us understand the code style of C + + at a high speed and learn C + + by analyzing code
Program Intent: Enter a set of numbers on the terminal. The number of times to output different digital outputs. The same number must be contiguous.
If you enter:
42 42 42 42 55 55 62 100 100 100
The terminal should output:
Occurs 5 TIMES55 occurs 2 times62 occurs 1 times100 occurs 3 times
Code:
#include <iostream>int main () {//currval is the number we are counting;we would read new value into Val. currval To hold the current computed data, the newly read data is placed in val int currval = 0, val = 0; Read first number and ensure that we had data to process. To ensure that there is actionable data. First read into an if (std::cin >> currval) { int cnt = 1;//store The count for the current value we ' re processing. CNT is used to count Number. How many times the currently stored data appears while (Std::cin >> val) {//Read the remaining numbers. Read the remaining data if (val = = currval) //if the value S is the same. Assume that the newly read data is the same as the current computed data ++cnt; else { std::cout<< currval << "occurs" << cnt << "Times" << Std::endl;currval = Val;
//remember the new value cnt = 1;} }//While loop ends here. While cycle end }// outermost if stat Ement ends here. If end return 0;}
2. Code Analysis:
2.1 Functions:
Each C + + program has at least one function, and one of the functions must be called Main. He is the entry point of the operating system when executing C + + programs.
In the above program, we also customize a main function. It is:
</pre><pre name= "code" class= "CPP" >int Main () {//currval is the number we are counting;we would read new V Alue into Val. Currval is used to store the data of the current calculation, and the newly read data will be placed in Val ... return 0;}
A function consists of 4 parts:
1) return type
In this function, its return type is int. Represents an integer.
2) Name of function
In this function is the main
3) Number of parameters (can be empty)
The parameters are filled in () behind Main. Null to change the function's number of parameters
4) Function body
Curly braces and within the section
The last statement of this function: return.
This is a statement that can be used to end a function. In this example, return can return a value to the caller of the function.
The returned value must be the same as the return type of the function. In this example, the function returns an integer of type 0.
2.2 Using library (function libraries):
The first line of the program:
#include<iostream>
When the program is implemented, we will not only customize some functions. It is also common to use functions that others have already written to simplify our coding.
Using someone else's function must introduce the corresponding function library header file.
Cheng a line of fast compilers we're going to use iostream libraary. The <> inside name is the header file we want to introduce.
Here we introduce the iostream. So that the input and output operations are now on the terminal. <> indicates the introduction of a standard file, to introduce its own file, using "".
2.3 Gaze:
Staring out of the content, the compiler will not run. Gaze is used to make it easier for others to read code.
C + + has two methods of staring
1) Single Gaze //
2) C Stare */* *
2.4 Standard input and output:
In this example, the iostream library is used to process standard inputs and outputs. It includes the IStream and the ostream. Represents input streams (input stream) and output streams (output stream), respectively. Stream (stream) means reading/writing a sequence of characters from input.
Std::cin >> Currval
Reads a data from the terminal. stored on the variable currval.
The Currval variable is used to hold the current calculated number.
std::cout<< currval << "occurs" << cnt << "Times" << Std::endl;
How many occurrences of the current number are output from the terminal.
The variable CNT records the number of occurrences of the numbers stored by the currval.
Endl represents the line break.
The input and output can be used in such a continuous way:
int v1 =0, v2 = 0;std::cin>>v1>>v2;std::cout<<v1<<v2<<std::endl;
this is because >> (<<) returns the operand to its left (operand)
2.5 namespaces:
Assuming that a different library is introduced, and the library assumes a function of the same name, the compiler will not know which to call when the function is called. In order to resolve this conflict, C + + uses a namespace. Notice that the
Std::cin >> Currval
STD Inside:: Indicates the use of the CIN function in the standard library.
the 2.6IF statement and the IF statement are used in combination with the input statement:
if (conditional statement) { //run content}
First run the statement condition statement within (), only the conditional statement is true to run the contents of {}. The common use is if (a = = B), assuming a equals B, then A==b is true and the {} content is run. Otherwise a==b is false and does not run {}. In fact, a conditional statement other than 0 is true.
In this example, there are
if (std::cin >> currval)
Note that it is true if Std::cin>>currval successfully read the data.
the 2.7WHILE statement and the IF statement are used in combination with the input statement:
while (conditional statement) { //run content}
run the condition statement first, assuming the condition is true, then run the insider in {}. Then continue to run the conditional statement. Suppose to continue to be true. The {} content continues to run. This cycle is not true until the condition is run.
As described in the IF statement. The ability to write statements such as the following when you do not know how much data to read
while (Std::cin>>val) {}
When the IStream is invalid. Std::cin>>currval read failed. The condition is false. Invalid case has
1) When we enter End-of-file (EOF) (Dos/windows:ctrl+z linux:ctrl+d)
2) entered illegal data, such as the data is empty, or the type does not match
3. Operation:
P9
/** Exercise 1.3:write A programs to print Hello,world on the standard output* write a program that is now printing hello,world*/#include <ios in normal output Tream>int Main () { std::cout<< "Hello,world" <<std::endl; return 0; }
P11
/** Exercise 1.8:indicate which,if any,of The following output statements is legal:* indicate which output statements are legal */#include <iostream >int Main () { std::cout<< "/*";//Legal output is/*// std::cout<< "*/";//Legal output is */// std::cout< </* "* *" */;//illegal last missing a "//std::cout<</*" */"/*"/* "* *" */;//Legal output
P13
/** Exercise 1.9:write A program this uses a while to sum the numbers from the 100* with while calculates the cumulative value of 50 to 100 */#include <io Stream>int Main () { int sum = 0,val =; while (val<101) { sum + = val; ++val; } std::cout<< "The sum of inclusive is" << sum <<std::endl; return 0; }
/**exercise 1.11 *write A program This prompts the user for both integers. Print each number in the range specified by Thoe Two intergers* prompts the user to enter two integers. Prints the number of two integers in a fly. */#include <iostream>int main () { int val1 = 0, val2 = 0, temp = 0; std::cout<< "Please input intergers" <<std::endl; if (STD::CIN>>VAL1) {if (std::cin>>temp) { if (temp>val1) val2 = temp; else{ val2 = val1; Val1 = temp; } while (Val1<=val2) { std::cout<<val1<<std::endl; ++val1; }}} return 0;}
(1) dearths from zero single row "C + + Primer" a simple C + + program