Accelerated C + + Learning notes and----Chapter II

Source: Internet
Author: User

This section mainly explains:

While statement

If statement

For statement

logical operators.

The new types that are designed in this section are:

BOOL Boolean value

Unsigned non-negative integers

Short at least 16-bit integer

Long

size_t unsigned integer type, which can hold the length of any object

String::size_type unsigned integer type, which can store the length of any string


Source code in the book: Frame.cpp

#include <iostream> #include <string>//say what standard-library names we useusing namespace Std;int main () {/  /ask for the person's namecout << "Please enter your first name:"//Read the namestring name;cin >> name;// Build the message, we intend to writeconst string greeting = "Hello," + name + "!"; /The number of blanks surrounding the greetingconst int pad = 1;//The number of rows and columns to writeconst int rows = Pad * 2 + 3;const string::size_type cols = greeting.size () + pad * 2 + 2;//write a blank line to separate the output FR Om the inputcout << endl;//write ' rows ' rows of output//Invariant:we has written ' r ' rows so farfor (int r = 0; R! = rows; ++r) {String::size_type c = 0;//Invariant:we has written ' C ' characters so far in the current rowwhile (c! = cols) {//  Is it time to write the greeting?if (r = = pad + 1 && c = = pad + 1) {cout << greeting;c + + greeting.size ();} else {//is we on the border?if (r = = 0 | | r= = Rows-1 | | c = = 0 | | c = = cols-1) cout << "*"; Elsecout << ""; ++c;}} cout << Endl;} return 0;}


Exercises

2-1

Change the code pad above to 0

2-2

#include <iostream> #include <string>//say what standard-library names we useusing std::cin; Using std::endl;using std::cout; Using Std::string;int Main () {//ask for the person ' s namecout << "Please enter your first name:"//Read the Namest Ring Name;cin >> name;//Build the message that we intend to writeconst string greeting = "Hello," + name + "!"; /The number of blanks surrounding the greetingconst int h_pad = 1;const int v_pad = 2;//the number of rows and columns T  o writeconst int rows = V_pad * 2 + 3;const string::size_type cols = greeting.size () + H_pad * 2 + 2;//write a blank line To separate the output from the Inputcout << endl;//write ' rows ' rows of output//Invariant:we has written ' R ' r OWS so farfor (int r = 0; R! = rows; ++r) {String::size_type c = 0;//Invariant:we has written ' C ' characters so far in T He current rowwhile (c! = cols) {//Is it time to write the greeting?if (r = = V_pad + 1 && c = = H_pad + 1) {cout &lt ;< GreeTing;c + = Greeting.size ();} else {//is we on the border?if (r = = 0 | | r = = ROWS-1 | | c = = 0 | | c = = cols-1) cout << "*"; Elsecout << ""; ++c;}} cout << Endl;} return 0;}

2-3

#include <iostream> #include <string>//say what standard-library names we useusing namespace Std;int main () {/  /ask for the person's namecout << "Please enter your first name:"//Read the namestring name;cin >> name;// Build the message, we intend to writeconst string greeting = "Hello," + name + "!";    /The number of blanks surrounding the greetingint pad;    cout << "Please input the number of the space:"; CIN >> pad;//The number of rows and columns to writeconst int rows = Pad * 2 + 3;const string::size_type cols = GRE Eting.size () + pad * 2 + 2;//write a blank line to separate the output from the Inputcout << endl;//write ' rows ' r oWS of output//Invariant:we has written ' r ' rows so farfor (int r = 0; R! = rows; ++r) {String::size_type c = 0;//Inva Riant:we has written ' C ' characters so far in the current rowwhile (c! = cols) {//Is it time to write the greeting?if ( r = = Pad + 1 && c = = pad + 1) {cout << GreetiNg;c + = Greeting.size ();}    else {//is we on the border?if (r = = 0 | | r = = ROWS-1 | | c = = 0 | | c = = cols-1) cout << "*"; Elsecout << ""; ++c;}} cout << Endl;} return 0;}

2-4

#include <iostream> #include <string>//say what standard-library names we useusing std::cin; Using std::endl;using std::cout; Using Std::string;int Main () {//ask for the person ' s namecout << "Please enter your first name:"//Read the names Tring name;cin >> name;//Build the message that we intend to writeconst string greeting = "Hello," + name + "!"; /The number of blanks surrounding the greetingconst int pad = 1;//The number of rows and columns to writeconst int rows = Pad * 2 + 3;const string::size_type cols = greeting.size () + pad * 2 + 2;const string spaces = String (Greeting.size () + Pad * 2, ');//write a blank line to separate the output from the Inputcout << endl;//write ' rows ' rows of output  Invariant:we has written ' r ' rows so farfor (int r = 0; R! = rows; ++r) {String::size_type c = 0;//Invariant:we has Written ' C ' characters so over the current rowwhile (c! = cols) {//Is it time to write the greeting?if (r = = Pad + 1 & amp;& c = = Pad + 1) {cout << greeting;c + = Greeting.size ();} else {//is we on the border?if (r = = 0 | | r = = ROWS-1 | | c = = 0 | | c = = cols-1) {cout << "*"; ++c;} else if (r = = Pad + 1) {cout << ""; ++c;} else {cout << spaces;c + = Spaces.size ();}}} cout << Endl;} return 0;}

2-5

#include <iostream>using namespace Std;int main () {    int length;    cout << "This program was intend to print a square and triangle:" << Endl;    cout << "Please input the length of border:";    cin >> length;    Print the square for    (int i = 0; i<length; i++)    {for        (int j = 0; J < length; J + +)        {            if (I ==0 | | i = = length-1)            {                cout << "*";            }            Else                if (j = = 0 | | j = = length-1)                    cout << "*";                else cout << "";        }        cout << Endl;    }    Print triangle .....    such as ..... ....    . return 0;}

............ The rest of the ellipsis is the contact loop and control flow ....


Accelerated C + + Learning notes and----Chapter II

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.