-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 13.4 (Completed)
303 lines (251 loc) · 6.48 KB
/
Exercise 13.4 (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
1. (Review) State whether a queue, a stack, or neither object is appropriate for each of the following
tasks, and indicate why or why not:
a. A list of customers waiting to be seated in a restaurant
A queue would work best because the customers will be seated in the order they arrive.
b. A group of student tests waiting to be graded
A stack would work best because the papers can be graded easier starting from the last person who turned in a paper
c. An address book listing names and phone numbers in alphabetical order
Neither the information has already been orginazed and there is no task be be completed
d. Patients waiting for examinations in a doctor’s office
A queue would work best because it will allow each patient to come and go in the order they arrived in.
2. (Practice) Enter and run Program 13.4.
Done!!
3. (Modify) Modify Program 13.4 to use a queue of integers rather than a queue of strings.
#include "stdafx.h"
#include <iostream>
#include <deque>
#include <string>
#include <cctype>
using namespace std;
int main()
{
int name;
deque<int> queue;
cout << "Enter as many names as you want, one per line" << endl;
cout << " To stop enter a single x" << endl;
// push names onto the queue
while (true)
{
cout << "Enter a name(or x to stop) : ";
cin >> name;
if (name == 20) break;
queue.push_front(name);
}
cout << "\nThe names in the queue are : \n";
// pop names from the queue
while (!queue.empty())
{
name = queue.back(); // retrieve the name
queue.pop_back(); // pop a name from the queue
cout << name << endl;
}
system("PAUSE");
return 0;
}
4. (Program) Write a C++ program that allows a user to enter a maximum of 20 characters in a
queue. Then have the program sort the queue contents in increasing order. For example, if the
queue’s contents are initially D, E, A, and B, its final contents are A, B, D, and E.
#include "stdafx.h"
#include < iostream >
#include < deque >
#include < queue >
#include < stack >
#include < map >
#include < string >
class CMyInts
{
public:
bool operator () (const char &lhs, const char &rhs)
{
if (lhs < rhs)
return false;
return true;
}
};
void main()
{
/*std::cout << "\n\nPriority Queue of Integer Values";
std::priority_queue < char > myInts;
myInts.push('A');
myInts.push('B');
myInts.push('G');
myInts.push('C');
while (myInts.size())
{
std::cout << "\n" << myInts.top();
myInts.pop();
}
std::cout << "\n\n";*/
std::cout << "\n\nPriority Queue of Integer Values - Sort Ascending" << std::endl;
std::priority_queue < char, std::vector < char >, CMyInts> myIntsAsc;
int MAX;
char letter;
std::cout << "Enter MAX number of elements" << std::endl;
std::cin >> MAX;
for (int i = 0; i < MAX; i++){
if (MAX > 20){
std::cout << "Not possible" << std::endl;
break;
}
std::cin >> letter;
myIntsAsc.push(letter);
}
while (myIntsAsc.size())
{
std::cout << "\n" << myIntsAsc.top();
myIntsAsc.pop();
}
std::cout << "\n\n";
system("PAUSE");
return;
}
5. (Program) Write a queue program that accepts an object consisting of an integer ID number
and a double-precision hourly pay rate.
#include "stdafx.h"
#include <iostream>
#include <deque>
#include <string>
#include <cctype>
#include <list>
using namespace std;
class IDnumhpr
{
private:
int IDnum;
double hpr;
public:
IDnumhpr(int ii, double hh){
IDnum = ii;
hpr = hh;
}
int getIDnum(){ return IDnum; }
double gethpr(){ return hpr; }
};
int main()
{
int num;
double num2;
deque<IDnumhpr> queue;
cout << "Enter as many names as you want, one per line" << endl;
cout << " To stop enter a single x" << endl;
// push names onto the queue
while (true)
{
cout << "Enter a name(or x to stop) : ";
cin >> num;
cin >> num2;
if (num == 1000 && num2 == 1000) break;
queue.push_front(IDnumhpr(num,num2));
}
cout << "\nThe names in the queue are : \n";
// pop names from the queue
while (!queue.empty())
{
cout << queue.back().getIDnum()<< endl;
cout << queue.back().gethpr() << endl;
queue.pop_back(); // pop a name from the queue
}
system("PAUSE");
return 0;
}
6. (Modify) Add a menu method to Program 13.4 that gives the user a choice of adding a name
to the queue, removing a name from the queue, or listing the queue’s contents without removing
any objects from it.
#include "stdafx.h"
#include <iostream>
#include <deque>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string name;
deque<string> queue;
cout << "Enter as many names as you want, one per line" << endl;
cout << "Menu" << endl;
cout << "Add a name by entering a string name" << endl;
cout << "Remove a name enter a single r" << endl;
cout << "List the queue's contents with/without removing any objects" << endl;
cout << "Enter a single x to exit" << endl;
// push names onto the queue
while (true)
{
cout << "Enter a name(or x to stop) : ";
getline(cin, name);
if (tolower(name.at(0)) == 'x'){ break; }
queue.push_front(name);
if (tolower(name.at(0)) == 'r'){
queue.pop_front();
}
}
cout << "\nThe names in the queue are : \n";
// pop names from the queue
while (!queue.empty())
{
name = queue.back(); // retrieve the name
queue.pop_back(); // pop a name from the queue
cout << name << endl;
}
system("PAUSE");
return 0;
}
7. (Program) A group of people have arrived at a bus stop and are lined up in this order:
1. Chaplin
2. West
3. Taylor
4. Laurel
5. Smith
6. Grisby
7. Oliver
8. Hardy
9. Burton
10. Garland
11. Wayne
12. Stewart
Read the names from an input file into a queue and display the order in which passengers
board the bus.
#include "stdafx.h"
#include <iostream>
#include <queue>
#include <string>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
string filename = "bus.dat";
string name;
ofstream outFile;
ifstream inFile;
inFile.open(filename.c_str());
outFile.open(filename.c_str());
if (outFile.fail())
{
std::cout << "The file was not successfully opened" << endl;
exit(1);
}
outFile << "Chaplin" << endl;
outFile << "West" << endl;
outFile << "Taylor" << endl;
outFile << "Laurel" << endl;
outFile << "Smith" << endl;
outFile << "Grisby" << endl;
outFile << "Oliver" << endl;
outFile << "Hardy" << endl;
outFile << "Burton" << endl;
outFile << "Garland" << endl;
outFile << "Wayne" << endl;
outFile << "Stewart";
outFile.close();
while (inFile.good()){
inFile >> name;
queue<string> q;
q.push(name);
cout << q.front() << endl;
q.pop();
}
inFile.close();
system("PAUSE");
return 0;
}