Wednesday, June 7, 2017

Bubbles

/*This code was written by A. El-Gadi of Tripoli University for educational purposes. The code is a model answer to a Spring-2017 2nd exam question. For any questions use the comment section below.*/

#include <iostream>
#include <cmath>

using namespace std;

const long double ldPI = 3.141592653589793238L;

class Air{
public:
 double quant;
 Air():quant(0){};
 Air(double q){quant=q;}
};


class Bubble{
    private:
    const double minthickness;// = 10;
    const double maxthickness;// = 1000;

    double matam;//material amount
    Air airam;

    double innerradius(){return pow(airam.quant*3.0/(4.0*ldPI),1.0/3.0);}
    double outerradius(){return pow((airam.quant+matam)*3.0/(4.0*ldPI),1.0/3.0);}

   

    bool impossibleBubble(){return thickness()>maxthickness||thickness()<minthickness;};
   
    void burst(){airam=0; matam=0;}
   
    public:

    double thickness(){return outerradius()-innerradius();}
    double getMatam(){return matam;}
    double getAir(){return airam.quant;}

    Bubble():minthickness(10), maxthickness(1000), matam(0), airam(Air()){};
    Bubble(double m, Air a):minthickness(10), maxthickness(1000), matam(m), airam(a) {if(impossibleBubble()){burst();}}

    Bubble operator+(Bubble b){double ml=matam+b.matam; double airl=airam.quant+b.airam.quant; return Bubble(ml,Air(airl));}
    Bubble operator+(Air a){double airl=airam.quant+a.quant; return Bubble(matam,Air(airl));}
    Bubble operator*(double factor){double airl=airam.quant*factor; return Bubble(matam,Air(airl));}

    Bubble &operator=(Bubble b){matam=b.matam; airam=b.airam; return *this;}
};

int main(){

Bubble b1(1e8, Air(10));
Bubble b2(0.5e8,Air(6));
Bubble b3;

Air a1(200);

b3=b1+b2;

cout<<b1.thickness()<<'\n';
cout<<b2.thickness()<<'\n';
cout<<b3.thickness()<<'\n';

cout<<b1.getMatam()<<'\n';
cout<<b2.getMatam()<<'\n';
cout<<b3.getMatam()<<'\n';


cout<<b1.getAir()<<'\n';
cout<<b2.getAir()<<'\n';
cout<<b3.getAir()<<'\n';

b1=b1+a1;

cout<<b1.thickness()<<'\n';
cout<<b2.thickness()<<'\n';
cout<<b3.thickness()<<'\n';

cout<<b1.getMatam()<<'\n';
cout<<b2.getMatam()<<'\n';
cout<<b3.getMatam()<<'\n';


cout<<b1.getAir()<<'\n';
cout<<b2.getAir()<<'\n';
cout<<b3.getAir()<<'\n';

b3=b3*2;

cout<<b1.thickness()<<'\n';
cout<<b2.thickness()<<'\n';
cout<<b3.thickness()<<'\n';

cout<<b1.getMatam()<<'\n';
cout<<b2.getMatam()<<'\n';
cout<<b3.getMatam()<<'\n';


cout<<b1.getAir()<<'\n';
cout<<b2.getAir()<<'\n';
cout<<b3.getAir()<<'\n';
}

No comments:

Post a Comment