注册 登录
编程论坛 C++教室

将输入的一个位数不确定的正整数按照标准的三位分节格式输出,82668634->82,668,634

简Greensoul 发布于 2011-12-03 17:03, 1463 次点击
编写程序,将用户输入的一个位数不确定的正整数按照标准的三位分节格式输出,例如当用户输入82668634时,程序应该输出82,668,634。我们现在在学指针和应用。
2 回复
#2
greedsst2011-12-04 15:18
弄个string,然后每隔三位输出一个逗号
<iomanip>里不知道有没有这种函数
#3
rjsp2011-12-05 08:24
原作者:namtso
程序代码:
#include <iostream>
#include <string>
#include <locale>
using namespace std;

class thousands_sep_facet:public std::numpunct<char>
{
public:
    explicit thousands_sep_facet( size_t r=0 ) : std::numpunct<char>(r)
    {
    }
protected:
    string do_grouping() const
    {
        return "\003";
    }
};

int main( void )
{
    cout << 1389992 << endl; // 1389992

    locale loc( locale(), new thousands_sep_facet );
    std::cout.imbue( loc );
    cout << 1389992 << endl; // 1,389,992

    return 0;
}

1