{"id":309367,"date":"2024-05-20T21:24:01","date_gmt":"2024-05-20T13:24:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/309367.html"},"modified":"2024-05-20T21:24:16","modified_gmt":"2024-05-20T13:24:16","slug":"%e6%80%8e%e4%b9%88%e7%94%a8c%ef%bc%83%e5%86%99%e4%b8%80%e4%b8%aa%e6%9c%89%e7%90%86%e6%95%b0%e7%9a%84%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/309367.html","title":{"rendered":"\u600e\u4e48\u7528C\uff03\u5199\u4e00\u4e2a\u6709\u7406\u6570\u7684\u7c7b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26225815\/a5e1b2a8-28b6-46ff-bc02-ab82716ad135.webp\" alt=\"\u600e\u4e48\u7528C\uff03\u5199\u4e00\u4e2a\u6709\u7406\u6570\u7684\u7c7b\" \/><\/p>\n<p><p>\u5728C#\u4e2d\uff0c\u5199\u4e00\u4e2a\u6709\u7406\u6570\u7684\u7c7b\u610f\u5473\u7740\u521b\u5efa\u4e00\u4e2a\u80fd\u591f\u8868\u793a\u5206\u6570\uff08\u5373\u6709\u7406\u6570\uff09\u5e76\u652f\u6301\u76f8\u5173\u8fd0\u7b97\u7684\u6570\u636e\u7c7b\u578b\u3002<strong>\u4e00\u4e2a\u4f18\u79c0\u7684\u6709\u7406\u6570\u7c7b\u5e94\u5f53\u652f\u6301\u521d\u59cb\u5316\u3001\u7ea6\u5206\u3001\u52a0\u51cf\u4e58\u9664\u8fd0\u7b97\u3001\u4ee5\u53ca\u4e0e\u5176\u4ed6\u7c7b\u578b\u6570\u636e\u7684\u4e92\u64cd\u4f5c<\/strong>\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u7ea6\u5206\u529f\u80fd\uff0c\u56e0\u4e3a\u5b83\u662f\u6709\u7406\u6570\u7c7b\u7684\u57fa\u7840\uff0c\u4fdd\u8bc1\u4e86\u6709\u7406\u6570\u4ee5\u6700\u7b80\u5f62\u5f0f\u5448\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u6709\u7406\u6570\u7c7b\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u6709\u7406\u6570\u7c7b\u7684\u57fa\u672c\u7ed3\u6784\u3002\u8fd9\u5305\u62ec\u4e86\u4e24\u4e2a\u4e3b\u8981\u7684\u6210\u5458\u53d8\u91cf\uff1a\u5206\u5b50\u548c\u5206\u6bcd\uff0c\u4ee5\u53ca\u6784\u9020\u51fd\u6570\u7528\u4e8e\u521d\u59cb\u5316\u8fd9\u4e24\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">public class RationalNumber<\/p>\n<p>{<\/p>\n<p>    private int numerator; \/\/ \u5206\u5b50<\/p>\n<p>    private int denominator; \/\/ \u5206\u6bcd<\/p>\n<p>    \/\/ \u6784\u9020\u51fd\u6570<\/p>\n<p>    public RationalNumber(int numerator, int denominator)<\/p>\n<p>    {<\/p>\n<p>        this.numerator = numerator;<\/p>\n<p>        this.denominator = denominator;<\/p>\n<p>        Normalize();<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9e\u73b0\u7ea6\u5206\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b9e\u73b0\u7ea6\u5206\u529f\u80fd\u7684\u5173\u952e\u5728\u4e8e\u7f16\u5199\u4e00\u4e2a\u80fd\u627e\u5230\u4e24\u4e2a\u6570\u6700\u5927\u516c\u7ea6\u6570\uff08GCD\uff09\u7684\u65b9\u6cd5\uff0c\u5e76\u5229\u7528\u5b83\u6765\u7ea6\u7b80\u5206\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">private int GCD(int a, int b)<\/p>\n<p>{<\/p>\n<p>    while (b != 0)<\/p>\n<p>    {<\/p>\n<p>        int temp = b;<\/p>\n<p>        b = a % b;<\/p>\n<p>        a = temp;<\/p>\n<p>    }<\/p>\n<p>    return a;<\/p>\n<p>}<\/p>\n<p>private void Normalize()<\/p>\n<p>{<\/p>\n<p>    int gcd = GCD(numerator, denominator);<\/p>\n<p>    numerator \/= gcd;<\/p>\n<p>    denominator \/= gcd;<\/p>\n<p>    if (denominator &lt; 0) \/\/ \u786e\u4fdd\u5206\u6bcd\u4e3a\u6b63<\/p>\n<p>    {<\/p>\n<p>        numerator = -numerator;<\/p>\n<p>        denominator = -denominator;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6807\u51c6\u5316\uff08Normalize\uff09\u662f\u786e\u4fdd\u6709\u7406\u6570\u4ee5\u5176\u6700\u7b80\u5f62\u5f0f\u8868\u8fbe\u7684\u8fc7\u7a0b<\/strong>\uff0c\u540c\u65f6\u4e5f\u5904\u7406\u4e86\u5206\u6bcd\u4e3a\u8d1f\u7684\u60c5\u51b5\uff0c\u786e\u4fdd\u4e86\u8d1f\u53f7\u53ea\u51fa\u73b0\u5728\u5206\u5b50\u4e0a\u3002\u8fd9\u662f\u6709\u7406\u6570\u7c7b\u6b63\u786e\u5b9e\u73b0\u7684\u5173\u952e\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u652f\u6301\u52a0\u51cf\u4e58\u9664\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>\u5bf9\u6709\u7406\u6570\u7c7b\u8fdb\u884c\u8fd0\u7b97\u91cd\u8f7d\uff0c\u4f7f\u5176\u80fd\u591f\u652f\u6301\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u56db\u5219\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">public static RationalNumber operator +(RationalNumber r1, RationalNumber r2)<\/p>\n<p>{<\/p>\n<p>    return new RationalNumber(r1.numerator * r2.denominator + r2.numerator * r1.denominator,<\/p>\n<p>                              r1.denominator * r2.denominator);<\/p>\n<p>}<\/p>\n<p>public static RationalNumber operator -(RationalNumber r1, RationalNumber r2)<\/p>\n<p>{<\/p>\n<p>    return new RationalNumber(r1.numerator * r2.denominator - r2.numerator * r1.denominator,<\/p>\n<p>                              r1.denominator * r2.denominator);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e58\u6cd5\u548c\u9664\u6cd5\u7684\u5b9e\u73b0\u4e0e\u52a0\u6cd5\u548c\u51cf\u6cd5\u76f8\u4f3c\uff0c<strong>\u5173\u952e\u5728\u4e8e\u6b63\u786e\u5904\u7406\u5206\u5b50\u548c\u5206\u6bcd\u7684\u4e58\u9664\u5173\u7cfb<\/strong>\uff0c\u5e76\u5728\u64cd\u4f5c\u7ed3\u675f\u540e\u901a\u8fc7\u6807\u51c6\u5316\u4fdd\u6301\u7ed3\u679c\u7684\u6700\u7b80\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4e0e\u5176\u4ed6\u7c7b\u578b\u6570\u636e\u7684\u4e92\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u6709\u7406\u6570\u7c7b\u4e0e\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff08\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\uff09\u7684\u4e92\u64cd\u4f5c\u662f\u786e\u4fdd\u8fd9\u4e2a\u7c7b\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u65b9\u4fbf\u4f7f\u7528\u7684\u91cd\u8981\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">public static implicit operator RationalNumber(int number)<\/p>\n<p>{<\/p>\n<p>    return new RationalNumber(number, 1);<\/p>\n<p>}<\/p>\n<p>public static explicit operator decimal(RationalNumber r)<\/p>\n<p>{<\/p>\n<p>    return (decimal)r.numerator \/ r.denominator;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u901a\u8fc7\u9690\u5f0f\u548c\u663e\u5f0f\u8f6c\u6362\u64cd\u4f5c\u7b26\uff0c\u4f7f\u5f97\u6709\u7406\u6570\u7c7b\u80fd\u591f\u5bb9\u6613\u5730\u4e0eC#\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\u548c\u8ba1\u7b97\uff0c\u6781\u5927\u5730\u589e\u5f3a\u4e86\u5176\u6613\u7528\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u6709\u7406\u6570\u7c7b\u7684\u5b9e\u73b0\u6db5\u76d6\u4e86\u6570\u636e\u8868\u793a\u3001\u8fd0\u7b97\u5904\u7406\u548c\u4e0e\u5176\u4ed6\u7c7b\u578b\u7684\u4e92\u64cd\u4f5c\u7b49\u591a\u4e2a\u65b9\u9762<\/strong>\uff0c\u662f\u4e00\u4e2a\u6d89\u53ca\u9762\u76f8\u5bf9\u5168\u9762\u7684\u7f16\u7a0b\u7ec3\u4e60\u3002\u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u5b66\u4e60\u5230C#\u4e2d\u7c7b\u7684\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff0c\u8fd8\u80fd\u6df1\u5165\u4e86\u89e3\u5982\u4f55\u5728\u5b9e\u9645\u4e2d\u5904\u7406\u6570\u5b66\u95ee\u9898\uff0c\u4ee5\u53ca\u5982\u4f55\u4f18\u96c5\u5730\u89e3\u51b3\u7f16\u7a0b\u4e2d\u7684\u6570\u503c\u8868\u793a\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u6709\u7406\u6570\u662f\u4ec0\u4e48\uff1f\u5728\u6570\u5b66\u4e2d\uff0c\u6709\u7406\u6570\u662f\u53ef\u4ee5\u8868\u793a\u4e3a\u4e24\u4e2a\u6574\u6570\u4e4b\u6bd4\u7684\u6570\u5b57\u3002\u4f8b\u5982\uff0c1\/2\u30013\/4\u3001-5\/6\u90fd\u662f\u6709\u7406\u6570\u3002\u6709\u7406\u6570\u7684\u7279\u70b9\u662f\u53ef\u4ee5\u8fdb\u884c\u52a0\u51cf\u4e58\u9664\u8fd0\u7b97\u3002\u90a3\u4e48\u5982\u4f55\u7528C\uff03\u7f16\u5199\u4e00\u4e2a\u6709\u7406\u6570\u7684\u7c7b\u5462\uff1f<\/strong><\/p>\n<p>\u5728C\uff03\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6709\u7406\u6570\u7684\u7c7b\uff0c\u4f7f\u7528\u4e24\u4e2a\u6574\u578b\u53d8\u91cf\u8868\u793a\u5206\u5b50\u548c\u5206\u6bcd\u3002\u5728\u7c7b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u65b9\u6cd5\u6765\u8fdb\u884c\u6709\u7406\u6570\u7684\u52a0\u51cf\u4e58\u9664\u3001\u5316\u7b80\u7b49\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9e\u73b0Add()\u65b9\u6cd5\u6765\u8fdb\u884c\u6709\u7406\u6570\u7684\u52a0\u6cd5\u8fd0\u7b97\uff0c\u5b9e\u73b0Subtract()\u65b9\u6cd5\u6765\u8fdb\u884c\u6709\u7406\u6570\u7684\u51cf\u6cd5\u8fd0\u7b97\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5b9e\u73b0\u6709\u7406\u6570\u7684\u52a0\u6cd5\u548c\u51cf\u6cd5\u8fd0\u7b97\uff1f<\/strong><\/p>\n<p>\u5728\u6709\u7406\u6570\u7684\u7c7b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0Add()\u548cSubtract()\u65b9\u6cd5\u6765\u8fdb\u884c\u6709\u7406\u6570\u7684\u52a0\u6cd5\u548c\u51cf\u6cd5\u8fd0\u7b97\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u9996\u5148\u9700\u8981\u627e\u5230\u4e24\u4e2a\u6709\u7406\u6570\u7684\u6700\u5c0f\u516c\u500d\u6570\u4f5c\u4e3a\u4e24\u4e2a\u6709\u7406\u6570\u7684\u5206\u6bcd\uff0c\u7136\u540e\u901a\u8fc7\u5206\u6bcd\u7684\u53d8\u6362\uff0c\u5c06\u4e24\u4e2a\u6709\u7406\u6570\u76f8\u52a0\u6216\u76f8\u51cf\u5e76\u5316\u7b80\u4e3a\u6700\u7b80\u5f62\u5f0f\u3002\u5177\u4f53\u7684\u5b9e\u73b0\u53ef\u4ee5\u4f7f\u7528\u8f97\u8f6c\u76f8\u9664\u6cd5\u6765\u5bfb\u627e\u6700\u5927\u516c\u7ea6\u6570\u548c\u6700\u5c0f\u516c\u500d\u6570\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5b9e\u73b0\u6709\u7406\u6570\u7684\u4e58\u6cd5\u548c\u9664\u6cd5\u8fd0\u7b97\uff1f<\/strong><\/p>\n<p>\u5728\u6709\u7406\u6570\u7684\u7c7b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0Multiply()\u548cDivide()\u65b9\u6cd5\u6765\u8fdb\u884c\u6709\u7406\u6570\u7684\u4e58\u6cd5\u548c\u9664\u6cd5\u8fd0\u7b97\u3002\u5728\u4e58\u6cd5\u8fd0\u7b97\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e24\u4e2a\u6709\u7406\u6570\u7684\u5206\u5b50\u548c\u5206\u6bcd\u5206\u522b\u76f8\u4e58\u5e76\u5316\u7b80\u4e3a\u6700\u7b80\u5f62\u5f0f\u3002\u5728\u9664\u6cd5\u8fd0\u7b97\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5c06\u9664\u6570\u53d6\u5012\u6570\uff0c\u5e76\u4e58\u4ee5\u88ab\u9664\u6570\uff0c\u7136\u540e\u518d\u5316\u7b80\u4e3a\u6700\u7b80\u5f62\u5f0f\u3002\u5728\u4e58\u6cd5\u548c\u9664\u6cd5\u8fd0\u7b97\u4e2d\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u4f7f\u7528\u8f97\u8f6c\u76f8\u9664\u6cd5\u6765\u5bfb\u627e\u6700\u5927\u516c\u7ea6\u6570\u5e76\u5316\u7b80\u6709\u7406\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728C#\u4e2d\uff0c\u5199\u4e00\u4e2a\u6709\u7406\u6570\u7684\u7c7b\u610f\u5473\u7740\u521b\u5efa\u4e00\u4e2a\u80fd\u591f\u8868\u793a\u5206\u6570\uff08\u5373\u6709\u7406\u6570\uff09\u5e76\u652f\u6301\u76f8\u5173\u8fd0\u7b97\u7684\u6570\u636e\u7c7b\u578b\u3002\u4e00\u4e2a\u4f18\u79c0\u7684\u6709\u7406\u6570\u7c7b\u5e94 [&hellip;]","protected":false},"author":3,"featured_media":309385,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/309367"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=309367"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/309367\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/309385"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=309367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=309367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=309367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}