{"id":371,"date":"2009-05-03T18:36:00","date_gmt":"2009-05-03T10:36:00","guid":{"rendered":""},"modified":"2013-11-17T19:27:31","modified_gmt":"2013-11-17T11:27:31","slug":"delphi%e5%b0%8f%e6%95%b0%e8%bd%ac%e6%8d%a2%e6%88%90%e5%88%86%e6%95%b0%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/371.html","title":{"rendered":"Delphi\u5c0f\u6570\u8f6c\u6362\u6210\u5206\u6570\u51fd\u6570"},"content":{"rendered":"<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\/\/=======================\u5c0f\u6570\u8f6c\u5206\u6570======================================\r\nfunction Fraction(decimal: double): string;\r\nvar\r\nintNumerator, intDenominator, intNegative: integer; \/\/ \u58f0\u660e\u6574\u6570\u53d8\u91cf\u4e3a\u957f\u6574\u6570\r\ndblFraction, dblDecimal, dblAccuracy, dblinteger: Double; \/\/ \u58f0\u660e\u6d6e\u70b9\u6570\u4e3a\u53cc\u7cbe\u5ea6\r\nbegin\r\ndblDecimal := decimal; \/\/\u53d6\u5f97\u76ee\u6807\u5c0f\u6570\r\nif trunc(decimal) = decimal then \/\/ \u5982\u679c\u662f\u6574\u6570\uff0c\u5219\u76f4\u8f6c\r\n    result := floattostr(decimal)\r\nelse\r\nbegin\r\n    if abs(decimal) &gt; 1 then \/\/\u5982\u679c\u5c0f\u6570\u5927\u4e8e1 \u5982 10.24 \uff0c\u8fdb\u884c\u62c6\u89e3\r\n    begin\r\n      dblinteger := trunc(decimal); \/\/\u53d6\u51fa\u6574\u6570\u90e8\u5206\r\n      dblDecimal := abs(frac(decimal)); \/\/\u53d6\u51fa\u5c0f\u6570\u90e8\u5206\r\n    end\r\n    else dblDecimal := decimal;\r\n\r\n    dblAccuracy := 0.01; \/\/\u8bbe\u7f6e\u7cbe\u5ea6\r\n    intNumerator := 0; \/\/\u521d\u59cb\u5206\u5b50\u4e3a0\r\n    intDenominator := 1; \/\/\u521d\u59cb\u5206\u6bcd\u4e3a1\r\n    intNegative := 1; \/\/\u7b26\u53f7\u6807\u8bb0\u4e3a\u6b63\r\n    if dblDecimal &lt; 0 then intNegative := -1; \/\/\u5982\u679c\u76ee\u6807\u4e3a\u8d1f\uff0c\u8bbe\u7f6e\u8d1f\u6807\u5fd7\u4f4d\r\n    dblFraction := 0; \/\/\u8bbe\u7f6e\u5206\u6570\u503c\u4e3a 0\/1\r\n    while Abs(dblFraction - dblDecimal) &gt; dblAccuracy do \/\/\u5982\u679c\u5f53\u524d\u6ca1\u6709\u8fbe\u5230\u7cbe\u5ea6\u8981\u6c42\u5c31\u7ee7\u7eed\u5faa\u73af\r\n    begin\r\n      if Abs(dblFraction) &gt; Abs(dblDecimal) then \/\/\u5982\u679c\u6211\u4eec\u7684\u5206\u6570\u5927\u4e8e\u76ee\u6807\r\n        intDenominator := intDenominator + 1 \/\/\u589e\u52a0\u5206\u6bcd\r\n      else \/\/\u5426\u5219\r\n        intNumerator := intNumerator + intNegative; \/\/\u589e\u52a0\u5206\u5b50\r\n      dblFraction := intNumerator \/ intDenominator; \/\/\u8ba1\u7b97\u65b0\u7684\u5206\u6570\r\n    end;\r\n   \/\/ edit2.Text := inttostr(intNumerator) + '\/' + inttostr(intDenominator);\r\n    if abs(decimal) &gt; 1 then \/\/\u5982\u679c\u5c0f\u6570\u5927\u4e8e1 \u5982 10.24 \uff0c\u8fdb\u884c\u62c6\u89e3\r\n      result := floattostr(dblinteger) + ' ' + inttostr(intNumerator) + '\/' + inttostr(intDenominator)\r\n    else\r\n      result := inttostr(intNumerator) + '\/' + inttostr(intDenominator);\r\nend;\r\nend;\r\n\r\nprocedure TForm1.Button10Click(Sender: TObject);\r\nbegin\r\nshowmessage(Fraction(8\/9));\r\nend;\r\n<\/pre>\n<p>\u4fee\u6539\u540e\u5316\u6210\u5047\u5206\u6570:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nfunction Fraction(decimal: double): string;\r\nvar\r\nintNumerator, intDenominator, intNegative: integer; \/\/ \u58f0\u660e\u6574\u6570\u53d8\u91cf\u4e3a\u957f\u6574\u6570\r\ndblFraction, dblDecimal, dblAccuracy, dblinteger: Double; \/\/ \u58f0\u660e\u6d6e\u70b9\u6570\u4e3a\u53cc\u7cbe\u5ea6\r\ntemp:string;\r\nbegin\r\ndblDecimal := decimal; \/\/\u53d6\u5f97\u76ee\u6807\u5c0f\u6570\r\nif trunc(decimal) = decimal then \/\/ \u5982\u679c\u662f\u6574\u6570\uff0c\u5219\u76f4\u8f6c\r\n    result := floattostr(decimal)\r\nelse\r\nbegin\r\n    if abs(decimal) &gt; 1 then \/\/\u5982\u679c\u5c0f\u6570\u5927\u4e8e1 \u5982 10.24 \uff0c\u8fdb\u884c\u62c6\u89e3\r\n    begin\r\n      dblinteger := trunc(decimal); \/\/\u53d6\u51fa\u6574\u6570\u90e8\u5206\r\n      dblDecimal := abs(frac(decimal)); \/\/\u53d6\u51fa\u5c0f\u6570\u90e8\u5206\r\n    end\r\n    else dblDecimal := decimal;\r\n\r\n    dblAccuracy := 0.01; \/\/\u8bbe\u7f6e\u7cbe\u5ea6\r\n    intNumerator := 0; \/\/\u521d\u59cb\u5206\u5b50\u4e3a0\r\n    intDenominator := 1; \/\/\u521d\u59cb\u5206\u6bcd\u4e3a1\r\n    intNegative := 1; \/\/\u7b26\u53f7\u6807\u8bb0\u4e3a\u6b63\r\n    if dblDecimal &lt; 0 then intNegative := -1; \/\/\u5982\u679c\u76ee\u6807\u4e3a\u8d1f\uff0c\u8bbe\u7f6e\u8d1f\u6807\u5fd7\u4f4d\r\n    dblFraction := 0; \/\/\u8bbe\u7f6e\u5206\u6570\u503c\u4e3a 0\/1\r\n    while Abs(dblFraction - dblDecimal) &gt; dblAccuracy do \/\/\u5982\u679c\u5f53\u524d\u6ca1\u6709\u8fbe\u5230\u7cbe\u5ea6\u8981\u6c42\u5c31\u7ee7\u7eed\u5faa\u73af\r\n    begin\r\n      if Abs(dblFraction) &gt; Abs(dblDecimal) then \/\/\u5982\u679c\u6211\u4eec\u7684\u5206\u6570\u5927\u4e8e\u76ee\u6807\r\n        intDenominator := intDenominator + 1 \/\/\u589e\u52a0\u5206\u6bcd\r\n      else \/\/\u5426\u5219\r\n        intNumerator := intNumerator + intNegative; \/\/\u589e\u52a0\u5206\u5b50\r\n      dblFraction := intNumerator \/ intDenominator; \/\/\u8ba1\u7b97\u65b0\u7684\u5206\u6570\r\n    end;\r\n   \/\/ edit2.Text := inttostr(intNumerator) + '\/' + inttostr(intDenominator);\r\n    if abs(decimal) &gt; 1 then \/\/\u5982\u679c\u5c0f\u6570\u5927\u4e8e1 \u5982 10.24 \uff0c\u8fdb\u884c\u62c6\u89e3\r\n     \/\/\u7ed3\u679c\uff1adblinteger\u4e3a\u6574\u6570\u90e8\u5206\uff0c intNumerator\u4e3a\u5206\u5b50\uff0cintDenominator\u4e3a\u5206\u6bcd\r\n     begin\r\n     if dblinteger&lt;0 then\r\n       begin dblinteger := -dblinteger;\r\n       temp := floattostr(dblinteger*intDenominator+intNumerator) + '\/' + inttostr(intDenominator);\r\n       \/\/result := floattostr(dblinteger) + '\u53c8' + inttostr(intNumerator) + '\/' + inttostr(intDenominator)\r\n       result := '-'+temp;\r\n       end else\r\n       begin\r\n       temp := floattostr(dblinteger*intDenominator+intNumerator) + '\/' + inttostr(intDenominator);\r\n       result :=temp;\r\n       end;\r\n     end\r\n    else\r\n      result := inttostr(intNumerator) + '\/' + inttostr(intDenominator);\r\nend;\r\nend;\r\n<\/pre>\n<p>\u8fd9\u4e2a\u7b97\u6cd5\u7684\u6839\u672c\u539f\u7406\u662f\uff1a\u4e00\u4e2a\u5206\u6570\u5bf9\u5e94\u4e00\u6761\u76f4\u7ebf\u7684\u659c\u7387\u3002\u7528\u6570\u5b66\u8bed\u8a00\u63cf\u8ff0\u5c31\u662f\uff1a\u4e00\u6761\u76f4\u7ebf\u7684\u659c\u7387\u662f\u65e0\u7a77\u5927(\u5782\u76f4\u4e8eX\u8f74)\u6216\u8005\u662f(Y2-Y1) \/ (X2-X1)\uff0c\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\u627e\u52302\u4e2a\u6574\u6570\uff0c\u5728\u6307\u5b9a\u7684\u7cbe\u5ea6\u8303\u56f4\u5185\u63a5\u8fd1\u8fd9\u4e2a\u659c\u7387\u3002\u5bf9\u4e8e\u6b63\u6570\u6765\u8bf4\uff0c\u6211\u4eec\u8bbe\u7f6e\u5206\u5b50\u4e3a0\uff0c\u5206\u6bcd\u4e3a1\uff0c\u7136\u540e\u6bd4\u8f83\u8fd9\u4e2a\u5206\u6570\u540c\u7ed9\u5b9a\u7684\u5341\u8fdb\u5236\u6570\u3002\u5982\u679c\u6211\u4eec\u7684\u5206\u6570\u592a\u5c0f\u4e86\uff08\u6bd4\u5982\uff0c\u6211\u4eec\u9009\u62e9\u7684\u70b9\u5728\u76f4\u7ebf\u7684\u4e0b\u9762\uff09\uff0c\u6211\u4eec\u5c31\u52a0\u5927\u5206\u5b50\u7684\u503c\u76f4\u5230\u8fd9\u4e2a\u5206\u6570\u592a\u5927\uff08\u6bd4\u5982\uff0c\u8fd9\u4e2a\u70b9\u5728\u76f4\u7ebf\u7684\u4e0a\u65b9\uff09\uff0c\u4e4b\u540e\u6211\u4eec\u5728\u589e\u52a0\u5206\u6bcd\u7684\u5927\u5c0f\u76f4\u5230\u8fd9\u4e2a\u70b9\u5728\u76f4\u7ebf\u4e0b\u65b9\u3002<br \/>\n\u5982\u679c\u6211\u4eec\u7684\u6700\u7ec8\u76ee\u6807\u662f\u65e0\u7406\u6570\uff08\u65e0\u9650\u4e0d\u5faa\u73af\u5c0f\u6570\uff09\uff0c\u8fd9\u4e2a\u7b97\u6cd5\u5c06\u4e00\u76f4\u7ee7\u7eed\uff0c\u589e\u52a0\u5206\u5b50\u548c\u5206\u6bcd\uff0c\u76f4\u5230\u6700\u7ec8\u7ed3\u679c\u5728\u6307\u5b9a\u7684\u7cbe\u5ea6\u4e0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/=======================\u5c0f\u6570\u8f6c\u5206\u6570========================= [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-371","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=371"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/371\/revisions"}],"predecessor-version":[{"id":4766,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/371\/revisions\/4766"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}