Bahram Dehghanian شنبه 2 اردیبهشت 1391 21:15 نظرات ()

تابعی که جمع اعداد کسری را شبیه سازی می کند.

 

..::برای مشاهده كد به ادامه مطلب بروید::..

جواب:

 

void sum(int a1, int a, int b1, int b)
        {
           
            {
                int sorat, makhraj;
                int temp, f1, f2 = 0;

                if (a == b)
                {
                    sorat = a1 + b1;
                    makhraj = a;
                }
                else if (a > b)
                {
                    if (a % b == 0)
                    {
                        temp = a / b;
                        b1 *= temp;
                        sorat = a1 + b1;
                        makhraj = a;
                    }
                    else
                    {
                        b1 *= a;
                        a1 *= b;
                        sorat = a1 + b1;
                        makhraj = a * b;
                    }
                }
                else
                {
                    if (b % a == 0)
                    {
                        temp = b / a;
                        a1 *= temp;
                        sorat = a1 + b1;
                        makhraj = b;
                    }
                    else
                    {
                        b1 *= a;
                        a1 *= b;
                        sorat = a1 + b1;
                        makhraj = a * b;
                    }
                }
                if (sorat > makhraj)
                    f1 = sorat;
                else
                    f1 = makhraj;
                //***************************************************
                for (int j = 2; j < f1; j++)
                    if (sorat % j == 0 && makhraj % j == 0)
                        f2 = j;
                //****************************************************
                if (sorat % makhraj == 0)
                {
                   
                    cout<<(sorat / makhraj);
                }

                else if (f2 != 0)
                {
                    sorat /= f2;
                    makhraj /= f2;

        cout<<sorat<<endl;
        cout<<"ــ\n";
        cout<<makhraj<<endl;
      }
      else
      {

        cout<<sorat<<endl;
        cout<<"ــ\n";
        cout<<makhraj<<endl;
                }
            }
        }