C++ :: Aufgabe #311 :: Lösung #4
5 Lösungen

#311
Mobilfunkanbieter (Monatsabrechnung)
Anfänger - C++
von JKooP
- 07.11.2020 um 09:42 Uhr
Ein Mobilfunkanbieter stellt folgende Pakete zur Verfügung:
Paket-Typ....Basispreis....Freiminuten....Minutenpreis
P_Basis........2,95€..............0...................0,10€
P_100..........3.95€.............100................0,12€
P_300..........7,95€.............300................0,15€
P_600..........12,95€...........600................0,20€
P_Flat..........29,95€...........unbegrenzt.....0,00€
Schreibe eine Funktion/Methode, bei der sowohl der Paket-Typ als auch die telefonierten Minuten übergeben werden.
Als Ergebnis soll der Gesamtpreis inklusive Basispreis für den Monat ausgegeben werden.
Viel Spaß
Paket-Typ....Basispreis....Freiminuten....Minutenpreis
P_Basis........2,95€..............0...................0,10€
P_100..........3.95€.............100................0,12€
P_300..........7,95€.............300................0,15€
P_600..........12,95€...........600................0,20€
P_Flat..........29,95€...........unbegrenzt.....0,00€
Schreibe eine Funktion/Methode, bei der sowohl der Paket-Typ als auch die telefonierten Minuten übergeben werden.
Als Ergebnis soll der Gesamtpreis inklusive Basispreis für den Monat ausgegeben werden.
Viel Spaß
#4

von EErza (80 Punkte)
- 11.11.2020 um 18:04 Uhr

#include <iostream> #include <string> #include <iomanip> using namespace std; struct P_Base { const double basePrice = 2.95; const double minutesPrice = 0.10; const string paketName = "P_Base"; } pbase; struct P_100 { const double basePrice = 3.95; const int freeMinutes = 100; const double minutesPrice = 0.12; const string paketName = "P_100"; } p100; struct P_300 { const double basePrice = 7.95; const int freeMinutes = 300; const double minutesPrice = 0.15; const string paketName = "P_300"; } p300; struct P_600 { const double basePrice = 12.95; const int freeMinutes = 600; const double minutesPrice = 0.20; const string paketName = "P_600"; } p600; struct P_Flat { const double basePrice = 29.95; const string paketName = "P_Flat"; } pflat; bool checkPaket(const string& paket) { if(paket == pbase.paketName || paket == p100.paketName || paket == p300.paketName || paket == p600.paketName || paket == pflat.paketName) { return true; } else { return false; } } void showPakets() { cout << setprecision(2) << fixed; cout << "----- Paket Typen und Preise -----\n"; cout << "-> Paket_Basis (" << pbase.paketName << "):\n"; cout << "\t- Basispreis: " << pbase.basePrice << "€\n"; cout << "\t- Freiminuten: 0\n"; cout << "\t- Minutenpreis: " << pbase.minutesPrice << "€\n\n"; cout << "-> Paket_100 (" << p100.paketName << "):\n"; cout << "\t- Basispreis: " << p100.basePrice << "€\n"; cout << "\t- Freiminuten: " << p100.freeMinutes << "\n"; cout << "\t- Minutenpreis: " << p100.minutesPrice << "€\n\n"; cout << "-> Paket_300 (" << p300.paketName << "):\n"; cout << "\t- Basispreis: " << p300.basePrice << "€\n"; cout << "\t- Freiminuten: " << p300.freeMinutes << "\n"; cout << "\t- Minutenpreis: " << p300.minutesPrice << "€\n\n"; cout << "-> Paket_600 (" << p600.paketName << "):\n"; cout << "\t- Basispreis: " << p600.basePrice << "€\n"; cout << "\t- Freiminuten: " << p600.freeMinutes << "\n"; cout << "\t- Minutenpreis: " << p600.minutesPrice << "€\n\n"; cout << "-> Paket_Flat (" << pflat.paketName << "):\n"; cout << "\t- Basispreis: " << pflat.basePrice << "€\n"; cout << "\t- Freiminuten: 0\n"; cout << "\t- Minutenpreis: 0.00€\n\n"; } double calcTotalPrice(const string& paket, int minutes) { if(paket == pbase.paketName) { return pbase.basePrice + (minutes * pbase.minutesPrice); } else if(paket == p100.paketName) { return p100.basePrice + (minutes > 100 ? p100.minutesPrice * (minutes - p100.freeMinutes) : 0.0); } else if(paket == p300.paketName) { return p300.basePrice + (minutes > 300 ? p300.minutesPrice * (minutes - p300.freeMinutes) : 0.0); } else if(paket == p600.paketName) { return p600.basePrice + (minutes > 600 ? p600.minutesPrice * (minutes - p600.freeMinutes) : 0.0); } else if(paket == pflat.paketName) { return pflat.basePrice; } else { return 0.0; } } int main() { string paket; int minutes; showPakets(); do { cout << "Paket wählen: "; cin >> paket; if(!checkPaket(paket)) { cout << "Falsches Paket angegeben!\n\n"; } } while(!checkPaket(paket)); cout << "Telefonierte Minuten: "; cin >> minutes; cout << "\nGesamtpreis = " << calcTotalPrice(paket, minutes) << "€\n"; }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1