程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Accelerated C++ 學習筆記及題解----第二章

Accelerated C++ 學習筆記及題解----第二章

編輯:C++入門知識

Accelerated C++ 學習筆記及題解----第二章


本節主要講解的是:

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 #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 h_pad = 1; const int v_pad = 2; // the number of rows and columns to write const 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 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 == v_pad + 1 && c == h_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-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;
}

2-5

#include 

using 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
............余下省略,就是聯系循環和控制流的....


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved