#include <iostream>
#include <iomanip>
#include <stdexcept>
using namespace std;
long double heronWurzel(double radikand, int genauigkeit=10);
double durchschnitt(double zahl1, double zahl2);
void pause();
int main()
{
int radikand=-1;
int genauigkeit=0;
long double wurzel=0.0;
while(radikand<0)
{
cout << "Geben Sie den Radikand ein: " << endl;
cin >> radikand;
if(radikand<0) cout << "Der Radikand darf nicht kleiner 0 sein!" << endl << endl;
}
cout << "Geben Sie die Genauigkeit der Berechnung an (Anzahl der Iterationen): " << endl;
cin >> genauigkeit;
if(genauigkeit <= 0)
{
cout << "Genauigkeit muss größer 0 sein! Genauigkeit hat nun den Standardwert 15." << endl << endl;
genauigkeit=15;
}
try
{
wurzel=heronWurzel(radikand, genauigkeit);
}
catch(...)
{
cerr << "Ein unerwarteter Fehler ist aufgetreten. Das Programm wird beendet.";
pause();
return 0;
}
cout << "Ergebnis: " << wurzel << endl;
cout << "Quadrat es Ergebnisses: " << (wurzel * wurzel);
pause();
return 0;
}
void pause()
{
cin.get();
cin.get();
}
long double heronWurzel(double radikand, int genauigkeit)
{
if(genauigkeit<1) return 0;
long double wurzel=durchschnitt(0,radikand);
for(int i=0; i<genauigkeit; i++)
{
wurzel=(wurzel+(radikand/wurzel))/2;
#ifdef debug
cout << i << "/" << genauigkeit << ": " << wurzel << endl;
pause();
#endif
}
return wurzel;
}
double durchschnitt(double zahl1, double zahl2)
{
return (zahl1+zahl2) / 2;
}