TOPIC

Can anyone fix where is the problem? C++

Akib Rahman asked 10 months ago

include include

using namespace std;

int function(int x,int y) { int a,c; if(x == 0 && y > 0)

    return 1;

if(x < y)
{
    a = x;
    x = y;
    y = a;
}

c = x % y;

if(c != 0)
    return c;
else
    return y;

}

int main() {

int m,b,q,r,f,j,n1,n2,d1,d2;
cin>>q;
string c1,c2,c3;

int i;
for(i = 0; i < q; i++)
{
    cin>>n1>>c1>>d1>>c2>>n2>>c3>>d2;

    if(c2 == "+")
    {
        m = (( n1 * d2 ) + ( n2 * d1 ));
        b = ( d1 * d2 );
    }
    else if(c2 == "-")
    {
        m = (( n1 * d2 ) - ( n2 * d1 ));
        b = ( d1 * d2 );
    }
    else if(c2 == "*")
    {
        m = ( n1 * n2 );
        b = ( d1 * d2 );
    }
    else if(c2 == "/")
    {
        m = ( n1 * d2 );
        b = ( n2 * d1 );
    }

    r = function( m, b );
    f = m / r ;
    j = b / r ;

    if(f > 0 && j > 0)
    {
        cout<<m<<"/"<<b<<" = "<<f<<"/"<<j<<endl;
    }
    else
    {
        if(j < 0)
        {
            f = -f;
            j = -j;
        }
        cout<<m<<"/"<<b<<" = "<<f<<"/"<<j<<endl;
    }
}
return 0;

}

Remember not post solutions. Your post may be reviewed by our moderators.

  • Tarcísio Mazur Junior replied 10 months ago

    Your gcd function is wrong. For example gcd (9,0) = 9 while yours presents 1. Search for Euclidean algorithm to solve

    MOD