SRM 149 DIV2 Level One - ドルのフォーマット

米ドルのフォーマットを出力する問題。


入力はドル12345と、セント6で$12,345.06と出力する。


全然ストリームがワカンネェヨ。

#include <iomanip>
#include <sstream>
#include <string>
using namespace std;

class FormatAmt
{
    public:
        static string amount(int dollers, int cents)
        {
            stringstream ss_dollers, ss_cents;
            string buffer;

            ss_dollers << dollers;
            buffer = ss_dollers.str();

            for (int n = 3; n < buffer.length(); n += 4)
                buffer.insert(buffer.length() - n, ",");

            ss_cents << setw(2) << setfill('0') << cents;


            return "$" + buffer + "." + ss_cents.str();
        }
};

激しく微妙。

ストリームは右から左へ流れてるのに、コンマ打ちは、左から右。うぅん。