本節主要講解的是:
while語句
if語句
for語句
邏輯運算符.
本節設計的新類型有:
bool 布爾值
unsigned 非負整數
short 至少16位整數
long
size_t 無符號整數類型,可以保存任何對象的長度
string::size_type 無符號整數類型,可以存儲任意字符串的長度
書中的源代碼:frame.cpp
#include#include // say what standard-library names we use using namespace std; int main() { // ask for the person's name cout << "Please enter your first name: "; // read the name string name; cin >> name; // build the message that we intend to write const string greeting = "Hello, " + name + "!"; // the number of blanks surrounding the greeting const int pad = 1; // the number of rows and columns to write const int rows = pad * 2 + 3; const string::size_type cols = greeting.size() + pad * 2 + 2; // write a blank line to separate the output from the input cout << endl; // write `rows' rows of output // invariant: we have written `r' rows so far for (int r = 0; r != rows; ++r) { string::size_type c = 0; // invariant: we have written `c' characters so far in the current row while (c != cols) { // is it time to write the greeting? if (r == pad + 1 && c == pad + 1) { cout << greeting; c += greeting.size(); } else { // are we on the border? if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1) cout << "*"; else cout << " "; ++c; } } cout << endl; } return 0; }
<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+zOK94jo8L3A+CjxwPjItMTwvcD4KPHA+vavJz8PmtcS0+sLrcGFkuMTOqjA8L3A+CjxwPjItMjwvcD4KPHA+PHByZSBjbGFzcz0="brush:java;">#include
2-3
#include#include // say what standard-library names we use using namespace std; int main() { // ask for the person's name cout << "Please enter your first name: "; // read the name string name; cin >> name; // build the message that we intend to write const string greeting = "Hello, " + name + "!"; // the number of blanks surrounding the greeting int pad ; cout << "Please input the number of the space :" ; cin >> pad; // the number of rows and columns to write const int rows = pad * 2 + 3; const string::size_type cols = greeting.size() + pad * 2 + 2; // write a blank line to separate the output from the input cout << endl; // write `rows' rows of output // invariant: we have written `r' rows so far for (int r = 0; r != rows; ++r) { string::size_type c = 0; // invariant: we have written `c' characters so far in the current row while (c != cols) { // is it time to write the greeting? if (r == pad + 1 && c == pad + 1) { cout << greeting; c += greeting.size(); } else { // are we on the border? if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1) cout << "*"; else cout << " "; ++c; } } cout << endl; } return 0; }
2-4
#include#include // say what standard-library names we use using std::cin; using std::endl; using std::cout; using std::string; int main() { // ask for the person's name cout << "Please enter your first name: "; // read the name string name; cin >> name; // build the message that we intend to write const string greeting = "Hello, " + name + "!"; // the number of blanks surrounding the greeting const int pad = 1; // the number of rows and columns to write const 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 input cout << endl; // write `rows' rows of output // invariant: we have written `r' rows so far for (int r = 0; r != rows; ++r) { string::size_type c = 0; // invariant: we have written `c' characters so far in the current row while (c != cols) { // is it time to write the greeting? if (r == pad + 1 && c == pad + 1) { cout << greeting; c += greeting.size(); } else { // are 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; }
#includeusing namespace std; int main() { int length; cout << "This program is intend to print a 正方形 and 三角形 :" << endl; cout << "Please input the length of border: " ; cin >> length; // print the 正方形 for (int i = 0; i
............余下省略,就是聯系循環和控制流的....