Thread: Macros question

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Arrow Macros question

    I am doing a simple calculator, but I am trying to use four macrous to do four things which are MULTIPLYDIVIDE(x,y), ADDSUBTRACT(x,y), TRIG(type,angle) // where type is cos, sin, tan and MODULUS(x,y). But I have no idea to macros. Does anyone can help me a little bit? THanks.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    >> #define MULTIPLYDIVIDE(x,y) x / y

    #define MULTIPLYDIVIDE(x,y) ( (x) / (y) )

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    I defined four macros, but the problem is those macros cannot not been mutiples functions. I mean... hmmm.. For example, the user may input 6 * 3 and I put them into MULTIPLYDIVIDE(6,3). The result is 2, but it shoulf be 18. Is it possible to do it?


    #define MULTIPLYDIVIDE(x,y) ((x) / (y))
    #define ADDSUBTRACT(x,y) ((x) + (y))
    #define TRIG(type,angle) sin(x)
    #define MODULUS(x,y) ((x) % (y))


  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you using macros? What's the point?
    Code:
    switch( task )
    {
        case '+': value = value_1 + value_2; break;
        case '/': value = value_1 / value_2; break;
        case '*': value = value_1 * value_2; break;
        case '-': value = value_1 - value_2; break;
    }
    Seriously, what is the purpose of you making macros? No, you cannot have a macro that takes just the two arguments to do the math on and have it do both the division or multiplication. It doesn't work that way. You have to specify what task you want it to do. Make one to multiply and one to divide.

    Seriously though, I see no point in even bothering with macros.

    Quzah.
    }
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    I have no idea either. I can do it in a easy way, but my homework require something like this. Maybe I have misunderstand it.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >No, you cannot have a macro that takes just the two arguments to do the math on and have it do both the division or multiplication.
    With a clever hack you probably could come up with something, but a more sane way would be to have a three argument macro:
    Code:
    #include <stdio.h>
    
    #define CALC(x,op,y) ((x) op (y))
    
    int main ( void )
    {
      printf ( "%d\n", CALC ( 1, +, 1 ) );
      printf ( "%d\n", CALC ( 5, -, 2 ) );
      printf ( "%d\n", CALC ( 2, *, 2 ) );
      printf ( "%d\n", CALC ( 10, /, 2 ) );
    
      return 0;
    }
    This is questionable style though, I wouldn't use it in production code unless there were no other way.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    Before I thought that same as your method. But my teacher said that is other way. Hereis the link about my homework. It should be a easy program.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The requirement is a very odd one. You should not have a two arguement macro when you really need three. It just is bad form. This will do the trick for you though:
    Code:
    struct values
    {
        long int value_1;
        long int value_2;
    };
    
    
    #define MULTIPLYDIVIDE(x,y)  y=='*' ? \
        (x.value_1 * x.value_2)  : \
        (x.value_1 / x.value_2)
    Usage of the macro would be:

    struct values mydata;
    mydata.value_1 = something;
    mydata.value_2 = somethingelse;

    sum = MULTIPLYDIVIDE( mydata, '/' );

    There. You basicly pass it three values by using a structure. You could create another structure for floating point values , with the same value 'value_1', and 'value_2' respectivly, so the macro works right.

    Hey, I didn't see in the instructions anything saying you cannot use structures.

    I'm sure there's some other messed up hack you could use to do a 2 argument macro, but you really need three arguments to do it "right".

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But my teacher said that is other way.
    Your teacher is either very sadistic, or an idiot. Really the only workable solution with two arguments that I can think of right now is to use an array or struct to hold both values, then pass the operation as the second argument:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define MULTIPLYDIVIDE(x,y) ((x[0]) y (x[1]))
    #define TRIG(type,angle)    (type)( (angle) )
    #define ADDSUBTRACT(x,y)    ((x[0]) y (x[1]))
    #define MODULUS(x,y)        ((x) % (y))
    
    int main ( void )
    {
      int a[] = {6, 3};
    
      printf ( "%d\n", MULTIPLYDIVIDE ( a, * ) );
      printf ( "%d\n", MULTIPLYDIVIDE ( a, / ) );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How about something like this?
    Code:
    #include <stdio.h> 
    
    #define ADDSUBTRACT(x,y)      ( (x) + (y) )
    #define MULTIPLYDIVIDE(x,y)   ( (x) * (y) )
    
    int main(void)
    {
       double a = 1.23, b = 4.56;
       printf("%f + %f = %f\n", a, b, ADDSUBTRACT(a, b));          /* add */
       printf("%f - %f = %f\n", a, b, ADDSUBTRACT(a, -b));         /* subtract */
       printf("%f * %f = %f\n", a, b, MULTIPLYDIVIDE(a, b));       /* multiply */
       printf("%f / %f = %f\n", a, b, MULTIPLYDIVIDE(a, 1.0 / b)); /* divide */
       return(0);
    }
    
    /* my output
    1.230000 + 4.560000 = 5.790000
    1.230000 - 4.560000 = -3.330000
    1.230000 * 4.560000 = 5.608800
    1.230000 / 4.560000 = 0.269737
    */

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define MULTIPLYDIVIDE( x, y ) (int)( ( x ) * ( y ) )
    #define ADDSUBTRACT( x, y ) ( ( x ) + ( y ) )
    // The * atan( 1 ) / 180.0 part converts to radians. atan(1) == pi.
    #define TRIG( type, angle ) (int)( type ( (double)( angle ) * ( atan( 1 ) / 180.0 ) ) )
    #define MODULUS( x, y ) ( ( x ) % ( y ) )
    
    int main( void ) {
    	int a = 6;
    	int b = 3;
    	// a / b = a * ( 1 / b )
    	printf( "%d\t%d\n", MULTIPLYDIVIDE( (double)a, 1.0 / (double)b ), MULTIPLYDIVIDE( (double)a, (double)b ) );
    	return 0;
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    Looks work well..... Thx....

    Originally posted by Dave_Sinkula
    How about something like this?
    Code:
    #include <stdio.h> 
    
    #define ADDSUBTRACT(x,y)      ( (x) + (y) )
    #define MULTIPLYDIVIDE(x,y)   ( (x) * (y) )
    
    int main(void)
    {
       double a = 1.23, b = 4.56;
       printf("%f + %f = %f\n", a, b, ADDSUBTRACT(a, b));          /* add */
       printf("%f - %f = %f\n", a, b, ADDSUBTRACT(a, -b));         /* subtract */
       printf("%f * %f = %f\n", a, b, MULTIPLYDIVIDE(a, b));       /* multiply */
       printf("%f / %f = %f\n", a, b, MULTIPLYDIVIDE(a, 1.0 / b)); /* divide */
       return(0);
    }
    
    /* my output
    1.230000 + 4.560000 = 5.790000
    1.230000 - 4.560000 = -3.330000
    1.230000 * 4.560000 = 5.608800
    1.230000 / 4.560000 = 0.269737
    */
    Last edited by anson1999; 03-06-2003 at 11:48 PM.

  13. #13
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    Sorry, I will do it in my next post and thank you for your helping.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about macros
    By lazyme in forum C++ Programming
    Replies: 19
    Last Post: 04-27-2009, 10:10 PM
  2. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  3. Quick question about VB Word macros
    By PJYelton in forum Tech Board
    Replies: 6
    Last Post: 05-28-2005, 10:26 PM
  4. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM