C语言编程:编写程序,可以将一个大数,如10000000,每隔四位,用一个逗号隔开输出,如1000,0000,急求!

 我来答
赫连沛凝ck
2012-07-01 · TA获得超过4178个赞
知道大有可为答主
回答量:2356
采纳率:100%
帮助的人:2530万
展开全部
需要经整数转换为字符串,并在适当的位置添加逗号即可。
/*
99500000 => 9950,0000
99600000 => 9960,0000
99700000 => 9970,0000
99800000 => 9980,0000
99900000 => 9990,0000
100000000 => 1,0000,0000
100100000 => 1,0010,0000
100200000 => 1,0020,0000
100300000 => 1,0030,0000
100400000 => 1,0040,0000
100500000 => 1,0050,0000
100600000 => 1,0060,0000
Press any key to continue
*/
#include <stdio.h>
char *AddComma(unsigned int num, char s[]) {
int i,n,t,count;
for(n = 0,count = 1;num;++n,++count) {
s[n] = num%10 + '0';
num /= 10;
if((count%4 == 0) && num) {
++n;
s[n] = ',';
}
}
s[n] = '\0';
for(i = 0; i < n/2; ++i) {
t = s[i];
s[i] = s[n - 1 - i];
s[n - 1 - i] = t;
}
return s;
}
int main() {
int i;
char s[15];
for(i = 99500000; i < 100600001; i += 100000)
printf("%d => %s\n",i,AddComma(i,s));
return 0;
}
追问
麻烦给出关键步骤的解释,我是新人,很多地方不是很懂。
追答
#include 
char *AddComma(unsigned int num, char s[]) { // 将数num转换为字符串,满足四位添加逗号
int i,n,t,count; // count计数位数
for(n = 0,count = 1;num;++n,++count) {
s[n] = num%10 + '0';// 将数值0 - 9转换为字符'0' - '9'
num /= 10; // 去除末位
if((count%4 == 0) && num) { // 满足4位且原数还不为0时添加','
++n;
s[n] = ',';
}
}
s[n] = '\0';// 字符串末位添加结束符'\0'
for(i = 0; i %s\n",i,AddComma(i,s));
return 0;
}
新新5F602
2012-07-01 · TA获得超过255个赞
知道小有建树答主
回答量:223
采纳率:0%
帮助的人:225万
展开全部
#include <StdAfx.h>
#include<iostream>
using namespace std;
#define TOTAL_LEN 256
#define SUB_LEN 4
void main()
{
int nLen = 0;
int nMore = 0;
char chTemp[SUB_LEN+1] = {0};
char chInput[TOTAL_LEN] = {0};
printf("退出请按:ctrl+c\n");
do
{
memset(chTemp, 0, SUB_LEN+1);
memset(chInput, 0, TOTAL_LEN);
scanf("%s", chInput);
nLen = strlen(chInput);
nMore = nLen % SUB_LEN;
if (nMore)
{
memcpy(chTemp, chInput, nMore);
printf("%s", chTemp);
if (nLen > SUB_LEN)
{
printf("%s", ",");
}
}

for (int i = nMore; i < nLen; i += SUB_LEN)
{
memcpy(chTemp, &chInput[i], SUB_LEN);
printf("%s", chTemp);

if (i < nLen - SUB_LEN)
{
printf("%s", ",");
}
}

printf("\n\n");
} while (1);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
a7940001
2012-07-01
知道答主
回答量:7
采纳率:0%
帮助的人:1.2万
展开全部
按下逗号
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式