#include <iostream> // >>, <<, cin, cout
using namespace std;
#include "Temperature.h" // Temperature
int main()
{
cout << "This program shows the Fahrenheit, Celsius, and\n"
<< "Kelvin equivalents of a temperature.\n\n";
char response;
Temperature theTemperature; // construction
do
{
cout << "Enter a temperature (e.g., 98.6 F): ";
theTemperature.read(cin); // input
cout << "--> "; // output
theTemperature.inFahrenheit().print(cout);
cout << " == ";
theTemperature.inCelsius().print(cout);
cout << " == ";
theTemperature.inKelvin().print(cout);
cout << endl;
cout << "\nDo you have more temperatures to convert? ";
cin >> response;
}
while (response == 'Y' || response == 'y');
}
|
This program shows the Fahrenheit, Celsius, and Kelvin equivalents of a temperature. Enter a temperature (e.g., 98.6 F): 212 F --> 212 F == 100 C == 373.15 K Do you have more temperatures to convert? Y Enter a temperature (e.g., 98.6 F): 0 C --> 32 F == 0 C == 273.15 K Do you have more temperatures to convert? Y Enter a temperature (e.g., 98.6 F): 100 K --> -279.67 F == -173.15 C == 100 K Do you have more temperatures to convert? N |
#include "Temperature.h" // class Temperature
// -------- Utility method -----------------------------------
bool Temperature::isValid(double degrees, char scale)
{
switch (scale)
{
case 'F': case 'f': // Fahrenheit
return degrees >= MIN_FAHRENHEIT;
case 'C': case 'c': // Celsius
return degrees >= MIN_CELSIUS;
case 'K': case 'k': // Kelvin
return degrees >= MIN_KELVIN;
default: // otherwise, invalid
return false;
}
}
|
#include "Temperature.h" // class Temperature
#include <cctype> // islower(), toupper()
#include <cstdlib> // exit()
using namespace std;
// -------- Explicit-value constructor --------------------------
Temperature::Temperature(double initialDegrees, char initialScale)
{
if ( isValid(initialDegrees, initialScale) )
{
if (islower(initialScale)) // if scale is lowercase
initialScale = toupper(initialScale); // convert it to uppercase
myDegrees = initialDegrees; // proceed with
myScale = initialScale; // initialization
} // otherwise, error msg
else
{
cerr << "\n*** Temperature constructor received invalid params "
<< initialDegrees << ' ' << initialScale << endl;
exit(1);
}
}
|
void Temperature::read(istream& in)
{
double inDegrees; // temporary variables to
char inScale; // store the input values
in >> inDegrees >> inScale; // read values from in
if ( isValid(inDegrees, inScale) ) // if they're valid
{
if ( islower(inScale) ) // if scale is lower case
inScale = toupper(inScale); // convert it to upper case
myScale = inScale; // assign input values
myDegrees = inDegrees; // to instance variables
}
else // otherwise
in.setstate(ios::failbit); // set fail bit in stream
}
|
Temperature Temperature::inFahrenheit() const
{
switch (myScale)
{
case 'F':
return Temperature(myDegrees, 'F');
case 'C':
return Temperature(myDegrees * 1.8 + 32.0, 'F');
case 'K':
return Temperature((myDegrees - 273.15) * 1.8 + 32.0, 'F');
}
}
|
bool Temperature::operator<(const Temperature& rightOperand) const
{
Temperature localTemp; // the equivalent of rightOperand,
// but in my scale
switch (myScale)
{
case 'C': localTemp = rightOperand.inCelsius();
break;
case 'F': localTemp = rightOperand.inFahrenheit();
break;
case 'K': localTemp = rightOperand.inKelvin();
break;
}
return myDegrees < localTemp.getDegrees();
}
|
bool Temperature::operator==(const Temperature& rightOperand) const
{
Temperature localTemp; // the equivalent of rightOperand,
// but in my scale
switch (myScale)
{
case 'C': localTemp = rightOperand.inCelsius();
break;
case 'F': localTemp = rightOperand.inFahrenheit();
break;
case 'K': localTemp = rightOperand.inKelvin();
break;
}
return myDegrees == localTemp.getDegrees();
}
|
#ifndef TEMPERATURE
#define TEMPERATURE
#include <iostream> // istream, ostream
using namespace std;
const double MIN_FAHRENHEIT = -459.67;
const double MIN_CELSIUS = -273.15;
const double MIN_KELVIN = 0.0;
class Temperature
{
public: // The class interface
Temperature();
Temperature(double initialDegrees, char initialScale);
double getDegrees() const;
char getScale() const;
Temperature inFahrenheit() const;
Temperature inCelsius() const;
Temperature inKelvin() const;
bool operator< (const Temperature& rightOperand) const;
bool operator== (const Temperature& rightOperand) const;
// ... other relational operators omitted ...
void print(ostream& out) const;
void read(istream& in);
static bool isValid(double degrees, char scale);
private: // Class invariant:
double myDegrees; // myScale == 'F' && myDegrees >= -459.67
char myScale; // || myScale == 'C' && myDegrees >= -273.15
// || myScale == 'K' && myDegrees >= 0.0
};
// ... Definitions of inline operations go here ...
#endif
|
#ifndef STUDENT // compile-once #define STUDENT // wrapper #include <iostream> // istream, ostream #include <string> // string using namespace std; class Student
{
public: // The Interface
// constructors
Student();
Student(long idNumber, const string & firstName,
const string & lastName, const string & year,
double credits, double gpa);
// accessors
long getID() const;
string getFirstName() const;
// ... Add getLastName(), getYear(), getCredits(), and getGPA()
// relational ops
bool operator== (const Student& rightOperand) const;
bool operator!= (const Student& rightOperand) const;
// ... Add operators <, >, <=, and >=.
// ... See the exercises at the end of this section for
// ... information about these operators.
// I/O
void read(istream& in);
void print(ostream& out) const;
private: // Implementation Details
// Examples:
long myIDNumber; // 123456789
string myFirstName, // Jane
myLastName, // Doe
myYear; // Senior
double myCredits, // 15.0
myGPA; // 3.75
};
// ** Put definitions of all accessors and relational operators here.
// ...
// ** Definitions of the constructors and the I/O methods are more
// ** complicated and should be put in Student.cpp.
#endif
|
// ... #includes go here ...
void fill(vector<Student>& sVec, const string& fileName);
int main()
{
const string INPUT_FILE = "students.txt";
cout << "This program provides an information retrieval system\n"
<<" by reading a series of student records from "
<< '\'' << INPUT_FILE << '\''
<< "\n and then allowing retrieval of any student's data.\n";
// 1. Declare studentVec and use fill() to read Students into it // 2. Loop: // a. Prompt for and read an integer studentID // b. Search studentVec for Student with that studentID // c. If such a Student object was found // Display the Student object // Otherwise // Display a "not found" message } // ... Definition of fill() goes here |
111223333 Bill Board Freshman 16.0 3.15 666554444 Jose CanuSee Sophomore 16.0 3.25 777889999 Ben Dover Junior 16.0 2.5 333221111 Stan Dupp Senior 8.0 3.75 444556666 Ellie Kat Senior 16.0 3.125 999887777 Isabelle Ringing Junior 16.0 3.8 |
This program provides an information retrieval system by reading a series of student records from 'students.dat' and then allowing retrieval of any student's data. Enter the ID # of a student (eof to quit): 333221111 333221111 Stan Dupp Senior 16.0000 3.7500 Enter the ID # of a student (eof to quit): 123456789 There is no student with ID # 123456789. Enter the ID # of a student (eof to quit): 999887777 999887777 Ringing, Isabelle Junior 8.0000 3.8000 Enter the ID # of a student (eof to quit): ^D |
#include <iostream> // >>, <<, cin, cout
using namespace std;
#include "Temperature.h" // Temperature
int main()
{
cout << "This program shows the Fahrenheit, Celsius, and\n"
<<"Kelvin equivalents of a temperature.\n\n";
char response;
Temperature theTemperature; // construction
do
{
cout << "Enter a temperature (e.g., 98.6 F): ";
cin >> theTemperature; // input
cout << "--> " // output
<< theTemperature.inFahrenheit()
<< " == "
<< theTemperature.inCelsius()
<< " == "
<< theTemperature.inKelvin()
<< endl;
cout << "\nDo you have more temperatures to convert? ";
cin >> response;
}
while (response == 'Y' || response == 'y');
}
|