//-------将小数转为最简分数------// /* 输入的小数的格式为0.ab(c)括号里面代表循环部分。 如果是有限小数的话:0.25=25*100,然后利用最大公约数的方法,对25/100进行化简; 如果是循环小数的话:0.abcDEFDEF。。。。。。可以: 100*0.abcDEFDEF。。。。=abc.DEFDEF。。。。 100000*0.abcDEFDEF。。。。=abcDEF,DEFDEF。。。。 */ #include<stdio.h> #include<math.h> //GCD函数是进行 int GCD(int a, int b) { int t; while(a % b != 0) { t = a % b; a = b; b = t; } return b; } int main(void) { char a[15] = {-1}; int n; int i; int sum = 0,sum2 = 0; int flag = 1; //表示该小数是不是循环小数,flag=1代表不是循环小数 int count = 1,count2 = 1; scanf("%d", &n); while(n--) //输入n组测试数据 { scanf("%s", &a); //输入要转化的数 for(i=2; i<strlen(a); i++) { if(a[i] == '(') //进行转换:将字符串转为整型数组 { flag = 0; continue; } if(flag == 1 && a[i] != '(') { sum = 10 * sum + a[i] - 48; count = count * 10; sum2 = sum; count2 = count; } else { if(a[i] != ')') { sum2 = 10 * sum2 + a[i] - 48; count2 = count2 * 10; } else break; } } if(flag == 1) { printf("%d/%d\n", sum / GCD(count, sum), count / GCD(count, sum)); flag = 1; sum = 0; sum2 = 0; count = 1; count2 = 1; continue; } count2 = count2 - count; sum2 = sum2 - sum; printf("%d/%d\n",sum2 / GCD(count2, sum2), count2 / GCD(count2, sum2)); flag = 1; sum = 0; sum2 = 0; count = 1; count2 = 1; } return 0; }