《Accelerated C++中文版》
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
int main()
{
//ask for the person's name
cout << "please enter your first name: ";
//read the name
std::string name;
cin >> name;
//build the message that intend to write
const std::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;
std::string::size_type cols = greeting.size() + pad * 2 + 2;
/*
const std::string spaces(greeting.size(), ' ');
const std::string second = "* " + spaces + " *";
const std::string first(second.size(), '*');
*/
cout << endl;
for (int r = 0; r != rows; ++r) {
std::string::size_type c = 0;
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;
}
system("PAUSE");
return 0;
}
文章评论(0条评论)
登录后参与讨论