// Practice Lab Midterm

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>

using namespace std;

bool isInt (double value) {
    double dummy;
    return bool(modf(value, &dummy) == 0);
}

double sqr(double value) {
    return value * value;
}

int main (void) {

    // Constants
    const double E = 207e9; //  Young's modulus for the material

    // Variables
    double p = 0, //  is the interface pressure (in Pa)
           b = 0, //  is the radius of the hub (in m)
           c = 0, // is the radius of the hub plus the tire thickness (in m)
           inter = 0, // interferance
           d = 0, // diameter
           t = 0, // tire thickness
           sumP = 0, // The sume of pressures
           aveP = 0, //  the average of all computed interface pressures
           count = 0, // # of computed pressures
           greatestP = 0, //  the greatest interface pressure
           greatestd = 0,
           greatestT = 0, // greatest tire thickness
           greatestInter = 0;

    // Get values from user
    cout << "Enter diameter, tirethickness, and interferance (-1 -1 -1 to stop) :";
    cin >> d >> t >> inter;

    while (!(d==-1 && t==-1 && inter==-1)) {
        if (!(d>=0.5 && d<= 2.5 && t >= 0.05 && t<=0.1 && inter >0 && inter < ( d *(0.5/100) ) )) {
            cout << "Error, unreasnable values";
            cout << endl;
            // do calculations
        } else {

            b = d/2;
            c = b+t;


            p = (inter/b) / ( (1/E) * (1 +( (pow(c,2)+ pow(b,2)) / (pow(c,2) - pow(b,2)))));
            cout << "The interferance pressure is " << p << " Pa" << endl;
            aveP = sumP / count;
            count = count + 1;

            if (( count==1 || p >= greatestP)) {

                greatestP = p;
                greatestT = t;
                greatestd = d;
                greatestInter = inter;

            } // end if



        } // end if

        cout << "Enter diameter, tirethickness, and interferance (-1 -1 -1 to stop) :";
        cin >> d >> t >> inter;

    } // end while
    if (count>0) {
        cout << "The average of all computed interface pressures is:  " << aveP << "Pa, and  the greatest interface pressure is: " << "greatestP" << " Pa, and the corresponding diameter is:" << greatestd << " m, tire thickness is: " << greatestT << "m, and interference is: " << greatestInter << " m";
        cout << endl;
    } // end if

    system("PAUSE"); return 0;

}
