This program aims to judge if the equation can be solved and give the answer.
Notice that it can't judge whether the equation is valid.
For example, HF + I2 = HI + F2, it may not happen but can still be solved.
First make the project
make && make clean
Then run the project
./main
Then input an equation, and then the program will give you the result.
End input by EOF.
C2H2 + H2 = CH4
H2O + Fe = Fe3O4 + H2
Cu + HNO3 = Cu(NO3)2 + NO2 + H2O
KMnO4 + NaOH + PH3 = K2MnO4 + Na2MnO4 + Na3PO4 + H2O
CrCl3 + KMnO4 + H2O = K2Cr2O7 + MnCl2 + HCl + MnCr2O7
Fe2(SO4)3 + H2O + Na2CO3 = Na2Fe6(SO4)4(OH)12 + Na2SO4 + CO2
K2Cr2O7 + CH3CH2OH + H2SO4 = Cr2(SO4)3 + CH3COOH + K2SO4 + H2O
CuCl2 + NH3H2O = [Cu(NH3)4]Cl2 + H2O
C + O2 = CO + CO2
C2H2 + H2 = CH4 + CH3CH3
H2O = H2O + H2 + O2
Fe = Au
A + B + C + D + E + F + G + H + I + J + K + L + M + N = A2B([C2D(EF[GH4]3IJK)3]4L2)3MN3
ABCDEFGHIJKLMN = A2B([C2D(EF[GH4]3IJK)3]4L2)3MN3
Provide gcd and lcm function.
Provide a class to describe chemicals, including name and elements it contains and the count. This is a core file, it mainly parse the chemical string to element map.
Provides basic Gaussian elimination to solve the matrix. Then give out the lcm of coefficient.
Provide basic class for fraction operator.
Provide function to parse input string and format the output result.
The main process is list below.
- parse input string, get the reactant and the resultant.
- parse each reactant and resultant, get the map of element and count.
- build a matrix base on element conservation, for each element, build a equation, the coefficients become a row of matrix.
- use Gaussian elimination to solve the matrix, then get the result.
- parse the result into string, and output the string.
And the most difficult part is 2 and 4.
For 2, I use DFA to parse the chemical string, it has 6 states. And the meaning of each state is described in the code. And I use a stack
to deal with ()
and []
, like building a calculator. There are many corner cases here, and I comment them in the code.
For 4, I transform the origin matrix to upper triangular matrix, and judge the result by the matrix's rank(one solution, no solutions, multiple solutions). If there's only one solution, then I set the last chemical's coefficient 1, and then determine others' coefficient. Lastly convert fraction coefficient to integer coefficient using lcm. There are many corner cases too.
If you find any bug, please write an email to me, Thank you very much!