分数问题 [英] split number problem
问题描述
目标:扫描一个数字是什么随机数字,拆分这个数字;
例如:输入5680
输出" ; 5,6,8,0;
输入" 45"
输出4,5;
>
-
_________________________________________
?òèè°?é?è?μ?ì????£?
$ b $bóDê? ??í??òa?μ£?
MSN£ mi*****@hotmail.com
---------
Aim: scanf a number what random digit,split this number;
for example: input "5680"
output "5,6,8,0";
input "45"
output "4,5";
--
_________________________________________
?òèè°?é?è?μ?ì???£?
óDê???í??òáa?μ£?
MSN£o mi*****@hotmail.com
---------
推荐答案
2003年9月3日星期三15: 05:30 +0800,nicolas写道:
On Wed, 03 Sep 2003 15:05:30 +0800, nicolas wrote:
目的:扫描一个数字是什么随机数字,拆分这个数字;
Aim: scanf a number what random digit,split this number;
这不可能是你的作业吗?
NPV
This wouldn''t by any chance happen to be your homework?
NPV
2003年9月3日星期三15: 05:30 +0800,nicolas写道
On Wed, 3 Sep 2003 15:05:30 +0800, nicolas wrote
目的:scanf一个数字随机数字,拆分这个数字;
例如:输入5680
Aim: scanf a number what random digit,split this number;
for example: input "5680"
这很简单:
scanf("%d", & number);
number_of_digits =(int)(log10(number))+ 1; / * include< math.h>链接-lm * /
if((string = malloc(number_of_digits + 1))== NULL)
/ *错误处理* /
token = string;
for(i = number_of_digits - 1; i> = 0; --i)
{
* string ++ =(int)(number / pow(10,i))+''0'';
number = number%(int)pow(10,i);
* string ++ ='','';
}
* string =''\ 0'';
printf(" ;%s \ n",令牌);
HTH
D.
-
PGP密钥39A40276,网址:wwwkeys.eu.pgp.net
key fingeprint:9A0B 61C6 B826 4B73 69BB 972B C5E7 A153 39A4 0276
That''s easy :
scanf("%d", &number);
number_of_digits = (int) (log10(number))+1; /* include <math.h> link with -lm */
if ((string = malloc(number_of_digits + 1)) == NULL)
/* Error handling */
token = string;
for (i = number_of_digits - 1; i >= 0; --i)
{
*string++ = (int) (number / pow(10,i)) + ''0'';
number = number % (int) pow(10,i);
*string++ = '','';
}
*string = ''\0'';
printf("%s\n", token);
HTH
D.
--
PGP key 39A40276 at wwwkeys.eu.pgp.net
key fingeprint: 9A0B 61C6 B826 4B73 69BB 972B C5E7 A153 39A4 0276
nicolas写道:
nicolas wrote:
目的:扫描一个数字是什么随机数字,拆分这个数字;
例如:input" 5680"
输出5,6,8,0;
输入45
输出4,5&qu ot ;;
Aim: scanf a number what random digit,split this number;
for example: input "5680"
output "5,6,8,0";
input "45"
output "4,5";
Nicolas ...
如果您不想使用math.h中的原型函数,<然后你可以做类似的事情:
#include< stdio.h>
#include< stdlib.h>
char * unsigned_to_digits(unsigned n)
{char * p;
unsigned digits = 0,copy = n;
做++数字;而(n / = 10); / * :) * /
if(p = malloc(2 *位数))
{p + = 2 *位数;
* --p =''\''';
do
{* - p =''0''+ copy%10;
if(copy / = 10)* - p ='','';
} while(copy);
}
返回p;
}
int main(无效)
{char * p;
无符号值;
scanf("%u"& value);
puts((p = unsigned_to_digits(value))?p:
" malloc()失败");
返回0;
}
-
Morris Dovey
美国爱荷华州西得梅因
C链接 http://www.iedu.com/c
Nicolas...
If you preferred not to use the functions prototyped in math.h,
then you could do something like:
#include <stdio.h>
#include <stdlib.h>
char *unsigned_to_digits(unsigned n)
{ char *p;
unsigned digits = 0,copy = n;
do ++digits; while (n /= 10); /* :) */
if (p = malloc(2*digits))
{ p += 2*digits;
*--p = ''\0'';
do
{ *--p = ''0'' + copy % 10;
if (copy /= 10) *--p = '','';
} while (copy);
}
return p;
}
int main(void)
{ char *p;
unsigned value;
scanf("%u",&value);
puts((p = unsigned_to_digits(value)) ? p :
"malloc() failure");
return 0;
}
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
这篇关于分数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!