(1) dearths from zero single row "C + + Primer" a simple C + + program

Source: Internet
Author: User


From zero single row "C + + Primer"


This Learning Harvest


0. Write in front

Dearths once had a C + + course, but did not study hard at the time, basically can not use C + + for project development. This time to re-learn C + +, first read the C++prime English version of the fifth edition, hoping to lay a solid foundation for future learning.



1. Procedure

A simple C + + program (P17) that helps us quickly understand the code style of C + + and learn C + + by analyzing code

Program Intent: Enter a set of numbers on the terminal to output the number of different digital outputs. The same number must be contiguous.

Suppose input:

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 in an    if (std::cin >> currval) {        int cnt = 1;//store The count for the current value we ' re process Ing. CNT is used to count 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 values are the same. If 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 looping end    }//  outermost if state ment ends here. If end    return 0;}


2. Code Analysis:
2.1 Functions:

Each C + + program has at least one function, one of which must be called Main. He is the entry point of the operating system when running C + + programs. In the above program, we also define a main function, which is:

</pre><pre name= "code" class= "CPP" >int Main () {//currval is the number    we are counting;we would read new VA Lue 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, which represents an integer.

2) Name of function

In this function is the main

3) parameter (can be empty)

parameter is filled in () after main, the parameter of the function is empty

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 case, 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>

in the implementation of the program, we not only define some of our own functions, we usually also use some of the functions that others have written to simplify our coding. Using someone else's function must introduce the corresponding function library header file.

Cheng Line High-speed compiler we're going to use iostream libraary. The <> inside name is the header file we want to introduce. Here we introduce the iostream in order to implement the input and output operation on the terminal. <> indicates the introduction of a standard file, to introduce its own file, using "".


2.3 Notes:

Commented out, not executed by the compiler, and comments are used to make it easier for others to read the code.

There are two kinds of annotation methods for C + +

1) Single-line comment //

2) C Comment /* */


2.4 Standard input and output:

In this example, the iostream library is used to process standard inputs and outputs. It contains the IStream and Ostream, respectively, representing input streams (input stream) and output streams (output stream). Stream (stream) means reading/writing a sequence of characters from input.

Std::cin >> Currval
reads a data from the terminal and stores it 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:

If a different library is introduced, and if there is a function of the same name in the library, the compiler will not know which one 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) {    //execute content}

The statement condition statement within () is executed first, and the contents within {} are executed only if the condition statement is true. A common usage is if (a = = B), if a equals B, then A==b is true, the {} content is executed, otherwise a==b is false and {} is not executed. In fact, a conditional statement other than 0 is true.

In this example, there are

if (std::cin >> currval)
Note that if Std::cin>>currval successfully read the data, it is true.


the 2.7WHILE statement and the IF statement are used in combination with the input statement:

while (conditional statement) {    //execute content}
The conditional statement is executed first, if the condition is true, the expert within {} is executed, then the conditional statement continues, and if it continues to be true, the {} content is resumed, so that it is false after the condition is executed.

As described in the IF statement, you can write the following statement when you do not know how much data to read

while (Std::cin>>val) {}

when the IStream is invalid, the Std::cin>>currval read fails with a false condition. 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 implementation to print hello,world*/#include in normal output <ios 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

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.