我在统一程序中平衡方程式时遇到问题。所以我写了一个叫做“均衡器”的脚本来平衡。我的问题是均衡器无法正常工作。有时会有一些“ 0”或溢出异常,但我看不到它了。我非常高兴,希望您能帮助我...
例如一个不起作用的方程:C₆H₆+ O 2→CO 2 + H 2 O
因此,脚本从根本上获得了解决此问题的功能:
pC a H 3 + bO 2→cCO 2 + dH 2 Oprivate float SolveEquation(string equation, List<float> vars, string varToSolve)
{
// 1a=1c
// 2a=1b
string leftSide = "";
string rightSide = "";
bool switchSide = false;
for (int x = 0; x < equation.Length; x++)
{
if (equation[x] == '=') switchSide = true;
if (!switchSide) leftSide += equation[x];
else if (switchSide && equation[x] != '=') rightSide += equation[x];
}
float divider = 1;
int sideToSolve = 0; // 0 = left 1 = right
// if left side of equation has the given var
if (leftSide.Contains(varToSolve) && !leftSide.Contains("+"))
{
if (leftSide.Replace(varToSolve, "") != "") divider = int.Parse(leftSide.Replace(varToSolve, ""));
sideToSolve = 1;
}
else if (leftSide.Contains(varToSolve))
{
for (int x = 1; x < 100; x++)
{
divider = x;
if (leftSide.Contains(x.ToString() + varToSolve)) leftSide = leftSide.Replace(x.ToString() + varToSolve, "");
leftSide = leftSide.Replace("+", "#");
leftSide = leftSide.Replace("-", "+");
leftSide = leftSide.Replace("#", "-");
rightSide += leftSide;
}
sideToSolve = 1;
}
// if right side of equation has the given var
if (rightSide.Contains(varToSolve) && !rightSide.Contains("+"))
{
if (rightSide.Replace(varToSolve, "") != "") divider = int.Parse(rightSide.Replace(varToSolve, ""));
sideToSolve = 0;
}
else if (rightSide.Contains(varToSolve))
{
for (int x = 1; x < 100; x++)
{
divider = x;
if (rightSide.Contains(x.ToString() + varToSolve)) rightSide = rightSide.Replace(x.ToString() + varToSolve, "");
rightSide = rightSide.Replace("+", "#");
rightSide = rightSide.Replace("-", "+");
rightSide = rightSide.Replace("#", "-");
leftSide += rightSide;
}
sideToSolve = 0;
}
float solution = 0;
List<float> values = new List<float>();
List<string> signs = new List<string>();
// come to a solution
if (sideToSolve == 0)
{
string localValue = "";
signs.Add("+");
values.Add(1);
int index = 0;
for (int x = 0; x < leftSide.Length; x++)
{
if (leftSide[x] == '+')
{
Debug.Log("Added one value from equation " + leftSide);
values.Add(1);
}
}
for (int x = 0; x < leftSide.Length; x++)
{
if (char.IsNumber(leftSide[x])) localValue += leftSide[x];
if (char.IsLetter(leftSide[x]))
{
if (vars[leftSide[x] - 65] == 0) return 0;
values[index] = vars[leftSide[x] - 65];
}
if (leftSide[x] == '+' || x == leftSide.Length - 1)
{
Debug.Log(rightSide + ": " + localValue);
if (localValue != "") values[index] *= float.Parse(localValue);
else return 0;
localValue = "";
index++;
signs.Add("+");
}
else if (leftSide[x] == '-' || x == leftSide.Length - 1)
{
Debug.Log(rightSide + ": " + localValue);
if (localValue != "") values[index] *= float.Parse(localValue);
else return 0;
localValue = "";
index++;
signs.Add("-");
}
}
}
if (sideToSolve == 1)
{
string localValue = "";
signs.Add("+");
values.Add(1);
int index = 0;
for (int x = 0; x < rightSide.Length; x++)
{
if (rightSide[x] == '+')
{
Debug.Log("Added one value from equation " + rightSide);
values.Add(1);
}
}
for (int x = 0; x < rightSide.Length; x++)
{
if (char.IsNumber(rightSide[x])) localValue += rightSide[x];
if (char.IsLetter(rightSide[x]))
{
if (vars[rightSide[x] - 65] == 0) return 0;
values[index] = vars[rightSide[x] - 65];
}
if (rightSide[x] == '+' || x == leftSide.Length - 1)
{
Debug.Log(rightSide + ": " + localValue);
if (localValue != "") values[index] *= float.Parse(localValue);
else return 0;
localValue = "";
index++;
signs.Add("+");
}
else if (rightSide[x] == '-' || x == leftSide.Length - 1)
{
Debug.Log(rightSide + ": " + localValue);
if (localValue != "") values[index] *= float.Parse(localValue);
else return 0;
localValue = "";
index++;
signs.Add("-");
}
}
}
for (int x = 0; x < values.Count; x++)
{
Debug.Log("Values Count: " + values.Count);
// Debug.Log("Sign Length: " + signs.Count + ", X Index: " + x);
if (signs[x] == "+") solution += values[x];
if (signs[x] == "-") solution -= values[x];
}
solution /= divider * globalMultiplicator;
return solution;
}