~cpp
#include <iostream>
#include <string>
// say what standard-library names we use
using std::cin;
using std::cout;
using std::string;
using std::endl;
int main() {
// ask for person's name
cout << "Please enter your first name: ";
// read the name
string name;
cin >> name;
// build the message that we intend to write
const string greeting = "Hello, " + name + "!";
// the number of blacks surrounding the greeting
const int pad = 1;
// the number of rows and columns to write
const int rows = pad * 2 + 3;
const string::size_type cols = greeting.size() + pad * 2 + 2;
// write blank line to separate the output from the input
cout << endl;
// write rows
// invariant: we have written r rows so far
for (int r = 0; r != rows ; ++r) {
string::size_type c = 0;
// invariant: we have written c
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;
}
return 0;
}
~cpp
// invariant : we have written r rows so far
int r = 0;
// setting r to 0 makes the invariant true
while( r != rows )
{
// we can assume that the invariant is true here
// writing a row of output makes the invariant false
std::cout << std::endl;
// incrementing r makes the invariant true again
++r;
}
// we can conclude that the invariant is true here
코드의 마지막 부분에서 invariant 가 다시 true 로 되는것일까요.? invariant 가 무엇인지 아직 개념이 잡히지 안아서. -_-a - 임인택