循环小数的处理

 关于循环小数的处理

问题描述:
    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)
Input :
   A single line with two space separated integers,N and D,1<=N,D<=100000.

Output :
   The decimal expansion, as detailed above. If the expansion exceeds 76 characters in length, print it on multiple lines with 76 characters per line.
例子:
输入:45 56
输出:0.803(571428)

程序代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>

  4. #define LEN 100               //最大小数位数
  5. typedef struct _div
  6. {
  7.     long int num;              //分子
  8.     long int den;              //分母
  9.     long int quot[LEN];        //商
  10.     long int resi[LEN];        //余数
  11.     unsigned long int cycle_point;    //循环点
  12.     unsigned long int length;         //商的个数
  13. }DIV;

  14. void divide(DIV &tmp)
  15. {
  16.       unsigned int len=0, i=0;
  17.       ldiv_t result;
  18.       if(!tmp.den)
  19.       {
  20.           printf("Denominator Error!/n");
  21.           exit(1);
  22.       }
  23.       while(len<LEN)
  24.       {
  25.           result=ldiv(tmp.num ,tmp.den);
  26.           tmp.quot[len]=result.quot;
  27.           tmp.length+=1;
  28.           if(result.rem!=0)
  29.           {
  30.               tmp.resi[len]=result.rem ;
  31.               tmp.num=result.rem*10;
  32.               while(i!=len)
  33.               {
  34.                   if(tmp.resi[i]==result.rem)
  35.                   {
  36.                       tmp.cycle_point=i;
  37.                       return ;
  38.                    }
  39.                    i++;
  40.               }
  41.            }
  42.            else
  43.                 return ;
  44.            len++;
  45.            i=0;        
  46.       }
  47. }

  48. int main()
  49. {
  50.     DIV oper;
  51.     unsigned int i=0, len=0 ,repair=0, flag=0;
  52.     oper.length=0;
  53.     printf(" 请输入分子(nume):");
  54.     scanf("%ld",&oper.num);
  55.     printf(" 请输入分母(demo):");
  56.     scanf("%ld",&oper.den);
  57.     divide(oper);
  58.     printf("%ld%c",oper.quot[i++],'.');
  59.     if(oper.length==1)
  60.         printf("%d",0);
  61.     else 
  62.     {
  63.         while(i<oper.length)
  64.         {
  65.             if(i==oper.cycle_point+1)
  66.             {
  67.                 printf("%c%ld",'(',oper.quot[i]);
  68.                 flag=1;
  69.             }
  70.             else
  71.                 printf("%ld",oper.quot[i]);
  72.             i++;
  73.         }
  74.         if(flag)
  75.             printf("%c",')');
  76.     }
  77.     printf("/n");
  78.     return 0;
  79. }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值