Can anybody help me with this?
I am really confused.
I need to:
# Replace POP, PUSH and STACK_IS_EMPTY functions with macros. Each macro will have exactly the same name with the function name it is replaced with.
# Implement another macro called EVAL. This macro will require one parameter and will take the cube or square of this parameter by calling sqr or cube functions depending on the value of OPERATION constant. You will define the value of OPERATION constant as 0 (for sqr) or 1 (for cube). Note1: you can also use constants SQR or CUBE instead of numbers- but you must define them first. Note2: EVAL macro you will implement will not expand into a statement containing any conditional expressions. It will expand into a single call to sqr or cube functions and nothing more. This can only be achieved by using #if and #elif statements in the macro definition.
# Implement a macro called DBG_OUT, which takes a single parameter and calls printf function if the value of the DEBUG constant is defined. If DEBUG constant is not defined then the macro will not do anything.
# Replace sqr and cube calls in the code with EVAL.
# Replace the line “printf("printing a number:\n");” with a call to DBG_OUT.
I have done so far:
Code:
int stack[256];
int stack_ind=0;
#define sqr(x) (x*x)
#define cube(x) (x*x*x)
#define POP() stack[--stack_ind]
#define PUSH(v) stack[stack_ind++]=v
#define STACK_IS_EMPTY() stack_ind==0
#define OPERATION 1
#define EVAL(x) PUSH(x)
#define DBG_OUT()
#ifdef DEBUG
printf("Printing a Number: \n")
printf("%d\n", POP())
#endif
#define DEBUG 1
int main() {
EVAL(sqr(1));
EVAL(cube(2));
EVAL(sqr(3));
while(!STACK_IS_EMPTY()) {
DBG_OUT();
}
return 0;
}
Is that all right (which I don't think so) ??
Original code is as follows:
Code:
int stack[256];
int stack_ind=0;
int sqr(int x){
return x*x;
}
int cube(int x){
return x*x*x;
}
int POP(){
return stack[--stack_ind];
}
void PUSH(int v){
stack[stack_ind++] = v;
}
int STACK_IS_EMPTY(){
return (int)(stack_ind == 0);
}
int main(){
PUSH( sqr(1) );
PUSH( cube(2) );
PUSH( sqr(3) );
while(!STACK_IS_EMPTY()){
printf("printing a number:\n");
printf("%d\n", POP());
}
return 0;
}