C++中如何将用户输入的一个不确定的正整数按照标准的三位分节格式输出,如8266864,输出为:82,668,634
答案:3 悬赏:20
解决时间 2021-03-13 19:42
- 提问者网友:伪善人独行者
- 2021-03-13 09:41
C++中如何将用户输入的一个不确定的正整数按照标准的三位分节格式输出,如8266864,输出为:82,668,634
最佳答案
- 二级知识专家网友:不服输的倔强
- 2021-03-13 10:22
请参copy考如下的代码百,要转换的数放在度变量i中。
const unsigned SECTION_COUNT = 3;
int i = 1234;
char buf[512];
_itoa(i, buf, 10);
unsigned len = strlen(buf);
unsigned pCnt = (len - 1) / SECTION_COUNT;
unsigned fix = len % SECTION_COUNT;
buf[len + pCnt] = '\0';
while (pCnt)
{
char* pos = buf + (pCnt - 1) * SECTION_COUNT + fix;
memmove(pos + pCnt, pos, SECTION_COUNT);
pos[--pCnt] = ',';
}
const unsigned SECTION_COUNT = 3;
int i = 1234;
char buf[512];
_itoa(i, buf, 10);
unsigned len = strlen(buf);
unsigned pCnt = (len - 1) / SECTION_COUNT;
unsigned fix = len % SECTION_COUNT;
buf[len + pCnt] = '\0';
while (pCnt)
{
char* pos = buf + (pCnt - 1) * SECTION_COUNT + fix;
memmove(pos + pCnt, pos, SECTION_COUNT);
pos[--pCnt] = ',';
}
全部回答
- 1楼网友:余生继续浪
- 2021-03-13 10:57
//c++,用char数组 #include <iostream> #include <string> #include <stdlib.h> using namespace std; int main() { int num; char *ch=new char [](); cout<<"请输入一个正整数:"<<endl; cin>>num; itoa(num,ch,10); cout<<"按照标准的三位分节格式:"<<endl; for(int i=0;i<strlen(ch);i++) { if((i%3==0)&&(i>0)) cout<<","<<ch[i]; else { cout<<ch[i]; } } return 0; }
//c++,string类型和strstream类型并用 #include <iostream> #include <string> #include <strstream> using namespace std; int main() { int num; string str; strstream s; cout<<"请输入一个正整数:"<<endl; cin>>num; s<<num; s>>str; for(int i=0;i<str.length();i++) { if(i%4==0) { str.insert(i,","); } } str.erase(0,1); cout<<str<<endl; return 0; }
- 2楼网友:萝莉姐姐鹿小北
- 2021-03-13 10:50
CString str;
输入zhidaoX
b = X % 1000;
X = X / 1000;
str = Format(b)
while X>0
Do
b=X % 1000
X = X / 1000
str = "," + str
if X > 0 Then
str = Format(b) + str // 带内0前缀
else
str = Format(b) + str // 不带容0前缀
End
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯