From 2c9056a23e1a55fd21a8e314c903d9325bffd62e Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Sat, 6 Sep 2025 09:37:14 +0200 Subject: Move CPP code here Signed-off-by: Carlos Maiolino --- CPP/cpp_book/chap2/greeting.cpp | 54 ++++++++++++++++++++++++++++++++++++++++ CPP/cpp_book/chap2/greeting.cpp~ | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 CPP/cpp_book/chap2/greeting.cpp create mode 100644 CPP/cpp_book/chap2/greeting.cpp~ (limited to 'CPP/cpp_book/chap2') diff --git a/CPP/cpp_book/chap2/greeting.cpp b/CPP/cpp_book/chap2/greeting.cpp new file mode 100644 index 0000000..516d36a --- /dev/null +++ b/CPP/cpp_book/chap2/greeting.cpp @@ -0,0 +1,54 @@ +#include +#include + +#define ROW_PAD 5 +#define COL_PAD 8 + +int main(void) +{ + using std::cout; using std::endl; + using std::cin; using std::string; + + const int rows = ROW_PAD * 2 + 3; // empty + first/last line + greeting + string name; + + cout << "Enter your name: "; + cin >> name; + + const string greeting = "Hello " + name + "!"; + const string::size_type cols = COL_PAD * 2 + greeting.size() + 2; + + cout << endl; + + /* + * Loop invariant: + * -We've written 'r' rows so far + */ + const string col_blank(COL_PAD, ' '); + const string row_blank(cols - 2, ' '); + for (int r = 0; r != rows; r++) { + + string::size_type c = 0; + /* + * Loop invariant: + * - We've written 'c' columns so far + */ + while (c != cols) { + if (r == ROW_PAD + 1 && c == 1) { + cout << col_blank << greeting << col_blank; + c += greeting.size() + (col_blank.size() * 2); + } else { + if (r == 0 || r == rows - 1 || + c == 0 || c == cols - 1) { + cout << "*"; + c++; + } else { + cout << row_blank; + c += row_blank.size(); + } + } + } + cout << endl; + } + return 0; +} diff --git a/CPP/cpp_book/chap2/greeting.cpp~ b/CPP/cpp_book/chap2/greeting.cpp~ new file mode 100644 index 0000000..c2a3b8f --- /dev/null +++ b/CPP/cpp_book/chap2/greeting.cpp~ @@ -0,0 +1,54 @@ +#include +#include + +#define ROW_PAD 5 +#define COL_PAD 8 + +int main(void) +{ + using std::cout; using std::endl; + using std::cin; using std::string; + + const int rows = ROW_PAD * 2 + 3; // empty + first/last line + greeting + string name; + + cout << "Enter your name: "; + cin >> name; + + const string greeting = "Hello " + name + "!"; + const string::size_type cols = COL_PAD * 2 + greeting.size() + 2; + + cout << endl; + + /* + * Loop invariant: + * -We've written 'r' rows so far + */ + const string col_blank(COL_PAD, ' '); + const string row_blank(cols - 2, ' '); + for (int r = 0; r != rows; r++) { + + string::size_type c = 0; + /* + * Loop invariant: + * - We've written 'c' columns so far + */ + while (c != cols) { + if (r == ROW_PAD + 1 && c == 1) { + cout << col_blank << greeting << col_blank; + c += greeting.size() + (blank.size() * 2); + } else { + if (r == 0 || r == rows - 1 || + c == 0 || c == cols - 1) { + cout << "*"; + c++; + } else { + cout << row_blank; + c += row_blank.size(); + } + } + } + cout << endl; + } + return 0; +} -- cgit v1.2.3