// ;----------------------------------------------------------- // ; Program Name: Class_Demo // ; File: Demoprob2.cpp // ; Description: Uses Sizeof, Setf, Precision, Width, Overflow // ; Author: Jim Adams // ; Environment: MS C++ 6.0 // ; Date Written: 10/02/2000 // ;----------------------------------------------------------- #define display cout << #define input cin >> #include #include // Variable definitions char status_byte ='Q'; int counter =0; long dummy =0; short i =0; short test_variable =0; short mask =0; long distance =100000; float cost =(float) 123456.66; double vector =654321.543211; // Start of main logic section void main(void) { cout << "This program demonstrates the sizeof operator" << endl; cout << " Integer Data Type:" << sizeof(counter) << endl; cout << " Float Data Type:" << sizeof(vector) << endl; cout << " Double Data Type:" << sizeof(vector) << endl; cout << "Character Data Type:" << sizeof(status_byte) << endl; cout << " Short Data Type:" << sizeof(short) << endl; cout << " Long Data Type:" << sizeof(dummy) << endl; // Using #defines to redefine the language display "Enter a single character:"; input status_byte; status_byte = status_byte + 1; display status_byte << endl; // Try the I/O Format routines cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(5); cout << "Vector: " << vector << endl; cout.precision(4); cout << "Vector: " << vector << endl; cout.precision(3); cout << "Vector: " << vector << endl; cout.precision(2); cout << "Vector: " << vector << endl; cout.precision(1); cout.setf(ios::showpos); cout << "Vector: " << vector << endl; cout.precision(0); cout << "Vector: " << vector << endl; // Try a loop and a division operator counter=0; while (counter <= 8) { vector = vector / 2; counter++; } cout << "New value of vector:" << vector << endl; // Work with overflow examples test_variable = 32760; while (i < 10) { cout << test_variable++ << endl; i++; } // Display the bits of a number // Use logical bitwise AND operator test_variable = 32767; mask=32768; i=15; cout << test_variable << " base 10 is "; while (i>=0) { if ((test_variable & mask) == 0) cout << "0 "; else cout << "1 "; i--; mask=pow(2,i); } cout << "in binary " << endl; }