Decay Simulator

venomlash 0 Tallied Votes 177 Views Share

This can be used for plotting radioactive decay of an unlimited* number of elements, decaying into each other.

*Limited only by memory and user attention span.

//element.h
#ifndef ELEMENT_H
#define ELEMENT_H
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

class element{
    private:
        string name;
        int decayto;
        int probdec;
        int numberof;
    public:
        element();
        void decin();
        void decout();
        void setdecto(int decto);
        void setprobdec(int prob);
        string getname();
        int getdec();
        int getprob();
        int getnum();
};
#endif

//element.cpp
#include "element.h"
using namespace std;

element::element(){
    cout<<"What is the name of the element/isotope?"<<endl;
    cin>>name;
    cout<<"How many "<<name<<" atoms are there at the start?"<<endl;
    cin>>numberof;
}
void element::decin(){
    numberof++;
}
void element::decout(){
    numberof--;
}
void element::setdecto(int decto){
    decayto=decto;
}
void element::setprobdec(int prob){
    probdec=prob;
}
string element::getname(){
    return name;
}
int element::getdec(){
    return decayto;
}
int element::getprob(){
    return probdec;
}
int element::getnum(){
    return numberof;
}

#include "element.h"
#include <vector>
#include <ctime>
#include <time.h>
using namespace std;

int randnum(){
    srand(rand()/2);
    return 1+100*rand()/(RAND_MAX+1);
}
vector <element> edit(vector <element> vec);
vector <element> decay(vector <element> vec);

int main(){
    int numof;
    vector <element> lmnts;
    cout<<"How many elements are involved?"<<endl;
    cin>>numof;
    for(int i=0; i<numof; i++){
        element atom;
        lmnts.push_back(atom);
    }
    lmnts=edit(lmnts);
    int conditions;
    cout<<"When will this simulation end?\n1: Time expires.\n2: One or more elements decays to";
    cout<<" zero."<<endl;
    cin>>conditions;
    if(conditions==1){
        int iter;
        cout<<"How many times should the decay simulator iterate?"<<endl;
        cin>>iter;
        for(int t=0; t<iter; t++){
            lmnts=decay(lmnts);
        }
        cout<<"Time has expired.";
    }
    else if(conditions==2){
        int all=lmnts.size();
        while(all==lmnts.size()){
            lmnts=decay(lmnts);
            all=0;
            for(int j=0; j<lmnts.size(); j++){
                if(lmnts[j].getnum()>0){
                    all++;
                }
            }
        }
        vector <element> blank;
        lmnts=blank;
        cout<<"One or more elements has decayed to zero.";
    }
    cout<<" Run again? (y/n)"<<endl;
    string input;
    cin>>input;
    if((input=="y")||(input=="Y")){
        int main();
        return 0;
    }
    else{
        return 1;
    }
}

vector <element> edit(vector <element> vec){
    for(int j=0; j<vec.size(); j++){
        cout<<"What does "<<vec[j].getname()<<" decay to?"<<endl;
        for(int i=0; i<vec.size(); i++){
            cout<<i+1<<": "<<vec[i].getname()<<endl;
        }
        int input;
        cin>>input;
        vec[j].setdecto(input);
        cout<<"What is the probability that in any given iteration that\n"<<vec[j].getname();
        cout<<" will decay? (If x%, enter x)."<<endl;
        cin>>input;
        vec[j].setprobdec(input);
    }
    return vec;
}

vector <element> decay(vector <element> vec){
    for(int r=0; r<vec.size(); r++){
        int decayed=0;
        for(int c=0; c<vec[r].getnum(); c++){
            int chance=randnum();
            if(vec[r].getprob()>=chance){
                decayed++;
                vec[r].decout();
                vec[vec[r].getdec()-1].decin();
            }
        }
        cout<<decayed<<" atoms of "<<vec[r].getname()<<" have decayed."<<endl;
        cin.ignore();
    }
    cout<<"Total atoms remaining:"<<endl;
    for(int n=0; n<vec.size(); n++){
        cout<<vec[n].getnum()<<" atoms of "<<vec[n].getname()<<" remaining."<<endl;
    }
    cout<<"Next iteration begins."<<endl;
    cin.ignore();
    return vec;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.