C++ :: Aufgabe #302 :: Lösung #2
3 Lösungen

#302
Spannung, Stromstärke, Widerstand
Anfänger - C++
von JKooP
- 04.10.2020 um 12:43 Uhr
Schreibe eine Methode/Funktion, die je nach Eingabe von 2 Werten den fehlenden Wert ermittelt.
Die möglichen einzugebenden Werte sind Spannung (U [Volt]), Widerstand (R [Ohm]) bzw. Stromstärke (I [Ampere]).
Der fehlende Wert kann null/nothing oder jedes andere beliebige Zeichen sein.
Die Formel zur Berechnung lautet: U = R * I
Beispiel:
U := 270.0
R := null
I := 3.0
R = I / U = 270 / 3 = 90.0 Ohm
Viel Spaß
Die möglichen einzugebenden Werte sind Spannung (U [Volt]), Widerstand (R [Ohm]) bzw. Stromstärke (I [Ampere]).
Der fehlende Wert kann null/nothing oder jedes andere beliebige Zeichen sein.
Die Formel zur Berechnung lautet: U = R * I
Beispiel:
U := 270.0
R := null
I := 3.0
R = I / U = 270 / 3 = 90.0 Ohm
Viel Spaß
#2

von Concado (110 Punkte)
- 26.10.2020 um 10:51 Uhr

#include <iostream> #include <algorithm> #include <math.h> static std::string u; static std::string i; static std::string r; bool is_digit(const char value) { if(value == '.') return true; else return std::isdigit(value); } bool is_numeric(const std::string& value) { return std::all_of(value.begin(), value.end(), is_digit); } int main() { std::cout << "U := \n"; std::getline(std::cin, u); std::cout << "R := \n"; std::getline(std::cin, r); std::cout << "I := \n"; std::getline(std::cin, i); if(!is_numeric(u)) std::cout << "U = R * I = " << r << " * " << i << " = " << std::round(std::stod(r) * std::stod(i)) << " Volt" << std::endl; else if(!is_numeric(r)) std::cout << "R = U / I = " << u << " / " << i << " = " << std::round(std::stod(u) / std::stod(i)) << " Ohm" << std::endl; else std::cout << "I = U / R = " << u << " / " << r << " = " << std::round(std::stod(u) / std::stod(r)) << " Ampere" << std::endl; std::cin.get(); }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1