怎么样在系统中把输入的小数转换为分数

解决方案 »

  1.   

    先弄成整数 再约分
    mod 用的比较多哦
      

  2.   

    最主要的工作在于约分约分首先应该建立一张质数表,如何建立质数表,应该不用我说了吧然后依次用质数表中的第个数去处理约分,用取mod的方法,如果分子分母都能整除,则约去.
      

  3.   


    ''函数实现小数转分数
    Public Function XtoF(str As Currency, Optional fenm As Integer = 32) As String ''只限于整除分数
       Dim Cfm As Currency
       Dim cfmmod As Integer
       On Error GoTo Erroreof
       
       Cfm = 1 / fenm
       XtoF = ""
       If str = 0 Then XtoF = "": Exit Function
       
       Dim point As Integer
       Dim dInt As String
       Dim dPoint As Currency
       Dim fint, fint1, fint2 As Integer
       If str <> 0 Then
            If str > 1 Then
                point = InStr(1, str, ".", 1)
                If point = 0 Then
                    XtoF = str:
                    Exit Function
                Else
                    dInt = Mid(str, 1, point - 1)
                    dPoint = CCur("0." & Mid(str, point + 1))
                    fint = InStr(1, XtoF(dPoint), "/", 1)
                    fint1 = CInt(Mid(XtoF(dPoint), 1, fint - 1))
                    fint2 = CInt(Mid(XtoF(dPoint), fint + 1))
                    
                    XtoF = CStr(dInt * fint2 + fint1) & "/" & CStr(fint2)
               
                End If
            Else
               If fenm Mod CInt(str / Cfm) = 0 Then
                XtoF = "1/" + CStr(fenm / CInt(str / Cfm))
               Else
                  cfmmod = Maxgys(fenm, CInt(str / Cfm))
                XtoF = CStr(CInt(str / Cfm / cfmmod)) + "/" + CStr(CInt(fenm / cfmmod))
               End If
            End If
       Else
           XtoF = "0"
       End If
       Exit Function
    Erroreof:
       XtoF = ""
    End Function
    Function Maxgys(num1 As Integer, num2 As Integer) As Integer
        Dim minnum, i As Integer
        minnum = num1
        If num1 > num2 Then minnum = num2
        For i = 1 To minnum
         If ((num1 Mod i) = 0) And ((num2 Mod i) = 0) Then Maxgys = i  
        Next i
    End Function
      

  4.   

    function FloatToMixedBinFract (Nbr: extended;
         MaxBinaryBits: integer; AllowedError: double; const DecFmt: string):
    string;
    var n,f,Miss: extended; Whole,Numer,Denom: Int64; Neg: boolean;
    begin
      n := Nbr;
      Neg := n<0; If Neg then n := -n;
      Denom := 1 shl MaxBinaryBits; {Make up the trial denominator.}
      Whole := trunc(n); {Whole number part}
      f := n-Whole; {Fraction part}
      Numer := round(f*Denom); {Nearest numerator}
      Miss := f-Numer/Denom; {Error amount}
      If Abs(Miss)>AllowedError {If binary representation error is too big }
        then Result := FormatFloat(DecFmt,n) { then use decimal fraction
    format, }
        else begin { else reduce binary fraction.}
          While (Numer<>0) and not Odd(Numer) do
              begin Numer := Numer div 2; Denom := Denom div 2 end;
          Result := IntToStr(Whole);
          If Numer>0
            then Result := Result + ' ' + IntToStr(Numer)+'/'+IntToStr(Denom);
          end;
      If Neg
        then Result := '(' + Result + ')';
    end;
    //分数转为小数
    function MixFractStrToFloat(const s: string): extended;
    var Whole,Numer,Denom: Int64; i: integer; Sgn,DKey: char;
    begin
      Denom := 1; Whole := 0; i:=0; Numer := 0;
      If i>length(s)
        then begin Result := 0; EXIT end;
      While (i<length(s)) and (s[i+1]<=' ') do inc(i);
      If (i<length(s)) and (s[i+1] in ['-','('])
        then begin Sgn := s[i+1]; inc(i) end
        else Sgn := #0;
      While (i<length(s)) and (s[i+1]<=' ') do inc(i);
      While (i<length(s)) and (s[i+1] in ['0'..'9'])
        do begin Whole := Whole * 10 + (ord(s[i+1])-ord('0')); inc(i) end;
      If (i<length(s)) and (s[i+1] in ['+',' ','.']) then begin
        DKey := s[i+1];
        inc(i);
        If DKey='.'
          then Denom := 1;
        While (i<length(s)) and (s[i+1] in ['0'..'9'])
          do begin
            Denom := Denom * 10;
            Numer := Numer * 10 + (ord(s[i+1])-ord('0'));
            inc(i) end;
        While (i<length(s)) and (s[i+1]<=' ') do inc(i);
        If DKey<>'.' then begin
          If (i<length(s)) and (s[i+1]='/')
            then inc(i);
          While (i<length(s)) and (s[i+1]<=' ') do inc(i);
          Denom := 0;
          While (i<length(s)) and (s[i+1] in ['0'..'9'])
            do begin Denom := Denom * 10 + (ord(s[i+1])-ord('0')); inc(i) end;
          end;
        end;
      If (i<length(s))
        then raise Exception.create('非法的分数 '+s);
      If (Denom=0)
        then raise Exception.create('没有分母'+s);
      Result := Whole + Numer/Denom;
      If Sgn<>#0
        then Result := -Result;
    end;