Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 51.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Net;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.IO;
  13. using System.Collections.Specialized;
  14. using System.Net.NetworkInformation;
  15.  
  16. namespace WindowsFormsApplication1
  17. {
  18.     public partial class Form1 : Form
  19.     {
  20.  
  21.         public bool CheckForInternetConnection()
  22.         {
  23.             try
  24.             {
  25.                 Ping myPing = new Ping();
  26.                 string host = "8.8.8.8";
  27.                 byte[] buffer = new byte[32];
  28.                 int timeout = 1000;
  29.                 PingOptions pingOptions = new PingOptions();
  30.                 PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
  31.                 return (reply.Status == IPStatus.Success);
  32.             }
  33.             catch (Exception)
  34.             {
  35.                 return false;
  36.             }
  37.         }
  38.  
  39.         public Form1()
  40.         {
  41.             InitializeComponent();
  42.             label6.Text = "";
  43.             if (!CheckForInternetConnection())
  44.             {
  45.                 checkBox1.Enabled = false;
  46.                 label5.Text = "Check internet connection";
  47.             }
  48.             else
  49.             {
  50.                 checkBox1.Enabled = true;
  51.                 label5.Text = "Internet connection is stable";
  52.             }
  53.             label7.Text = "";
  54.             label8.Text = "";
  55.             label9.Text = "";
  56.         }
  57.  
  58.         bool isErrorGlobal;
  59.         Point pointA = new Point(33, 271);
  60.         Point pointB = new Point(372, 271);
  61.         //Variables
  62.         public List<string> acids = new List<string> //Acids list
  63.         {
  64.             "HNO2", "HNO3", "H2CO3", "HCl", "H3PO4", "H2SiO3", "H2SO2", "H2SO3", "H2SO4", "HClO3","HClO4", "HBr", "HMnO4"
  65.         };
  66.  
  67.         public Dictionary<string, int> salts = new Dictionary<string, int> //Salts list
  68.         {
  69.             {"NO2", -1}, {"NO3", -1}, {"CO3", -2}, {"Cl", -1}, {"PO4", -3}, {"SiO3", -2}, {"SO2", -2}, {"SO3", -2}, {"SO4", -2}, {"OH", -1}, {"ClO3", -1}, {"ClO4", -1 }, {"Br", -1}, {"MnO4", -1}
  70.         };
  71.  
  72.         public List<string> saltString = new List<string>() //Salt string list (for checking)
  73.             {
  74.                 "NO2", "NO3", "CO3", "Cl", "PO4", "SiO3", "SO2", "SO3", "SO4", "OH", "ClO3", "Br", "MnO4", "ClO4"
  75.             };
  76.  
  77.         public List<string> metals = new List<string> //Metals
  78.         {
  79.             "Li","Be","Na","Mg","Al","K","Ca","Sc","Ti","V" ,"Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Rb","Sr","Y"  ,"Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Cs","Ba",
  80.             "La","Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po"
  81.         };
  82.  
  83.         public Dictionary<string, int> elems = new Dictionary<string, int> //Elements with ions
  84.         {
  85.             {"H", 1},
  86.             { "Li", 1}, {"Be", 2}, {"B", 3}, {"N", -3}, {"O", -2}, {"F", -1},
  87.             {"Na", 1}, {"Mg", 2}, {"Al", 3}, {"Si", 4}, {"P", -3 }, {"Cu", 2 }, {"S", 6}, {"Cl", -1},
  88.             {"K", 1}, {"Ca", 2}, {"Sc", 3}, {"Zn", 2}, {"Ga", 3}, {"Ge", 4}, {"As", -3}, {"Se", -2}, {"Br", -1},
  89.             {"Rb", 1}, {"Sr", 2}, {"Y", 3}, {"Zr", 4}, {"Mo", 6}, {"Tc", 7}, {"Rh", 3}, {"Ag", 1}, {"Cd", 2}, {"In", 3}, {"Te", -2}, {"I", -1},
  90.             {"Cs", 1}, {"Ba", 2}, {"La", 3}, {"Hf", 4}, {"Ta", 5}, {"W", 6}, {"Re", 7}, {"Os", 4}, {"Ir", 4}, {"At", -1},
  91.             {"Fr", 1}, {"Ra", 2}, {"Ac", 3}, {"Ti", 4}, {"V", 5}, {"Cr", 3}, {"Mn", 4}, {"Fe", 3},
  92.             {"Co", 2}, {"Ni", 2}, {"Nb", 5 }, {"Ru", 4}, {"Pd", 2 }, {"Sn", 4},
  93.             {"Sb", 5}, {"Pt", 4 }, {"Au", 3}, {"Hg", 2}, {"Tl", 1}, {"Pb",  2}, {"Bi",  3},
  94.             {"Po", 4}
  95.         };
  96.  
  97.         public List<string> activeMetals = new List<string>
  98.         {
  99.             "Li", "K", "Ba", "Ca", "Na", "Cs", "Rb"
  100.         };
  101.  
  102.         private void button1_Click(object sender, EventArgs e)
  103.         {
  104.             textBox4.Text = ""; //Refresh text
  105.             isErrorGlobal = false; //Refresh error
  106.             string reac1 = textBox1.Text; //Reactant1 text
  107.             string reac2 = textBox3.Text; //Reactant2 text
  108.             string reactionType = ""; //Refresh reaction  
  109.             int match1 = Regex.Matches(reac1, @"[a-zA-Z]").Count; //Find letter count (error checking)
  110.             int match2 = Regex.Matches(reac2, @"[a-zA-Z]").Count;
  111.             if (match1 == 0 || match2 == 0) //Check error
  112.             {
  113.                 isErrorGlobal = true;
  114.                 Error();
  115.                 return;
  116.             }
  117.             Term react1 = new Term(); //Term1
  118.             react1.components = new List<Component>();
  119.             Term react2 = new Term(); //Term2
  120.             react2.components = new List<Component>();
  121.             SetTerm(ref react1, reac1, true); //Set Term1
  122.             SetTerm(ref react2, reac2, true); //Set Term2
  123.             if (isErrorGlobal) //If found error, dont go through
  124.             {
  125.                 return;
  126.             }
  127.             //Reaction types :
  128.             if (react2.IsEmpty)
  129.             {
  130.                 reactionType = "Decompression"; //Not implemented, cause its too hard :D
  131.                 textBox4.Text = "Decompression";
  132.                 textBox2.Text = "Not implemented, it's way too hard :)";
  133.                 return;
  134.             }
  135.             else
  136.             {
  137.                 //Main reaction types
  138.                 if (react1.isSalt && react2.isSalt && react1.hasMetal && react2.hasMetal)
  139.                 {
  140.                     reactionType = "Double replacement";
  141.                 }
  142.                 else if ((react1.isSalt && react2.isMetal) || (react1.isMetal && react2.isSalt))
  143.                 {
  144.                     reactionType = "Replacement";
  145.                 }
  146.                 else if ((react1.isHC && react2.text.Contains("O") && react2.termCount == 1) || (react2.isHC && react1.text.Contains("O") && react1.termCount == 1))
  147.                 {
  148.                     reactionType = "Combustion";
  149.                 }
  150.                 else if (((react1.isMetal && react2.text.Contains("O") && react2.termCount == 1) || (react1.text.Contains("O") && react1.termCount == 1 && react2.isMetal)) && (react1.hasMetal || react2.hasMetal))
  151.                 {
  152.                     reactionType = "Oxidization";
  153.                 }
  154.                 else if ((react1.isMetal && react2.isAcid) || (react1.isAcid && react2.isMetal))
  155.                 {
  156.                     reactionType = "Replacement (Acid)";
  157.                 }
  158.                 else if ((react1.isOxide && react2.isAcid) || (react1.isAcid && react2.isOxide))
  159.                 {
  160.                     reactionType = "Double replacement (Acid)";
  161.                 }
  162.                 else if ((react1.isMetal && react2.text == "CO2") || (react1.text == "CO2" && react2.isMetal))
  163.                 {
  164.                     reactionType = "Replacement (C)";
  165.                 }
  166.                 else if ((react1.isOxide && react2.text == "C") || (react1.text == "C" && react2.isOxide))
  167.                 {
  168.                     reactionType = "Deoxidation (C)";
  169.                 }
  170.                 else if ((react1.hasMetal && react2.isSaltFormer) || (react1.isSaltFormer && react2.hasMetal) ||
  171.                     (react1.components[0].text == "H" && react1.termCount == 1 && react2.components[0].text == "O" && react2.termCount == 1) ||
  172.                     (react1.components[0].text == "O" && react1.termCount == 1 && react2.components[0].text == "H" && react2.termCount == 1) ||
  173.                     ((react1.isOxide && react2.isWater) || (react1.isWater && react2.isOxide)))
  174.                 {
  175.                     reactionType = "Synthesis";
  176.                 }
  177.                 else if ((react1.isActiveMetal && react2.isWater) || (react2.isActiveMetal && react1.isWater))
  178.                 {
  179.                     reactionType = "Replacement (Active)";
  180.                 }
  181.                 else if ((react1.isMetal && !react1.isActiveMetal && react2.isWater) || (react1.isWater && react2.isMetal && !react2.isActiveMetal))
  182.                 {
  183.                     reactionType = "Replacement (W)";
  184.                 }
  185.                 else if ((react1.isSalt && react2.isAcid) || (react2.isSalt && react1.isAcid))
  186.                 {
  187.                     reactionType = "Double replacement (Salt and Acid)";
  188.                 }
  189.             }
  190.             //Checking ions:
  191.             FindIons(ref react1);
  192.             FindIons(ref react2);
  193.  
  194.             //Solving
  195.             Solve(reactionType, ref react1, ref react2);
  196.  
  197.             //Refresh text
  198.             react1.RefreshText();
  199.             react2.RefreshText();
  200.  
  201.             if (react1.salt == "OH" && react1.text[react1.text.IndexOf('(') - 1] == 'O')
  202.             {
  203.                 react1.text = react1.text.Remove(react1.text.IndexOf('(') - 1, 1);
  204.             }
  205.             if (react2.salt == "OH" && react2.text[react2.text.IndexOf('(') - 1] == 'O')
  206.             {
  207.                 react2.text = react2.text.Remove(react2.text.IndexOf('(') - 1, 1);
  208.             }
  209.  
  210.             if (checkBox2.Checked)
  211.             {
  212.                 animation(react1.text, react2.text, textBox2.Text);
  213.             }
  214.             //Final : setting textBox and balancing
  215.             string a = react1.text + "+" + react2.text + "=" + textBox2.Text;
  216.             if(checkBox1.Checked)
  217.             {
  218.                 if (CheckForInternetConnection())
  219.                 {
  220.                     WebClient client = new WebClient(); //Start new client
  221.                     progressBar1.Value += 10;
  222.                     //Set headers
  223.                     client.Headers.Add("user-agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
  224.                     string b = a; //Make new temp string
  225.                     progressBar1.Value += 10;
  226.                     b = b.Replace("+", "%2B"); //Replace HTML symbols
  227.                     b = b.Replace("=", "%3D");
  228.                     string URL = "http://www.webqc.org/balance.php?reaction=" + b; //Set URL
  229.                     progressBar1.Value += 10;
  230.                     string webData = client.DownloadString(URL); //Download string
  231.                     progressBar1.Value += 10;
  232.                     //Make Answer
  233.                     string answer = webData.Substring(webData.IndexOf("Balanced equation") + 12, 700);
  234.                     progressBar1.Value += 10;
  235.                     answer = answer.Replace(" ", string.Empty);
  236.                     progressBar1.Value += 10;
  237.                     answer = answer.Substring(answer.IndexOf("<br>") + 4, 500);
  238.                     progressBar1.Value += 10;
  239.                     answer = answer.Substring(0, answer.IndexOf("</b>"));
  240.                     progressBar1.Value += 10;
  241.                     answer = answer.Replace("<sub>", "");
  242.                     progressBar1.Value += 10;
  243.                     answer = answer.Replace("</sub>", "");
  244.                     answer = answer.Replace("<spanclass='blue'>", "");
  245.                     answer = answer.Replace("</span>", "");
  246.                     progressBar1.Value += 10;
  247.                     textBox2.Text = answer;
  248.                     //Set Label Text
  249.                     checkBox1.Enabled = true;
  250.                     label5.Text = "Internet connection is stable";
  251.                 }
  252.                 else
  253.                 {
  254.                     //Set Label Text
  255.                     checkBox1.Checked = false;
  256.                     checkBox1.Enabled = false;
  257.                     label5.Text = "Check internet connection";
  258.                 }
  259.             }
  260.             else if(!checkBox1.Checked || !CheckForInternetConnection())
  261.             {
  262.                 //If not ticked : just set a
  263.                 textBox2.Text = a;
  264.             }
  265.             //Reaction type
  266.             textBox4.Text = reactionType;
  267.             progressBar1.Value = 0;
  268.         }
  269.  
  270.         async Task animation(string term1, string term2, string products)
  271.         {
  272.             label7.Show();
  273.             label8.Show();
  274.             label9.Hide();
  275.             label7.Text = term1;
  276.             label8.Text = term2;
  277.             label9.Text = products;
  278.             Point currentA = pointA, currentB = pointB;
  279.             do
  280.             {
  281.                 currentA.X += 1;
  282.                 currentB.X -= 1;
  283.                 label7.Location = currentA;
  284.                 label8.Location = currentB;
  285.                 await Task.Delay(10);
  286.             } while (label7.Location.X < 180);
  287.             label9.Show();
  288.             label7.Hide();
  289.             label8.Hide();
  290.         }
  291.  
  292.         void FindIons(ref Term t)
  293.         {
  294.             //Temp variables
  295.             List<Component> cTemp = t.components;
  296.             List<Component> finalTemp = new List<Component>();
  297.  
  298.             //Loop thru all components
  299.             for (int i = 0; i < cTemp.Count; i++)
  300.             {
  301.                 if (cTemp[i].ion == 0) //If cant find ion (Carbon)
  302.                 {
  303.                     if (t.termCount > 1)
  304.                     {
  305.                         //New component C
  306.                         Component c = new Component();
  307.                         Component cNext = new Component();
  308.                         c = cTemp[i];
  309.                         cNext = cTemp[i + 1];
  310.                         if (cNext.ion > 0) //If there is [O] - ion will be positive
  311.                         {
  312.                             c.ion = -4;
  313.                         }
  314.                         else //If [H] - negative
  315.                         {
  316.                             c.ion = 4;
  317.                         }
  318.                         //Add to components list
  319.                         finalTemp.Add(c);
  320.                     }
  321.                     else
  322.                     {
  323.                         //New component C
  324.                         Component c = new Component();
  325.                         c.ion = -4;
  326.                         finalTemp.Add(c);
  327.                     }
  328.                 }
  329.                 else
  330.                 {
  331.  
  332.                     //Add to components list, ion already found
  333.                     finalTemp.Add(cTemp[i]);
  334.                 }
  335.             }
  336.             t.components = finalTemp; //If everything is right Term's components is on his place.
  337.  
  338.             //Find total ion count (Error check)
  339.             if (t.leftSide && t.termCount > 1)
  340.             {
  341.                 int totalIon = 0;
  342.                 if (t.isSalt || t.isAcid)
  343.                 {
  344.                     totalIon = t.components[0].ion * t.components[0].SubCoefficent + t.saltMultiplier * t.saltIon;
  345.                 }
  346.                 else
  347.                 {
  348.                     for (int i = 0; i < t.termCount; i++)
  349.                     {
  350.                         totalIon += t.components[i].ion * t.components[i].SubCoefficent;
  351.                     }
  352.                 }
  353.  
  354.                 if (totalIon != 0) //If its not equal to 0 (Standard)
  355.                 {
  356.                     if (t.isOxide)
  357.                     {
  358.                         BalanceOxide(ref t);
  359.                         t.RefreshText();
  360.                     }
  361.                     else
  362.                     {
  363.                         Balance(ref t);
  364.                         t.RefreshText();
  365.                     }
  366.                     label6.Text = "Ions fixed!";
  367.                 }
  368.             }
  369.         }
  370.  
  371.         void SetTerm(ref Term t, string text, bool isLeft) //Setting term (Hardest one :D)
  372.         {
  373.             //If text is there
  374.             if (text != "")
  375.             {
  376.                 //Temp variable
  377.                 string newT = text;
  378.                 int coef = 0; //Coefficent
  379.                 bool isMetal, isAcid, isSalt, hasMetal, isWater, isHC, isSaltFormer, isOxide = false, isActiveMetal; //Booleans
  380.  
  381.                 //Temp variable
  382.                 List<Component> lastComp = new List<Component>();
  383.  
  384.                 //Salt multiplier : like (OH)3
  385.                 int saltMultiplier = 1;
  386.                 //If it contains [(] [)] - remove it and add salt multiplier
  387.                 if (newT.Contains("(") && newT.Contains(")"))
  388.                 {
  389.                     string a = "";
  390.                     if (char.IsDigit(newT.Last()))
  391.                     {
  392.                         a = newT.Substring(newT.IndexOf(')') + 1, 1);
  393.                         newT = newT.Remove(newT.IndexOf(')') + 1, 1);
  394.                         saltMultiplier = int.Parse(a);
  395.                     }
  396.                     newT = newT.Remove(newT.IndexOf('('), 1);
  397.                     newT = newT.Remove(newT.IndexOf(')'), 1);
  398.                 }
  399.  
  400.                 //Get salt string
  401.                 string salt = "";
  402.  
  403.                 //Find coefficent
  404.                 if (char.IsDigit(newT.First())) //2<-Cu
  405.                 {
  406.                     string a = newT.Substring(0, 1);
  407.                     coef = int.Parse(a);
  408.                     newT = newT.Remove(0, 1);
  409.                 }
  410.                 else
  411.                 {
  412.                     coef = 1;
  413.                 }
  414.                 int termCount = 0; //Terms count
  415.                 List<int> termLocation = new List<int>(); //Temp variable : term locations
  416.                 List<string> termsWithSubscript = new List<string>(); //Temps with subscripts
  417.                 for (int i = 0; i < newT.Length; i++) //Loop thru all
  418.                 {
  419.                     if (char.IsUpper(newT[i])) //Found start of term
  420.                     {
  421.                         termCount++;
  422.                         termLocation.Add(i);
  423.                     }
  424.                 }
  425.                 for (int i = 0; i <= termCount - 1; i++) //Loop thru all for cutting terms
  426.                 {
  427.                     string tempTerm = "";
  428.                     if (i > termCount - 2) //If it's last one
  429.                     {
  430.                         tempTerm = newT.Substring(termLocation[i], newT.Length - termLocation[i]);
  431.                     }
  432.                     else //Else
  433.                     {
  434.                         tempTerm = newT.Substring(termLocation[i], termLocation[i + 1] - termLocation[i]);
  435.                     }
  436.                     termsWithSubscript.Add(tempTerm);
  437.                 }
  438.  
  439.  
  440.                 if(termCount == 0)
  441.                 {
  442.                     isErrorGlobal = true;
  443.                     Error();
  444.                     return;
  445.                 }
  446.                 //Check isMetal, isAcid, isSalt, isWater, hasMetal, isHydrocarbon
  447.                 isMetal = (metals.Contains(newT) && termCount == 1);
  448.                 isAcid = (acids.Contains(newT) || (newT.StartsWith("H") && saltString.Any(newT.Contains)));
  449.                 hasMetal = (metals.Any(newT.Contains));
  450.                 isSalt = (salts.Keys.ToList().Any(newT.Contains) && hasMetal && !isAcid);
  451.                 isWater = (newT == "H2O");
  452.                 isHC = (newT.Contains("C") && newT.Contains("H") && termCount == 2);
  453.                 isSaltFormer = saltString.Contains(newT);
  454.                 isActiveMetal = (activeMetals.Contains(newT));
  455.                 //Oxide checking
  456.                 if (hasMetal && newT.Contains("O") && termCount == 2)
  457.                 {
  458.                     isOxide = true;
  459.                 }
  460.                 if (isSalt)
  461.                 {
  462.                     int indexEndMetal = 0; //Metal ending index
  463.                     foreach (string metal in metals) //Loop thru all metals in [Metals list]
  464.                     {
  465.                         if (newT.Substring(0, 2).Contains(metal))
  466.                         {
  467.                             if (coef == 1)
  468.                             {
  469.                                 indexEndMetal = metal.Length; //Index end : metal length
  470.                             }
  471.                             else
  472.                             {
  473.                                 indexEndMetal = metal.Length + 1; //If coef != 1 : +1
  474.                             }
  475.                             if (indexEndMetal > newT.Length)
  476.                             {
  477.                                 if (char.IsDigit(newT[indexEndMetal])) //If it's digit : add +1
  478.                                 {
  479.                                     indexEndMetal += 1;
  480.                                 }
  481.                             }
  482.                         }
  483.                     }
  484.                     salt = newT.Substring(indexEndMetal, newT.Length - indexEndMetal); //Cut salt
  485.                     int saltUpperCount = 0; //Salt terms count
  486.                     for(int i = 0; i < salt.Length; i++) //Loop thru all
  487.                     {
  488.                         if(char.IsUpper(salt[i]))
  489.                         {
  490.                             saltUpperCount++; //Increment
  491.                         }
  492.                     }
  493.                     if(char.IsDigit(salt.Last()) && saltUpperCount == 1) //If it's digit
  494.                     {
  495.                         salt = salt.Remove(salt.Length - 1, 1); //Remove
  496.                     }
  497.                     if(char.IsDigit(salt.First()))
  498.                     {
  499.                         salt = salt.Remove(0, 1);
  500.                     }
  501.                 }
  502.                 else if(isAcid)
  503.                 {
  504.                     //H2SO4
  505.                     //01234
  506.                     int indexEndH = 1;
  507.                     if(char.IsDigit(newT[indexEndH]))
  508.                     {
  509.                         indexEndH += 1;
  510.                     }
  511.                     string a = newT.Substring(indexEndH, newT.Length - indexEndH);
  512.                     if(char.IsDigit(a[0]))
  513.                     {
  514.                         a = a.Substring(1, a.Length - 1);
  515.                     }
  516.                     salt = a;
  517.                 }
  518.                 //Cutting substrings
  519.                 List<int> saltSubstrings = new List<int>();
  520.                 //Salt ion
  521.                 int saltIon = 0;
  522.                 for (int i = 0; i < termsWithSubscript.Count; i++) //Loop thru all
  523.                 {
  524.                     //Temp component
  525.                     Component comp = new Component();
  526.                     int tempCoef = 0;
  527.                     string tempComp = termsWithSubscript[i];
  528.                     if (char.IsDigit(tempComp.Last())) //If it's digit
  529.                     {
  530.                         string a = tempComp.Substring(tempComp.Length - 1, 1);
  531.                         tempCoef = int.Parse(a);
  532.                         tempComp = tempComp.Remove(tempComp.Length - 1, 1);
  533.                     }
  534.                     else
  535.                     {
  536.                         tempCoef = 1;
  537.                     }
  538.  
  539.                     //Adjusting text and subcoefficent
  540.                     comp.text = tempComp;
  541.                     comp.SubCoefficent = tempCoef;
  542.                     //Carbon is element with -4 and 4 ion
  543.                     if (comp.text != "C")
  544.                     {
  545.                         //Find elements in all elements list, if no - Error.
  546.                         try
  547.                         {
  548.                             comp.ion = elems[comp.text];
  549.                         }
  550.                         catch (KeyNotFoundException)
  551.                         {
  552.                             isErrorGlobal = true;
  553.                             Error();
  554.                             return;
  555.                         }
  556.                     }
  557.                     else
  558.                     {
  559.                         //0 for finding in FindIons() function
  560.                         comp.ion = 0;
  561.                     }
  562.  
  563.                     //If it's salt
  564.                     if (isSalt)
  565.                     {
  566.                         if (salt.Contains(comp.text)) //Find if it's salt
  567.                         {
  568.                             //Is salt part : for synthesis reaction
  569.                             comp.isSaltPart = true;
  570.                             saltSubstrings.Add(comp.SubCoefficent);
  571.                         }
  572.                     }
  573.                     lastComp.Add(comp);
  574.                 }
  575.  
  576.  
  577.                 //Salt ion
  578.                 if (isSalt || isAcid)
  579.                 {
  580.                     //Finding salt ion
  581.                     saltIon = salts[salt];
  582.                 }
  583.                 //Apply
  584.                 t.text = newT;
  585.                 t.coefficent = coef;
  586.                 t.components = lastComp;
  587.                 t.termCount = termCount;
  588.                 t.isMetal = isMetal;
  589.                 t.isAcid = isAcid;
  590.                 t.isSalt = isSalt;
  591.                 t.isWater = isWater;
  592.                 t.hasMetal = hasMetal;
  593.                 t.isHC = isHC;
  594.                 t.leftSide = isLeft;
  595.                 t.salt = salt;
  596.                 t.isOxide = isOxide;
  597.                 t.saltIon = saltIon;
  598.                 t.saltSubstrings = saltSubstrings;
  599.                 t.saltMultiplier = saltMultiplier;
  600.                 t.IsEmpty = false;
  601.                 t.isActiveMetal = isActiveMetal;
  602.                 t.isSaltFormer = isSaltFormer;
  603.             }
  604.             else
  605.             {
  606.                 //String is empty, T will be empty.
  607.                 t.IsEmpty = true;
  608.             }
  609.         }
  610.  
  611.         void Solve(string type, ref Term reac1, ref Term reac2)
  612.         {
  613.             //Products
  614.             Term prod1 = new Term();
  615.             prod1.components = new List<Component>(); //Creating new list.
  616.             Term prod2 = new Term();
  617.             prod2.components = new List<Component>();
  618.  
  619.             if (type == "Replacement") //Type is replacement (salt + metal)
  620.             {
  621.                 if (reac1.isSalt)
  622.                 {
  623.                     //Product1
  624.                     string newT = "";
  625.  
  626.                     //Product2
  627.                     string newT2 = "";
  628.  
  629.                     //Solving, swapping salts
  630.                     newT = reac1.text;
  631.                     newT2 = reac2.text;
  632.                     string t2Salt = reac1.salt;
  633.                     newT = newT.Remove(newT.IndexOf(reac1.salt), reac1.salt.Length);
  634.                     newT2 = newT2 + t2Salt;
  635.  
  636.                     //Setting terms
  637.                     SetTerm(ref prod1, newT, false);
  638.                     SetTerm(ref prod2, newT2, false);
  639.  
  640.                     //Getting component ions
  641.                     int allComponentIons = 0;
  642.  
  643.                     foreach (Component c in prod2.components)
  644.                     {
  645.                         allComponentIons += c.ion * c.SubCoefficent;
  646.                     }
  647.                     if (allComponentIons != 0)
  648.                     {
  649.                         //If it's not 0, call Balance() method
  650.                         Balance(ref prod2);
  651.                     }
  652.                     //Refreshing text
  653.                     prod1.RefreshText();
  654.                     prod2.RefreshText();
  655.                     //After everyhing done show text
  656.                     textBox2.Text = prod1.text + "+" + prod2.text;
  657.                 }
  658.                 else if (reac2.isSalt) //The same, but swapped
  659.                 {
  660.                     //Swapping
  661.                     Term swapTerm = new Term();
  662.                     swapTerm = reac2;
  663.                     reac2 = reac1;
  664.                     reac1 = swapTerm;
  665.                     //Product1
  666.                     string newT = "";
  667.  
  668.                     //Product2
  669.                     string newT2 = "";
  670.  
  671.                     //Solving
  672.                     newT = reac1.text;
  673.                     newT2 = reac2.text;
  674.                     string t2Salt = reac1.salt;
  675.                     newT = newT.Remove(newT.IndexOf(reac1.salt), reac1.salt.Length);
  676.                     newT2 = newT2 + t2Salt;
  677.  
  678.                     SetTerm(ref prod1, newT, false);
  679.                     SetTerm(ref prod2, newT2, false);
  680.  
  681.                     int allComponentIons = 0;
  682.  
  683.                     foreach (Component c in prod2.components)
  684.                     {
  685.                         allComponentIons += c.ion * c.SubCoefficent;
  686.                     }
  687.                     if (allComponentIons != 0)
  688.                     {
  689.                         Balance(ref prod2);
  690.                     }
  691.                     //Refreshing text
  692.                     prod1.RefreshText();
  693.                     prod2.RefreshText();
  694.                     textBox2.Text = prod1.text + "+" + prod2.text;
  695.                 }
  696.             }
  697.             else if (type == "Double replacement") //(salt + salt)
  698.             {
  699.                 //Prod1
  700.                 string newT = "";
  701.  
  702.                 //Prod2
  703.                 string newT2 = "";
  704.  
  705.                 //Solving, Swapping salts
  706.                 newT = reac1.text;
  707.                 newT2 = reac2.text;
  708.                 string saltNewT = newT.Substring(newT.IndexOf(reac1.salt), reac1.salt.Length);
  709.                 string saltNewT2 = newT2.Substring(newT2.IndexOf(reac2.salt), reac2.salt.Length);
  710.                 if(newT.Contains("(") && newT.Contains(")"))
  711.                 {
  712.                     newT = newT.Remove(newT.IndexOf("("), 1);
  713.                     newT = newT.Remove(newT.IndexOf(")"), 1);
  714.                     if(char.IsDigit(newT.Last()))
  715.                     {
  716.                         newT = newT.Remove(newT.Length - 1, 1);
  717.                     }
  718.                 }
  719.                 if (newT2.Contains("(") && newT2.Contains(")"))
  720.                 {
  721.                     newT2 = newT2.Remove(newT2.IndexOf("("), 1);
  722.                     newT2 = newT2.Remove(newT2.IndexOf(")"), 1);
  723.                     if (char.IsDigit(newT2.Last()))
  724.                     {
  725.                         newT2 = newT2.Remove(newT2.Length - 1, 1);
  726.                     }
  727.                 }
  728.                 newT = newT.Remove(newT.IndexOf(reac1.salt), reac1.salt.Length);
  729.                 newT2 = newT2.Remove(newT2.IndexOf(reac2.salt), reac2.salt.Length);
  730.                 newT += saltNewT2;
  731.                 newT2 += saltNewT;
  732.  
  733.                 //Setting terms
  734.                 SetTerm(ref prod1, newT, false);
  735.                 SetTerm(ref prod2, newT2, false);
  736.  
  737.                 //Balancing
  738.                 Balance(ref prod1);
  739.                 Balance(ref prod2);
  740.  
  741.                 //Refreshing text
  742.                 prod1.RefreshText();
  743.                 prod2.RefreshText();
  744.  
  745.                 textBox2.Text = prod1.text + "+" + prod2.text;
  746.             }
  747.             else if (type == "Combustion") // CH + O2
  748.             {
  749.                 //Setting new terms
  750.                 string newT = "";
  751.                 string newt2 = "";
  752.  
  753.                 //Solving
  754.                 newT = "CO2";
  755.                 newt2 = "H2O";
  756.  
  757.                 //Setting terms
  758.                 SetTerm(ref prod1, newT, false);
  759.                 SetTerm(ref prod2, newt2, false);
  760.  
  761.                 //Refreshing
  762.                 prod1.RefreshText();
  763.                 prod2.RefreshText();
  764.  
  765.                 //Showing
  766.                 textBox2.Text = prod1.text + "+" + prod2.text;
  767.             }
  768.             else if (type == "Oxidization")
  769.             {
  770.                 //Setting new term
  771.                 string newT = "";
  772.  
  773.                 //Solving
  774.                 string metal = reac1.text;
  775.                 newT = metal + "O";
  776.  
  777.                 //Setting term
  778.                 SetTerm(ref prod1, newT, false);
  779.  
  780.                 //Balancing (Oxide!)
  781.                 BalanceOxide(ref prod1); //The same as Balance but some oriented for oxygen balancing
  782.  
  783.                 //Refreshing text
  784.                 prod1.RefreshText();
  785.  
  786.                 //Showing text
  787.                 textBox2.Text = prod1.text;
  788.             }
  789.             else if (type == "Synthesis") //Item + Item -> bigger item
  790.             {
  791.                 //Setting new term
  792.                 string newT = "";
  793.  
  794.                 //Solving
  795.                 if ((reac1.isOxide && reac2.isWater) || (reac1.isWater && reac2.isOxide)) //Metal oxide + water -> Metal hydrooxide
  796.                 {
  797.                     //Solving
  798.                     newT = reac1.components[0].text + "OH";
  799.  
  800.                     //Setting term, balancing, refreshing and showing
  801.                     SetTerm(ref prod1, newT, false);
  802.                     Balance(ref prod1);
  803.                     prod1.RefreshText();
  804.                     textBox2.Text = prod1.text;
  805.                 }
  806.                 else if ((reac1.isMetal && reac2.isSaltFormer) || (reac1.isSaltFormer && reac2.isMetal)) //Metal + salt -> metal salt
  807.                 {
  808.                     //Solving
  809.                     newT = reac1.text + reac2.text;
  810.  
  811.                     //Setting term, balancing, refreshing and showing
  812.                     SetTerm(ref prod1, newT, false);
  813.                     Balance(ref prod1);
  814.                     prod1.RefreshText();
  815.                     textBox2.Text = prod1.text;
  816.                 }
  817.             }
  818.             else if (type == "Decompression") //Decompression is too hard :D
  819.             {
  820.                 textBox2.Text = "Working on it, please wait :)";
  821.             }
  822.             else if (type == "Replacement (Acid)")
  823.             {
  824.                 //Solving
  825.                 string newT = "";
  826.                 if (reac1.isMetal)
  827.                 {
  828.                     newT = reac1.components[0].text;
  829.                     newT += reac2.salt;
  830.                 }
  831.                 else
  832.                 {
  833.                     newT = reac2.components[0].text;
  834.                     newT += reac1.salt;
  835.                 }
  836.                 //Setting, balancing, refreshing and showing
  837.                 SetTerm(ref prod1, newT, false);
  838.                 Balance(ref prod1);
  839.                 prod1.RefreshText();
  840.                 textBox2.Text = prod1.text + "+H2";
  841.             }
  842.             else if(type == "Double replacement (Acid)")
  843.             {
  844.                 //Solving
  845.                 string newT = "";
  846.                 if(reac1.isOxide)
  847.                 {
  848.                     newT = reac1.components[0].text;
  849.                     newT += reac2.salt;
  850.                 }
  851.                 else
  852.                 {
  853.                     newT = reac2.components[0].text;
  854.                     newT += reac1.salt;
  855.                 }
  856.                 //Setting, balancing, refreshing and showing
  857.                 SetTerm(ref prod1, newT, false);
  858.                 Balance(ref prod1);
  859.                 prod1.RefreshText();
  860.                 textBox2.Text = prod1.text + "+H2O";
  861.             }
  862.             else if(type == "Replacement (C)")
  863.             {
  864.                 //Solving
  865.                 string newT = "";
  866.                 if(reac1.isMetal)
  867.                 {
  868.                     newT = reac1.components[0].text;
  869.                     newT += "O";
  870.                 }
  871.                 else
  872.                 {
  873.                     newT = reac2.components[0].text;
  874.                     newT += "O";
  875.                 }
  876.                 //Setting, balancing, refreshing and showing
  877.                 SetTerm(ref prod1, newT, false);
  878.                 BalanceOxide(ref prod1);
  879.                 prod1.RefreshText();
  880.                 textBox2.Text = prod1.text + "+C";
  881.             }
  882.             else if(type == "Deoxidation (C)")
  883.             {
  884.                 //Solving
  885.                 string newT = "";
  886.                 if(reac1.isOxide)
  887.                 {
  888.                     newT = reac1.components[0].text;
  889.                 }
  890.                 else
  891.                 {
  892.                     newT = reac2.components[0].text;
  893.                 }
  894.                 //Setting, balancing, refreshing and showing
  895.                 SetTerm(ref prod1, newT, false);
  896.                 prod1.RefreshText();
  897.                 textBox2.Text = prod1.text + "+CO2";
  898.             }
  899.             else if(type == "Replacement (Active)")
  900.             {
  901.                 //Solving
  902.                 string newT = "";
  903.                 if(reac1.isActiveMetal)
  904.                 {
  905.                     newT = reac1.components[0].text;
  906.                     newT += "OH";
  907.                 }
  908.                 else
  909.                 {
  910.                     newT = reac2.components[0].text;
  911.                     newT += "OH";
  912.                 }
  913.                 //Setting, balancing, refreshing and showing
  914.                 SetTerm(ref prod1, newT, false);
  915.                 Balance(ref prod1);
  916.                 prod1.RefreshText();
  917.                 textBox2.Text = prod1.text + "+H2";
  918.             }
  919.             else if(type == "Replacement (W)")
  920.             {
  921.                 //Solving
  922.                 string newT = "";
  923.                 if(reac1.isMetal)
  924.                 {
  925.                     newT = reac1.components[0].text;
  926.                     newT += "O";
  927.                 }
  928.                 else
  929.                 {
  930.                     newT = reac2.components[0].text;
  931.                     newT += "O";
  932.                 }
  933.                 //Setting, balancing, refreshing and showing
  934.                 SetTerm(ref prod1, newT, false);
  935.                 BalanceOxide(ref prod1);
  936.                 prod1.RefreshText();
  937.                 textBox2.Text = prod1.text + "+H2";
  938.             }
  939.             else if(type == "Double replacement (Salt and Acid)")
  940.             {
  941.                 //Solving
  942.                 string newT = "";
  943.                 string newT2 = "";
  944.                 if(reac1.isSalt)
  945.                 {
  946.                     newT = reac1.components[0].text;
  947.                     newT += reac2.salt;
  948.                     newT2 = "H";
  949.                     newT2 += reac1.salt;
  950.                     if(newT2 == "HOH")
  951.                     {
  952.                         newT2 = "H2O";
  953.                     }
  954.                 }
  955.                 else
  956.                 {
  957.                     newT = reac2.components[0].text;
  958.                     newT += reac1.salt;
  959.                     newT2 = "H";
  960.                     newT2 += reac2.salt;
  961.                     if (newT2 == "HOH")
  962.                     {
  963.                         newT2 = "H2O";
  964.                     }
  965.                 }
  966.                 //Setting, balancing, refreshing and showing
  967.                 SetTerm(ref prod1, newT, false);
  968.                 SetTerm(ref prod2, newT2, false);
  969.                 Balance(ref prod1);
  970.                 if (prod2.text == "H2O")
  971.                 {
  972.                     BalanceOxide(ref prod2);
  973.                 }
  974.                 else
  975.                 {
  976.                     Balance(ref prod2);
  977.                 }
  978.                 prod1.RefreshText();
  979.                 prod2.RefreshText();
  980.                 textBox2.Text = prod1.text + "+" + prod2.text;
  981.             }
  982.         }
  983.  
  984.         //Oxide balancing
  985.         void BalanceOxide(ref Term t)
  986.         {
  987.             int oxideIon = -2; // [O] ion
  988.             int ionOther = t.components[0].ion; //Other metal ion
  989.             int subCoefOxide = 1, subCoefMetal = 1; //Subcoefficents
  990.  
  991.             for (int i = 1; i < 16; i++) //Looping thru 2
  992.             {
  993.                 for (int j = 1; j < 16; j++) //2
  994.                 {
  995.                     if ((i * ionOther) + (j * oxideIon) == 0) //Bruteforce method for calculating subcoefficent
  996.                     {
  997.                         //Setting and breaking
  998.                         subCoefMetal = i;
  999.                         subCoefOxide = j;
  1000.                         break;
  1001.                     }
  1002.                 }
  1003.             }
  1004.             if (subCoefMetal != 1 && subCoefOxide != 1) //If there is an GCF
  1005.             {
  1006.                 if (subCoefMetal > subCoefOxide)
  1007.                 {
  1008.                     for (int i = subCoefMetal; i > 0; i--) //GCF finding algorythm
  1009.                     {
  1010.                         if (subCoefMetal % i == 0 && subCoefOxide % i == 0)
  1011.                         {
  1012.                             //Fractioning
  1013.                             subCoefMetal /= i;
  1014.                             subCoefOxide /= i;
  1015.                             break;
  1016.                         }
  1017.                     }
  1018.                 }
  1019.                 else if (subCoefOxide > subCoefMetal) //The same but reversed
  1020.                 {
  1021.                     for (int i = subCoefOxide; i > 0; i--)
  1022.                     {
  1023.                         if (subCoefMetal % i == 0 && subCoefOxide % i == 0)
  1024.                         {
  1025.                             subCoefMetal /= i;
  1026.                             subCoefOxide /= i;
  1027.                             break;
  1028.                         }
  1029.                     }
  1030.                 }
  1031.                 //If they re the same set them as 1, 1
  1032.                 else if (subCoefMetal == subCoefOxide)
  1033.                 {
  1034.                     subCoefMetal = 1;
  1035.                     subCoefOxide = 1;
  1036.                 }
  1037.             }
  1038.            
  1039.  
  1040.             //Setting everything
  1041.             Component c1 = t.components[0];
  1042.             Component c2 = t.components[1];
  1043.             c1.SubCoefficent = subCoefMetal;
  1044.             c2.SubCoefficent = subCoefOxide;
  1045.             t.components[0] = c1;
  1046.             t.components[1] = c2;
  1047.  
  1048.         }
  1049.  
  1050.         //Balancing (now it's the hardest balancing :D)
  1051.         void Balance(ref Term t)
  1052.         {
  1053.             //Temp variables
  1054.             List<Component> newComponents = new List<Component>();
  1055.             Term newT = t;
  1056.             //Total ion
  1057.             int compTotalIon = 0;
  1058.             //Is Error
  1059.             bool isError = false;
  1060.             //A,B,C - subcoefficents
  1061.             int a = 0, b = 0, c = 0;
  1062.             //Salts ions
  1063.             int saltA = 0, saltB = 0;
  1064.             if (newT.termCount == 2)
  1065.             {
  1066.                 //If term count is 2
  1067.                 //Setting newT
  1068.                 Component a1 = newT.components[0];
  1069.                 Component b1 = newT.components[1];
  1070.                 a1.SubCoefficent = 1;
  1071.                 b1.SubCoefficent = 1;
  1072.                 if (b1.isSaltPart)
  1073.                 {
  1074.                     b1.SubCoefficent = t.saltSubstrings[0];
  1075.                 }
  1076.                 saltA = 1;
  1077.                 newT.components[0] = a1;
  1078.                 newT.components[1] = b1;
  1079.             }
  1080.             else if (newT.termCount == 3)
  1081.             {
  1082.                 //If term count is 3
  1083.                 //Do the same but 1 more
  1084.                 Component a2 = newT.components[0];
  1085.                 Component b2 = newT.components[1];
  1086.                 Component c2 = newT.components[2];
  1087.                 a2.SubCoefficent = 1;
  1088.                 b2.SubCoefficent = 1;
  1089.                 c2.SubCoefficent = 1;
  1090.                 if (b2.isSaltPart)
  1091.                 {
  1092.                     b2.SubCoefficent = t.saltSubstrings[0];
  1093.                 }
  1094.                 if (c2.isSaltPart)
  1095.                 {
  1096.                     c2.SubCoefficent = t.saltSubstrings[1];
  1097.                 }
  1098.  
  1099.                 saltA = b2.SubCoefficent;
  1100.                 saltB = c2.SubCoefficent;
  1101.                 newT.components[0] = a2;
  1102.                 newT.components[1] = b2;
  1103.                 newT.components[2] = c2;
  1104.             }
  1105.             //Finding total ion count
  1106.             compTotalIon = newT.components[0].ion * 1 + newT.saltIon;
  1107.  
  1108.             //If it's not 0
  1109.             if (compTotalIon != 0)
  1110.             {
  1111.                 for (int i = 1; i < 16; i++) //Loop thru
  1112.                 {
  1113.                     for (int j = 1; j < 16; j++) //2
  1114.                     {
  1115.                         if (newT.termCount == 2) //Term count = 2
  1116.                         {
  1117.                             //Finding error
  1118.                             if ((newT.components[0].ion > 0 && newT.saltIon > 0) || (newT.saltIon < 0 && newT.components[0].ion < 0))
  1119.                             {
  1120.                                 //ERROR
  1121.                                 isErrorGlobal = true;
  1122.                                 break;
  1123.                             }
  1124.                             if ((i * newT.components[0].ion) + (j * newT.saltIon) == 0) //Bruteforce
  1125.                             {
  1126.                                 //Assigning
  1127.                                 a = i;
  1128.                                 b = j * saltA;
  1129.                             }
  1130.                         }
  1131.                         else if (newT.termCount == 3) //If terms = 3
  1132.                         {
  1133.                             //Check for error
  1134.                             if ((newT.components[0].ion > 0 && newT.saltIon > 0) || (newT.components[0].ion < 0 && newT.saltIon < 0))
  1135.                             {
  1136.                                 //ERROR
  1137.                                 isError = true;
  1138.                                 break;
  1139.                             }
  1140.                             //Bruteforce
  1141.                             if ((i * newT.components[0].ion) + (j * newT.saltIon) == 0)
  1142.                             {
  1143.                                 //Assigning
  1144.                                 a = i;
  1145.                                 b = saltA * j;
  1146.                                 c = saltB * j;
  1147.                             }
  1148.                         }
  1149.                     }
  1150.                 }
  1151.             }
  1152.             else //Else not do 1, saltA, saltB, like CuSO4
  1153.             {
  1154.                 a = 1;
  1155.                 b = saltA;
  1156.                 c = saltB;
  1157.             }
  1158.  
  1159.             //If is error, return
  1160.             if (isError)
  1161.             {
  1162.                 isError = true;
  1163.                 Error();
  1164.                 return;
  1165.             }
  1166.             //Finding GCF
  1167.             if (newT.termCount == 2)
  1168.             {
  1169.                 //GCF Finding algorythm
  1170.                 int highest = 0;
  1171.                 if(a > b)
  1172.                 {
  1173.                     highest = a;
  1174.                 }
  1175.                 else
  1176.                 {
  1177.                     highest = b;
  1178.                 }
  1179.                 if(a != 1 && b != 1)
  1180.                 {
  1181.                     for(int i = highest; i > 1; i--)
  1182.                     {
  1183.                         if(a % i == 0 && b % i == 0)
  1184.                         {
  1185.                             a /= i;
  1186.                             b /= i;
  1187.                             break;
  1188.                         }
  1189.                     }
  1190.                 }
  1191.                 //Assigning
  1192.                 Component a1 = newT.components[0];
  1193.                 Component b1 = newT.components[1];
  1194.                 a1.SubCoefficent = a;
  1195.                 b1.SubCoefficent = b;
  1196.                 newComponents.Add(a1);
  1197.                 newComponents.Add(b1);
  1198.             }
  1199.             //If term count == 3
  1200.             else if (newT.termCount == 3)
  1201.             {
  1202.                 //GCF algorythm for 3 numbers
  1203.                 int[] abc = new int[3] { a, b, c };
  1204.                 int highest = abc.Max();
  1205.                 if(abc[0] != 1 && abc[1] != 1 && abc[2] != 1)
  1206.                 {
  1207.                     for(int i = highest; i > 1; i--)
  1208.                     {
  1209.                         if(a % i == 0 && b % i == 0 && c % i == 0)
  1210.                         {
  1211.                             a /= i;
  1212.                             b /= i;
  1213.                             c /= i;
  1214.                             break;
  1215.                         }
  1216.                     }
  1217.                 }
  1218.                 //Assigning
  1219.                 Component a2 = newT.components[0];
  1220.                 Component b2 = newT.components[1];
  1221.                 Component c2 = newT.components[2];
  1222.                 a2.SubCoefficent = a;
  1223.                 b2.SubCoefficent = b;
  1224.                 c2.SubCoefficent = c;
  1225.                 newComponents.Add(a2);
  1226.                 newComponents.Add(b2);
  1227.                 newComponents.Add(c2);
  1228.             }
  1229.             //Refreshing and setting to main Term
  1230.             t.components = newComponents;
  1231.             t.RefreshText();
  1232.         }
  1233.  
  1234.         void Error()
  1235.         {
  1236.             //Show error
  1237.             textBox2.Text = "ERROR! Write a real elements or check ions!";
  1238.         }
  1239.  
  1240.         struct Component
  1241.         {
  1242.             public int ion; //Ion (-2, 2)
  1243.             public string text; //Text (Cu)
  1244.             public int SubCoefficent; //Coefficent (Cu2 - 2)
  1245.             public bool isSaltPart; //True or false (SO4 - part is S or O)
  1246.         }
  1247.  
  1248.         struct Term
  1249.         {
  1250.             public string text; //CuSO4
  1251.             public int coefficent; //Coefficent before term
  1252.             public List<Component> components; //Its components (upper)
  1253.             public int termCount; //Term count
  1254.             public string salt; //Salt string
  1255.             public int saltIon; //Salt ion
  1256.             public List<int> saltSubstrings; //Salt subcoefficents
  1257.             public int saltMultiplier; //Salt multiplier
  1258.  
  1259.             //Booleans
  1260.             public bool isMetal;
  1261.             public bool isAcid;
  1262.             public bool isSalt;
  1263.             public bool isWater;
  1264.             public bool hasMetal;
  1265.             public bool isHC;
  1266.             public bool isOxide;
  1267.             public bool leftSide;
  1268.             public bool IsEmpty;
  1269.             public bool isSaltFormer;
  1270.             public bool isActiveMetal;
  1271.  
  1272.             //Check for errors
  1273.             void CheckErrors()
  1274.             {
  1275.                 if(components.Count == 1)
  1276.                 {
  1277.                     Component comp = components[0];
  1278.                     comp.SubCoefficent = 1;
  1279.                     components[0] = comp;
  1280.                 }
  1281.             }
  1282.            
  1283.             //Refresh text
  1284.             public void RefreshText()
  1285.             {
  1286.                 RecalculateSaltCoefficent();
  1287.                 CheckErrors();
  1288.                 text = "";
  1289.                 if (!isAcid)
  1290.                 {
  1291.                     foreach (Component c in components)
  1292.                     {
  1293.                         if (c.SubCoefficent != 1)
  1294.                         {
  1295.                             text += c.text + c.SubCoefficent.ToString();
  1296.                         }
  1297.                         else
  1298.                         {
  1299.                             text += c.text;
  1300.                         }
  1301.                     }
  1302.                 }
  1303.                 else
  1304.                 {
  1305.                     if (components[0].SubCoefficent != 1)
  1306.                     {
  1307.                         text = components[0].text + components[0].SubCoefficent + salt;
  1308.                     }
  1309.                     else
  1310.                     {
  1311.                         text = components[0].text + salt;
  1312.                     }
  1313.                 }
  1314.                 if(isSalt)
  1315.                 {
  1316.                     if(saltSubstrings.Count == 2 && saltMultiplier != 1)
  1317.                     {
  1318.                         int saltStart = 0, saltEnd = 0;
  1319.                         string saltNewString = "";
  1320.                         if(components[1].SubCoefficent != 1)
  1321.                         {
  1322.                             saltNewString += components[1].text + components[1].SubCoefficent;
  1323.                         }
  1324.                         else
  1325.                         {
  1326.                             saltNewString += components[1].text;
  1327.                         }
  1328.                         if(components[2].SubCoefficent != 1)
  1329.                         {
  1330.                             saltNewString += components[2].text + components[2].SubCoefficent;
  1331.                         }
  1332.                         else
  1333.                         {
  1334.                             saltNewString = components[2].text;
  1335.                         }
  1336.                         saltStart = text.IndexOf(saltNewString);
  1337.                         saltEnd = saltStart + saltNewString.Length;
  1338.                         text = text.Remove(saltStart, saltEnd - saltStart);
  1339.                         text += "(" + salt + ")" + saltMultiplier;
  1340.                     }
  1341.                 }
  1342.             }
  1343.             //Recalculate coefficent
  1344.             void RecalculateSaltCoefficent()
  1345.             {
  1346.                 if (isSalt)
  1347.                 {
  1348.                     if (saltSubstrings.Count == 2)
  1349.                     {
  1350.                         if (components[1].SubCoefficent > saltSubstrings[0] && components[2].SubCoefficent > saltSubstrings[1])
  1351.                         {
  1352.                             int newSaltCoefficent = 1;
  1353.                             newSaltCoefficent = components[1].SubCoefficent / saltSubstrings[0];
  1354.                             saltMultiplier = newSaltCoefficent;
  1355.                         }
  1356.                     }
  1357.                 }
  1358.             }
  1359.         }
  1360.  
  1361.         //Copy button
  1362.         private void button3_Click(object sender, EventArgs e)
  1363.         {
  1364.             if (textBox2.Text != "")
  1365.             {
  1366.                 System.Windows.Forms.Clipboard.SetText(textBox2.Text);
  1367.             }
  1368.         }
  1369.  
  1370.         //Back button
  1371.         private void button2_Click(object sender, EventArgs e)
  1372.         {
  1373.             Form2 f2 = new Form2();
  1374.             f2.Show();
  1375.             this.Close();
  1376.         }
  1377.     }
  1378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement