//新增:一位数的无限循环 //问题:辗转相除法出现 ‘非法指令’ 有限小数得到小数位数(modf) 如何实现浮点型的精确判断 //思路:9/10 = 90 /100(由此,无需考虑输入小数的位数) 无限计算方式,适合有限与无限 不断除以公约数,要可以除以本身,5/15要除以5才行
#include
#include
int main() { int determine; scanf("%d",&determine);//determine为1进入有限循环,为0进入无限循环 if (determine) { //有限小数点位数 int weishu = 6; double double1; scanf("%lf",&double1); double substitute_double1 = double1;
double double10;
do
{
double10 = double1*10;
double1 = double10 - (int)double10;
weishu++;
} while ((int)double1 != 0);
//得到分子分母
int sub_fm = pow(10,weishu);
int sub_fz = substitute_double1*sub_fm;
//do-while循环:当除以公约数后,再次进行,直到没有公约数可除
int result;
do
{
result = 0;
//判断大小
int min;
min = (sub_fm>sub_fz)?sub_fz:sub_fm;
//循环除以公约数
for (int control = 2;control < min;control++) {
if (sub_fm%control == 0 && sub_fz%control == 0) {
sub_fz/=control;
sub_fm/=control;
result = 1;
break;
}
}
} while (result);
//输出有限分式结构
printf("%f的分式结构%d/%d\n",substitute_double1,sub_fz,sub_fm);
//
} else {
/*设x= 0.33333...
10x=3.33333..
10x-x=3
9x = 3;
x = 1/3 = 0.33333....
*/
//无限计算(最后一位循环)
double double2;
int xunhuan;
scanf("%lf的%d循环",&double2,&xunhuan);
int xh_cishu;
double a = 0.01;//a变量指0.3+3*0.01中的0.01,目的是获得0.333333
for (xh_cishu = 0; xh_cishu < 3;xh_cishu++) {
double2+=xunhuan*a;
a*=0.1;
printf("%f\n",double2);
}
double x = double2*10+xunhuan*a*10 - double2;
int fm = 9*1000000;
int fz = x*1000000;//得到含有有效数据的分子与分母
//复制于有限小数,目的:得到最简分子分母
int wu_result;
do
{
wu_result = 0;
//判断大小
int min;
min = (fm>fz)?fz:fm;
//循环除以公约数
for (int control = 2;control <= min;control++) {
if (fm%control == 0 && fz%control == 0) {
fz/=control;
fm/=control;
wu_result = 1;
break;
}
}
} while (wu_result);
printf("%f最后一位无限循环的分式结构为%d/%d",double2,fz,fm);
}
return 0;
}