
C语言编程:编写程序,可以将一个大数,如10000000,每隔四位,用一个逗号隔开输出,如1000,0000,急求!
展开全部
需要经整数转换为字符串,并在适当的位置添加逗号即可。
/*
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;
}
/*
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;
}
展开全部
#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);
}
#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);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询