Fractions to Decimals
Write a program that will accept a fraction of the form N/D,
where N is the numerator and D is the denominator and print the decimal representation.
If the decimal representation has a repeating sequence of digits, indicate the sequence by enclosing it in brackets.
For example, 1/3 = .33333333...is denoted as 0.(3), and 41/333 = 0.123123123...is denoted as 0.(123).
Use xxx.0 to denote an integer.
Typical conversions are:
1/3 = 0.(3)
22/5 = 4.4
1/7 = 0.(142857)
2/2 = 1.0
3/8 = 0.375
45/56 = 0.803(571428)
PROGRAM NAME: fracdec
INPUT FORMAT
A single line with two space separated integers, N and D, 1 <= N,D <= 100000.
45 56
OUTPUT FORMAT
The decimal expansion, as detailed above.
If the expansion exceeds 76 characters in length, print it on multiple lines with 76 characters per line.
0.803(571428)
Description
写一个程序,输入一个形如N/D的分数(N是分子,D是分母),输出它的小数形式。如果小数有循环节的话,把循环节放在一对圆括号中。
例如, 1/3 = .33333333 写成0.(3) 41/333 = 0.123123123... 写成0.(123)
用xxx.0 成表示整数典型的转化例子:
1/3 = 0.(3)
22/5 = 4.4
1/7 = 0.(142857)
2/2 = 1.0
3/8 = 0.375
45/56 = 0.803(571428)
Input
单独的一行包括被空格分开的 N和D, 1 <= N,D <= 100000。
Output
小数的表示方法上面说的很明白了,如果输出的长度超过76个字符,每行输出76个。
Sample Input
45 56
Sample Output
0.803(571428)
程序中用到的库函数
(1)sprintf函数
功能:把格式化的数据写入某个字符串缓冲区。
头文件:在C中 <stdio.h> 或 在C++中 <cstdio>
原型:int sprintf( char *buffer, const char *format, [ argument] … );
参数列表
buffer:char型指针,指向将要写入的字符串的缓冲区。
format:格式化字符串。
[argument]...:可选参数,可以是任何类型的数据。
返回值:字符串长度(strlen)
strcat 只能连接字符串(一段以‘