c语言求无限不循环小数 循环结构
2个回答
展开全部
#include <stdio.h>
#include <math.h>
#include <string.h>
int main() {
int rator,nator,len;
char s[36],tmp[10];
while(scanf("%d%d",&rator,&nator) == 2) {
s[0] = '\0';
if(nator == 0) {
printf("分母为0,致命错误!\n");
continue;
}
if(rator * nator < 0) {
s[0] = '-';
s[1] = '\0';
}
sprintf(tmp,"%d",rator/nator);
strcat(s,tmp);
printf("%d/%d = ",abs(rator),abs(nator));
rator = abs(rator) % abs(nator);
nator = abs(nator);
len = strlen(s);
s[len++] = '.';
if(rator == 0) {
s[len++] = '0';
s[len] = '\0';
printf("%s\n",s);
continue;
}
s[len] = '\0';
while(len < 21) {// 21位长度
rator *= 10;
while(rator < nator) {
s[len++] = '0';
s[len] = '\0';
rator *= 10;
}
sprintf(tmp,"%d",rator/nator);
strcat(s,tmp);
rator = rator % nator;
if(rator == 0) break;
len = strlen(s);
}
printf("%s\n",s);
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询