Thread: struct again

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    31

    struct again

    how do i extend the struct number type to add the fraction type, and how do i extend the display funtion so that it also handles fractional numbers, displaying them in the format numerator / denominator.
    (thanx stoned coder by the way)


    Code:
    ****************struct fraction************* 
    struct fraction 
    { 
    double numerator; 
    double demoninator; 
    } 
    
    double Agg( struct fraction* frac) 
    
    { 
    return frac->numerator + frac->denominator; 
    } 
    
    
    ****************Program to extend***************** 
    
    enum tag { is_int, is_double }; 
    
    union value { 
    int i; 
    double d; 
    }; 
    
    struct number { 
    enum tag t; 
    union value v; 
    }; 
    
    void display (struct number n) { 
    switch (n.t) { 
    case is_int: 
    printf("%d", n.v.i); break; 
    case is_double: 
    printf("%g", n.v.d); break; 
    }; 
    } 
    
    struct number plus (struct number n1, struct number n2) { 
    struct number result; 
    if (n1.t == is_int && n2.t == is_int) { 
    result.t = is_int; 
    result.v.i = n1.v.i + n2.v.i; 
    } else { 
    result.t = is_double; 
    result.v.d = 0.0; 
    result.v.d += n1.t == is_double ? n1.v.d : (double)n1.v.i; 
    result.v.d += n2.t == is_double ? n2.v.d : (double)n2.v.i; 
    } 
    return result; 
    } 
    
    int main () { 
    struct number x; 
    struct number pi; 
    x.t = is_int; x.v.i = 42; 
    pi.t = is_double; pi.v.d = 3.14159; 
    display(plus(x, pi)); 
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The formula for adding two fractions (?) is this: (a / b) + (c / d) = ((a * d) + (b * c)) / (b * d)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    31
    No i dont think thats right, does anyone else have any ieas?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Pure mathematically that formula is correct. But if that wasn't what you asked for, then please be more specific.

    BTW: You should really try to make the code better structured. It is rather hard to read...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    31
    i dont want to actually add two fractions together, i want to add the fraction function to the rest of the code.... they are two separate pieces of code, and i want them together, and then i want to extend the function called display so that it handles fractional numbers, displaying them in the format numerator / denominator.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Here's something for you to look at:
    Code:
    typedef struct
    {
       double Numerator;
       double Demoninator;
    }FRACTION;
    
    
    FRACTION CreateFraction(double Numerator, double Denominator)
    {
       FRACTION Number;
       Number.Numerator = Numerator;
       Number.Denominator = Denominator;
       return Number;
    }
    
    
    void PrintFraction(FRACTION Number)
    {
       printf("%g/%g", Number.Numerator, Number.Denominator);
    }
    
    
    int main()
    {
       FRACTION MyFraction = CreateFraction(34, 7);
       PrintFraction(MyFraction);
       getch(); //If you have conio.h
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM