C++ :: Aufgabe #305 :: Lösung #1

2 Lösungen Lösungen öffentlich
#305

Berechnung von Widerständen

Anfänger - C++ von JKooP - 04.10.2020 um 10:58 Uhr
Schreibe eine Methode bzw. Funktion, mit der man den Gesamtwiderstand von Parallel- bzw. Reihenschaltungen berechnen kann.
Dabei sollen sowohl die Werte der Einzelwiderstände als auch die Art der Schaltung vom Benutzer erfragt werden.

Variante 1: 2 separate Funktionen: f(float a, float b, … float n)
Variante 2: 1 kombinierte Funktion: f(artDerSchaltung, float a, float b, … float n)

Viel Spaß


#1
vote_ok
von The1tobi (200 Punkte) - 10.11.2020 um 21:15 Uhr
Quellcode ausblenden C-Code
#include <iostream>
#include <math.h>

using namespace std;
// Variante 1:
double resistorSerial(float res1 , float res2)
{
	float resSerGes = 0;
	
	resSerGes = res1 + res2;
	
	return resSerGes;
}
double resistorSerial(float res1 , float res2 , float res3 , float res4)
{
	float resSerGes = 0;
	
	resSerGes = res1 + res2 + res3 + res4;
	
	return resSerGes;
}
double resistorSerial(float res1 , float res2 , float res3 , float res4 , float res5)
{
	float resSerGes = 0;
	
	resSerGes = res1 + res2 + res3 + res4 + res5;
	
	return resSerGes;
}
double resistorPara(float res1 , float res2 , float res3)
{
	double resParGes = 0;

	resParGes = pow((1/res1 + 1/res2 + 1/res3) , -1); 
	
	return resParGes;	
}
// Variante 2:
double resistorChoose(int artDerSchaltung , float res1 , float res2 , float res3)
{
	double resistorErg;
	
	if(artDerSchaltung == 1)
	{	
	
	resistorErg = res1 + res2 + res3;
	
	return resistorErg;
	}
	
	if(artDerSchaltung == 2)
	{
		resistorErg = pow((1/res1 + 1/res2 + 1/res3) , -1); 
		
		return resistorErg;
	}
	
	return resistorErg;
}

int main ()
{
	
	cout << "Fuer Seriel Berechnung = 1" << endl;
	cout << "Fuer Parallel Berechnung = 2" << endl << endl << endl << endl;
	
	cout << "Serien Widerstand =" << resistorSerial(100 , 200 , 300, 500) << " Ohm" << endl;
	cout << "Parallel Widerstand = " << resistorPara(100 , 100 , 100) << " Ohm" << endl << endl;
	
	cout << "Serien Widerstand =" << resistorChoose(1, 100 , 200 , 300) << " Ohm" << endl;
	cout << "Parallel Widerstand = " << resistorChoose(2 ,100 , 100 , 100) << " Ohm" << endl;
	
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben