(1) A simple C ++ program named "c ++ Primer" in the wind color from zero to single, and "cprimer" c ++

Source: Internet
Author: User

(1) A simple C ++ program named "c ++ Primer" in the wind color from zero to single, and "cprimer" c ++


C ++ Primer


What we learned


0. Preface

I used to take C ++ courses, but I didn't study it seriously at the time. I basically cannot use c ++ for project development. After learning c ++ again, I will first read the fifth version of c ++ Prime in English, hoping to lay a solid foundation for future study.



1. Program

A simple c ++ Program (P17) helps us quickly understand the c ++ code style and learn c ++ through analysis code

Program intent: enter a set of numbers on the terminal to output the number of outputs of different numbers. The same number must be continuous.

Assume that:

42 42 42 42 55 55 62 100 100 100

The terminal should output:

42 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 will read new value into val. currVal is used to store the data currently calculated. The newly read data will be placed in val int currVal = 0, val = 0; // read first number and ensure that we have data to process. to ensure that there is operational data, read an if (std: cin> currVal) {int cnt = 1; // store the count for the current value we're processing. cnt is used to count the number of times the current 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 currently calculated data, ++ cnt; else {std :: cout <currVal <"occurs" <cnt <"times" <std: endl; currVal = val; // remember the new value cnt = 1 ;}} // while loop ends here. while loop ends} // outermost if statement ends here. if it ends return 0 ;}


2. Code Analysis:
2.1 functions:

Each c ++ program has at least one function, one of which must be called main. It is the entry to the program when the operating system runs the c ++ program. In the above program, we also defined a main function, which is:

</Pre> <pre name = "code" class = "cpp"> int main () {// currVal is the number we are counting; we will read new value into val. currVal is used to store the currently computed data. The newly read data will be placed in val ...... return 0 ;}
A function consists of four parts:

1) return type

In this function, the return type is int, indicating an integer.

2) function name

In this function

3) parameter (can be blank)

The parameter is entered in () after main, and the parameter of the function to be modified is null.

4) function subject

Curly braces and Inner Parts

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 belong to the same type as the function. In this example, the function returns an integer of 0.


2.2 use Library (function Library ):

The first line of the program:

#include<iostream>

In program implementation, we not only define some functions, but also use some functions that have been written by others to simplify our coding. When using other functions, the header file of the corresponding function library must be introduced.

Program D-Line High-Speed compiler we will use iostream libraary. <> The name is the header file to be introduced. Here we introduce iostream to implement input and output operations on the terminal. <> Indicates that a standard file is introduced. To import your own file, use "".


2.3 notes:

Comments are not executed by the compiler. They are used to help others read the code.

C ++ has two annotation methods:

1) Single Row comment//

2) c Annotation/**/


2.4 Standard Input and Output:

In this example, iostream library is used to process standard input and output. It contains istream and ostream, which represent input streams and output streams respectively ). Stream indicates reading/writing a series of sequential characters from the input/output device.

std::cin >> currVal
Read a data from the terminal and store it on the variable currVal. The currVal variable is used to store the number currently calculated.

std::cout<< currVal << " occurs " << cnt <<"times" << std::endl;
Number of times that the current number appears on the terminal. The variable cnt records the number of occurrences of currVal. Endl indicates line feed.

The input and output can be used consecutively as follows:

int v1 =0, v2 = 0;std::cin>>v1>>v2;std::cout<<v1<<v2<<std::endl;

This is because >>( <) returns the operator on its left (operand)


2.5 namespace:

If different libraries are introduced and functions with the same name exist in the database, the compiler will not know which function to call when calling this function. To solve this conflict, c ++ uses the namespace. Notes

std::cin >> currVal
Std: indicates that the cin function in the standard library is used.


2.6IF statement and combination of IF statement and input statement:

If (Condition Statement) {// execution content}

First, execute the statement Condition Statement in (). The content in {} is executed only when the condition statement is true. The common usage is if (a = B). if a is equal to B, a = B is true. Execute {}. Otherwise, a = B is false, not executed in. in fact, a condition statement that is not 0 is true.

In this example

if(std::cin >> currVal)
Note that if std: cin> currVal successfully reads data, it is true.


2.7WHILE statement and the combination of IF statement and input statement:

While (Condition Statement) {// execution content}
First, execute the condition statement. If the condition is true, the expert in {} will be executed, and then the conditional statement will be executed. If the condition continues to be true, the content of {} will be executed, the condition is false after execution.

As described in the if statement, you can write the following statement if you do not know how much data you want to read.

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

When istream is invalid, std: cin> currVal fails to be read. The condition is false. Invalid scenarios:

1) when we enter end-of-file (eof) (Dos/Windows: ctrl + z Linux: ctrl + d)

2) illegal data is input. If the data is null or the data type is incorrect



3. Job:

P9

/** Exercise 1.3: Write a program to print Hello, World on the standard output * Write a program to print Hello in the standard output, world */# include <iostream> int main () {std: cout <"Hello, World" <std: endl; return 0 ;}

P11

/** Exercise 1.8: Indicate which, if any, of the following output statements are legal: * specifies which output statements are valid */# include <iostream> int main () {std: cout <"/*"; // The valid output is/* // std: cout <"*/"; // The valid output is * // std: cout </* "*/" */; // The Last invalid "// std :: cout </* "*/"/* "/*" */; // valid


P13
/** Exercise 1.9: write a program that uses a while to sum the numbers from 50 to 100 * use while to calculate the cumulative value from 50 to 100 */# include <iostream> int main () {int sum = 0, val = 50; while (val <101) {sum + = val; ++ val;} std :: cout <"The sum of 50 to 100 cumulative sive is" <sum <std: endl; return 0 ;}

/** Exercise 1.11 * Write a program that prompts the user for two integers. Print each number in the range specified by thoe two intergers * prompts the user to enter two integers. Print the number of two integers. */# Include <iostream> int main () {int val1 = 0, val2 = 0, temp = 0; std :: cout <"please input two 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 ;}



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.