
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.

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

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))
:confused:

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.
}

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.

>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

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.

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.

>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

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
*/

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;
}

Looks work well..... Thx....
Quote:
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
*/

Sorry, I will do it in my next post and thank you for your helping.