递归检查表达式有效性的布尔函数

Boolean Function to Check Validity of Expression Recursively?

本文关键字:布尔 函数 有效性 表达式 检查 检查表 递归      更新时间:2023-10-16

我想创建一种形式为的解析器

#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
using namespace std;
bool isValid(istringstream& is)
{
  char ch;
  is.get(ch); //I know get(ch) is a good start but this is as for as I got :)
  .......
  ....
}
int main()
{
  string s;
  while(getline(cin,s))
  {
    istringstream is(s);
    cout<<(isValid(is)? "Expression OK" : "Not OK")<<endl;
  }
}

一个布尔函数,如果char的序列为"5"或"(5+3)"、"((5/3)+6)"或"((4+2)+1)+6"形式,则返回TRUE。。。等等,对于任何其他情况为FALSE

基本上,如果表达式是个位数或形式为"开括号个位数加符号个位数闭括号",则该表达式将被视为有效

  • 有效表达式=个位数

  • 有效表达式=(有效表达式+无效表达式

考虑到上面表单的大小没有限制(左括号和右括号的数量等),我想使用递归来实现这一点

作为一个新手,我…谢谢你的帮助!

要执行递归解决方案,您需要先将字符串读取到缓冲区中,然后执行以下操作:

int expression(char* str) {
    if (*str == '(') {
        int e1 = expression(str + 1);
        if (e1 == -1 || *(str + 1 + e) != '+') {
            return -1;
        }
        int e2 = expression(str + 1 + e + 1);
        if (e2 == -1 || *(str + 1 + e + 1 + e2) != ')') {
            return -1;
        }
        return 1 + e1 + 1 + e2 + 1;
    }
    if (*str >= '0' || *str <= '9') {
        return 1;
    }
    return -1;
}
bool isvalid(char* str) {
    int e1 = expression(str);
    if (e1 < 0) {
        return false;
    }
    if (e1 == strlen(str)) {
        return true;
    }
    if (*(str + e1) != '+') {
        return false;
    }
    int e2 = expression(str + e1 + 1);
    if (e2 < 0) {
        return false;
    }
    return (e1 + 1 + e2 == strlen(str));
}

基本上,表达式函数在其参数处返回有效表达式的长度。如果它的参数以圆括号开头,那么它会获取其后表达式的长度,然后验证加号,然后验证下一个表达式后的右括号。如果参数以数字开头,则返回1。如果出现问题,返回-1。然后使用这个函数,我们可以通过一些和和字符串的长度来判断字符串是否有效。

我根本没有测试过这个函数,但我能想到的唯一可能失败的情况是过多的括号:((5))。

递归的一种替代方法可以是某种词法解析,比如:

enum {
    ExpectingLeftExpression,
    ExpectingRightExpression,
    ExpectingPlus,
    ExpectingEnd,
} ParseState;
// returns true if str is valid
bool check(char* str) {
    ParseState state = ExpectingLeftExpression;
    do {
        switch (state) {
            case ExpectingLeftExpression:
                if (*str == '(') {
                } else if (*str >= '0' && *str <= '9') {
                    state = ExpectingPlus;
                } else {
                    printf("Error: Expected left hand expression.");
                    return false;
                }
            break;
            case ExpectingPlus:
                if (*str == '+') {
                    state = ExpectingRightExpression;
                } else {
                    printf("Error: Expected plus.");
                    return false;
                }
            break;
            case ExpectingRightExpression:
                if (*str == '(') {
                    state = ExpectingLeftExpression;
                } else if (*str >= '0' && *str <= '9') {
                    state = ExpectingEnd;
                } else {
                    printf("Error: Expected right hand expression.");
                    return false;
                }
            break;
        }
    } while (*(++str));
    return true;
}

这个函数根本不完整,但你应该能够看到它的去向。无论如何,我认为递归在这种情况下效果更好。