原创 第一个比较像样的C++程序

2009-3-16 13:48 2129 4 4 分类: 软件与OS

《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条评论)

登录后参与讨论
我要评论
0
4
关闭 站长推荐上一条 /2 下一条