尝试递归地验证数学表达式 - 这段代码有什么问题?
有效表达式 = 个位数
AND
有效表达式 = ( 有效表达式 + 有效表达式 )
这意味着我尝试创建的布尔函数只接受以下类型的有效表达式:
5
(5+3)
(6+(3+2))
((7+1)+(5+1))
............... etc.
我希望函数的参数保持原样 (istringstream& is),并且我想在函数中使用 get(ch)。
我也想递归地做。但是,我做错了,它只验证类型的表达式:个位数
我的递归问题出在哪里?我知道这甚至不是一个正确的递归,我确信正确的递归甚至可以用更少的行数并且没有嵌套的 if 来完成这项工作。
感谢您提供任何有用的建议!
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
using namespace std;
bool isvalid(istringstream& is)
{
char ch;
is.get(ch);
if(ch-'0'>=0 || ch-'0'<=9) return true;
if(ch=='(' && isvalid(is))
{
is.get(ch);
if(ch=='+' && isvalid(is))
{
is.get(ch);
if(ch==')') return true;
}
}
return false;
}
bool empty(istringstream& is)
{
char ch;
is.get(ch);
return is.fail();
}
int main()
{
string s;
while(getline(cin,s))
{
istringstream is(s);
cout<<(isvalid(is) && empty(is) ? "Expression OK" : "Not OK")<<endl;
}
}
Valid Expression = single digit
AND
Valid Expression = ( Valid Expression + Valid Expression )
Which means that the boolean function that I'm trying to create will only accept as valid, expressions of the following type:
5
(5+3)
(6+(3+2))
((7+1)+(5+1))
............... etc.
I want my function's parameters to stay as they are (istringstream& is) and I want to use a get(ch) in my function.
I also want to do it recursively. However, I'm doing something wrong and it only validates expressions of type : single digit
Where's the problem in my recursion? And I know it's not even a proper recursion, I'm sure a proper recursion could do the job even in fewer lines and with no nested ifs..
Thank you for any helpful suggestions!
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
using namespace std;
bool isvalid(istringstream& is)
{
char ch;
is.get(ch);
if(ch-'0'>=0 || ch-'0'<=9) return true;
if(ch=='(' && isvalid(is))
{
is.get(ch);
if(ch=='+' && isvalid(is))
{
is.get(ch);
if(ch==')') return true;
}
}
return false;
}
bool empty(istringstream& is)
{
char ch;
is.get(ch);
return is.fail();
}
int main()
{
string s;
while(getline(cin,s))
{
istringstream is(s);
cout<<(isvalid(is) && empty(is) ? "Expression OK" : "Not OK")<<endl;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这需要是一个 &&表情,不是吗?
This needs to be an && expression, doesn't it?
考虑一下字符串
()
。看起来像这样:isvalid("()"): ch = '(' is not in 0-9, so it go to the next line
ch == '(',因此它评估 isValid(is)。
第二级:isvalid(")"):ch = ')',同样,不是 0-9,因此它转到下一行
ch != '(',因此它短路了布尔表达式的计算并返回 false
返回第一级: isvalid(is) 失败,因此我们跳过 if 块并返回 false
Think about the string
()
. That will look like this:isvalid("()"): ch = '(' is not in 0-9, so it goes to the next line
ch == '(', so it evaluates isValid(is).
second level: isvalid(")"): ch = ')', again, not 0-9, so it goes to the next line
ch != '(', so it shortcircuits the evaluation of the boolean expression and returns false
back to the first level: isvalid(is) failed, so we skip the if block and return false
在
if(ch==')' && 中isvalid(is)) return true;
我认为您不需要
&& isvalid(is)
部分。它不符合你的语法In
if(ch==')' && isvalid(is)) return true;
I don't think you need the
&& isvalid(is)
part. It does not follow your grammar内部 if(ch==')' && ...) 永远是 false (因为 ch=='+' 如果你到达那个 if)——缺少 get(ch) 。
The inner if(ch==')' && ...) will always be false (since ch=='+' if you get to that if) -- there's a get(ch) missing.