feat: complete chapter2 labs{1,2,3}
This commit is contained in:
parent
619fee1ce9
commit
e7be1ed974
10 changed files with 207 additions and 0 deletions
11
meson.build
11
meson.build
|
@ -7,3 +7,14 @@ project(
|
||||||
|
|
||||||
c_1_lab_1 = executable('c_1_lab_1', 'src/chapter_1/c_1_lab_1.cpp', install: true)
|
c_1_lab_1 = executable('c_1_lab_1', 'src/chapter_1/c_1_lab_1.cpp', install: true)
|
||||||
c_1_ice_1 = executable('c_1_ice_1', 'src/chapter_1/c_1_ice_1.cpp', install: true)
|
c_1_ice_1 = executable('c_1_ice_1', 'src/chapter_1/c_1_ice_1.cpp', install: true)
|
||||||
|
|
||||||
|
c_2_lab_1 = executable('c_2_lab_1', 'src/chapter_2/lab_1/main.cpp', install: true)
|
||||||
|
c_2_lab_1_arg = executable(
|
||||||
|
'c_2_lab_1_arg',
|
||||||
|
'src/chapter_2/lab_1/main.cpp',
|
||||||
|
install: true,
|
||||||
|
cpp_args: ['-DCHECK_ARG'],
|
||||||
|
)
|
||||||
|
|
||||||
|
c_2_lab_2 = executable('c_2_lab_2', 'src/chapter_2/lab_2/main.cpp', install: true)
|
||||||
|
c_2_lab_3 = executable('c_2_lab_3', 'src/chapter_2/lab_3/main.cpp', install: true)
|
||||||
|
|
3
src/chapter_2/lab_1/lab2part1.dat
Normal file
3
src/chapter_2/lab_1/lab2part1.dat
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
10 20 30
|
||||||
|
56 6 8 9 11
|
||||||
|
12 34 87 3
|
1
src/chapter_2/lab_1/lab2part1a.txt
Normal file
1
src/chapter_2/lab_1/lab2part1a.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
1 2 3 4 5 6 -1 -2 -3
|
62
src/chapter_2/lab_1/main.cpp
Normal file
62
src/chapter_2/lab_1/main.cpp
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
#include <cstdlib> //needed for exit function
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// Place fileSum prototype (declaration) here
|
||||||
|
int fileSum(string filename);
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
string filename;
|
||||||
|
|
||||||
|
#ifdef CHECK_ARG // This is a flag to the compiler to include this code if
|
||||||
|
// CHECK_ARG is defined
|
||||||
|
if (argc < 2) {
|
||||||
|
cout
|
||||||
|
<< "Please enter a file on the command line\n"; // Too little arguements
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (argc > 2) {
|
||||||
|
cout << "Too many arguements! Only 1 file is expected\n"; // Too many
|
||||||
|
// arguements
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
filename =
|
||||||
|
argv[1]; // set the file name to the second element in the command line
|
||||||
|
#endif
|
||||||
|
#ifndef CHECK_ARG // This is a flag to the compiler to include this code if
|
||||||
|
// CHECK_ARG is NOT defined
|
||||||
|
cout << "Enter the name of the input file: ";
|
||||||
|
cin >> filename; // set the file name to what the user inputs (interactive)
|
||||||
|
cout << "\n";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
sum = fileSum(filename);
|
||||||
|
|
||||||
|
cout << "Sum: " << sum << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Place fileSum implementation here
|
||||||
|
int fileSum(string filename) {
|
||||||
|
fstream file;
|
||||||
|
file.open(filename);
|
||||||
|
|
||||||
|
if (!file.is_open()) {
|
||||||
|
cout << "Error opening " << filename << endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int num;
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
while (file >> num) {
|
||||||
|
sum += num;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
}
|
1
src/chapter_2/lab_2/alpha.txt
Normal file
1
src/chapter_2/lab_2/alpha.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
abcdefghijklmnopqrstuvwxyz
|
47
src/chapter_2/lab_2/main.cpp
Normal file
47
src/chapter_2/lab_2/main.cpp
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// Place charCnt prototype (declaration) here
|
||||||
|
int charCnt(string filename, char ch);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
string filename;
|
||||||
|
char ch;
|
||||||
|
int chCnt = 0;
|
||||||
|
|
||||||
|
cout << "Enter the name of the input file: ";
|
||||||
|
cin >> filename;
|
||||||
|
cout << endl;
|
||||||
|
cout << "Enter a character: ";
|
||||||
|
cin.ignore(); // ignores newline left in stream after previous input statement
|
||||||
|
cin.get(ch);
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
chCnt = charCnt(filename, ch);
|
||||||
|
cout << "# of " << ch << "'s: " << chCnt << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Place charCnt implementation here
|
||||||
|
int charCnt(string filename, char ch) {
|
||||||
|
fstream file = fstream(filename);
|
||||||
|
|
||||||
|
if (!file.is_open()) {
|
||||||
|
cout << "Error opening " << filename << endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
char currentCh;
|
||||||
|
|
||||||
|
while (file.get(currentCh)) {
|
||||||
|
if (currentCh == ch)
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
1
src/chapter_2/lab_3/data1.csv
Normal file
1
src/chapter_2/lab_3/data1.csv
Normal file
|
@ -0,0 +1 @@
|
||||||
|
1,2,3,4,5,6
|
|
1
src/chapter_2/lab_3/data2.csv
Normal file
1
src/chapter_2/lab_3/data2.csv
Normal file
|
@ -0,0 +1 @@
|
||||||
|
6,4,8,10
|
|
79
src/chapter_2/lab_3/main.cpp
Normal file
79
src/chapter_2/lab_3/main.cpp
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
#include <algorithm>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <numeric>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
string inputFile;
|
||||||
|
string outputFile;
|
||||||
|
|
||||||
|
// Assign to inputFile value of 2nd command line argument
|
||||||
|
// Assign to outputFile value of 3rd command line argument
|
||||||
|
inputFile = argv[1];
|
||||||
|
outputFile = argv[2];
|
||||||
|
|
||||||
|
// Create input stream and open input csv file.
|
||||||
|
ifstream input(inputFile);
|
||||||
|
|
||||||
|
// Verify file opened correctly.
|
||||||
|
// Output error message and return 1 if file stream did not open correctly.
|
||||||
|
if (!input.is_open()) {
|
||||||
|
cout << "Error opening " << inputFile << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read in integers from input file to vector.
|
||||||
|
string line;
|
||||||
|
getline(input, line);
|
||||||
|
|
||||||
|
vector<int> values;
|
||||||
|
stringstream ss(line);
|
||||||
|
string value;
|
||||||
|
|
||||||
|
while (getline(ss, value, ',')) {
|
||||||
|
values.push_back(stoi(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close input stream.
|
||||||
|
input.close();
|
||||||
|
|
||||||
|
// Get integer average of all values read in.
|
||||||
|
int sum = std::accumulate(values.begin(), values.end(), 0);
|
||||||
|
int average = sum / static_cast<int>(values.size());
|
||||||
|
|
||||||
|
// Convert each value within vector to be the difference between the original
|
||||||
|
// value and the average.
|
||||||
|
for_each(values.begin(), values.end(),
|
||||||
|
[average](int &num) { num -= average; });
|
||||||
|
|
||||||
|
// Create output stream and open/create output csv file.
|
||||||
|
ofstream output(outputFile);
|
||||||
|
|
||||||
|
// Verify file opened or was created correctly.
|
||||||
|
// Output error message and return 1 if file stream did not open
|
||||||
|
// correctly.
|
||||||
|
if (!output.is_open()) {
|
||||||
|
cout << "Error opening " << outputFile << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write converted values into output csv file, each integer separated by
|
||||||
|
// a comma.
|
||||||
|
for (unsigned int i = 0; i < values.size(); i++) {
|
||||||
|
output << values[i];
|
||||||
|
if (i < values.size() - 1) {
|
||||||
|
output << ",";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close output stream.
|
||||||
|
output.close();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
1
src/chapter_2/lab_3/out1.csv
Normal file
1
src/chapter_2/lab_3/out1.csv
Normal file
|
@ -0,0 +1 @@
|
||||||
|
-2,-1,0,1,2,3
|
|
Loading…
Reference in a new issue