sorting and PLF example 2 with modern form.
a lot of C# to be real!
This commit is contained in:
91
PLF/plf/bsp2_modern.cpp
Normal file
91
PLF/plf/bsp2_modern.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void add_mountain(vector<string>& names, vector<int>& heights) {
|
||||
cout << "Name des Berges: ";
|
||||
string name;
|
||||
getline(cin >> ws, name);
|
||||
|
||||
// Check if already exists
|
||||
if (find(names.begin(), names.end(), name) != names.end()) {
|
||||
cout << "Schon vorhanden." << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
cout << "Höhe: ";
|
||||
int height;
|
||||
if (!(cin >> height)) {
|
||||
cout << "Ungültige Eingabe! Bitte eine Zahl angeben." << endl;
|
||||
cin.clear();
|
||||
cin.ignore(numeric_limits<streamsize>::max(), '\n');
|
||||
return;
|
||||
}
|
||||
|
||||
names.push_back(name);
|
||||
heights.push_back(height);
|
||||
cout << "'" << name << "' mit " << height << " m wurde hinzugefügt." << endl;
|
||||
}
|
||||
|
||||
void display_mountains(const vector<string>& names, const vector<int>& heights) {
|
||||
cout << "\nBergliste:\n";
|
||||
for (size_t i = 0; i < names.size(); ++i) {
|
||||
cout << " • " << names[i] << ": " << heights[i] << " m\n";
|
||||
}
|
||||
}
|
||||
|
||||
pair<string, int> get_highest_mountain(const vector<string>& names, const vector<int>& heights) {
|
||||
auto it = max_element(heights.begin(), heights.end());
|
||||
size_t index = distance(heights.begin(), it);
|
||||
return { names[index], *it };
|
||||
}
|
||||
|
||||
double calculate_average_height(const vector<int>& heights) {
|
||||
double sum = accumulate(heights.begin(), heights.end(), 0);
|
||||
return sum / heights.size();
|
||||
}
|
||||
|
||||
int count_3000ers(const vector<int>& heights) {
|
||||
return count_if(heights.begin(), heights.end(), [](int h) { return h >= 3000; });
|
||||
}
|
||||
|
||||
int main() {
|
||||
cout << "Berge Österreich" << endl;
|
||||
|
||||
vector<string> mountain_names = { "Großglockner", "Wildspitze", "Weißkugel", "Großvenediger" };
|
||||
vector<int> mountain_heights = { 3798, 3768, 3738, 3657 };
|
||||
|
||||
while (true) {
|
||||
cout << "Neue Eingabe (j/n)? ";
|
||||
string choice;
|
||||
cin >> choice;
|
||||
|
||||
if (choice == "j" || choice == "J") {
|
||||
add_mountain(mountain_names, mountain_heights);
|
||||
} else if (choice == "n" || choice == "N") {
|
||||
break;
|
||||
} else {
|
||||
cout << "Bitte nur 'j' oder 'n' eingeben." << endl;
|
||||
}
|
||||
}
|
||||
|
||||
display_mountains(mountain_names, mountain_heights);
|
||||
|
||||
auto [highest_name, highest_height] = get_highest_mountain(mountain_names, mountain_heights);
|
||||
cout << "\nDer höchste Berg ist der " << highest_name
|
||||
<< " mit " << highest_height << " m.\n";
|
||||
|
||||
cout << fixed << setprecision(1);
|
||||
double avg_height = calculate_average_height(mountain_heights);
|
||||
cout << "Die durchschnittliche Höhe beträgt " << avg_height << " m.\n";
|
||||
|
||||
int count = count_3000ers(mountain_heights);
|
||||
cout << "In der Liste sind " << count << " 3000er gespeichert.\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user