"C + + programming principles and Practice" reading notes (i)

Source: Internet
Author: User
Tags case statement

Programming is an art that describes the problem solving scheme as a form that a computer can perform. Much of the work in programming is spent on solving solutions and on their refinement. In general, you only need to understand the problem itself in the process of actually writing the program to solve a problem.
Why learn the C + + programming language? Learning programming cannot be done without a programming language, and C + + directly supports the key concepts and techniques used in real-world software. C + + is one of the most widely used programming languages, with almost no limitations in its application areas. From the depths of the ocean to the surface of Mars, C + + programs can be found everywhere. C + + is an open international standard organization that is fully considered and carefully designed. High-quality and free C + + implementations can be found on any computer platform. Moreover, the programming ideas you have learned in C + + can be used directly in other programming languages such as C, C #, Fortran, and Java. For one last reason, I like the fact that C + + is good for writing beautiful, efficient code.

Here is a version of the classic first program. It outputs "hello,world!" on your screen:

#include <iostream>using namespace Std;int main (void) {cout << "hello,world!\n";    Output "hello,world!" return 0;}

There is an object of type int named Age, which holds the integer 42. By using a character variable, we can read a character from the input and print it out as follows:

#include <iostream> #include <string>using namespace Std;int main (void) {cout << "Please enter your FIR   St name (followed by ' enter '): \ n "String first_name;   Cin >> First_Name;   cout << "Hello," << first_name << "!\n"; return 0;}

The input Operation >> ("Get from") is type-sensitive, and the value it reads needs to be consistent with the type of the variable. For example:
Read name and age

int main (void) {cout << "Please enter your first name and age\n";    String first_name;    int age;    Cin >> First_Name;    CIN >> age;    cout << "Hello," << first_name << "(age" << age << ") \ n"; return 0;}

An example of a floating-point number:

Int main () {    cout <<  "Please enter a floating-point  value: ";    double n;    cin >> n;     cout <<  "n == "  << n          <<  "\n n+1 == "  << n+1          <<  "\n three times n == "  < < 3*n         <<\ntwice n ==  "  << n+n;         <<  "\nsquared of  n ==  " << n*n         << " \ nhalf of n ==  "<< n/2         < <  "\nsquare root of n ==  " << sqrt (n)           << endl;}

The string + means the connection, for example:

int main () {cout << "Please enter your first and second names\n";    string first;    string second;    CIN >> First >> second;    String name = First + ' +second; cout >> "Hello," << name << "\ n";}

The comparison of strings is particularly useful:

int main () {cout << "please enter name\n";    string first;    string second;    CIN >> fisrt >> second;    if (first = = second) cout << "that ' s the same name twice\n";    if (first < second) cout << first << "is alphabetically before" << second << "\ n"; if (Fist > second) cout << first << "is alphabetically after" << second << "\ n";}

To delete a repeating word:

int main () {string previous = "";    String current;  while (CIN >> Curren) {if (previous = = Curren) cout << "repeated word:" << current        << ' \ n ';    previous = current; }}

Repeat Word Count:

int main () {int number_of_words = 0;    We place the word counter at 0 string previous = "";    String current;    while (CIN >> current) {++number_of_current; Each time you enter a word, increment the counter if (previous = = current) cout << word number << number_of_words <&L T        "Repeated:" << current << "\ n";    previous = current; }}

The IF statement is the simplest choice statement and can be selected in two alternative branches. For example

int main () {int a = 0;    int b = 0;    cout << "Please enter integers\n";        Cin >> a >> b;    if (a < b) cout << "Max" ("<< a <<", "<< b <<") is "<< b <<" \ n "; else cout << "Max" ("<< a <<", << b << ") is" << b << "\ n";}

Switch statement, based on the selection of comparisons of numeric values with multiple constants.

Int main () {    const double cm_per_inch = 2.54;     double length = 1;    char unit =  ' A ';     cout <<  "please enter a length followed by a  Unit (c or i): \ n ";    cin >> length >> unit;     switch (unit)     {         case  "I":             cout <<  length <<  "in == "  << cm_per_inch*length <<  " Cm\n ";             break;         case  ' C ':              cout << length <<  "cm == "  << length/cm_per_inch <<   "in\n";             break;         default:              cout <<  "Sorry, i don ' t know a unit called  '"  << unit <<  ' \ n ';              break;    }}

A switch statement is more readable than an if statement, especially when compared to multiple constants. The value in parentheses after the keyword switch is compared with a set of constants, each marked with a case statement. If the value is equal to a constant, the case statement is selected to be executed, and each statement ends with a break. If the value is not equal to any of the constants after the case, select Execute Default statement. Although the default statement is not required, we recommend that you add it unless you are fully sure that the given branch has covered all the cases.

While statement

int main () {int i = 0;        while (I <) {cout << i << "\ T" << Square (i) << "\ n";    ++i; }}

For statement

int main () {for (int i = 0; i < ++i) {cout << i << "\ T" << Square (i) << ' \ n '    ; }}


Function

int square (int x) {return x*x;}    int main () {cout << square (2) << ' \ n '; cout << square << ' \ n ';}

Clearly, the description in C + + is more concise than the description in natural language (English, Chinese). This applies in many cases, and the purpose of the programming language is to describe our thoughts in a more concise and accurate manner.
The method of function definition is described as follows:

type function name (parameter table) function body

Where the type is the return value type of the function, the function name is the token of the function, in parentheses is the parameter table, and the function body is the statement that implements function function. Each element of a parameter table is called a parameter or form parameter, and the parameter table can be empty. If the function does not need to return any results, the return value type can be set to void.

Why use a function: When you need to implement a part of a calculation task independently, you can define it as a function, because you can:
(1) to realize the separation of computational logic.
(2) Make the code clear (by using the function name).
(3) The use of functions, so that the same code in the program can be used multiple times.
(4) Reduce the workload of the program debugging.

A vector is a set of data elements stored sequentially that can be accessed through an index.

Vectof<int> V (6);vector<string> philosopher (4);

Abnormal

Int main () {   try   {       vector< int> v;       int x;        while  (cin >> x)  v.push_back (x);        for (int  i = 0; i <= v.size ();  ++i)             cout <<  "v["  << i <<  "]=="  < < v[i] << endl;   }catch (Out_of_range)    {        cerr <<  "oops! range error\n";        return 1;   }   catch (...)    {       cerr <<  "exception:  Something went wrong\n ";        return 2;   }} 


"C + + programming principles and Practice" reading notes (i)

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.