Skip to content

Commit f716a0f

Browse files
committed
Add more core and advanced bonus examples
1 parent 9329888 commit f716a0f

12 files changed

Lines changed: 440 additions & 0 deletions

File tree

languages/cpp/02-core/file-io-basics/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ g++ -std=c++17 -Wall -Wextra -pedantic example/main.cpp -o file_io_example
99
./file_io_example
1010
```
1111

12+
## More Examples
13+
14+
- `example/csv-like-reader.cpp`:
15+
16+
```bash
17+
g++ -std=c++17 -Wall -Wextra -pedantic example/csv-like-reader.cpp -o file_io_csv_like_reader
18+
./file_io_csv_like_reader
19+
```
20+
1221
## Topics Covered
1322

1423
- `std::ifstream` for reading files.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <fstream>
2+
#include <iostream>
3+
#include <sstream>
4+
#include <string>
5+
#include <vector>
6+
7+
using namespace std;
8+
9+
struct StudentScore {
10+
string name;
11+
int score;
12+
};
13+
14+
int main() {
15+
string path;
16+
cout << "CSV-like input file path (name,score): ";
17+
getline(cin, path);
18+
19+
ifstream input(path);
20+
if (!input) {
21+
cout << "Could not open file: " << path << '\n';
22+
return 0;
23+
}
24+
25+
vector<StudentScore> rows;
26+
string line;
27+
int lineNumber = 0;
28+
29+
while (getline(input, line)) {
30+
++lineNumber;
31+
if (line.empty()) {
32+
continue;
33+
}
34+
35+
istringstream row(line);
36+
string name;
37+
string scoreText;
38+
39+
if (!getline(row, name, ',') || !getline(row, scoreText)) {
40+
cout << "Skipping malformed line " << lineNumber << ": " << line << '\n';
41+
continue;
42+
}
43+
44+
istringstream scoreStream(scoreText);
45+
int score = 0;
46+
if (!(scoreStream >> score) || score < 0 || score > 100) {
47+
cout << "Skipping invalid score on line " << lineNumber << ": " << scoreText << '\n';
48+
continue;
49+
}
50+
51+
rows.push_back({name, score});
52+
}
53+
54+
cout << "\nValid rows: " << rows.size() << '\n';
55+
for (const StudentScore& row : rows) {
56+
cout << "- " << row.name << ": " << row.score << '\n';
57+
}
58+
59+
return 0;
60+
}

languages/cpp/02-core/maps-and-frequency-counting/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ g++ -std=c++17 -Wall -Wextra -pedantic example/main.cpp -o maps_frequency_exampl
99
./maps_frequency_example
1010
```
1111

12+
## More Examples
13+
14+
- `example/top-k-frequency.cpp`:
15+
16+
```bash
17+
g++ -std=c++17 -Wall -Wextra -pedantic example/top-k-frequency.cpp -o maps_top_k_frequency
18+
./maps_top_k_frequency
19+
```
20+
1221
## Topics Covered
1322

1423
- `std::map<Key, Value>` basics.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <algorithm>
2+
#include <iostream>
3+
#include <map>
4+
#include <string>
5+
#include <vector>
6+
7+
using namespace std;
8+
9+
struct FrequencyItem {
10+
string word;
11+
int count;
12+
};
13+
14+
int main() {
15+
int n = 0;
16+
cout << "How many words? ";
17+
cin >> n;
18+
19+
if (n <= 0) {
20+
cout << "Please enter a positive number.\n";
21+
return 0;
22+
}
23+
24+
map<string, int> frequency;
25+
for (int i = 0; i < n; ++i) {
26+
string word;
27+
cout << "Word " << (i + 1) << ": ";
28+
cin >> word;
29+
++frequency[word];
30+
}
31+
32+
int k = 0;
33+
cout << "How many top words to show? ";
34+
cin >> k;
35+
36+
if (k <= 0) {
37+
cout << "Please enter a positive value for k.\n";
38+
return 0;
39+
}
40+
41+
vector<FrequencyItem> items;
42+
items.reserve(frequency.size());
43+
for (const pair<const string, int>& entry : frequency) {
44+
items.push_back({entry.first, entry.second});
45+
}
46+
47+
sort(items.begin(), items.end(), [](const FrequencyItem& a, const FrequencyItem& b) {
48+
if (a.count != b.count) {
49+
return a.count > b.count;
50+
}
51+
return a.word < b.word;
52+
});
53+
54+
if (k > static_cast<int>(items.size())) {
55+
k = static_cast<int>(items.size());
56+
}
57+
58+
cout << "\nTop " << k << " words:\n";
59+
for (int i = 0; i < k; ++i) {
60+
cout << (i + 1) << ". " << items[i].word << " -> " << items[i].count << '\n';
61+
}
62+
63+
return 0;
64+
}

languages/cpp/02-core/sorting-and-searching/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ g++ -std=c++17 -Wall -Wextra -pedantic example/main.cpp -o sorting_searching_exa
99
./sorting_searching_example
1010
```
1111

12+
## More Examples
13+
14+
- `example/stable-vs-unstable-sort.cpp`:
15+
16+
```bash
17+
g++ -std=c++17 -Wall -Wextra -pedantic example/stable-vs-unstable-sort.cpp -o sorting_stable_vs_unstable
18+
./sorting_stable_vs_unstable
19+
```
20+
1221
## Topics Covered
1322

1423
- Manual sorting with nested loops.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <algorithm>
2+
#include <iostream>
3+
#include <string>
4+
#include <vector>
5+
6+
using namespace std;
7+
8+
struct Record {
9+
int score;
10+
string name;
11+
};
12+
13+
void printRecords(const vector<Record>& records, const string& title) {
14+
cout << title << '\n';
15+
for (const Record& record : records) {
16+
cout << "- score=" << record.score << ", name=" << record.name << '\n';
17+
}
18+
cout << '\n';
19+
}
20+
21+
int main() {
22+
vector<Record> records = {
23+
{90, "Ana"},
24+
{75, "Bob"},
25+
{90, "Carla"},
26+
{75, "Diego"},
27+
{90, "Emma"}
28+
};
29+
30+
printRecords(records, "Original order:");
31+
32+
vector<Record> unstable = records;
33+
sort(unstable.begin(), unstable.end(), [](const Record& a, const Record& b) {
34+
return a.score < b.score;
35+
});
36+
printRecords(unstable, "After std::sort (not guaranteed stable):");
37+
38+
vector<Record> stable = records;
39+
stable_sort(stable.begin(), stable.end(), [](const Record& a, const Record& b) {
40+
return a.score < b.score;
41+
});
42+
printRecords(stable, "After std::stable_sort:");
43+
44+
cout << "Notice how records with equal scores keep relative order with stable_sort.\n";
45+
return 0;
46+
}

languages/cpp/03-advanced/inheritance-and-polymorphism/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ g++ -std=c++17 -Wall -Wextra -pedantic example/main.cpp -o inheritance_polymorph
99
./inheritance_polymorphism_example
1010
```
1111

12+
## More Examples
13+
14+
- `example/polymorphic-menu.cpp`:
15+
16+
```bash
17+
g++ -std=c++17 -Wall -Wextra -pedantic example/polymorphic-menu.cpp -o inheritance_polymorphic_menu
18+
./inheritance_polymorphic_menu
19+
```
20+
1221
## Topics Covered
1322

1423
- Base and derived classes.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <iostream>
2+
#include <memory>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
class Shape {
8+
public:
9+
virtual ~Shape() = default;
10+
virtual double area() const = 0;
11+
virtual const char* name() const = 0;
12+
};
13+
14+
class Circle : public Shape {
15+
public:
16+
explicit Circle(double radiusValue) : radius(radiusValue) {}
17+
18+
double area() const override {
19+
return 3.1415926535 * radius * radius;
20+
}
21+
22+
const char* name() const override {
23+
return "Circle";
24+
}
25+
26+
private:
27+
double radius;
28+
};
29+
30+
class Rectangle : public Shape {
31+
public:
32+
Rectangle(double widthValue, double heightValue) : width(widthValue), height(heightValue) {}
33+
34+
double area() const override {
35+
return width * height;
36+
}
37+
38+
const char* name() const override {
39+
return "Rectangle";
40+
}
41+
42+
private:
43+
double width;
44+
double height;
45+
};
46+
47+
int main() {
48+
vector<unique_ptr<Shape>> shapes;
49+
50+
while (true) {
51+
int option = 0;
52+
cout << "\nMenu\n";
53+
cout << "1) Add circle\n";
54+
cout << "2) Add rectangle\n";
55+
cout << "3) List areas\n";
56+
cout << "4) Exit\n";
57+
cout << "Choose: ";
58+
cin >> option;
59+
60+
if (option == 1) {
61+
double radius = 0.0;
62+
cout << "Radius: ";
63+
cin >> radius;
64+
if (radius > 0.0) {
65+
shapes.push_back(make_unique<Circle>(radius));
66+
} else {
67+
cout << "Radius must be positive.\n";
68+
}
69+
} else if (option == 2) {
70+
double width = 0.0;
71+
double height = 0.0;
72+
cout << "Width: ";
73+
cin >> width;
74+
cout << "Height: ";
75+
cin >> height;
76+
if (width > 0.0 && height > 0.0) {
77+
shapes.push_back(make_unique<Rectangle>(width, height));
78+
} else {
79+
cout << "Width and height must be positive.\n";
80+
}
81+
} else if (option == 3) {
82+
if (shapes.empty()) {
83+
cout << "No shapes added yet.\n";
84+
continue;
85+
}
86+
87+
double totalArea = 0.0;
88+
for (const unique_ptr<Shape>& shape : shapes) {
89+
const double shapeArea = shape->area();
90+
totalArea += shapeArea;
91+
cout << "- " << shape->name() << " area: " << shapeArea << '\n';
92+
}
93+
cout << "Total area: " << totalArea << '\n';
94+
} else if (option == 4) {
95+
cout << "Goodbye.\n";
96+
break;
97+
} else {
98+
cout << "Invalid option.\n";
99+
}
100+
}
101+
102+
return 0;
103+
}

languages/cpp/03-advanced/structs-and-classes/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ g++ -std=c++17 -Wall -Wextra -pedantic example/main.cpp -o structs_and_classes_e
99
./structs_and_classes_example
1010
```
1111

12+
## More Examples
13+
14+
- `example/class-with-validation.cpp`:
15+
16+
```bash
17+
g++ -std=c++17 -Wall -Wextra -pedantic example/class-with-validation.cpp -o structs_class_with_validation
18+
./structs_class_with_validation
19+
```
20+
1221
## Topics Covered
1322

1423
- Default access: `struct` public, `class` private.

0 commit comments

Comments
 (0)