add solution for lab07
This commit is contained in:
14
.vscode/launch.json
vendored
14
.vscode/launch.json
vendored
@@ -104,6 +104,20 @@
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "/usr/bin/gdb",
|
||||
"preLaunchTask": "C/C++: g++ build active file"
|
||||
},
|
||||
{
|
||||
"name": "lab07",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${fileDirname}/${fileBasenameNoExtension}",
|
||||
"args": [
|
||||
"input.txt"
|
||||
],
|
||||
"cwd": "${fileDirname}",
|
||||
"environment": [],
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "/usr/bin/gdb",
|
||||
"preLaunchTask": "C/C++: g++ build single active file"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
43
labs/maps/checkpoint1.cpp
Normal file
43
labs/maps/checkpoint1.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 2) {
|
||||
cerr << "Usage: " << argv[0] << " inputfile" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
ifstream fin(argv[1]);
|
||||
if (!fin) {
|
||||
cerr << "Error opening file " << argv[1] << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Use operator[] to count frequency of each integer.
|
||||
map<int, int> counts;
|
||||
int number;
|
||||
while (fin >> number) {
|
||||
counts[number] = counts[number] + 1;
|
||||
}
|
||||
|
||||
// Find the maximum frequency.
|
||||
int maxCount = 0;
|
||||
for (map<int, int>::iterator it = counts.begin(); it != counts.end(); ++it) {
|
||||
if (it->second > maxCount) {
|
||||
maxCount = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
// Print all numbers with frequency equal to maxCount.
|
||||
cout << "Modes: ";
|
||||
for (map<int, int>::iterator it = counts.begin(); it != counts.end(); ++it) {
|
||||
if (it->second == maxCount) {
|
||||
cout << it->first << " ";
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
labs/maps/checkpoint2
Executable file
BIN
labs/maps/checkpoint2
Executable file
Binary file not shown.
48
labs/maps/checkpoint2.cpp
Normal file
48
labs/maps/checkpoint2.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 2) {
|
||||
cerr << "Usage: " << argv[0] << " inputfile" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
ifstream fin(argv[1]);
|
||||
if (!fin) {
|
||||
cerr << "Error opening file " << argv[1] << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Map to count frequencies; using find/insert instead of operator[].
|
||||
map<int, int> counts;
|
||||
int number;
|
||||
while (fin >> number) {
|
||||
map<int, int>::iterator it = counts.find(number);
|
||||
if (it != counts.end()) {
|
||||
it->second = it->second + 1;
|
||||
} else {
|
||||
counts.insert(pair<int, int>(number, 1));
|
||||
}
|
||||
}
|
||||
|
||||
// Find the maximum frequency.
|
||||
int maxCount = 0;
|
||||
for (map<int, int>::iterator it = counts.begin(); it != counts.end(); ++it) {
|
||||
if (it->second > maxCount) {
|
||||
maxCount = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
// Print all numbers with frequency equal to maxCount.
|
||||
cout << "Modes: ";
|
||||
for (map<int, int>::iterator it = counts.begin(); it != counts.end(); ++it) {
|
||||
if (it->second == maxCount) {
|
||||
cout << it->first << " ";
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
labs/maps/input.txt
Normal file
1
labs/maps/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
19 83 -12 83 65 19 45 -12 45 19 45
|
||||
BIN
labs/maps/phonebook2
Executable file
BIN
labs/maps/phonebook2
Executable file
Binary file not shown.
42
labs/maps/phonebook2.cpp
Normal file
42
labs/maps/phonebook2.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
// Add a number, name pair to the phonebook using a map.
|
||||
void add(map<int, string>& phonebook, int number, const string &name) {
|
||||
map<int, string>::iterator it = phonebook.find(number);
|
||||
if (it != phonebook.end()) {
|
||||
it->second = name;
|
||||
} else {
|
||||
phonebook.insert(pair<int, string>(number, name));
|
||||
}
|
||||
}
|
||||
|
||||
// Given a phone number, determine who is calling.
|
||||
void identify(const map<int, string>& phonebook, int number) {
|
||||
map<int, string>::const_iterator it = phonebook.find(number);
|
||||
if (it == phonebook.end())
|
||||
cout << "unknown caller!" << endl;
|
||||
else
|
||||
cout << it->second << " is calling!" << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Init
|
||||
map<int, string> phonebook;
|
||||
|
||||
// Add
|
||||
add(phonebook, 1111, "fred");
|
||||
add(phonebook, 2222, "sally");
|
||||
add(phonebook, 3333, "george");
|
||||
add(phonebook, 1234567, "alice");
|
||||
add(phonebook, 7654321, "bob");
|
||||
|
||||
// Test
|
||||
identify(phonebook, 2222);
|
||||
identify(phonebook, 4444);
|
||||
identify(phonebook, 1234567);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user