Dealing with Data |
|---|
// limits.cpp -- some integer limits
#include <iostream.h>
#include <limits.h> // defines limits for types
int main(void)
{
int n_int = INT_MAX; // initialize n_int to max int value
short n_short = SHRT_MAX; // symbols defined in limits.h file
long n_long = LONG_MAX;
// sizeof operator yields size of type or of variable
cout << "int is " << sizeof (int) << " bytes.\n";
cout << "short is " << sizeof n_short << " bytes.\n";
cout << "long is " << sizeof n_long << " bytes.\n\n";
cout << "Maximum values:\n";
cout << "int: " << n_int << "\n";
cout << "short: " << n_short << "\n";
cout << "long: " << n_long << "\n\n";
cout << "Minimum int value = " << INT_MIN << "\n";
return 0;
}
// exceed.cpp -- exceeding some integer limits
#include <iostream.h>
#define ZERO 0 // makes ZERO symbol for 0 value
#include <limits.h> // defines INT_MAX as largest int value
int main(void)
{
int sam = INT_MAX; // initialize a variable to max value
unsigned sue = sam; // okay if variable sam already defined
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited.\nAdd $1 to each account.\nNow ";
sam = sam + 1;
sue = sue + 1;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited.\nPoor Sam!\n";
sam = ZERO;
sue = ZERO;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited.\n";
cout << "Take $1 from each account.\nNow ";
sam = sam - 1;
sue = sue - 1;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited.\nLucky Sue!\n";
return 0;
}
// hexoct.cpp -- shows hex and octal constants
#include <iostream.h>
int main(void)
{
int chest = 42; // decimal integer constant
int waist = 0x42; // hexadecimal integer constant
int inseam = 042; // octal integer constant
cout << "Monsieur cuts a striking figure!\n";
cout << "chest = " << chest << "\n";
cout << "waist = " << waist << "\n";
cout << "inseam = " << inseam << "\n";
return 0;
}
// chartype.cpp -- the char type
#include <iostream.h>
int main(void)
{
char ch; // declare a char variable
cout << "Enter a character:\n";
cin >> ch;
cout << "Holla! ";
cout << "Thank you for the " << ch << " character.\n";
return 0;
}
// morechar.cpp -- the char type and int type contrasted
#include <iostream.h>
int main(void)
{
char c = 'M'; // assign ASCII code for M to c
int i = c; // store same code in an int
cout << "The ASCII code for " << c << " is " << i << "\n";
cout << "Add one to the character code:\n";
c = c + 1;
i = c;
cout << "The ASCII code for " << c << " is " << i << '\n';
// using the cout.put() member function to display a char
cout << "Displaying char c using cout.put(c): ";
cout.put(c);
// using cout.put() to display a char constant
cout.put('!');
cout << "\nDone\n";
return 0;
}
// bondini.cpp -- using escape sequences
#include <iostream.h>
int main(void)
{
cout << "\aOperation \"HyperHype\" is now activated!\n";
cout << "Enter your agent code:________\b\b\b\b\b\b\b\b";
long code;
cin >> code;
cout << "\aYou entered " << code << "...\n";
cout << "\aCode verified! Proceed with Plan Z3!\n";
return 0;
}
// floatnum.cpp -- floating-point types in Microsoft VC++ 1.0
#include <iostream.h>
int main(void)
{
cout.setf(ios::fixed, ios::floatfield); // fixed-point
float tub = 10.0 / 3.0; // good to about 6 places
double mint = 10.0 / 3.0; // good to about 15 places
const float million = 1.0e6;
cout << "tub = " << tub;
cout << ", a million tubs = " << million * tub;
cout << ",\nand ten million tubs = ";
cout << 10 * million * tub << "\n";
cout << "mint = " << mint << " and a million mints = ";
cout << million * mint << "\n";
return 0;
}
// fltadd.cpp -- precision problems with float
#include <iostream.h>
int main(void)
{
float a = 2.34E+22;
float b = a + 1;
cout << "a = " << a << "\n";
cout << "b - a = " << b - a << "\n";
return 0;
}
// arith.cpp -- some C++ arithmetic
#include <iostream.h>
int main(void)
{
float hats, heads;
cout << "Enter a number: ";
cin >> hats;
cout << "Enter another number: ";
cin >> heads;
cout << "hats = " << hats << "; heads = " << heads << "\n";
cout << "hats + heads = " << hats + heads << "\n";
cout << "hats - heads = " << hats - heads << "\n";
cout << "hats * heads = " << hats * heads << "\n";
cout << "hats / heads = " << hats / heads << "\n";
return 0;
}
// divide.cpp -- integer and floating-point division
#include <iostream.h>
int main(void)
{
cout.setf(ios::fixed, ios::floatfield);
cout << "Integer division: 9/5 = " << 9 / 5 << "\n";
cout << "Floating-point division: 9.0/5.0 = ";
cout << 9.0 / 5.0 << "\n";
cout << "Mixed division: 9.0/5 = " << 9.0 / 5 << "\n";
cout << "double constants: 1e7/9.0 = ";
cout << 1.e7 / 9.0 << "\n";
cout << "float constants: 1e7f/9.0f = ";
cout << 1.e7f / 9.0f << "\n";
return 0;
}
// modulus.cpp -- uses % operator to convert lbs to stone
#include <iostream.h>
int main(void)
{
const int Lbs_per_stn = 14;
int lbs;
cout << "Enter your weight in pounds: ";
cin >> lbs;
int stone = lbs / Lbs_per_stn; // whole stone
int pounds = lbs % Lbs_per_stn; // remainder in pounds
cout << lbs << " pounds are " << stone;
cout << " stone, " << pounds << " pound(s).\n";
return 0;
}
// assign.cpp -- type changes on assignment
#include <iostream.h>
int main(void)
{
float tree = 3; // int converted to float
int guess = 3.9832; // float converted to int
int debt = 3.0E12; // result not defined in C++
cout << "tree = " << tree << "\n";
cout << "guess = " << guess << "\n";
cout << "debt = " << debt << "\n";
return 0;
}
// typecast.cpp -- forcing type changes
#include <iostream.h>
int main(void)
{
int auks, bats, coots;
// the following statement adds the values as double,
// then converts the result to int
auks = 19.99 + 11.99;
// these statements add values as int
bats = (int) 19.99 + (int) 11.99; // old C syntax
coots = int (19.99) + int (11.99); // new C++ syntax
cout << "auks = " << auks << ", bats = " << bats;
cout << ", coots = " << coots << '\n';
char ch = 'Z';
cout << "The code for " << ch << " is "; // print as char
cout << int(ch) << '\n'; // print as int
return 0;
}