📄 pex3_11.cpp
字号:
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
enum unit {metric, English};
class Height
{
private:
char name[20]; // person's name
unit measuretype; // measurement type used
float h; // height in measuretype units (ft. or cm.)
public:
// constructor
Height(char nm[] = "", float ht = 0.0, unit m = metric);
// I/O operations
void PrintHeight(void) const;
void ReadHeight(void);
// data access methods
float GetHeight(void) const;
void Convert(unit m);
};
Height::Height(char nm[], float ht, unit m): h(ht), measuretype(m)
{
// use C++ string library function to copy nm to name
strcpy(name,nm);
}
// prints height in appropriate unit
void Height::PrintHeight(void) const
{
cout << name << " ";
cout << h << (measuretype == metric ? " centimeters":" inches") << endl;
}
// input name, measurement type and height
void Height::ReadHeight(void)
{
char c;
cout << "Enter name: ";
cin.getline(name,20,'\n');
cout << "Enter measurement type (M or E) and height: ";
cin >> c >> h;
if (c == 'M')
measuretype = metric;
else
measuretype = English;
// clear through end of line
do
cin.get(c);
while (c != '\n');
}
// return height
float Height::GetHeight(void) const
{
return h;
}
// convert h to measurement m
void Height::Convert(unit m)
{
if (measuretype == m)
return;
if (m == metric)
h = h * 2.54;
else
h = h/2.54;
measuretype = m;
}
// convert the list a to measurement m.
// all units of a are assumed to be in the same unit of measurement
void ConvertList(Height a[], int n, unit m)
{
for(int i=0;i < n;i++)
a[i].Convert(m);
}
// sort the n element Height array a in ascending order.
// all units of a are assumed to be in the same unit of measurement
void Sort(Height a[], int n)
{
int i, j;
Height tmp;
// implement n-1 passes
for(i = 0; i < n-1; i++)
// put object with maximum height within the collection
// a[i+1]...a[n-1] in a[i]
for(j = i+1; j < n; j++)
// exchange if a[i].GetHeight() > a[j].GetHeight()
if (a[i].GetHeight() > a[j].GetHeight())
{
tmp = a[i];
a[i]=a[j];
a[j] = tmp;
}
}
// compute and return the tallest person in list a. all elements
// of a are assumed to be in the same unit of measurement
Height TallestPerson(Height a[], int n)
{
Height max = a[0];
for(int i=1;i < n;i++)
if (a[i].GetHeight() > max.GetHeight())
max = a[i];
return max;
}
void main(void)
{
Height person[5] = {
Height("George Wilson", 67, English),
Height("Andrea Borden", 70, English),
Height("Sandra Dealba", 64, English) };
Height tallperson;
cout.setf(ios::fixed | ios::showpoint);
cout.precision(1);
// read the last to array entries
person[3].ReadHeight();
person[4].ReadHeight();
// print the original list
cout << endl << "Original list:" << endl;
for(int i=0;i < 5;i++)
person[i].PrintHeight();
// convert all list elements to metric
ConvertList(person,5,metric);
// print the list after conversion to metric
cout << endl << "List after converting to metric:" << endl;
for(i=0;i < 5;i++)
person[i].PrintHeight();
// print the tallest person in the list
cout << endl << "Tallest person: ";
tallperson = TallestPerson(person,5);
tallperson.PrintHeight();
// sort the list of metric heights
Sort(person, 5);
// print the sorted list
cout << endl << "The sorted list:" << endl;
for(i=0;i < 5;i++)
person[i].PrintHeight();
}
/*
<Run>
Enter name: Bill McPherson
Enter measurement type (M or E) and height: E 75
Enter name: Shannon Jackson
Enter measurement type (M or E) and height: E 72
Original list:
George Wilson 67.0 inches
Andrea Borden 70.0 inches
Sandra Dealba 64.0 inches
Bill McPherson 75.0 inches
Shannon Jackson 72.0 inches
List after converting to metric:
George Wilson 170.2 centimeters
Andrea Borden 177.8 centimeters
Sandra Dealba 162.6 centimeters
Bill McPherson 190.5 centimeters
Shannon Jackson 182.9 centimeters
Tallest person: Bill McPherson 190.5 centimeters
The sorted list:
Sandra Dealba 162.6 centimeters
George Wilson 170.2 centimeters
Andrea Borden 177.8 centimeters
Shannon Jackson 182.9 centimeters
Bill McPherson 190.5 centimeters
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -