LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-19-2005, 03:16 PM   #1
drumstick
Member
 
Registered: Mar 2005
Location: Istanbul
Distribution: Ubuntu Desktop 6.06, SuSE Linux 9.3 Professional
Posts: 35

Rep: Reputation: 15
Macros In C


Hi there,

I am new to macros and I am stuck up with something.

What is requested from me is:

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.


And the code is a stack implementation as follows:

Code:
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;
}
I am stuck up at the defining process of the OPERATION constant. I guess it's so easy. But as I said I am new to macros. I would really appreciate if anyone can help me. Thanks in advance.

Last edited by drumstick; 11-21-2005 at 03:02 PM.
 
Old 11-19-2005, 04:05 PM   #2
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
I'd guess that your clients will (assuming you define SQR and CUBE for them):
Code:
#define OPERATION SQR
int sqr = EVAL(somevalue)
Your macro definition will have to define values for SQR and CUBE and then test OPERATION in the conditional part of the macro definition (the #if and #elif statements mentioned).

If you don't define SQR and CUBE, you'll just test OPERATION for being 0 or 1, respectively.

Good luck!
 
Old 11-21-2005, 01:37 PM   #3
drumstick
Member
 
Registered: Mar 2005
Location: Istanbul
Distribution: Ubuntu Desktop 6.06, SuSE Linux 9.3 Professional
Posts: 35

Original Poster
Rep: Reputation: 15
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;
}
 
Old 11-21-2005, 02:16 PM   #4
geeman2.0
Member
 
Registered: Feb 2005
Location: Ontario, Canada
Distribution: Gentoo, Slackware
Posts: 345

Rep: Reputation: 30
This smells like a homework assignment to me...
 
Old 11-21-2005, 02:19 PM   #5
drumstick
Member
 
Registered: Mar 2005
Location: Istanbul
Distribution: Ubuntu Desktop 6.06, SuSE Linux 9.3 Professional
Posts: 35

Original Poster
Rep: Reputation: 15
Yes it is...
 
Old 11-21-2005, 02:21 PM   #6
drumstick
Member
 
Registered: Mar 2005
Location: Istanbul
Distribution: Ubuntu Desktop 6.06, SuSE Linux 9.3 Professional
Posts: 35

Original Poster
Rep: Reputation: 15
And deadline is reaching...
 
Old 11-21-2005, 04:29 PM   #7
drumstick
Member
 
Registered: Mar 2005
Location: Istanbul
Distribution: Ubuntu Desktop 6.06, SuSE Linux 9.3 Professional
Posts: 35

Original Poster
Rep: Reputation: 15
Thanks to everyone who has helped (eddiebaby1023) and not helped (geeman2.0).

Here is the implementaion if anyone wonders:

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 EVAL(x) PUSH(x)
#define OPERATION 0
#define DEBUG

#define DBG_OUT()\
  printf("printing a number: ");

int main() {
  #if OPERATION==0
    EVAL(sqr(1));
    EVAL(sqr(3));
  #elif OPERATION==1
    EVAL(cube(2));
  #endif

  while(!STACK_IS_EMPTY()) {
    #ifdef DEBUG
      DBG_OUT()
    #endif
    printf("%d\n", POP());
  }
  return 0;
}
 
Old 11-21-2005, 04:48 PM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Since it's homework (and once upon a time I was a teacher...) Let me just point out one problem with your solution:

just try to print out the result of cube(2+2), you'll not get the cube of 4 - or 64 but you will get 12 or 2+2*2+2*2+2. It's fairly easy to fix but I will let you ponder it for a while...

graeme.
 
Old 11-21-2005, 05:37 PM   #9
geeman2.0
Member
 
Registered: Feb 2005
Location: Ontario, Canada
Distribution: Gentoo, Slackware
Posts: 345

Rep: Reputation: 30
Well if you'd bothered to read the rules when you signed up you'd know that you shouldn't ask LQ to do your homework for you
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Emacs and macros pittopitto Linux - Software 1 10-22-2005 04:27 AM
vi editor macros sailu_mvn Linux - Software 6 10-12-2005 02:44 AM
Macros Twi7ch Linux - General 2 08-27-2005 10:37 PM
Question about macros Nerox Programming 4 04-03-2005 04:34 AM
Help with NASM macros? 95se Programming 0 04-02-2005 07:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration