A chemical balancing tool in C++ (new version) by Frank Yang.
ChemicalBalancingUtilityNew
(abbr. CBU_Balancer
) is a C++ header that includes methods for balancing your chemical equations. With perfect encapsulation, you can efficiently and conveniently balance equations anywhere in your program. My program also comes with a CBU_Console
library, an original console designed for users who do not wish to delve deeply into C++.
The following program uses the CBU_Console
class that comes with my project. The boot()
method of this class will open a console where you can balance equations freely.
- Create a new project and
main.cpp
. Be sure your project version is C++17 or later. - Download
CBU_Balancer.h
andCBU_Console.h
and include them in yourmain.cpp
:#include "CBU_Balancer.h" #include "CBU_Console.h"
- Call
CBU_Console
in yourmain.cpp
and then you can use the powerfulChemicalBalancingUtility
!int main() { CBU_Console console = CBU_Console(); console.boot(); return 0; }
- In the following console, type
quit()
to quit the console; typemultiple_results(off)
to disable multiple results, typemultiple_results(on)
to allow it. Multiple results is enabled by default (which is to provide at least one absolutely correct answer). Directly type your chemical equation to call the built-inChemicalBalancingUtility
to balance, for example,HNO3 -> NO2 + O2 + H2O
CBU_Balancer
is the main class. This class allows you to balance equations that appear in your program (this is an extension of C++ functionality).
Use CBU_Balancer balancer = CBU_Balancer()
to create a new ChemicalBalancingUtilityNew
instance (all the needed features must be implemented through this instance);
void balance(const std::string& equation)
receives a chemical equation as input and try to balance it. The balancing data will be stored inbalancer
. E.g.,balancer.balance("C+O2->CO2");
void balance_with_given_compounds(const std::vector<std::string>& reactants, const std::vector<std::string>& products)
receives twostd::vector<std::string>
objects (as reactants and products) and try to balance them. E.g.,balancer.balance_with_given_compounds({"Zn", "HCl"}, {"ZnCl2", "H2"});
std::pair<std::vector<std::string>, std::vector<std::string>> get_reactants_and_products()
returns an object where itsfirst
is the stored reactants vector and itssecond
is the products vector. E.g.,auto reactants_and_products = balancer.get_reactants_and_products(); auto reactants = reactants_and_products.first; auto products = reactants_and_products.second;
std::pair<std::vector<std::string>, std::vector<std::vector<int>>> get_main_matrix()
returns the information of stored equation data. Itsfirst
is the elements occurred in the equation, and itssecond
is the main matrix of the equation. For the main matrix, its row is each element (sorted bystd::sort
) and its column is each compound (by given order);void clear_data()
clears all stored balancing data. This is a must when you're to balance another equation.- The following shows an overall sample:
balancer.balance("C+O2->CO2"); std::cout << balancer.get_result() << std::endl; balancer.clear_data(); balancer.balance_with_given_compounds({"Zn", "HCl"}, {"ZnCl2", "H2"}); std::cout << balancer.get_main_matrix() << std::endl; std::cout << balancer.get_result() << std::endl;
- Use
set_multiple_results(bool option)
to set if you want a multiple results. - Use
set_max_coef(unsigned max_coef)
to set the maximum possible coefficient in the balanced equation. I suggest thatmax_coef
should not be greater than 30. - Use
set_log_status(bool option)
to set if you wantstd::clog
messages when have.