The #define directives for simple mathematical operations C++ code example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info:

To do: Doing some mathematical operations to demonstrate the use of #define preprocessor directives in C++ programming

To show: How to use the #define preprocessor directives in C++ programming

 

#include <iostream>

using namespace std;

 

#define THREETIMES(x) (x)*(x)*(x)

#define CIRAREA(y) (PI)*(y)*(y)

#define REC(z, a) (z)*(a)

#define PI 3.14159

 

int main(void)

{

double p = 2.5;

double r = 3.5, s, t, u = 1.5, v = 2.5;

 

cout<<"Power to three of "<<p<<" is "<<THREETIMES(p)<<endl;

cout<<"Circle circumference = 2*PI*r = "<<(2*PI*r)<<endl;

s = CIRAREA(r+p);

cout<<"Circle area = PI*r*r = "<<s<<endl;

t = REC(u, v);

cout<<"Rectangle area = u*v = "<<t<<endl;

 

return 0;

}

 

Output example:

 

Power to three of 2.5 is 15.625

Circle circumference = 2*PI*r = 21.9911

Circle area = PI*r*r = 113.097

Rectangle area = u*v = 3.75

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index