设为首页 加入收藏

TOP

Accelerated C++ 学习笔记及题解----第二章
2015-07-20 17:43:48 来源: 作者: 【 】 浏览:1
Tags:Accelerated 学习 笔记 题解 ---- 第二章

本节主要讲解的是:

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.2cto.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
   
    
............余下省略,就是联系循环和控制流的....


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇zoj3811Untrusted Patrol((bfs+并.. 下一篇树和二叉树总结及算法实现

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)
·SQL CREATE INDEX 语 (2025-12-25 01:21:45)
·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)