开发者

C program which adds two integers as fractions

开发者 https://www.devze.com 2023-04-08 04:37 出处:网络
I am writing a C program which adds two nonzero integers which are given as fractions. The integers are input as n1 d1 n2 d2. There seems to be an error when certain negative numbers are input, such a

I am writing a C program which adds two nonzero integers which are given as fractions. The integers are input as n1 d1 n2 d2. There seems to be an error when certain negative numbers are input, such as in the following combination ( 1 2 -1 2). This should state that the numbers being added are 1/2 and -1/2, and then state that the sum is 0. Instead, the program just shows a blank line and does not accept any further input. The second problem I have is that when the sentinel value is input the program is supposed to display a termination message and then end, instead it displays "floating point exception".

 int
    main( void )
    {
int n1;
int d1;
int n2;
int d2;
int g;
int p;
int q;
int again;
int sumn;
int sumd;

again = 1;

while ( again == 1 ) {

  printf( "Please enter 4 nonzero integers representing the fractions: " );
  scanf( "%d%d%d%d", &n1, &d1, &n2, &d2 );

  if ( n1 == 0 && n2 == 0 && d1 == 0 && d2 == 0 ) {
    again = 0;
  }
  else {
    again = 1;
  }
  if ( ( n1 == 0 || n2 == 0 || d1 == 0 || d2 == 0 ) && ( n1 != 0 || n2 != 0 || d1 != 0 || d2 != 0 ) ) {
    printf( "One or more of the integers is zero\n" );
    again = 1;
  }
    else {

    g = gcd( d1, d2 );
    p = ( ( n1 * ( d2 / g ) ) + ( n2 * ( d1 / g ) ) );
    q = ( d1 * ( d2 / g ) );

    sumn = ( p / ( gcd( p, q ) ) );
    sumd = ( q / ( gcd开发者_StackOverflow( p, q ) ) );

    printf( "The fractions are: %d/%d and %d/%d\n", n1, d1, n2, d2 );

    if ( sumn == sumd ) {
      printf( "Their sum is: 1\n" );
    }
    else {
      if ( sumd == 1 ) {
        printf( "Their sum is: %d\n", sumn );
      }
      else {
        printf( "Their sum is: %d/%d\n", sumn, sumd );
      }
    }
  }
}
printf( "***** Program Terminated *****\n" );

return (EXIT_SUCCESS);
}

int gcd( int a, int b )
{
while ( a != b ) {
  if ( a > b ) {
    a = ( a - b );
  }
  else {
    b = ( b - a );
  }
}
return a;
}


You have an error in the gcd function. When a is negative, it's an infinite loop. Therefore the console hangs and won't take any more input.

int gcd( int a, int b )
{
    while ( a != b ) {
       cout << a << "  " << b << endl;
       if ( a > b ) {
          a = ( a - b );
       }
       else {
          b = ( b - a );
        }
    }
    return a;
}

Output: gcd(-1,2)

-1  2
-1  3
-1  4
-1  5
-1  6
-1  7
-1  8
-1  9
-1  10
-1  11
-1  12
-1  13
-1  14
...

p.s. Yes I know the question is tagged C, but I'm just printing out data in C++.

EDIT:

The fix is to make a and b positive:

int gcd( int a, int b )
{
    if (a < 0)
        a = -a;
    if (b < 0)
        b = -b;

    while ( a != b ){
       if ( a > b ){
           a = ( a - b );
       }
       else{
           b = ( b - a );
       }
    }
    return a;
}


When you enter sentinel value ( I assume all equal to zero) it executes your first if case inside while and then goes on to execute 2nd else case i.e. this part else {

g = gcd( d1, d2 );
p = ( ( n1 * ( d2 / g ) ) + ( n2 * ( d1 / g ) ) );
....

and goes on to calculate gcd which I guess is triggering floating point exception


GCD should be taken care of by providing only positive values. As mystical pointed, you should make the numbers positive. Moreover you should also check if any number coming in is not zero. So, edit the GCD function as follows:

int gcd( int a, int b )
{
 if (a==0 || b==0)
       return 1;
 if (a < 0)
        a = -a;
 if (b < 0)
        b = -b;
while ( a != b ) {
  if ( a > b ) {
    a = ( a - b );
  }
  else {
    b = ( b - a );
  }
}
return a;
}

You can now see you get correct result results for even the combination like (1 2 -1 2) or even (-1 -1 -2 -2).

And to solve the sentinel problem, edit the while loop as:

while ( again == 1 ) {

  printf( "Please enter 4 nonzero integers representing the fractions: " );
  scanf( "%d%d%d%d", &n1, &d1, &n2, &d2 );

  if ( n1 == 0 && n2 == 0 && d1 == 0 && d2 == 0 ) {
    again = 0;
  }
  else {    
  if ( ( n1 == 0 || n2 == 0 || d1 == 0 || d2 == 0 ) && ( n1 != 0 || n2 != 0 || d1 != 0 || d2 != 0 ) ) {
    printf( "One or more of the integers is zero\n" );
    again = 1;
  }
    else {

    g = gcd( d1, d2 );
    p = ( ( n1 * ( d2 / g ) ) + ( n2 * ( d1 / g ) ) );
    q = ( d1 * ( d2 / g ) );


    sumn = ( p / ( gcd( p, q ) ) );
    sumd = ( q / ( gcd( p, q ) ) );


    printf( "The fractions are: %d/%d and %d/%d\n", n1, d1, n2, d2 );

    if ( sumn == sumd ) {
      printf( "Their sum is: 1\n" );
    }
    else {
      if ( sumd == 1 ) {
        printf( "Their sum is: %d\n", sumn );
      }
      else {
        printf( "Their sum is: %d/%d\n", sumn, sumd );
      }
    }
  }
 }
}

-Sandip

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号