In 'comp.lang.c', fix <fi*@here.com> wrote:

Fraction.c:

#include "Fraction.h"

<snipped>

First of all, I highly recommend that you change your code organization. It's

a very bad idea to have code included in a .h. I suggest a very common

approach as follow:

/* main.c */

#include "Fraction.h"

#include <stdio.h>

/* macros ================================================== ============ */

/* constants ================================================== ========= */

/* types ================================================== ============= */

/* structures ================================================== ======== */

/* private variables ================================================== = */

/* private functions ================================================== = */

static void Case1c (void)

{

printFraction (newFraction (0, 4));

printf ("\n");

printFraction (newFraction (4, 0));

printf ("\n");

printFraction (newFraction (0, 0));

printf ("\n");

}

/* entry point ================================================== ======= */

int main (void)

{

printf ("Case 1c:\n");

Case1c ();

return 0;

}

/* public variables ================================================== == */

/* fraction.h */

#ifndef H_FRACTION

#define H_FRACTION

/* macros ================================================== ============ */

/* constants ================================================== ========= */

/* types ================================================== ============= */

/* structures ================================================== ======== */

typedef struct

{

int sign;

int numerator;

int denominator;

}

Fraction;

/* internal public functions =========================================== */

/* entry points ================================================== ====== */

Fraction newFraction (int num, int den);

void printFraction (Fraction fr);

/* public variables ================================================== == */

#endif /* guard */

/* fraction.c */

#include <stdio.h>

#include "fraction.h"

/* macros ================================================== ============ */

/* constants ================================================== ========= */

/* types ================================================== ============= */

/* structures ================================================== ======== */

/* private variables ================================================== = */

/* private functions ================================================== = */

static int signOf (int number)

{

if (number < 0)

return -1;

else

return 1;

}

static int gcd (int num1, int num2)

{

if (num1 < num2)

return gcd (num2, num1);

else if ((num1 % num2) == 0)

return num2;

else

return gcd (num2, num1 % num2);

}

static void normalize (Fraction * fr)

{

int factor;

if (fr->denominator == 0) /* Invalid function */

{

fr = NULL; /* set it to NULL and return */

printf ("Set fr = NULL\n");

if (fr != NULL)

printf ("Not null");

else

printf ("null");

return;

}

/* Take the signs form the numerator and denominator to the sign variable

*/

fr->sign *= signOf (fr->numerator);

fr->sign *= signOf (fr->denominator);

/* Absolute the numerator and denominator */

fr->numerator *= signOf (fr->numerator);

fr->denominator *= signOf (fr->denominator);

if (fr->numerator == 0)

{ /* If the numerator is 0, simplify it to 0/1

*/

fr->denominator = 1;

fr->sign = 1;

}

else

{ /* Find the gcd of the numerator and

denominator, divide them by the gcd */

factor = gcd (fr->numerator, fr->denominator);

fr->numerator /= factor;

fr->denominator /= factor;

}

}

/* internal public functions =========================================== */

/* entry points ================================================== ====== */

Fraction newFraction (int num, int den)

{

Fraction fr;

Fraction *frp;

fr.numerator = num;

fr.denominator = den;

fr.sign = 1;

frp = &fr;

normalize (frp);

if (frp != NULL)

printf ("Not null");

else

printf ("null");

return fr;

}

void printFraction (Fraction fr)

{

if (&fr == NULL)

printf ("Invalid fraction");

if (fr.sign == -1)

printf ("-");

printf ("%i/%i", fr.numerator, fr.denominator);

}

/* public variables ================================================== == */

Note that I have no changed your code.

--

-ed-

em**********@noos.fr [remove YOURBRA before answering me]

The C-language FAQ:

http://www.eskimo.com/~scs/C-faq/top.html
C-reference:

http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c :

http://www.isty-info.uvsq.fr/~rumeau/fclc/