标签:blog http os io ar for 2014 sp 代码
本节主要讲解的是:
while语句
if语句
for语句
逻辑运算符.
本节设计的新类型有:
bool 布尔值
unsigned 非负整数
short 至少16位整数
long
size_t 无符号整数类型,可以保存任何对象的长度
string::size_type 无符号整数类型,可以存储任意字符串的长度
书中的源代码:frame.cpp
#include <iostream>
#include <string>
// 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;
}
题解:
2-1
将上面的代码pad改为0
2-2
#include <iostream>
#include <string>
// 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 <iostream>
#include <string>
// 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 <iostream>
#include <string>
// 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;
}#include <iostream>
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<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 三角形
..............such as....
return 0;
}
Accelerated C++ 学习笔记及题解----第二章
标签:blog http os io ar for 2014 sp 代码
原文地址:http://blog.csdn.net/suool/article/details/39137061