sorting and PLF example 2 with modern form.

a lot of C# to be real!
This commit is contained in:
2025-10-23 11:46:36 +02:00
parent cb1b91ba0f
commit 85670b8a9e
16 changed files with 842 additions and 11 deletions

33
PLF/plf/bsp2.py Normal file
View File

@@ -0,0 +1,33 @@
print("Berge Österreich")
b = ["Großglockner","Wildspitze","Weißkugel","Großvenediger"]
h = [3798,3768,3738,3657]
while True:
ein=str(input("Neue Eingabe (j/n)? "))
if ein == "j":
nb = str(input("Name des Berges: "))
nh = int(input("Höhe: "))
if nb in b:
print("Schon vorhanden")
else:
b.append(nb)
h.append(nh)
else:
break
for x,y in zip(b,h):
print(x,":",y)
ma = max(h)
i = h.index(ma)
print("Der höchste Berg der Liste ist der",b[i],"mit einer Höhe von",ma,"m")
d = sum(h)/len(h)
print("Die durchschnittliche Höhe aller Berge beträgt",d,"m.")
z = 0
for x in h:
if x >= 3000:
z+=1
print("In der Liste sind",z,"3000er gespeichert.")

91
PLF/plf/bsp2_modern.cpp Normal file
View 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;
}

74
PLF/plf/bsp2_modern.py Normal file
View File

@@ -0,0 +1,74 @@
from typing import List
def add_mountain(names: List[str], heights: List[int]) -> None:
"""Add a new mountain entry if it doesn't already exist."""
name = input("Name des Berges: ").strip()
if name in names:
print("Schon vorhanden.")
return
try:
height = int(input("Höhe: ").strip())
except ValueError:
print("Ungültige Eingabe! Bitte eine Zahl angeben.")
return
names.append(name)
heights.append(height)
print(f"'{name}' mit {height} m wurde hinzugefügt.")
def display_mountains(names: List[str], heights: List[int]) -> None:
"""Print the list of all mountains and their heights."""
print("\nBergliste:")
for name, height in zip(names, heights):
print(f"{name}: {height} m")
def get_highest_mountain(names: List[str], heights: List[int]) -> tuple[str, int]:
"""Return the highest mountain as (name, height)."""
max_height = max(heights)
index = heights.index(max_height)
return names[index], max_height
def calculate_average_height(heights: List[int]) -> float:
"""Return the average height of all mountains."""
return sum(heights) / len(heights)
def count_3000ers(heights: List[int]) -> int:
"""Return how many mountains are 3000 meters or higher."""
return sum(h >= 3000 for h in heights)
def main() -> None:
"""Main program loop."""
print("Berge Österreich")
mountain_names = ["Großglockner", "Wildspitze", "Weißkugel", "Großvenediger"]
mountain_heights = [3798, 3768, 3738, 3657]
while True:
choice = input("Neue Eingabe (j/n)? ").strip().lower()
if choice == "j":
add_mountain(mountain_names, mountain_heights)
elif choice == "n":
break
else:
print("Bitte nur 'j' oder 'n' eingeben.")
display_mountains(mountain_names, mountain_heights)
highest_name, highest_height = get_highest_mountain(mountain_names, mountain_heights)
print(f"\nDer höchste Berg ist der {highest_name} mit {highest_height} m.")
avg_height = calculate_average_height(mountain_heights)
print(f"Die durchschnittliche Höhe beträgt {avg_height:.1f} m.")
count = count_3000ers(mountain_heights)
print(f"In der Liste sind {count} 3000er gespeichert.")
if __name__ == "__main__":
main()