将输入的一个位数不确定的正整数按照标准的三位分节格式输出,82668634->82,668,634
编写程序,将用户输入的一个位数不确定的正整数按照标准的三位分节格式输出,例如当用户输入82668634时,程序应该输出82,668,634。我们现在在学指针和应用。
弄个string,然后每隔三位输出一个逗号
<iomanip>里不知道有没有这种函数
程序代码:#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;
}