PQ-Formel in C++

Romsl

Erfahrenes Mitglied
Hi,

ich muss ein Programm schreiben, dass durch Kommandozeilenparameter die reellen Lösungen ausrechnet (Ax^2+Bx+C=0) => Argumente (./program A B C)
Es soll auch wieder jeglicher verbrauchter Speicher freigegeben werden.

Jetzt meine Frage, ist der folgende Programmcode korrekt?

Code:
#include <iostream>
#include <Math.h>

using namespace std;

/*
  Description: These program calculate the quadratic equation. The form
  looks like: Ax^2 + Bx + C = 0. These three coefficient should be enter
  into the command line statement.
*/
class CalcQuad
{
    public:
        
        //"Diskriminante" will be stored here
        double d;
        
        //calculation results will be stored here
        double result1;
        double result2;
        
        /*
         * standard constructor
         */
        CalcQuad()
        {
            //empty constructor
        }    
        
        void solveQuadratic(double a, double b, double c)
        {
            //Calculation of the "Diskriminante"
            d = ((b * b) - (4 * a * c));
            
            /* If the "Diskrimante" is bigger than 0 then the result of the calculation
             * have two real numbers, if the "Diskriminante" is 0 then the result is one
             * real number, else if the "D" is lower than 0 there is no real number, else
             * the calculation deliver no result.
             */
            if (d == 0)
            {                
                result1 = ((-b) / (2 * a));
                
                cout << "Es gibt eine reelle Loesung" << endl;
                
                cout << "x = " << result1 << endl;
            }
            else if (d > 0)
            {
                result1 = (((-b) + d) / (2 * a));
                result2 = (((-b) - d) / (2 * a));
                
                cout << "Es gibt zwei reelle Loesungen" << endl;
                
                cout << "x1 = " << result1 << endl;
                cout << "x2 = " << result2 << endl;
            }
            else if (d < 0)
            {
                cout << "Es gibt keine reelle Loesung" << endl;
            }
            else
            {
                cout << "Programm-Fehler. Bitte erneut versuchen!" << endl;
            }
        }
        
        /*
         * Destructor of the class CalcQuad freed the memory.
         */
        ~CalcQuad()
        {   
            delete &d;
            //delete &result1;
            //delete &result2;    
        }    
};        

/* Starting point of the calculation program. All three command line statements will be
 * read in three variables x, y and z. These double variables will be delivered to the
 * solveQuadratic function.
 */
int main(int argc, char *argv[])
{
    if (argc == 4)
    {
        //convert chars to double values
        double x = atof(argv[1]);
        double y = atof(argv[2]);
        double z = atof(argv[3]);
        
        CalcQuad *calculation = new CalcQuad();
  
        calculation->solveQuadratic(x, y, z);
        
        delete calculation;
    }
    else
    {
        cout << "Um die Berechnung der Quadratischen Gleichung durchfuehren zu koennen" << endl;
        cout << "muessen die Parameter in folgender Reihenfolge eingegeben werden." << endl;
        cout << "A B C (Ax^2 + Bx + C = 0)" << endl;
        cout << "" << endl;
        cout << "Beispiel: ./CalcQuad 3 6 2" << endl;
    }

    return 0;
}

Danke für jede Art von Hilfe

Romsl
 
Hi,
1.) wenn du noch die Wurzel der Diskriminante im Fall von 2 Lösungen ziehst
sollte das passen:

Code:
...
else if (d > 0)      
            {
                result1 = (((-b) + sqrt(d))/ (2 * a));
                result2 = (((-b) - sqrt(d)) / (2 * a));                                                                                    
                
                cout << "Es gibt zwei reelle Loesungen" << endl;                                                                           
                
                cout << "x1 = " << result1 << endl;
                cout << "x2 = " << result2 << endl;                                                                      
}                  
...
2.)
Code:
            else
            {
                cout << "Programm-Fehler. Bitte erneut versuchen!" << endl;                                                                
            }
Das kannst du dir sparen...

3.)

DITO, da d nicht dynamisch erzeugt wird und eine Member der Klasse CalcQuad vom Typ double ist, wird es eh gelöscht wenn du dein "calculation" Objekt
löschst....


Ich hoffe ich hab nix übersehen...

Gruß

RedWing
 
Zurück