S
Servus,
ich weiß das man nicht die Arbeit von anderen machen soll, aber ich konnts mir nicht verkneifen Ich arbeite mich gerade selber in C++ ein und habe dies mal als schöne Übung angesehen. Da ich in 2 Monate selber ein Studium beginne, hat nichts mit C++ zu tun (Maschinenbau)
In deiner Aufgabenstellung ist C++ als Sprache explizit hervorgehoben, weshalb du auch mit Klassen und Strukturen arbeiten solltest. Ich habe jetzt nur mal eine Klasse erstellt, würde dir aber empfehlen das ganze ein wenig abstrakter zu gestallten. Ein Beispiel wäre die Klasse Falkultät für den alternativen Lösungsweg. Wenn du denn folgenden Code in deine Arbeit ein fließen lässt, würde ich gerne deine Implementation des 2. Lösungsweges sehen. Entweder hier oder per Mail an thomas-enzinger(at)web(punkt)de.
xroads42 schrieb:
Bei 2 hoch k kann man noch einen "kniff" anwenden...
Tipp: (Basis des Zahlensystemes)^n ist gleich das Verschieben der Zahl um n-Stellen. Die CPU rechnet mit Dualzahlen -> a * 2^n = a << n
Im Dezimalsystem 340 * 10^3 = 340.000
Wer Lust hat den Code durchzusehen bitte ich um so viel Kritik wie nur möglich Eines habe ich selber auch an zu greiden. Die Main-Methode sollte in einer neuen *.cpp ausgelagert sein.
beleg108.h
/*
* #
* # ***** BEGIN GPL LICENSE BLOCK *****
* #
* # This program is free software; you can redistribute it and/or
* # modify it under the terms of the GNU General Public License
* # as published by the Free Software Foundation; either version 3
* # of the License, or (at your option) any later version.
* #
* # This program is distributed in the hope that it will be useful,
* # but WITHOUT ANY WARRANTY; without even the implied warranty of
* # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* # GNU General Public License for more details.
* #
* # You should have received a copy of the GNU General Public License
* # along with this program; if not, write to the Free Software Foundation,
* # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* # or
* # http://www.gnu.org/licenses/gpl-3.0.html
* #
* # ***** END GPL LICENCE BLOCK *****
* #
*/
/*
* File: beleg108.h
* Author: Siassei
*
* Created on 4. Januar 2009, 22:42
*/
#ifndef _BELEG108_H
#define _BELEG108_H
#include <iostream>
#include <cmath>
using namespace std;
#ifdef linux
#define PAUSE "echo warten auf ENTER; read"
#else
#define PAUSE "PAUSE"
#endif
namespace siassei {
/**
* Class - Description
*
*/
template<class T>
class Beleg108 {
/// -----------------------------------------------------------------------
///
/// Constant
///
/// -----------------------------------------------------------------------
public:
static const int MAX_ITERATIONEN = 10;
/// -----------------------------------------------------------------------
///
/// Fields
///
/// -----------------------------------------------------------------------
private:
T x;
T res;
int nenner, zahler;
int k, cnt;
T epsilon;
/// -----------------------------------------------------------------------
///
/// Constructors
///
/// -----------------------------------------------------------------------
public:
Beleg108(T x);
virtual ~Beleg108();
/// -----------------------------------------------------------------------
///
/// Methoden
///
/// -----------------------------------------------------------------------
public:
T getResult() const;
int getIterationszyklen() const;
T getEspilon() const;
void setEpsilon(T val);
void calcaSinh();
private:
void calcWithSummenformel();
// void calcWithAlternativ();
bool validateResult(T newRes) const;
void clear();
};
}
#endif /* _BELEG108_H */
beleg108.cpp
/*
* #
* # ***** BEGIN GPL LICENSE BLOCK *****
* #
* # This program is free software; you can redistribute it and/or
* # modify it under the terms of the GNU General Public License
* # as published by the Free Software Foundation; either version 3
* # of the License, or (at your option) any later version.
* #
* # This program is distributed in the hope that it will be useful,
* # but WITHOUT ANY WARRANTY; without even the implied warranty of
* # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* # GNU General Public License for more details.
* #
* # You should have received a copy of the GNU General Public License
* # along with this program; if not, write to the Free Software Foundation,
* # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* # or
* # http://www.gnu.org/licenses/gpl-3.0.html
* #
* # ***** END GPL LICENCE BLOCK *****
* #
*/
/*
* File: beleg108.cpp
* Author: Siassei
*
* Created on 4. Januar 2009, 22:42
*/
#include "beleg108.h"
/*****************************************************************************************
FUNKTION: Main
PACKAGE:
SCOPE:
DETAILS:
BESCHREIBUNG:
RÜCKGABEWERT:
ARGUMENTE:
EXCEPTION:
*****************************************************************************************/
int main() {
double x;
bool check = false;
bool input = true;
cout << "Beleg108 by Siassei" << endl;
// Eingabe
while (input && !check) {
cout << endl << "Eingabe der Variablen zur Berechnung von arcsinh(x):" << endl;
cout << "var x = |x| < 1: ";
cin >> x;
cout << endl;
if (x < numeric_limits<double>::epsilon() || x > 1.0) {
cout << endl;
cout << "Die Eingabe entspricht nicht den Definitionsbereiches der Summenformel." << endl;
cout << "Moechten Sie die Eingabe wiederholen (w) oder das Programm beenden (b)?" << endl;
string res;
cin >> res;
if (res != "w")
input = false;
} else {
check = true;
}
} // Ende Eingabe
if (check) {
double res;
cout << "Berechne ..." << endl;
siassei::Beleg108<double>* prog = new siassei::Beleg108<double>(x);
prog->calcaSinh();
res = prog->getResult();
cout << endl;
cout << "Result: " << res << endl;
cout << endl;
delete prog;
} else {
cout << "Ihre Eingabe konnte nicht verarbeitet werden." << endl;
} // Ende Alg.
cout << "The End" << endl;
cout << "This tool is created by Siassei alias Enzinter Thomas. All rights reserved ;)" << endl;
system(PAUSE);
}
/*****************************************************************************************
FUNKTION: Beleg108
PACKAGE: siassei
SCOPE: public
DETAILS:
BESCHREIBUNG:
RÜCKGABEWERT:
ARGUMENTE:
EXCEPTION:
*****************************************************************************************/
template<class T>
inline siassei::Beleg108<T>::Beleg108(T x) {
this->zahler = 1;
this->nenner = 2 * 3;
this->k = 3;
this->x = x;
this->cnt = 0;
this->epsilon = numeric_limits<T>::epsilon();
}
/*****************************************************************************************
FUNKTION: ~Beleg108
PACKAGE: siassei
SCOPE: public
DETAILS:
BESCHREIBUNG:
RÜCKGABEWERT:
ARGUMENTE:
EXCEPTION:
*****************************************************************************************/
template<class T>
inline siassei::Beleg108<T>::~Beleg108() {
// delete this->zahler;
// delete this->nenner;
// delete this->res;
//
// delete this->x;
//
// delete this->cnt;
// delete this->k;
// delete this->epsilon;
}
/*****************************************************************************************
FUNKTION: calcWithSummenformel
PACKAGE: siassei
SCOPE: private
DETAILS:
BESCHREIBUNG:
RÜCKGABEWERT:
ARGUMENTE:
EXCEPTION:
*****************************************************************************************/
template<class T>
void siassei::Beleg108<T>::calcWithSummenformel() {
T newRes = x - (((T) zahler) / ((T) nenner)) * ((T) pow(this->x, k));
while (!validateResult(newRes)) {
// genaueres Result
this->res = newRes;
// Formel weiter entwickeln
nenner /= k;
zahler *= k;
nenner *= ++k;
nenner *= ++k;
// vereinigen
if (cnt % 2)
newRes -= (((T) zahler) / ((T) nenner)) * ((T) pow(this->x, k));
else
newRes += (((T) zahler) / ((T) nenner)) * ((T) pow(this->x, k));
// Counter um eins erhöhen
cnt++;
}
};
/*****************************************************************************************
FUNKTION: calcWithAlternativ
PACKAGE: siassei
SCOPE: private
DETAILS:
BESCHREIBUNG:
RÜCKGABEWERT:
ARGUMENTE:
EXCEPTION:
*****************************************************************************************/
//template<class T>
//bool siassei::Beleg108<T>::calcWithAlternativ() {
//
//};
/*****************************************************************************************
FUNKTION: getResult
PACKAGE: siassei
SCOPE: public
DETAILS:
BESCHREIBUNG:
RÜCKGABEWERT:
ARGUMENTE:
EXCEPTION:
*****************************************************************************************/
template<class T>
inline T siassei::Beleg108<T>::getResult() const {
return this->res;
};
/*****************************************************************************************
FUNKTION: getResult
PACKAGE: siassei
SCOPE: public
DETAILS:
BESCHREIBUNG:
RÜCKGABEWERT:
ARGUMENTE:
EXCEPTION:
*****************************************************************************************/
template<class T>
inline int siassei::Beleg108<T>::getIterationszyklen() const {
return this->k;
};
/*****************************************************************************************
FUNKTION: setEpsilon
PACKAGE: siassei
SCOPE: public
DETAILS:
BESCHREIBUNG:
RÜCKGABEWERT:
ARGUMENTE:
EXCEPTION:
*****************************************************************************************/
template<class T>
inline void siassei::Beleg108<T>::setEpsilon(T val) {
this->epsilon = val;
};
/*****************************************************************************************
FUNKTION: getEspilon
PACKAGE: siassei
SCOPE: public
DETAILS:
BESCHREIBUNG:
RÜCKGABEWERT:
ARGUMENTE:
EXCEPTION:
*****************************************************************************************/
template<class T>
inline T siassei::Beleg108<T>::getEspilon() const {
return this->epsilon;
};
/*****************************************************************************************
FUNKTION: calcSinh
PACKAGE: siassei
SCOPE: public
DETAILS:
BESCHREIBUNG:
RÜCKGABEWERT:
ARGUMENTE:
EXCEPTION:
*****************************************************************************************/
template<class T>
void siassei::Beleg108<T>::calcaSinh() {
this->clear();
this->calcWithSummenformel();
// Hier nach einer erfolgreicher Implementierung der Alternativen
// möglichkeit, könnte man hier den Alg. bestimmen
// this->calcWithAlternative();
};
/*****************************************************************************************
FUNKTION: calcSinh
PACKAGE: siassei
SCOPE: private
DETAILS:
BESCHREIBUNG:
RÜCKGABEWERT:
ARGUMENTE:
EXCEPTION:
*****************************************************************************************/
template<class T>
inline void siassei::Beleg108<T>::clear() {
this->zahler = 1;
this->nenner = 2 * 3;
this->k = 3;
this->cnt = 0;
this->epsilon = numeric_limits<T>::epsilon();
};
/*****************************************************************************************
FUNKTION: validateResult
PACKAGE: siassei
SCOPE: private
DETAILS:
BESCHREIBUNG: Diese Funktion verlgeicht das übermittelte Ergebnis
mit dem vorhandenen. Wenn Der Wertunterschied kleiner als
das Epsilon ist oder die max. Iterationsanzahl über-
schritten wurde.
RÜCKGABEWERT: true or false
ARGUMENTE: T newResult
EXCEPTION:
*****************************************************************************************/
template<class T>
inline bool siassei::Beleg108<T>::validateResult(T newRes) const {
return (this->cnt >= Beleg108<T>::MAX_ITERATIONEN ||
(newRes + this->epsilon) > this->res && (newRes - this->epsilon) < this->res);
};