/
main.rb
163 lines (160 loc) · 8.26 KB
/
main.rb
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require_relative "chemical_parser.rb"
require_relative "chemical_analysis.rb"
def askForMc(reaction)
puts "Reactants:"
reaction[:reactants].each_with_index do |product, i|
puts " (#{i}): #{chemical_formula_to_string(product[:chemical])}"
end
puts "Products:"
reaction[:products].each_with_index do |product, i|
puts " (#{i+reaction[:reactants].length}): #{chemical_formula_to_string(product[:chemical])}"
end
chemIndex = gets.chomp.to_i
if chemIndex >= reaction[:reactants].length
return reaction[:products][chemIndex - reaction[:reactants].length]
else
return reaction[:reactants][chemIndex]
end
end
def askForUnit()
puts "(l): liters","(mc): molecules","(g): grams"
unit = gets.chomp.downcase
if unit == "l"
return :liters
elsif unit == "mc"
return :molecules
elsif unit == "g"
return :grams
end
end
def unitToAbbr(unit)
if unit == :liters
return "L"
elsif unit == :molecules
return "mc"
elsif unit == :grams
return "g"
end
end
def unitToRatio(unit, formula)
if unit == :liters
return 22.4
elsif unit == :molecules
return 6.022e23
elsif unit == :grams
return molar_mass(formula)[:mass]
end
end
if __FILE__ == $0
puts %Q(┌────────────────────────────────────────────────────┐
│░█▀▀░▀█▀░█▀█░▀█▀░█▀▀░█░█░▀█▀░█▀█░█▄█░█▀▀░▀█▀░█▀▄░█░█│
│░▀▀█░░█░░█░█░░█░░█░░░█▀█░░█░░█░█░█░█░█▀▀░░█░░█▀▄░░█░│
│░▀▀▀░░▀░░▀▀▀░▀▀▀░▀▀▀░▀░▀░▀▀▀░▀▀▀░▀░▀░▀▀▀░░▀░░▀░▀░░▀░│
└────────────────────────────────────────────────────┘)
loop do
puts "Hello, and welcome to the stoichiometry toolbox. Type `r` for a reaction or `f` for a single chemical formula, then strike `Return`."
programMode = gets.chomp.downcase
if programMode == "r"
puts "Input the reactant(s), separated by a `+`. Note: do not add the coefficients. They will be automatically generated."
reactantStrings = gets.chomp.gsub(/\s/, "").split("+")
reactantFormulas = reactantStrings.map { |reactantString| parse_chemical_formula(lex_chemical_formula(reactantString)) }
puts "Input the product(s), separated by a `+`."
productStrings = gets.chomp.gsub(/\s/, "").split("+")
productFormulas = productStrings.map { |productString| parse_chemical_formula(lex_chemical_formula(productString)) }
reaction = create_reaction_from_chemicals(reactantFormulas, productFormulas)
print "main(): reaction:" if $DEBUG
pp reaction if $DEBUG
balancedReaction = balance_reaction(reaction)
puts "Original reaction:"
puts " #{reaction_to_string(reaction)}"
puts "Balanced reaction:"
puts " #{reaction_to_string(balancedReaction)}"
loop do
puts "What would you like to do with this reaction?"
puts " Perform a stoichiometric (c)onversion"
puts " Find the (l)imiting reactant"
puts " (Q)uit to the main page"
programBranch = gets.chomp
programBranch.downcase!
if programBranch == "c"
puts "What unit are you asking for?"
wantedUnit = askForUnit()
puts "Of?"
wantedMc = askForMc(balancedReaction)
puts "What quantity are you given?"
givenQuantity = gets.chomp.to_f
puts "What unit are you given?"
givenUnit = askForUnit()
puts "Of?"
givenMc = askForMc(balancedReaction)
puts stoichiometric_chart(
givenQuantity,
unitToRatio(givenUnit, givenMc[:chemical]), unitToAbbr(givenUnit), chemical_formula_to_string(givenMc[:chemical]),
[wantedMc[:coefficient], givenMc[:coefficient]],
unitToRatio(wantedUnit, wantedMc[:chemical]), unitToAbbr(wantedUnit), chemical_formula_to_string(wantedMc[:chemical])
)
elsif programBranch == "l"
puts "Which product is the desired product? (or type `n` for none)"
balancedReaction[:products].each_with_index do |product, i|
puts "(#{i}): #{chemical_formula_to_string(product[:chemical])}"
end
desiredProductInput = gets.chomp.downcase
reactantMasses = []
balancedReaction[:reactants].each_with_index do |reactant, i|
puts "How many grams of #{chemical_formula_to_string(reactant[:chemical])} are in the reaction?"
reactantMasses[i] = gets.chomp.to_f
end
if desiredProductInput != "n"
desiredProduct = balancedReaction[:products][desiredProductInput.to_i]
puts "Stoichiometric charts:"
balancedReaction[:reactants].each_with_index do |reactant, i|
puts "\e[4m#{chemical_formula_to_string(reactant[:chemical])}:\e[0m"
puts stoichiometric_chart(
reactantMasses[i],
molar_mass(reactant[:chemical])[:mass], "g", chemical_formula_to_string(reactant[:chemical]),
[desiredProduct[:coefficient], reactant[:coefficient]],
molar_mass(desiredProduct[:chemical])[:mass], "g", chemical_formula_to_string(desiredProduct[:chemical])
)
puts
end
else
puts "Stoichiometric charts:"
balancedReaction[:reactants].each_with_index do |reactant, i|
puts "\e[4m#{chemical_formula_to_string(reactant[:chemical])}:\e[0m"
puts stoichiometric_chart(
reactantMasses[i],
molar_mass(reactant[:chemical])[:mass], "g", chemical_formula_to_string(reactant[:chemical]),
nil,
nil, nil, nil
)
puts
end
end
elsif programBranch == "q"
break
end
end
elsif programMode == "f"
puts "Input a chemical formula."
formula = parse_chemical_formula(lex_chemical_formula(gets.chomp))
print "main(): formula:" if $DEBUG
pp formula if $DEBUG
molarMass = molar_mass(formula)[:mass].round(1)
puts "Molar mass equation:"
puts " #{molar_mass(formula)[:string]}"
puts "Molar mass:"
puts " #{molarMass} g."
puts "Percent composition:"
get_chemical_sum(formula).each do |chemSym, count|
puts "\e[4m#{chemSym.to_s}:\e[0m"
topLine = (count * $PeriodicTable[chemSym.to_s].round(1))
bottomLine = molarMass
puts " #{topLine} "
puts " " + ("-" * ([topLine.to_s.length,bottomLine.to_s.length].max + 2)) + " = #{100* topLine / bottomLine}%"
puts " #{bottomLine} "
end
else
puts "Invalid program mode. Exiting..."
end
end
end