C++ 如何化简分数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7777142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:28:28  来源:igfitidea点击:

How to simplify a fraction

c++calgorithmmathgreatest-common-divisor

提问by fuddin

I want to simplify a fraction in my application. The fraction is like, x/y where x and y are integers. I want to simplify the fraction to its simplest form. Can anyone please give me hints how to do it. Thanks in advance.

我想简化我的应用程序中的一小部分。分数类似于 x/y,其中 x 和 y 是整数。我想将分数简化为最简单的形式。任何人都可以给我提示如何做到这一点。提前致谢。

回答by cnicutar

  • Compute the greatest common divisor for x and y
  • Divide both of them by the GCD
  • 计算 x 和 y 的最大公约数
  • 将两者除以 GCD

Euclid's algorithmis an easy way to compute the GCD.

Euclid 算法是计算 GCD 的一种简单方法。

回答by Peter Alexander

Divide both by gcd(x,y)

除以 gcd(x,y)

The Binary GCD algorithmis a fast way to compute the GCD on a computer.

二进制GCD算法是计算在计算机上的GCD的快捷方式。

回答by Zaryab khan

     #include<iostream>
      using namespace std;
        struct fraction
        {
              int n1, d1, n2, d2, s1, s2;
             };
         void simplification(int a,int b)
       {
                bool e = true;
            int t; int z;
        for (int i = (a*b); i > 1;i--)
        { if ((a%i==0)&&(b%i==0))
       {
        t = a / i;
      z = b / i;

         }
else
         {
            e = false;
       }
         }
         cout << "simplest form=" << t << "/" << z << endl;

      }
      void sum(int num1, int deno1, int num2, int deno2)
        {
            int k,y;
          k = num1* deno2 + num2*deno1;
          y = deno2*deno1;
           cout << "addition of given fraction = " << k << "/" << y << endl;
         simplification(k, y);
      }
     void sub(int num1, int deno1, int num2, int deno2)
     {              
           int k, y;

            k = num1*deno2 - num2*deno1;
        y = deno1*deno2;
            cout << "Substraction of given fraction = " << k << "/" << y << endl;

          }
         void mul(int num1, int deno1, int num2, int deno2)
            {
            int k, y;

            k = num1*num2;
                y = deno1*deno2;
                 cout << "multiplication of given fration= " << k<< "/" <<y;                                        cout<< endl;
                simplification(k, y);
            }

        void div(int num1, int deno1, int num2, int deno2)
          {
          int k, y;
       ;
     k = num1*deno1;
     y = deno1*num2;
        cout << "division of given fraction" << k << "/" << y << endl;
    simplification(k, y);
     }


      int main()
       {    fraction a;
            cout << "enter numirator of f1=";cin >> a.n1;
          cout << "enter denominator of f1=";cin >> a.d1;
            cout << "enter numirator of f2=";cin >> a.n2;
        cout << "enter denominator of f2=";cin >> a.d2;
            cout << "f1= " << a.n1 << "/" << a.d1 << endl;
                cout << "f2= " << a.n2 << "/" << a.d2 << endl;
            mul(a.n1, a.d1, a.n2, a.d2);
        div(a.n1, a.d1, a.n2, a.d2); 
                sub(a.n1, a.d1, a.n2, a.d2);
                sum(a.n1, a.d1, a.n2, a.d2);
         system("pause");
           }