// Filename: TEAMMD.CPP
// Prints a table of team names and wins for three weeks
// using width-modifying conversion characters
#include <iostream.h>
void main()
{
cout.width(10); cout << "Parrots";
cout.width(10); cout << "Rams";
cout.width(10); cout << "Kings";
cout.width(10); cout << "Titans";
cout.width(10); cout << "Chargers" << endl;
cout.width(10); cout << 3;
cout.width(10); cout << 5;
cout.width(10); cout << 2;
cout.width(10); cout << 1;
cout.width(10); cout << 0 << endl;
cout.width(10); cout << 2;
cout.width(10); cout << 5;
cout.width(10); cout << 1;
cout.width(10); cout << 0;
cout.width(10); cout << 1 << endl;
cout.width(10); cout << 2;
cout.width(10); cout << 6;
cout.width(10); cout << 4;
cout.width(10); cout << 3;
cout.width(10); cout << 0 << endl;
return;
}
======================
// Filename: TESTAVG.CPP
// Computes student test averages. The program expects five
// test scores, but if the user enters -99 as a score, a
// break statement terminates the loop early.
#include <iostream.h>
void main()
{
int testScore;
int count = 0;
float avg;
do
{ cout << "Please enter the next score: ";
cin >> testScore;
cin.ignore(80, '\n'); // See last unit of Lesson 3
if (testScore == -99)
{ break; }
avg += float(testScore);
count++; // Put this in the next parentheses for efficiency
} while (count < 5); // At most five iterations
avg /= count;
cout << "The test average is " << avg << endl;
if (avg >= 90.0)
{ cout << "Congratulations on your A!" << endl; }
return;
}
======================
// Filename : THIS.CPP
// A simple demonstration of using the this pointer
//
#include <iostream.h>
class Search
{
public:
int IsTheSameAs(Search * s)
{
if (s == this)
return 1;
else
return 0;
}
};
void main()
{
Search s[5];
Search * test = &s[3];
for (int i = 0; i < 5; i++)
{
if (s[i].IsTheSameAs(test))
{
cout << "The index is " << i;
break;
}
}
}
===================
// Filename: TIME1.CPP
// Tests the time() function
#include <time.h>
#include <iostream.h>
void main()
{
time_t lt;
// Passing time() a NULL pointer makes it
// return the time in seconds
lt = time(NULL); // NULL is defined in stdio.h
cout << "The number of seconds since 1-1-1970: " << lt;
return;
}
=======================
// Filename: TIME2.CPP
// Printing date and time values
#include <iostream.h>
#include <time.h>
void main()
{
struct tm *local; // Pointer to the time structure
time_t t;
t = time(NULL); // Store seconds time in t
local = localtime(&t); // Fills the structure-pointed data
cout << "The current seconds are " << local->tm_sec << endl;
cout << "The current minutes are " << local->tm_min << endl;
cout << "The current hours are " << local->tm_hour << endl;
cout << "The current day is " << local->tm_mday << endl;
cout << "The current month is " << local->tm_mon << endl;
cout << "The current year (since 1900) is "
<< local->tm_year << endl;
cout << "The current weekday is " << local->tm_wday << endl;
cout << "The current day of year is "
<< local->tm_yday << endl;
cout << "The current Daylight Savings Flag is "
<< local->tm_isdst << endl;
// DST is 0 in during winter months, 1 otherwise
return;
}
=======================
// Filename: TRCKMATH.CPP
// Computes several layers of expressions
#include <iostream.h>
void main()
{
int x;
x = 2 + 5 * 2 - 1 % 2;
x += 14;
x -= 5;
x *= 2 - 7;
cout << "The value of x is " << x << endl;
return;
}
=========================
// Filename: TYPECAST.CPP
// Applies the typecast operator to a single expression
// in several different ways to show the different results
#include <iostream.h>
void main()
{
double answer; // Make variable large to hold any precision
int i = 9;
float f = 7.8;
double d = 16.4;
// Apply the typecast in several ways
answer = (i * f * d);
cout << "The answer after (i * f * d) is "
<< answer << endl;
answer = float(i * f * d); // The typecast
cout << "The answer after float(i * f * d) is "
<< answer << endl;
answer = (i * int(f) * int(d));
cout << "The answer after (i * int(f) * int(d)) is "
<< answer << endl;
answer = int(i * f * d);
cout << "The answer after int(i * f * d) is "
<< answer << endl;
answer = float(i * f * d);
cout << "The answer after float(i * f * d) is "
<< answer << endl;
return;
}
Tidak ada komentar:
Posting Komentar