Translates infix formulas into MathML and vice-versa.
using System.Collections.Generic;
public class TranslateMath
{
public static int Main(string[] args)
{
string line;
string trimmed;
string result;
string str;
long len;
StringBuilder sb = new StringBuilder(1024);
Console.WriteLine("This program translates infix formulas into MathML and");
Console.WriteLine("vice-versa. Enter or return on an empty line triggers");
Console.WriteLine("translation. Ctrl-C quits");
while (true)
{
Console.WriteLine("Enter infix formula or MathML expression (Ctrl-C to quit):");
Console.Write("> ");
line = Console.ReadLine();
while (line != null)
{
trimmed = line.Trim();
len = trimmed.Length;
if (len > 0)
{
sb.AppendLine(trimmed);
}
else
{
str = sb.ToString();
result = (str[0] == '<') ? translateMathML(str) : translateInfix(str);
Console.WriteLine("Result:\n\n" + result + "\n\n");
sb = new StringBuilder(1024);
break;
}
line = Console.ReadLine();
}
}
}
public static string translateInfix(string formula)
{
ASTNode math = libsbml.parseFormula(formula);
string result = libsbml.writeMathMLToString(math);
return result;
}
public static string translateMathML(string xml)
{
ASTNode math = libsbml.readMathMLFromString(xml);
string result = libsbml.formulaToString(math);
return result;
}
}