验证mathamaticl方程 - 适当的括号关闭 [英] Validate mathamaticl equation - proper brackets closing
本文介绍了验证mathamaticl方程 - 适当的括号关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个复杂的字符串(一些数学方程式)
例如。 (这个等式是用户输入)
((如果> 2))(((x * _count)+ absoluteVal)sigmaVal))((i *(x / 3))(sin30 / tan)))prob_val))
我需要检查这些括号是否正确关闭
谢谢
解决方案
使用堆栈推送和弹出,请查看:
1. c example [ ^ ]
2. java example [ ^ ]
只需使用递增的计数器单步执行字符关于开头括号和减量o结束的。当计数器在关闭括号处为零时断开(不匹配的右括号)。当在字符串的末尾时,计数器必须为零(否则为不匹配的开启括号)。
AC / C ++示例:
< pre lang =c ++> // 成功时返回零,
// < 0在不匹配的右括号和
// > 0在不匹配的开始括号上。
int CheckBrackets( const < span class =code-keyword> char * s)
{
int count = 0 跨度>;
while (* s)
{
if (' (' == * s)
++ count;
else if (' )' == * s)
{
if ( - count< 0 )
break ;
}
++ s;
}
返回计数;
}
static bool validateFun( string 输入)
{
bool falag = true ;
string strBrace = string .Empty;
char [] currentCharArray = input.ToCharArray();
for ( int i = 0 ; i < currentCharArray.Length; i ++)
{
char currentChar = currentCharArray [i];
if (currentChar == ' ('' )
{
strBrace = strBrace + currentChar.ToString();
}
else if (currentChar == ' )')
{
strBrace = strBrace + currentChar.ToString();
}
}
if (input.Split(' < span class =code-string>(')。长度 - 1 == input.Split( ' )')。长度 - 1 )
{
string removeBrace = strBrace.Replace( (), );
removeBrace = removeBrace.Trim();
if (removeBrace == )
{
Console.WriteLine( valid);
}
其他
{
falag = true ;
}
}
其他
falag = false ;
return falag;
}
I have a complex string (some mathematical equation)
for eg. (this equation is user entry)
((if>2))(((x*_count)+ absoluteVal)sigmaVal))((i*(x/3))(sin30/tan)))prob_val))
I need to check these brackets has proper closing
Thanks
解决方案
Use stack push and pop, check out:
1. c example[^]
2. java example[^]
Just step through the characters using a counter that increments on openening brackets and decrements on closing ones. Break when the counter is zero at a closing bracket (unmatched closing bracket). When at the end of the string, the counter must be zero (unmatched openening bracket otherwise).
A C/C++ example:
// Returns zero upon success, // < 0 upon unmatched closing bracket and // > 0 upon unmatched opening bracket(s). int CheckBrackets(const char *s) { int count = 0; while (*s) { if ('(' == *s) ++count; else if (')' == *s) { if (--count < 0) break; } ++s; } return count; }
static bool validateFun(string input) { bool falag = true; string strBrace = string.Empty; char[] currentCharArray = input.ToCharArray(); for (int i = 0; i < currentCharArray.Length; i++) { char currentChar = currentCharArray[i]; if (currentChar == '(') { strBrace = strBrace + currentChar.ToString(); } else if (currentChar == ')') { strBrace = strBrace + currentChar.ToString(); } } if (input.Split('(').Length - 1 == input.Split(')').Length - 1) { string removeBrace = strBrace.Replace("()", ""); removeBrace = removeBrace.Trim(); if (removeBrace == "") { Console.WriteLine("valid"); } else { falag = true; } } else falag = false; return falag; }
这篇关于验证mathamaticl方程 - 适当的括号关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文