-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository
/
Copy pathmain.cpp
More file actions
More file actions
Latest commit
135 lines (126 loc) · 4.37 KB
/
main.cpp
File metadata and controls
135 lines (126 loc) · 4.37 KB
You must be signed in to make or propose changes
More edit options
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <iomanip>
struct Measurement {
std::string character;
std::string unit;
int multiplier;
};
struct Isotopes {
std::string name;
long double value;
};
int main()
{
/* This is a multi-line comment.
* the matching asterisks to the left
* can make this easier to read
*/
/* if they choose 'seconds' then the half-lives (in the form of years,
* scientific notation) are converted into seconds by multiplying
* them by the number of seconds in the gregorian calendar.
* dividing them by 3600 (amount of seconds in an hour)
* yields hours. */
const int GREGORIAN_SECONDS = 31556952;
std::vector<Measurement> measurement = {
{"s","second",GREGORIAN_SECONDS/1},
{"m","minute",GREGORIAN_SECONDS/60},
{"h","hour",GREGORIAN_SECONDS/3600},
{"d","day",GREGORIAN_SECONDS/86400},
{"y","year",GREGORIAN_SECONDS/GREGORIAN_SECONDS}
};
std::string errorMessage = "Invalid character. ";
/* user prompt. 'print' = (std::cout <<). 'submit' = (std::cin >>).
* exitLoop1 is initialized as false. while (exitLoop1)
* means "while exitLoop1 is true". the (!) makes it negative.
* exitLoop1 only becomes "true" by entering valid characters.
* "if-else" conditionals are utilized instead of "switch" statements
* for their ability to use strings and logical comparisons. */
bool exitLoop1 = false;
std::string response1{};
int responseIndex1{};
int count1{};
std::cout << "Would you like to measure in:\n";
for (const auto & m:measurement) {
std::cout << "<" << measurement[count1].character << "> " << measurement[count1].unit << "s\n";
count1 += 1;
}
while (!exitLoop1) {
std::cin >> response1;
if (response1 == measurement[0].character) {
responseIndex1 = 0;
exitLoop1 = true;
}
else if (response1 == measurement[1].character) {
responseIndex1 = 1;
exitLoop1 = true;
}
else if (response1 == measurement[2].character) {
responseIndex1 = 2;
exitLoop1 = true;
}
else if (response1 == measurement[3].character) {
responseIndex1 = 3;
exitLoop1 = true;
}
else if (response1 == measurement[4].character) {
responseIndex1 = 4;
exitLoop1 = true;
}
else {
std::cout << errorMessage;
}
}
std::vector<Isotopes> isotopes =
{
{"Uranium-233",1.592e5*measurement[responseIndex1].multiplier},
{"Uranium-235",7.04e8*measurement[responseIndex1].multiplier},
{"Uranium-238",4.463e9*measurement[responseIndex1].multiplier},
{"Plutonium-239",2.411e4*measurement[responseIndex1].multiplier},
{"Thorium-232",1.40e10*measurement[responseIndex1].multiplier}
};
// user prompt
bool exitLoop2 = false;
int response2{};
int count2 = 0;
std::cout << "Select a fertile radioactive isotope:\n";
for (const auto & i:isotopes) {
count2 += 1;
std::cout << "<" << count2 << "> " << i.name << '\n';
}
while (!exitLoop2) {
std::cin >> response2;
if (response2 == 1) {
exitLoop2 = true;
}
else if (response2 == 2) {
exitLoop2 = true;
}
else if (response2 == 3) {
exitLoop2 = true;
}
else if (response2 == 4) {
exitLoop2 = true;
}
else if (response2 == 5) {
exitLoop2 = true;
}
else {
std::cout << errorMessage;
}
}
/* since the index "[]" for a vector begins at 0, "-1" is used
* (since user responses begin at 1). if this was not used,
* then response2 would equal 6 instead of 5. since 6 is not
* present in the index, it would not return anything. */
int responseIndex2{response2-1};
/* decay constant | the natural logarithm of 2 divided by the half life.
* print the decay constant as a number with 20 decimal places. */
const double decay = (std::log(2)) / isotopes[responseIndex2].value;
std::cout
<< "Probability per " << measurement[responseIndex1].unit << " for a single " << isotopes[response2-1].name
<< " nucleus to decay: " << std::fixed << std::setprecision(25) << decay;
return 0;
}