#include #include #include #include using namespace std; class Formatter { public: Formatter(int w): width {w} {} string adjust_left(string const & s) const { ostringstream oss; oss << setw(width) << std::left << s; return oss.str(); } string center(string const & s) const { ostringstream oss; int pre { (width - s.length())/2 }; int post { pre + s.length() }; if ( pre + post != width ) ++post; oss << std::left << setw(pre) << ' ' << setw(post) << s; return oss.str(); } string adjust_right(string const & s) const { ostringstream oss; oss << std::right << setw(width) << s; return oss.str(); } private: int width; }; int main() { Formatter fmt{35}; cout << '|' << fmt.adjust_left("LEFT") << "|\n" << '|' << fmt.center("CENTER") << "|\n" << '|' << fmt.adjust_right("RIGHT") << '|' << endl; /* Skriver ut enligt nedan: |LEFT | | CENTER | | RIGHT| */ }