11/3/2022 0 Comments Equation maker chemistry![]() ![]() #Equation maker chemistry codeThe parser takes a lot of code to implement and is ugly, but it is robust. Your text input is parsed by a hand-written tokenizer and a recursive descent parser with one token of look-ahead. In the code, this functionality is found in Equation, Term, Group, ChemElem. For example: H 3O +, S 2â.Īn Equation object is a list of Term objects for the left side and a list of Term objects for the right side. For example: (H 2O) 6, (C(OH) 3) 2.Ī Term object is a list of ChemElem or Group objects, with an integer electric charge for the whole term. For example: Fe, H 2.Ī Group object is a parenthesized list of ChemElem or Group objects, with a positive integer repetition count for the whole group. The exception is that e by itself represents an electron.Īn ChemElem object is a chemical element with a positive integer repetition count. For example, these are elements: H, Na, Uuq. How it works Data representationĪ raw chemical element is a string of letters that begins with an uppercase letter and is followed by any number of lowercase letters. In this case, each term that has a negative coefficient should be put on the other side of the equation, and its new coefficient should be the absolute value of the negative coefficient. Note: For simplicity of implementation, if the equation is successfully balanced but one or more terms have a negative coefficient, the program doesnât consider this outcome to be an error condition. This error should not happen, but if it does please contact me. The author/programmer made a serious logic mistake. There is no workaround the code would need to be rewritten to use bigints. I donât expect this error to occur for real-world chemical formulas, only deliberately contrived ones. ![]() Your equation used numbers that are too big, or a term has an element that occurs too many times, or the internal calculation used numbers that are too big. Furthermore, the equation can be separated as H â H 2 and O â O 2, each of which does have a unique solution. For example, H + O â H 2 + O 2 has no unique solution because two solutions are 2H + 4O â H 2 + 2O 2 and 6H + 2O â 3H 2 + O 2, which are not multiples of each other. ![]() Your equation can be considered as two or more independent equations added together. There exist multiple solutions to your equation that are not simply multiples of each other. For example, C â N 2 has no solution because the only solution is 0C â 0N 2. The only mathematical solution to your equation has all coefficients set to zero, which is a trivial solution for every chemical equation. Check each letter carefully, and follow the examples as a guide to the correct syntax. Your input does not describe a proper chemical equation. Syntax guide Feature & demoįoo 5+ + Bar 3â â FooBar 2 + FooBar â The source TypeScript code and compiled JavaScript code are available for viewing. This program was hand-written in JavaScript in year 2011, received minor feature updates and clarifications and refactorings throughout the years, and was ported to TypeScript in 2018. Because the program is entirely client-side JavaScript code, this web page can be saved and used offline. The algorithm used is Gauss-Jordan elimination, slightly modified to operate using only integer coefficients (not fractions). The program calculates the coefficients to balance your given chemical equation. This is an easy-to-use, no-nonsense chemical equation balancer. Chemical equation balancer (JavaScript) Program Input: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |