pr0905.cpp

来自「practice c++, it is from the book http:/」· C++ 代码 · 共 27 行

CPP
27
字号
//  Programming with C++, Second Edition, by John R. Hubbard
//  Copyright McGraw-Hill, 2000
//  Problem 9.5 on page 222
//  Reprinting names

#include <iostream>  // defines the cout object
using namespace std;

int main()
{ string word, first, last;
  char c;
  bool is_first, is_last = true;
  string name[32];
  int n=0;
  while (cin >> word)
  { cin.get(c);          // should be either a blank or a newline
    is_first = is_last;          //  current word is a first name
    is_last = bool(c == '\n');    //  current word is a last name
    if (is_first) first = word;
    else if (is_last) name[n++] = word + ", " + first;
    else first += " " + word.substr(0,1) + ".";    // add initial
  }
  --n;
  for (int i=0; i<n; i++)
    cout << '\t' << i+1 << ". " << name[i] << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?