C++. Macros. Directive #define. Examples

Macros. Directive #define. Examples


Contents


Search other websites:




1. The concept of macros. Directive #define

Duplicate pieces of code can appear in programs. To call frequently used code snippets, in C++ programs you can use:

  • functions;
  • macros.

The #define directive is used to declare a macro. The general form of declaring a macro is as follows:

#define MacroName(parameters) expression

here

  • MacroName – the name of macro;
  • parameters – parameters that the macro receives;
  • expression – expression that the macro evaluates. The expression is placed on the same line as the #define directive.

Once a macro is declared, it can be used as a call to a regular function.

 

2. Implementation of macros in programs. An example that demonstrates the use of 3 macros in programs

Task 1. Develop a program that implements 3 macros:

  • SQR(x) – returns the value x2;
  • POWER_4(x) – returns x4;
  • SIGN(x) – determines the sign of the number.

C++ program text

#include <iostream>
using namespace std;

// Topic: Macro expansion
// The preprocessor directive #define is used
// #define Macro_Name(Parameters) (Expression)

// 1. Macro that squares the number x
#define SQR(x) ((x) * (x))

// 2. Macro that raises x to the power of 4
#define POWER_4(x) (SQR(x) * SQR(x))

// 3. Macro that returns -1 if the number is negative, 0 - if zero, 1 - if positive
#define SIGN(x) (((x)<0)? -1 :(((x)==0) ? 0 : 1))

void main()
{
  // Demonstration of using macros in the program
  // 1. Declare variables
  int x;

  // 2. Input x
  cout << "x = ";
  cin >> x;

  // 3. Call macros and display the result on the screen
  cout << "SQR(x) = " << SQR(x) << endl;
  cout << "POWER4(x) = " << POWER_4(x) << endl;
  cout << "SIGN(x) = " << SIGN(x) << ::endl;

  system("pause");
}

 

3. An example where one macro uses the result of another macro. Calculating the area of a triangle

This example demonstrates calling a macro from another macro. The task of calculating the area of a triangle along the sides a, b, c is solved. If it is impossible to form a triangle from the sides a, b, c, then the macro returns 0.

The program text

#include <iostream>
using namespace std;

// Macros. Directive #define
// Calculate the area of the triangle along the sides a, b, c.
// If it is impossible to create a triangle from the sides a, b, c, then return 0.
// Calculating a semi-perimeter
#define P(a, b, c) ((a+b+c)/2.0)

// Determining whether it is possible to form a triangle from the sides a, b, c
#define IS_TR(a, b, c) ((((a+b)>c)&&((b+c)>a)&&((a+c)>b)) ? true : false)

// Calculation the area. Two other macros are called from this macro: IS_TRIANGLE and P
#define AREA_TR(a, b, c) ((IS_TR(a,b,c)) ? (sqrt(P(a,b,c)*(P(a,b,c)-a)*(P(a,b,c)-b)*(P(a,b,c)-c))) : 0)

void main()
{
  // Demonstration of work of macros
  double a, b, c;
  double area; // result
  double sp; // semiperimeter

  cout << "a = "; cin >> a;
  cout << "b = "; cin >> b;
  cout << "c = "; cin >> c;

  sp = P(a, b, c);
  area = AREA_TR(a, b, c);

  cout << "semiperimeter = " << sp << endl;
  cout << "area = " << area << endl;
}

The result of the program

a = 5
b = 8
c = 4.5
semiperimeter = 8.75
area = 10.2269

 

4. An example of splitting a problem solution into several macros. Solving a quadratic equation

The example demonstrates solving a quadratic equation. The program declares 4 macros. It also determines whether the equation has roots.

The program text is as follows

#include <iostream>
using namespace std;

// Macros. Directive #define
// Calculating the discriminant
#define D(a, b, c) ((double)b*b-4*a*c)

// Calculating the discriminant
#define IS_ROOTS(a, b, c) ((D(a, b, c)>=0) ? true : false)

// Calculation of roots
#define X1(a, b, c) (IS_ROOTS(a, b, c) ? ((-b - sqrt(D(a, b, c)))/(2.0*a)) : 0)
#define X2(a, b, c) (IS_ROOTS(a, b, c) ? ((-b + sqrt(D(a, b, c)))/(2.0*a)) : 0)

void main()
{
  double a, b, c;
  double d;
  double x1, x2;

  cout << "a = "; cin >> a;
  cout << "b = "; cin >> b;
  cout << "c = "; cin >> c;

  if (IS_ROOTS(a, b, c))
  {
    x1 = X1(a, b, c);
    x2 = X2(a, b, c);
    cout << "x1 = " << x1 << endl;
    cout << "x2 = " << x2 << endl;
  }
  else
  {
    cout << "The equation has no solutions";
  }
}

The result of the program

a = 4
b = -5
c = -6
x1 = -0.75
x2 = 2

 


Related topics