Problem bei der "Mitternachtsformel"

Aracon

Aracon

Routinier
Hi ich will ein kleines Programm zur Übung schreiben, es soll mir die Lösungen der Mitternachtsformel berechnen (http://de.wikipedia.org/wiki/Mitternachtsformel)

Aber ab und an kommt nur Müll raus. Hier der Code:
Code:
#include	<iostream>
#include	<string>
#include	<cmath>
using namespace std;

float a,b,c;
float x1, x2, *px1, *px2;
bool temp;

bool check(float a, float b, float c);
float mitter(float a, float b, float c);
int get();

double hilf = sqrt(b*b - 4*a*c);

int main()
{
	get();
	check(a, b, c);
	if(temp ==  0)
	{	cout	<<	"Sorry es gibt keine Lösung!"	<<	endl;
		return 0;
	}
	else
		mitter(a, b, c);
		cout	<<	"Die Lösungen sind:"	<<	endl	<< *px1	<< "und" <<	*px2	<<	endl;
		return 0;
}



int get()
{
	cout	<<	"a:"	<<	endl;
	cin	>> 	a;
	cout	<<	"b:"	<<	endl;
	cin	>>	b;
	cout	<<	"c:"	<<	endl;
	cin	>>	c;
}

bool check(float a, float b, float c)
{
	if(hilf*hilf < 0)
		return temp = 0;
	else
		return temp = 1;
}



float mitter(float a, float b, float c)
{
	x1 =  ((-b + hilf) / (2*a));
	x2 =  ((-b - hilf) / (2*a));
	px1 = &x1;
	px2 = &x2;
}
 
Hi,

du hast 2 Fehler gemacht.

1. Du hast hilf vor dem get berechnet, da sind aber alle Werte null. Daher kommt nix sinnvolles raus. Also vor main nur double hilf; und nach dem get() dann
hilf = sqrt(b*b - 4*a*c);

2. Wenn du die Wurzel aus einer negativen Zahl ziehst, ist das Ergebnis nicht kleiner null und hilf*hilf kann NIE null werden. An der Stelle nimmst du einfach noch ein hilf2 oder so, mit
hilf2 = b*b - 4*a*c;
auch nach dem get. Wenn du dann abfragst, ob hilf2 kleiner 0 ist (statt hilf*hilf) funktioniert es mit den Wiki-Beispielen bei mir.

Das return temp=0 kannst du auch durch return 0 ersetzen und dafuer den Funktionsaufruf so machen
temp = check(a, b, c);
schliesslich soll deine Funktion ja ein bool zurueckgeben.

Ich hoffe mal, jetzt klappt es!
 

Ähnliche Themen

Zugriff Ubuntu 16.04. auf Freigabe 18.04. LTS nicht möglich

String auf Konsole ausgeben

Funktion nicht gefunden

Ausführbare C-Datei von Mac OS auf Embedded Linux ausführen

Probleme mit srand()

Zurück
Oben