Michael Sazonov
Michael Sazonov

Reputation: 1533

Making a long code shorter

people!

I've been told to create next code as homework. If you compile it - you'l easely see it's purpose. Now, my question is whether there is a way to make it shorter (I'm new to C). I must use structs and struct pointers. This might seem a lame question - sorry for that. As well, I would like to know whether it's alright to call "main()" repeatedly.

#include <stdio.h>

typedef struct frac{
    int num;
    int den;
};

int reducer( struct frac *fi ){
    if( fi->num == 0 ) return 0;
    if( fi->den == 1 ) return 1;
    if( fi->num % fi->den == 0 ){
        fi->num /= fi->den;
        fi->den /= fi->den;
        return reducer( fi );
    }
    if( fi->num % 2 == 0 && fi->den % 2 == 0 ){
        fi->num /= 2;
        fi->den /= 2;
        return reducer( fi );
    }
    else if( fi->num % 3 == 0 && fi->den % 3 == 0 ){
        fi->num /= 3;
        fi->den /= 3;
        return reducer( fi );
    }
}

int main(){
    char c , tt;
    struct frac one , two , multi , quot , sum , diff , *o , *t , *m , *q , *s , *d;
    printf( "Please, enter the first fraction, ieg. 3/8:\n" );
    scanf( "%d/%d%c" , &one.num , &one.den , &tt );
    printf( "Now the second fraction (numerator/denominator):\n" );
    scanf( "%d/%d%c" , &two.num , &two.den , &tt );
    o = &one;
    t = &two;
    m = &multi;
    q = &quot;
    s = &sum;
    d = &diff;
    m->num = o->num * t->num; // product numerator
    m->den = o->den * t->den; // product denominator
    q->num = o->num * t->den; // quotient numerator
    q->den = o->den * t->num; // quotient denominator and so on...
    s->num = q->num + q->den;
    s->den = m->den;
    d->num = q->num - q->den;
    d->den = m->den;
    reducer( q );
    reducer( m );
    reducer( s );
    reducer( d );
    printf( "%d/%d + %d/%d = %d/%d\n" , o->num , o->den , t->num , t->den , s->num , s->den );
    printf( "%d/%d - %d/%d = %d/%d\n" , o->num , o->den , t->num , t->den , d->num , d->den );
    printf( "%d/%d * %d/%d = %d/%d\n" , o->num , o->den , t->num , t->den , m->num , m->den );
    printf( "%d/%d : %d/%d = %d/%d\n" , o->num , o->den , t->num , t->den , q->num , q->den );
    printf( "\nWould you like to make another calculation? (y/n):\n" );
    scanf( "%c" , &c );
    if( c == 121 || c == 89 ){
        return main();
    }
    return 0;
}

Upvotes: 1

Views: 209

Answers (2)

Victor Stafusa
Victor Stafusa

Reputation: 14613

This is wrong. Can it reduce a fraction 21/49? It does not looks like. The reducer function does not always return. In the reducer function, you will need to compute higher and higher denominators (tip: use a while loop), until it can't be divided anymore.

The main recursion works. But this is a really bad practice and tends to ending up in a messy bugged code. If you put anything before that return 0 after the recursion, your code will start to behave crazy. Use a while or do...while loop instead.

Further, you may avoid some variables in the main function.

Upvotes: 1

alf
alf

Reputation: 18550

Here are some suggestions:

  • Use a loop instead of recursion. It's more natural in this scenario, and it doesn't make the stack grow with each iteration:

    int finish;
    do
    {
        //...
        printf( "\nWould you like to make another calculation? (y/n):\n" );
        scanf( "%c" , &c );
        finish = c != 121 && c == 89;
    }
    while (!finish)
    
  • You can remove all the pointer declarations and work directly with the fractions themselves.

    multi.num = one.num * two.num; // product numerator
    // ...
    reducer(&quot)
    

Hope it helps!

Upvotes: 1

Related Questions