ch3-Mathops (Print macro, math operations for ints and floats)

Chapter_3     Exercise_3-10     Constants_in_C++ Boolean     Exercise_3-11







Mathops.cpp     TCP1, p. 169-170         download


// Mathematical operators
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

// A macro to display a string and a value:
#define PRINT(STR, VAR) \
cout << STR " = " << VAR << endl

int main()
{
int i, j, k;
float u, v, w; // Applies to doubles, too
cout << "Enter an integer: ";
cin >> j;
cout << "Enter another integer: ";
cin >> k;
PRINT("j",j); PRINT("k",k);
i = j + k; PRINT("j + k",i);
i = j - k; PRINT("j - k",i);
i = k / j; PRINT("k / j",i);
i = k * j; PRINT("k * j",i);
i = k % j; PRINT("k % j",i);
// The following only works with integers:
j %= k; PRINT("j %= k", j);
cout << "Enter a floating-point number: ";
cin >> v;
cout << "Enter another floating-point number: ";
cin >> w;
PRINT("v",v); PRINT("w",w);
u = v + w; PRINT("v + w", u);
u = v - w; PRINT("v - w", u);
u = v * w; PRINT("v * w", u);
u = v / w; PRINT("v / w", u);
// The following works for ints, chars, and doubles too:
PRINT("u", u); PRINT("v", v);
u += v; PRINT("u += v", u);
u -= v; PRINT("u -= v", u);
u *= v; PRINT("u *= v", u);
u /= v; PRINT("u /= v", u);

return 0;
}
/*
g++ Mathops.cpp -o Mathops
./Mathops
Enter an integer: 1234
Enter another integer: 456789
j = 1234
k = 456789
j + k = 458023
j - k = -455555
k / j = 370
k * j = 563677626
k % j = 209
j %= k = 1234
Enter a floating-point number: 123.456
Enter another floating-point number: 7890.1234
v = 123.456
w = 7890.12
v + w = 8013.58
v - w = -7766.67
v * w = 974083
v / w = 0.0156469
u = 0.0156469
v = 123.456
u += v = 123.472
u -= v = 0.0156479
u *= v = 1.93183
u /= v = 0.0156479
*/









Chapter_3     Exercise_3-10     Constants_in_C++ BACK_TO_TOP Boolean     Exercise_3-11



Comments

Popular posts from this blog

Contents