S
Sudip_inn
Guest
i found a link Validate an arithmetic expression - akshatsharma80
which has a function which can tell me expression is valid or not. just tell me below routine is perfect one or should i use other?
.net has anything in-built ? tell me few best to achieve this. thanks
static void Main(string[] args)
{
ArrayList arrexpressions = new ArrayList();
arrexpressions.Add("a+b-c");
arrexpressions.Add("a+b-c+");
arrexpressions.Add("a++b-c");
arrexpressions.Add("a+(b-c)");
arrexpressions.Add("a+(b-c)*(d-e)");
arrexpressions.Add("a+((b-c)*(d-e))");
arrexpressions.Add("a+((b-c)/(d*e)+(f/g))");
arrexpressions.Add("a+((((b-c)*d)*s)*q)");
arrexpressions.Add("a+(b-cc)");
foreach (string s1 in arrexpressions)
{
string s = s1;
s = "(" + s + ")";
s = s.Replace(" ", string.Empty);
Console.WriteLine("Exp : " + s);
Console.WriteLine(validate(s) == true ? "true" : "false");
Console.WriteLine();
}
Console.Read();
}
public static bool validate(string expression)
{
int previous = 0;
int previous1 = 0;
string expEvaluated = string.Empty;
int operatorOperand = 1;
for (int i = 0; i < expression.Length; i++)
{
char c = expression;
if (c == ')')
{
}
else if (c == '(')
{
int j = expression.IndexOf(')', i);
if (j == -1)
return false;
string substring = expression.Substring(i + 1, j - i - 1);
while (getcharactercount(substring, '(') != getcharactercount(substring, ')'))
{
if (j < expression.Length - 1)
j = expression.IndexOf(')', j + 1);
else
break;
substring = expression.Substring(i + 1, j - i - 1);
} i = j - 1; //Changing the counter i to point to the next character
//validating the sub expression
if (validate(substring) == true)
{
if (previous != 0 && previous1 != 0 && previous > previous1)
{
previous1 = operatorOperand;
operatorOperand++;
previous = 0;
}
else if (previous != 0 && previous1 != 0 && previous <= previous1)
{
return false;
}
else if (previous1 != 0)
{
return false;
}
else
{
previous1 = operatorOperand;
operatorOperand++;
}
}
else
{
return false;
}
}
else if (c == '+' || c == '-' || c == '*' || c == '/')
{
if (previous != 0)
{
return false;
}
previous = operatorOperand;
operatorOperand++;
}
else
{
if (previous != 0 && previous1 != 0 && previous > previous1)
{
previous1 = operatorOperand;
operatorOperand++;
previous = 0;
}
else if (previous != 0 && previous1 != 0 && previous <= previous1)
{
return false;
}
else if (previous1 != 0)
{
return false;
}
else
{
previous1 = operatorOperand;
operatorOperand++;
}
}
}
if (previous != 0)
return false;
return true;
}
public static int getcharactercount(string exp, char _c)
{
int count = 0;
foreach (char c in exp)
{
if (c == _c)
count++;
}
return count;
}
Continue reading...
which has a function which can tell me expression is valid or not. just tell me below routine is perfect one or should i use other?
.net has anything in-built ? tell me few best to achieve this. thanks
static void Main(string[] args)
{
ArrayList arrexpressions = new ArrayList();
arrexpressions.Add("a+b-c");
arrexpressions.Add("a+b-c+");
arrexpressions.Add("a++b-c");
arrexpressions.Add("a+(b-c)");
arrexpressions.Add("a+(b-c)*(d-e)");
arrexpressions.Add("a+((b-c)*(d-e))");
arrexpressions.Add("a+((b-c)/(d*e)+(f/g))");
arrexpressions.Add("a+((((b-c)*d)*s)*q)");
arrexpressions.Add("a+(b-cc)");
foreach (string s1 in arrexpressions)
{
string s = s1;
s = "(" + s + ")";
s = s.Replace(" ", string.Empty);
Console.WriteLine("Exp : " + s);
Console.WriteLine(validate(s) == true ? "true" : "false");
Console.WriteLine();
}
Console.Read();
}
public static bool validate(string expression)
{
int previous = 0;
int previous1 = 0;
string expEvaluated = string.Empty;
int operatorOperand = 1;
for (int i = 0; i < expression.Length; i++)
{
char c = expression;
if (c == ')')
{
}
else if (c == '(')
{
int j = expression.IndexOf(')', i);
if (j == -1)
return false;
string substring = expression.Substring(i + 1, j - i - 1);
while (getcharactercount(substring, '(') != getcharactercount(substring, ')'))
{
if (j < expression.Length - 1)
j = expression.IndexOf(')', j + 1);
else
break;
substring = expression.Substring(i + 1, j - i - 1);
} i = j - 1; //Changing the counter i to point to the next character
//validating the sub expression
if (validate(substring) == true)
{
if (previous != 0 && previous1 != 0 && previous > previous1)
{
previous1 = operatorOperand;
operatorOperand++;
previous = 0;
}
else if (previous != 0 && previous1 != 0 && previous <= previous1)
{
return false;
}
else if (previous1 != 0)
{
return false;
}
else
{
previous1 = operatorOperand;
operatorOperand++;
}
}
else
{
return false;
}
}
else if (c == '+' || c == '-' || c == '*' || c == '/')
{
if (previous != 0)
{
return false;
}
previous = operatorOperand;
operatorOperand++;
}
else
{
if (previous != 0 && previous1 != 0 && previous > previous1)
{
previous1 = operatorOperand;
operatorOperand++;
previous = 0;
}
else if (previous != 0 && previous1 != 0 && previous <= previous1)
{
return false;
}
else if (previous1 != 0)
{
return false;
}
else
{
previous1 = operatorOperand;
operatorOperand++;
}
}
}
if (previous != 0)
return false;
return true;
}
public static int getcharactercount(string exp, char _c)
{
int count = 0;
foreach (char c in exp)
{
if (c == _c)
count++;
}
return count;
}
Continue reading...