I want to extract variables from math expression using c#. I wrote this code and it works right:
List<string> Variables = new List<string>();
string temp = string.Empty;
Console.WriteLine("Please enter ur expression");
string Expression = Console.ReadLine().Trim();
int Index;
for (Index = 0; Index <= Expression.Length - 1; Index++)
{
if (char.IsLetter(Expression[Index]))
{
temp = temp + Expression[Index];
}
else
{
if (temp.Length > 0)
{
Variables.Add(temp);
temp = string.Empty;
}
}
}
if (temp.Length > 0)
{
Variables.Add(temp);
}
foreach (string item in Variables)
{
Console.WriteLine(item);
}
Console.ReadKey();
I have to detect SIN and COS from expression so I will remove SIN and COS from Variables.
- Is it good way ?
- IS it possible to do this with Regular expressions or better ways ?
- Does this code need refactoring ?
After extract I want replace variables with values from input and I will calculate the expression result.