Accelerated C++ Chapter02

どもです。

メモ

  • size_t を使う場合は,cstddef をインクルード。
    • cstddef をインクルードするので,別に,std::size_t としても大丈夫みたい。
    • 面倒臭いから,size_t で済ますけど。warningもでないし問題ないだろう。

解答(長いよ)

Exercise 2-1 についてはちょいとずるをした。

#include <iostream>

using namespace std;

void ex2_1()
{
  cout << "Please input your first name: ";
  string name;
  cin >> name;

  const string greeting = "Hello, " + name + "!";
  const int pad = 0;            // changed
  const int rows = pad * 2 + 3;
  const string::size_type cols = pad * 2 + 2 + greeting.size();

  for (int r = 0; r != rows; ++r) {
    string::size_type c = 0;
    while (c != cols) {
      if (r == pad + 1 && c == pad + 1) {
        cout << greeting;
        c += greeting.size();
      }
      else {
        if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1) {
          cout << "*";
        }
        else {
          cout << " ";
        }
        ++c;
      }
    }
    cout << endl;
  }
}

void ex2_2()
{
  cout << "Please input your first name: ";
  string name;
  cin >> name;

  const string greeting = "Hello, " + name + "!";
  const int vpad = 3;
  const int hpad = 1;
  const int rows = vpad * 2 + 3;
  const string::size_type cols = hpad * 2 + 2 + greeting.size();

  for (int r = 0; r != rows; ++r) {
    string::size_type c = 0;
    while (c != cols) {
      if (r == vpad + 1 && c == hpad + 1) {
        cout << greeting;
        c += greeting.size();
      }
      else {
        if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1) {
          cout << "*";
        }
        else {
          cout << " ";
        }
        ++c;
      }
    }
    cout << endl;
  }
}

void ex2_3()
{
  cout << "Please input your first name: ";
  string name;
  cin >> name;

  const string greeting = "Hello, " + name + "!";
  cout << "Please input the amount of spacing: ";

  int pad;
  cin >> pad;
  if (pad < 0) { pad = 0; }

  const int rows = pad * 2 + 3;
  const int cols = pad * 2 + 2 + greeting.size();

  for (int r = 0; r != rows; ++r) {
    int c = 0;
    while (c != cols) {
      if (r == pad + 1 && c == pad + 1) {
        cout << greeting;
        c += greeting.size();
      }
      else {
        if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1) {
          cout << "*";
        }
        else {
          cout << " ";
        }
        ++c;
      }
    }
    cout << endl;
  }
}

void ex2_4()
{
  cout << "Please input your first name: ";
  string name;
  cin >> name;

  const string greeting = "Hello, " + name + "!";
  const int pad = 3;
  const int rows = pad * 2 + 3;
  const int cols = pad * 2 + 2 + greeting.size();
  const string spaces(greeting.size() + pad * 2, ' ');

  for (int r = 0; r != rows; ++r) {
    // Starting form column nubmer zero
    int c = 0;
    while (c != cols) {
      if (r == pad + 1 && c == pad + 1) {
        cout << greeting;
        c += greeting.size();
      }
      else {
        if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1) {
          // We are on a top, bottom, corner, or left or right side.
          cout << "*";
          ++c;
        }
        else if (r == pad + 1) {
          // We are on the greeting line but not on the greeting message itself.
          cout << ' ';
          ++c;
        }
        else {
          // We are on an almost blank line.
          cout << spaces;
          c += spaces.size();
        }
      }
    }
    cout << endl;
  }
}

void rectangle(const size_t height, const size_t width)
{
  // ********
  // *      *
  // *      *
  // ********
  for (size_t h = 0; h != height; ++h) {
    if (h == 0 || h == height - 1) {
      cout << string(width, '*');
    }
    else {
      cout << "*";
      cout << string(width - 2, ' ');
      cout << "*";
    }
    cout << endl;
  }
}

void square(const size_t len)
{
  rectangle(len, len * 2);
}

void triangle(const size_t height)
{
  // height = 4 as an example
  //     *
  //    * *
  //   *   *
  //  *******
  if (height < 1) { return; }
  const size_t width = height * 2 - 1;
  const char ch = '*';
  size_t nspaces = (width - 1) / 2;
  for (size_t h = 0; h != height; ++h) {
    if (h == 0) {
      // we are on the top
      cout << string(nspaces, ' ');
      cout << ch;
      cout << string(nspaces, ' ');
    }
    else if (h == height - 1) {
      // we are on the bottom
      cout << string(width, ch);
    }
    else {
      // output left-side blanks
      cout << string(nspaces, ' ');
      cout << ch;
      // output inner blanks
      cout << string(2 * h - 1, ' ');
      cout << ch;
      // output right-side blanks
      cout << string(nspaces, ' ');
    }
    cout << endl;
    --nspaces;
  }
}

void ex2_5()
{
  cout << "Square:" << endl;
  square(4);
  cout << "Rectangle:" << endl;
  rectangle(4, 12);
  cout << "Triangle:" << endl;
  triangle(5);
}

void ex2_7()
{
  for (int i = 10; i > -6; --i) {
    cout << i << ", ";
  }
  cout << endl;
}

void ex2_8()
{
  for (int i = 1; i < 10; ++i) {
    for (int j = 1; j < 10; ++j) {
      cout << i * j << ", ";
    }
    cout << endl;
  }
}

void ex2_9()
{
  cout << "Please input a integer: ";
  int first_num;
  cin >> first_num;
  cout << "Please input another integer: ";
  int second_num;
  cin >> second_num;

  if (first_num < second_num) {
    cout << second_num << " is greater than " << first_num << endl;
  }
  else if (first_num > second_num) {
    cout << first_num << " is greater than " << second_num << endl;
  }
  else {
    cout << first_num << " and " << second_num << " are equal" << endl;
  }
}

int main()
{
  cout << "==================== ex2-1 ====================" << endl;
  ex2_1();
  cout << "==================== ex2-2 ====================" << endl;
  ex2_2();
  cout << "==================== ex2-3 ====================" << endl;
  ex2_3();
  cout << "==================== ex2-4 ====================" << endl;
  ex2_4();
  cout << "==================== ex2-5 ====================" << endl;
  ex2_5();
  cout << "==================== ex2-7 ====================" << endl;
  ex2_7();
  cout << "==================== ex2-8 ====================" << endl;
  ex2_8();
  cout << "==================== ex2-9 ====================" << endl;
  ex2_9();
}