-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBohr_Energy_Calc_A1.cpp
120 lines (92 loc) · 3.82 KB
/
Bohr_Energy_Calc_A1.cpp
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
// Assignment 1 - calculate transition energy using simple Bohr formula
//Include headers
#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<limits>
using namespace std;
//Define Global variables
const double electron{ 1.602e-19 };
bool y {true};
//Define Functions
//Electron volt function
double photon_energy_eV(int atomic_number, int initial_quantumNumber, int final_quantumNumber){
double energy_diff;
energy_diff = -13.6*pow(atomic_number,2)*((1 / pow(initial_quantumNumber,2)) - (1 / pow(final_quantumNumber,2)));
return energy_diff;
}
//Joules function
double photon_energy_J(int atomic_number, int initial_quantumNumber, int final_quantumNumber){
double energy_diff;
energy_diff = -13.6*electron*pow(atomic_number, 2)*((1 / pow(initial_quantumNumber, 2)) - (1 / pow(final_quantumNumber, 2)));
return energy_diff;
}
int main(){
while (y){
//declare variables
int n_initial, n_final;
double Energy_J;
double Energy_eV;
int atomic_number;
// Ask user to enter atomic number
cout << "Please enter the atomic number you, Z:" << endl;
cin >> atomic_number; cout << endl;
//check atomic number is valid
while (cin.fail() || atomic_number > 118 || atomic_number <= 0 || cin.peek() != '\n'){
cout << "Sorry the atomic number was not vaild!" << endl << " Remember it must be an integer within 1 and 118." << endl;
cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cin >> atomic_number;
}
// Ask user to enter initial and final quantum numbers
cout << "Please enter your initial quantum value followed by your final quantum value:" << endl;
cin >> n_initial; //input initial QM state
cin >> n_final; //input final QM state
//test if QM # positive integer
while (cin.fail() || n_initial <= 0 || n_final <= 0 || cin.peek() != '\n'){
cout << "Quantum nubers need to be positive integer and non-zero" << endl << "please eneter the initial quantum state followed by the final state:" << endl;
cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> n_initial;
cin >> n_final;
}
//test if initial QM # > final QM #
while (n_initial < n_final || n_initial == n_final){
cout << "Initial Quantum state must be greater than final state to emitt photon" << endl << "please eneter the initial quantum state followed by the final state" << endl;
cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> n_initial;
cin >> n_final;
}
// ask if he want answer in eV or Joules
while (true){
cout << "Do you want your answer in Joules or eV?" << endl;
string conversion;
vector <string> poss_J{ "Joules", "joules", "J", "j", "Joule", "joule" }; //accepted Joule inputs
vector <string> poss_eV{ "e", "eV", "EV", "ev", "electron Volts" }; //accepted eV inputs
cin >> conversion;
//User has selected Joules and spit out answer
if (std::find(poss_J.begin(), poss_J.end(), conversion) != poss_J.end()){
Energy_J = photon_energy_J(atomic_number, n_initial, n_final);
cout << "The photon energy of the energy transition is: " << setprecision(4) << Energy_J << " J" << endl;
break;
}
//User has selected eV and display answer
if (std::find(poss_eV.begin(), poss_eV.end(), conversion) != poss_eV.end()){
Energy_eV = photon_energy_eV(atomic_number, n_initial, n_final);
cout << "The photon energy of the energy transition is: " << setprecision(4) << Energy_eV << " eV" << endl;
break;
}
//No valid unit selection
else{
cout << "You have not selected Joules or eV." << endl;
}
}
//repeat operation
cout << "Repeat? [y/n]" << endl;
char choice;
cin >> choice;
if (choice == 'n'){
y = false;
}
}
return 0;
}