1. Int, int. int. , , , , Int.
, Int, . , , .
// lab5_1.cpp
// uses a class to model an integer data type
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Int //(not the same as int)
{
private:
int i;
public:
Int() //create an Int
{ i = 0; }
Int(int ii) //create and initialize an Int
{ i = ii; }
void add(Int i2, Int i3) //add two Ints
{ i = i2.i + i3.i; }
void display() //display an Int
{ cout << i; }
};
////////////////////////////////////////////////////////////////
int main()
{
Int Int1(7); //create and initialize an Int
Int Int2(11); //create and initialize an Int
Int Int3; //create an Int
Int3.add(Int1, Int2); //add two Ints
cout << "\nInt3 = "; Int3.display(); //display result
cout << endl;
return 0;
}
2. . 50 , , . .
toLLBooth. . , unsigned int, , , double, . . payingCar() 0,50 . , (), , . display() . , , .
, . , , , . Esc .
// lab5_2.cpp
// uses class to model toll booth
#include <iostream>
using namespace std;
#include <conio.h>
const char ESC = 27; //escape key ASCII code
const double TOLL = 0.5; //toll is 50 cents
////////////////////////////////////////////////////////////////
class tollBooth
{
private:
unsigned int totalCars; //total cars passed today
|
|
double totalCash; //total money collected today
public: //constructor
tollBooth(): totalCars(0), totalCash(0.0)
{ }
void payingCar() //a car paid
{ totalCars++; totalCash += TOLL; }
void nopayCar() //a car didn't pay
{ totalCars++; }
void display() const //display totals
{ cout << "\nCars=" << totalCars
<< ", cash=" << totalCash
<< endl; }
};
////////////////////////////////////////////////////////////////
int main()
{
tollBooth booth1; //create a toll booth
char ch;
cout << "\nPress 0 for each non-paying car,"
<< "\n 1 for each paying car,"
<< "\n Esc to exit the program.\n";
do {
ch = getche(); //get character
if(ch == '0') //if it's 0, car didn't pay
booth1.nopayCar();
if(ch == '1') //if it's 1, car paid
booth1.payingCar();
} while(ch!= ESC); //exit loop on Esc key
booth1.display(); //display totals
return 0;
}
3. time, int, , . , . , 11:59:59, , time, .
main() (, ) . , . , .
// lab5_3.cpp
// uses class to model a time data type
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class time
{
private:
int hrs, mins, secs;
public:
time(): hrs(0), mins(0), secs(0) //no-arg constructor
{ }
//3-arg constructor
time(int h, int m, int s): hrs(h), mins(m), secs(s)
{ }
void display() const //format 11:59:59
{ cout << hrs << ":" << mins << ":" << secs; }
void add_time(time t1, time t2) //add two times
{
secs = t1.secs + t2.secs; //add seconds
if(secs > 59) //if overflow,
{ secs -= 60; mins++; } // carry a minute
mins += t1.mins + t2.mins; //add minutes
if(mins > 59) //if overflow,
{ mins -= 60; hrs++; } // carry an hour
hrs += t1.hrs + t2.hrs; //add hours
}
};
////////////////////////////////////////////////////////////////
int main()
{
const time time1(5, 59, 59); //creates and initialze
const time time2(4, 30, 30); // two times
time time3; //create another time
time3.add_time(time1, time2); //add two times
cout << "time3 = "; time3.display(); //display result
cout << endl;
return 0;
}
4. employee, 4 3. int float . . main(), .
|
|
5. 5 3, date. int: month, day year. getdate() 12/31/02, showdate() .
6. employee 4, date etype (. 6 3). date . : , , . . employee, . getemploy() putemploy(), . , switch etype. main(), , .
7. . , . 149 34.8 17 31.5 . 14934.8' W, 1731.5' S. 60 ( 60 , ). 0 180 . 0 90 .
angle, : int , float char (N, S, W). , . , , , , , , 17959.9' . , , . main(), , . () '\xF8'.
8. , , 1, 2 . .
, , ( , , . , .). .
, . main(), , , : : 2 . .
9. fraction 8 3 fraction. : . 3/5 . , , . main(), , . , .
|
|
10. ship, . , 8. angle (. 7). , , , , . main(), ship, .
11. , 12 4 , fraction . , . , , . . , lowterms(), :
void fraction::lowterms() //
{
long tnum, tden, temp,gcd;
tnum = labs(num); //
tden = labs(den); // cmath
if(tden == 0) { //
cout << ; exit(1);
} else if(tnum == 0) {
num = 0;
den = 1;
return;
}
//
while(tnum!= 0) {
if(tnum < tden) { // , .
temp = tnum;
tnum = tden;
tden = temp;
}
tnum = tnum tden;
}
gcd = tden;// .
num = num / gcd;
den = den / gcd;
}
, , . , , .
12. , , . , fraction, 11. . , , 0 1. , ( , 6):
1/61/31/22/35/6
1/61/361/181/121/95/36
1/31/181/91/62/95/18
1/21/121/61/41/35/12
2/31/92/91/34/95/9
5/65/365/185/125/925/36