-
Notifications
You must be signed in to change notification settings - Fork 0
/
vv
493 lines (356 loc) · 11.9 KB
/
vv
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
11111 #include <iostream>
using namespace std;
int main() {
int someNumbers[7] = {3, 7, 2, 6, 5, 8, 5 };
int i = 6;
do {
cout << someNumbers[i] << endl;
--i;
} while(i >= 0); // Correct the loop condition
return 0;
}
#include <iostream>
using namespace std;
int main() {
int someNumbers[7] = {1, 1, 2, 3, 5, 8, 5 };
for (int i = 6; i >= 0; --i) {
cout << someNumbers[i] << endl;
}
return 0;
}
2222222 #include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
vector<string> words;
string input;
cout << "Enter words (type ZZZ to stop):" << endl;
while (true) {
cin >> input;
if (input == "ZZZ") {
break;
} else {
if (input != "ZZZ") {
input[0] = toupper(input[0]); // Convert the first character to uppercase
words.push_back(input);
}
}
}
int size = words.size();
if (size == 0) {
cout << "There is no middle word" << endl;
} else if (size % 2 == 1){
int middleIndex = size / 2;
cout << "The middle word is " << words[middleIndex] << endl;
} else {
cout << "The two early words are " << words[0] << " " << words[1] << endl; // Add space between the two words
}
return 0;
}
33333
Write function prototypes appropriate to these comments:
/家*
* openInputFile
* \param[in, out]
* \param[in]
* \precondition
* \postcondition
* \return
**/
opens a file for input based on the given filename ifs is the stream to be opened
filename a string containing the filename ifs is declared in the calling function
ifs is in a good state for reading data (if successful) bool true if the operation was successful
* openOutputFile
opens a file for input based on the given filename
* \param[in, out]
ofs is the stream to be opened
* Iparam[in]
filename a string containing the filename
* \precondition
ofs is declared in the calling function
* \postcondition
ofs is in a good state for writing data (if successful)
* \return
bool true if the operation was shrcessful
ま/
/=8
* writeFile
param[in, out]
Sparam[in]
\precondition postcondition
writes the contents of the uniquelordList to an output
file stream ofs is the target stream to write uniquewordlist is an unordered_map‹string, int>
to be written ofs is declared in the calling function and in good state ofs has recieved the uniqueWordlist and is in a good state (if successful)
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <string>
/**
* Opens a file for input based on the given filename.
* \param[in, out] ifs The stream to be opened.
* \param[in] filename A string containing the filename.
* \precondition ifs is declared in the calling function.
* \postcondition ifs is in a good state for reading data (if successful).
* \return bool True if the operation was successful.
*/
bool openInputFile(std::ifstream& ifs, const std::string& filename);
/**
* Opens a file for output based on the given filename.
* \param[in, out] ofs The stream to be opened.
* \param[in] filename A string containing the filename.
* \precondition ofs is declared in the calling function.
* \postcondition ofs is in a good state for writing data (if successful).
* \return bool True if the operation was successful.
*/
bool openOutputFile(std::ofstream& ofs, const std::string& filename);
/**
* Writes the contents of the uniqueWordList to an output file stream in CSV format.
* Each word and its count appear on individual lines with the word and count separated by a comma.
* For example:
* four,4
* three,3
* two,2
* one,1
* \param[in, out] ofs The target stream to write in CSV format.
* \param[in] uniqueWordList An unordered_map<string, int> to be written.
* \precondition ofs is declared in the calling function and in good state.
* \postcondition ofs has received the uniqueWordList and is in a good state (if successful).
*/
void writeCSVFile(std::ofstream& ofs, const std::unordered_map<std::string, int>& uniqueWordList) {
for (const auto& entry : uniqueWordList) {
ofs << entry.first << "," << entry.second << std::endl;
}
}
int main() {
std::ifstream inputFileStream;
std::ofstream outputFileStream;
std::unordered_map<std::string, int> uniqueWordList;
if (openInputFile(inputFileStream, "input.txt")) {
// Process input and populate uniqueWordList
// ...
if (openOutputFile(outputFileStream, "output.csv")) {
writeCSVFile(outputFileStream, uniqueWordList);
outputFileStream.close();
} else {
std::cerr << "Error opening output file." << std::endl;
}
inputFileStream.close();
} else {
std::cerr << "Error opening input file." << std::endl;
}
return 0;
}
44444
#include <fstream>
ifstream inputFile;
inputFile.open("input.txt");
if (inputFile.is_open()) {
// Read from the file using inputFile
// ...
inputFile.close();
} else {
// Handle the case when the file couldn't be opened
}
55555
Write the code to:
• declare an appropriate file stream variable for output
• call the openouputFile() function correctly
• call the writefile() function if openouputFile ( worked
Assume that the program will always open the file "sample.csv do not change function name
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <string>
bool openOutputFile(std::ofstream& ofs, const std::string& filename);
void writeCSVFile(std::ofstream& ofs, const std::unordered_map<std::string, int>& uniqueWordList) {
for (const auto& entry : uniqueWordList) {
ofs << entry.first << "," << entry.second << std::endl;
}
}
int main() {
std::ofstream outputFileStream("sample.csv");
if (outputFileStream.is_open()) {
std::unordered_map<std::string, int> uniqueWordList;
// ... (populate uniqueWordList with data)
writeCSVFile(outputFileStream, uniqueWordList);
outputFileStream.close();
} else {
std::cerr << "Error opening output file." << std::endl;
}
return 0;
}
6666666
Write the definition code for the openinputFile() function. Recall the comments for this function:
* openInputFile
opens a file for input based on the given filename
* \param[in,out] ifs is the stream to be opened
* \param[in]
filename a string containing the filename
* \precondition
ifs is declared in the calling function
* \postcondition
ifs is in a good state for reading data (if successful)
* (return
bool true if the operation was successful
#include <fstream>
bool openInputFile(std::ifstream& ifs, const std::string& filename) {
ifs.open(filename);
if (ifs.is_open()) {
return true;
} else {
std::cerr << "Error opening input file: " << filename << std::endl;
return false;
}
}
777777
Write the definition code for the openinputfile) function. Recall the comments for this function:
* openoutputFile opens a file for input based on the given filenane
* \paran[in, out]
ofs is the stream to be opened
* \param[in]
filename a strins containing the filename
• \precondition
ofs is declaned in the calling function
• \postcondition
ofs is in a good state for writing data (if successful)
* return
bool true if the operation was successful
#include <fstream>
bool openOutputFile(std::ofstream& ofs, const std::string& filename) {
ofs.open(filename);
if (ofs.is_open()) {
return true;
} else {
std::cerr << "Error opening output file: " << filename << std::endl;
return false;
}
}
88888888
Write the definition code for the openinputFiled function. Recall the comments for this function
* writerile
* (param[in,out]
* \param[in]
* \precondition
# \postcondition
* (return
writes the contents of the uniquewordlist to an output file stream ofs is the target stream to write uniquewordList is an unordered_map‹string, int> to be written ofs is declared in the calling function and in good state ofs has recieved the uniquelordlist and is in a good state (if successful) nothing
• The format of the file should be comma separated value (CSV) format.
* Each word and its count should appear on individual lines with the
* word and the count separated byla comma.
* For example:
* Four, 4.
*three,3
* two, 2
• one, 1
#include <fstream>
#include <unordered_map>
bool openOutputFile(std::ofstream& ofs, const std::string& filename) {
ofs.open(filename);
if (ofs.is_open()) {
return true;
} else {
std::cerr << "Error opening output file: " << filename << std::endl;
return false;
}
}
void writeFile(std::ofstream& ofs, const std::unordered_map<std::string, int>& uniqueWordList) {
auto it = uniqueWordList.begin();
while (it != uniqueWordList.end()) {
ofs << it->first << "," << it->second << std::endl;
++it;
}
}
int main() {
std::ofstream outputFileStream;
if (openOutputFile(outputFileStream, "output.csv")) {
std::unordered_map<std::string, int> uniqueWordList;
// Populate uniqueWordList (assume it is filled with data)
writeFile(outputFileStream, uniqueWordList);
outputFileStream.close();
} else {
std::cerr << "Error opening output file." << std::endl;
}
return 0;
}
#include <fstream>
#include <unordered_map>
bool openOutputFile(std::ofstream& ofs, const std::string& filename) {
ofs.open(filename);
if (ofs.is_open()) {
return true;
} else {
std::cerr << "Error opening output file: " << filename << std::endl;
return false;
}
}
void writeFile(std::ofstream& ofs, const std::unordered_map<std::string, int>& uniqueWordList) {
for (const auto& entry : uniqueWordList) {
ofs << entry.first << "," << entry.second << std::endl;
}
}
int main() {
std::ofstream outputFileStream;
if (openOutputFile(outputFileStream, "output.csv")) {
std::unordered_map<std::string, int> uniqueWordList;
// Populate uniqueWordList (assume it is filled with data)
writeFile(outputFileStream, uniqueWordList);
outputFileStream.close();
} else {
std::cerr << "Error opening output file." << std::endl;
}
return 0;
}
999999999
#pragma once
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct Record {
string title;
string artist;
int year;
};
class Library {
public:
Library(int splitYear); // Constructor
void add(const Record& newRecord); // Function to add a new Record
bool writeToFileNamed(const string& fileName); // Function to write to a file
// Additional member functions can be declared here
private:
int splitYear; // Member variable to store the split year
vector<Record> modernRecords; // List for modern Records
vector<Record> classicRecords; // List for classic Records
};
101010101010101010101010
#include "Library.h"
Library::Library(int splitYear) : splitYear(splitYear) {
// Constructor implementation
}
void Library::add(const Record& newRecord) {
if (newRecord.year >= splitYear) {
// Add to the modernRecords list
modernRecords.push_back(newRecord);
} else {
// Add to the classicRecords list
classicRecords.push_back(newRecord);
}
}
bool Library::writeToFileNamed(const string& fileName) {
ofstream outputFile(fileName);
if (outputFile.is_open()) {
// Write modernRecords to the file
for (const auto& record : modernRecords) {
outputFile << record.title << "," << record.artist << "," << record.year << endl;
}
// Write classicRecords to the file
for (const auto& record : classicRecords) {
outputFile << record.title << "," << record.artist << "," << record.year << endl;
}
outputFile.close();
return true;
} else {
return false;
}
}