Quadratic equation solver with C++ (long post)

Posted on Updated on

I took from the webpage ( http://www.purplemath.com/modules/quadform.htm ) this example:

Here are some examples of how the Quadratic Formula works:

Solve x2 + 3x – 4 = 0
This quadratic happens to factor:

x2 + 3x – 4 = (x + 4)(x – 1) = 0

On paper the process looks like this:

qform02

6 years ago I wrote this Quadratic Equation solver program using C. If you can understand how it works then um .. let me know because I forgot ..

/* **************************************************************
Quadratic Equation solver program
Name: Ben Lo Date: 32/02/09
*************************************************************** */
#include<stdio.h>
#include<math.h> /* include math.h to calculate square root*/
 
double stageOne(int a, int b, int c);
double stageTwo(int b, double tempA, double tempB, double tempC);
double stageThree(int b, double tempA, double tResult);
double stageThreeAdd(int b, double tempA, double tResult);
double stageThreeSubtract(int b, double tempA, double tResult);
 
/* calculate values for x1 with the addition method */
double stageThreeAdd(int b, double tempA, double tResult2) {
double tResult4, tResult3;
 
printf("----------------------\n");
tResult3 = (b - tResult2);
printf("Value of b: %f\n", b);
 
tResult4 = (tResult3 / tempA);
printf("x1 = %f \n", tResult4);
printf("-----------------------\n");
 
}
 
// ================================================================
 
/* calculate values for x2 with subtraction */
double stageThreeSubtract(int b, double tempA, double tResult2) {
double tResult4, tResult3;
 
/* the formula states that when calculating x1 & x2 *
* b must be -b */
printf("-----------------------\n");
tResult3 = (b + tResult2);
printf("Value of b: %f\n", tResult3);
 
tResult4 = (tResult3 / tempA);
printf("x2 = %f \n", tResult4);
printf("------------------------\n");
 
}
 
 
// ===============================================================
 
/* this function works out the 3rd calculation in 2 different ways */
double stageThree(int b, double tempA, double tResult2) {
 
b = -b;
 
stageThreeAdd(b , tempA, tResult2);
stageThreeSubtract(b , tempA, tResult2);
 
}
 
// ===============================================================
 
/* this function works out the 2nd calculation */
double stageTwo(int b, double tempA, double tempB, double tempC) {
float tResult, tResult2;
 
tResult = (tempB + tempC);
tResult2 = sqrt(tResult);
 
printf("( b2-4ac ) : %f\n", tResult);
printf("-b ± sqrt( b2-4ac ) : %f\n", tResult2);
printf("\n");
 
if (tResult
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("ERROR: cannot display square root of this negative number\n");
printf(" as this will create an imaginary number.\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
stageThree(b, tempA, tResult2);
 
}
 
// =================================================================
/* This function works out the first calculation */
double stageOne(int a, int b, int c){
double tempA, tempB, tempC;
 
tempB = (b * b); /* square B */
tempC = ((a * c) * -4); /* times a to c then times it by -4 */
tempA = (a * 2); /* times a by 2 */
 
printf("-b ± sqrt( b2-4ac ) : %f , %f , %f\n",tempA, tempB, tempC);
stageTwo(b, tempA, tempB, tempC);
// call to function stageTwo to calculate further
}
 
/* --------------------------------------------------------------- */
 
int main(){
 
int a, b, c;
 
printf("\n---------------------------------------------.\n");
printf(". .\n");
printf(". x1,2 = -b ± sqrt( b2-4ac ) .\n");
printf(". ------------------- .\n");
printf(". 2a .\n");
printf(". .\n");
printf("---------------------------------------------\n");
printf("Quadratic Equation solver program.\n");
printf("Enter the quadratic parameters a, b and C: \n");
scanf("%d %d %d", &a, &b, &c);
printf("\n");
printf("*********************************************\n");
printf("\n");
 
stageOne(a, b, c);
}

Today I wrote a very quick but clean(er) version of the program I pasted above but this time using C++.

The header file:

// #include "firstSolution.h"
#ifndef _FIRST_SOLUTION_H
#define _FIRST_SOLUTION_H
 
class firstSolution {
private:
    int a_value;
    int b_value;
    int c_value;
    int total;
 
protected:
    int result[2];
 
public:
    firstSolution();   // コンストラクタ
    ~firstSolution();  // デストラクタ
 
    void enterValues();
    void getValues();
    void calculateSquareRoot(int a, int b, int c);
};
 
#endif // _FIRST_SOLUTION_H

And the cpp file:

#include "firstSolution.h"
#include <math.h>
#include <iostream>
 
using namespace std;
 
firstSolution::firstSolution() {
    // コンストラクタ 
}
 
firstSolution::~firstSolution() {
    // デストラクタ 
}
 
/*
 * a、b、cの値を取得
 */
void firstSolution::enterValues() {
    cout << "aの値を入力してください: " << "\n";
    cin >> a_value;
 
    cout << "bの値を入力してください: " << "\n";
    cin >> b_value;
 
    cout << "cの値を入力してください: " << "\n";
    cin >> c_value;
 
    cout << "aの値を [ " <<  a_value << " ]として入力されました。\n";
    cout << "bの値を [ " <<  b_value << " ]として入力されました。\n";
    cout << "cの値を [ " <<  c_value << " ]として入力されました。\n";
 
    this->calculateSquareRoot(a_value, b_value, c_value);
}
 
/*
 * 平方根の計算
 */
void firstSolution::calculateSquareRoot(int a, int b, int c) {
    total = ((b*b) + (4 * (a*c)));                      //ax2 + bx + c = 0
    result[0] = (((b * -1) - (sqrt(total))) / 2);       //平方根計算する前の +
    result[1] = (((b * -1) + sqrt(total)) / 2);         //平方根計算する前の ー
}
 
/*
 * 処理された回答を取得
 */
void firstSolution::getValues() {
    cout << "(+)計算後:" <<  result[0] << "\n";
    cout << "(ー)計算後:" <<  result[1] << "\n";  
}
 
/*
 *  ax2 * bx + c = 0 は実際にax2 * bx - c = 0になっているか後に判定処理を書く。
 */
int main () {
    firstSolution fs;
 
    fs.enterValues();
    fs.getValues();
    return 0;
}

When run, you enter values for a, b, and c and it will calculate (according to the example in the website) the results for you:

aの値を入力してください:
1
bの値を入力してください:
3
cの値を入力してください:
4
aの値を [ 1 ]として入力されました。
bの値を [ 3 ]として入力されました。
cの値を [ 4 ]として入力されました。
(+)計算後:-4
(ー)計算後:1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s