-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 11.3 (Completed)
433 lines (388 loc) · 10.8 KB
/
Exercise 11.3 (Completed)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
1. (For review) a. Define the three new data type conversions introduced in this section and
the method of performing each conversion.
Conversion from a class type to a built-in type
Conversion from a built-in type to a class type
Conversion from a class type to a class type
b. Define the terms “type conversion constructor” and “conversion operator function” and
describe how they’re used in user-defined conversions.
type conversion constructor - is any constructor whose first argument is not a member of its
class and whose remaining arguments, if any, have default values.
conversion operator function - Converting from a class data type to a class data type is done in the same manner as a conversion
from a class to a built-in data type.
2. (Program) Write a C++ program that declares a class named Time having integer data members
named hours, minutes, and seconds. Include a type conversion constructor that converts
a long integer, representing the elapsed seconds from midnight, into an equivalent representation
as hours:minutes:seconds. For example, the long integer 30336 should convert to the time
8:25:36. Use military time—for example, 2:30 p.m. is represented as 14:30:00. The relationship
between time representations is as follows:
elapsed seconds = hours × 3600 + minutes × 60 + seconds
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
// class declaration section
class Time
{
private:
int hours;
int minutes;
int seconds;
public:
Time(int = 7, int = 4, int = 20); // constructor
Time (long);
void showTime(); // method to display a Time
};
// class implementation section
Time::Time(int hh, int mm, int ss)
{
hours = hh;
minutes = mm;
seconds = ss;
}
void Time::showTime()
{
cout << setfill('0')
<< setw(2) << hours << ':'
<< setw(2) << minutes<< ':'
<< setw(2) << seconds;
return;
}
//conversion operator definition for converting a long int to a Time
Time::Time(long findTime)
{
hours = int(findTime / 3600);
minutes = int((findTime - hours * 3600) / 60);
seconds = int(findTime - hours * 3600 - minutes * 60);
}
int main()
{
Time oldTime(30336L), newTime;
newTime = Time(oldTime);
newTime.showTime();
cout << endl;
system("pause");
return 0;
}
3. (Program) A Julian date is represented as the number of days from a known base date. The
following pseudocode shows one algorithm for converting from a Gregorian date, in the form
month/day/year, to a Julian date with a base date of 00/00/0000. All calculations in this algorithm
use integer arithmetic, which means the fractional part of all divisions must be discarded. In
this algorithm, M = month, D = day, and Y = year.
If M is less than or equal to 2
Set the variable MP = 0 and YP = Y - 1
Else
Set MP = int(0.4 × M + 2.3) and YP = Y
EndIf
T = int(YP / 4) - int(YP / 100) + int(YP / 400)
Julian date = 365 × Y + 31 × (M - 1) + D + T - MP
Using this algorithm, modify Program 11.7 to cast from a Gregorian Date object to its corresponding
Julian representation as a long integer. Test your program by using the Gregorian
dates 1/31/2011 and 3/16/2012, which correspond to the Julian dates 734533 and 734944.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
// class declaration section
class Date
{
private:
int month;
int day;
int year;
public:
Date(int = 3, int = 16, int = 2012); // constructor
operator long();
void showDate(); // method to display a Date
};
// class implementation section
Date::Date(int mm, int dd, int yy)
{
month = mm;
day = dd;
year = yy;
}
void Date::showDate()
{
cout << setfill('0')
<< setw(2) << month << '/'
<< setw(2) << day<< '/'
<< setw(2) << year;
return;
}
//conversion operator definition for converting a long int to a Date
Date::operator long ()
{
long yymmdd;
int mp, yp,t, julian;
if (month <= 2){
mp = 0;
yp = year;
}
else{ mp = int(.04 * month + 2.3);
yp = year;
}
t = int(yp / 4) - int(yp / 100) + int(yp / 400);
julian = 365 * year + 31 * (month - 1) + day + t - mp;
return(julian);
}
int main()
{
Date Gregorian(1, 31, 2011), Gregorian2(3,16,2012);
long Julian, Julian2;
Julian = long(Gregorian);
Julian2 = long(Gregorian2);
cout << "The Gregorian date is" << endl;
Gregorian.showDate();
cout << endl;
Gregorian2.showDate();
cout << endl;
cout << "This date, as a Julian integer, is " << Julian << " and " << Julian2 << endl;
cout << endl;
system("pause");
return 0;
}
4. (Modify) Modify the program written for Exercise 2 to include a conversion operator function
that converts an object of type Time into a long integer representing the number of seconds
from midnight.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
// class declaration section
class Time
{
private:
int hours;
int minutes;
int seconds;
public:
Time(int = 7, int = 4, int = 20); // constructor
operator long();
void showTime(); // method to display a Time
};
// class implementation section
Time::Time(int hh, int mm, int ss)
{
hours = hh;
minutes = mm;
seconds = ss;
}
void Time::showTime()
{
cout << setfill('0')
<< setw(2) << hours << ':'
<< setw(2) << minutes << ':'
<< setw(2) << seconds;
return;
}
//conversion operator definition for converting a long int to a Time
Time::operator long ()
{
long elapsedsec = hours * 3600 + minutes * 60 + seconds;
return elapsedsec;
}
int main()
{
Time oldTime(7, 23, 20);
long newTime;
newTime = long(oldTime);
cout << "This time, as a long integer from midnight is " << newTime << endl;
cout << endl;
system("pause");
return 0;
}
5. (Program) Write a C++ program that has a Date class and a Julian class. The Date class
should be the same Date class used in Program 11.6, and the Julian class should represent a
date as a long integer. For this program, include a conversion operator function in the Date
class that converts a Date object to a Julian object, using the algorithm shown in Exercise 3.
Test your program by converting 1/31/2011 and 3/16/2012, which correspond to the Julian
dates 734533 and 734944.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
// forward declaration of class Intdate
class Intdate;
// class declaration section for Date
class Date
{
private:
int month, day, year;
public:
Date(int = 7, int = 4, int = 2012); // constructor
operator Intdate(); // conversion operator from Date to Intdate
void showDate();
};
// class declaration section for Intdate
class Intdate
{
private:
long yyyymmdd;
public:
Intdate(long = 0); // constructor
operator Date(); // conversion operator from Intdate to Date
void showint();
};
// class implementation section for Date
Date::Date(int mm, int dd, int yyyy) // constructor
{
month = mm;
day = dd;
year = yyyy;
}
// conversion operator function converting from Date to Intdate class
Date::operator Intdate() // must return an Intdate object
{
long julian;
int mp, yp, t;
if (month <= 2){
mp = 0;
yp = year;
}
else{
mp = int(.04 * month + 2.3);
yp = year;
}
t = int(yp / 4) - int(yp / 100) + int(yp / 400);
julian = 365 * year + 31 * (month - 1) + day + t - mp;
return(Intdate(julian));
}
// member function to display a Date
void Date::showDate()
{
cout << setfill('0')
<< setw(2) << month << '/'
<< setw(2) << day << '/'
<< setw(2) << year % 100;
return;
}
// class implementation section for Intdate
Intdate::Intdate(long ymd) // constructor
{
yyyymmdd = ymd;
}
// conversion operator function converting from Intdate to Date class
Intdate::operator Date() // must return a Date object
{
int mo, da, yr;
yr = int(yyyymmdd / 10000.0);
mo = int((yyyymmdd - yr * 10000.0) / 100.0);
da = int(yyyymmdd - yr * 10000.0 - mo * 100.0);
return(Date(mo, da, yr));
}
// member function to display an Intdate
void Intdate::showint()
{
cout << yyyymmdd;
return;
}
int main()
{
Date a(1, 31, 2011), b(3,16,2012); // declare two Date objects
Intdate c, d; // declare two Intdate objects
c = Intdate(b); // cast c into a Date object
d = Intdate(a); // cast a into an Intdate object
cout << " a's date is ";
a.showDate();
cout << "\n as an Intdate object this date is ";
d.showint();
cout << "\n b's date is ";
b.showDate();
cout << "\n as a Intdate object this date is ";
c.showint();
cout << endl;
system("PAUSE");
return 0;
}
6. (Program) Write a C++ program that has a Time class and an Ltime class. The Time class
should have integer data members named hours, minutes, and seconds, and the Ltime class
should have a long integer data member named elsecs, which represents the number of
elapsed seconds since midnight. For the Time class, include a conversion operator function
named Ltime() that converts a Time object to an Ltime object. For the Ltime class, include
a conversion operator function named Time() that converts an Ltime object to a Time object.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
// forward declaration of class Intdate
class Ltime;
// class declaration section for Date
class Time
{
private:
int hours, minutes, seconds;
public:
Time(int = 7, int = 4, int = 20); // constructor
operator Ltime(); // conversion operator from Date to Intdate
void showTime();
};
// class declaration section for Intdate
class Ltime
{
private:
long hhmmss;
public:
Ltime(long = 0); // constructor
operator Time(); // conversion operator from Intdate to Date
void showint();
};
// class implementation section for Date
Time::Time(int hh, int mm, int ss) // constructor
{
hours = hh;
minutes = mm;
seconds = ss;
}
// conversion operator function converting from Date to Intdate class
Time::operator Ltime() // must return an Intdate object
{
long elapsedsec = hours * 3600 + minutes * 60 + seconds;
return(Ltime(elapsedsec));
}
// member function to display a Date
void Time::showTime()
{
cout << setfill('0')
<< setw(2) << hours << ':'
<< setw(2) << minutes << ':'
<< setw(2) << seconds;
return;
}
// class implementation section for Intdate
Ltime::Ltime(long hms) // constructor
{
hhmmss = hms;
}
// conversion operator function converting from Intdate to Date class
Ltime::operator Time() // must return a Date object
{
long hours, minutes, seconds;
hours = int(hhmmss / 3600);
minutes = int((hhmmss - hours * 3600) / 60);
seconds = int(hhmmss - hours * 3600 - minutes * 60);
return(Time(hours, minutes, seconds));
}
// member function to display an Intdate
void Ltime::showint()
{
cout << hhmmss;
return;
}
int main()
{
Time a(1, 31, 20), b; // declare two Date objects
Ltime c, d; // declare two Intdate objects
c = Time(b); // cast c into a Date object
d = Ltime(a); // cast a into an Intdate object
cout << " a's date is ";
a.showTime();
cout << "\n as an Time object this time is ";
d.showint();
cout << "\n c's time is ";
c.showint();
cout << "\n as a Time object this time is ";
b.showTime();
cout << endl;
system("PAUSE");
return 0;
}