Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何编写字符串并查看它是否是表达式?_C++_Algorithm_C++11_Recursion_C++14 - Fatal编程技术网

C++ 如何编写字符串并查看它是否是表达式?

C++ 如何编写字符串并查看它是否是表达式?,c++,algorithm,c++11,recursion,c++14,C++,Algorithm,C++11,Recursion,C++14,我现在正在用递归解决一个算法问题。因此,在这个问题中,您需要了解的是,如果您输入的字符串是一个表达式。例如,字符串“256+300-500”-这是一个表达式。所以表达式是一个包含数字和符号“+”和“-”的字符串。符号+和-不能一个挨着一个,也不能有to+或2-挨着一个。所以“256++300”和“256+-600-500”不是一个表达式。此外,如果数字中包含字母,则它不是表达式“54e2”。所以请帮我做这个节目。我已经做了一部分,看看数字字符串是否是一个数字 #include <iostr

我现在正在用递归解决一个算法问题。因此,在这个问题中,您需要了解的是,如果您输入的字符串是一个表达式。例如,字符串“256+300-500”-这是一个表达式。所以表达式是一个包含数字和符号“+”和“-”的字符串。符号+和-不能一个挨着一个,也不能有to+或2-挨着一个。所以“256++300”和“256+-600-500”不是一个表达式。此外,如果数字中包含字母,则它不是表达式“54e2”。所以请帮我做这个节目。我已经做了一部分,看看数字字符串是否是一个数字

#include <iostream>
#include <cmath>
#include<string>
using namespace std;
int cif(char c)
{
if(isdigit(c))return 1;
else return 0;
}
int num (string s)
{
int z=s.length();
if(z==1) return cif(s[0]);
else
    {
      char c =s[0];
      s=s.substr(1);
      if(cif(c) && num(s))return 1; else return 0;
    }
}
int main()
{
 cout << num("2353Y3554");
 return 0;
}
#包括
#包括
#包括
使用名称空间std;
到岸价整数(字符c)
{
if(isdigit(c))返回1;
否则返回0;
}
int num(字符串s)
{
intz=s.length();
如果(z==1)返回到岸价(s[0]);
其他的
{
字符c=s[0];
s=s.substr(1);
if(cif(c)和&num(s))返回1;否则返回0;
}
}
int main()
{

不能使用带有小支票的循环

bool is_leeter(char str)
{
    char abc[27]= "abcdefghijklmnopqrstuvwxyz";
    char ABC[27]= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for(int i=0;i<27;i++)
        if((abc[i] || ABC[i]) == str)
            return true;
    true false;
}

bool is_expression(char str)
{
    char exp[5]= "+-*/";
    for(int i=0;i<5;i++)
        if(exp[i] == str)
            return true;
    true false;
}

for(int i=0;i<strlen(str);i++)
{

    if(!is_leeter(str[i]) && !is_expression(str[i]) && !is_expression(str[i+1]))
    //do your thing
bool is_leeter(char str)
{
char abc[27]=“abcdefghijklmnopqrstuvwxyz”;
char ABC[27]=“ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
对于(int i=0;i我认为您的问题迫切需要一个现代的
regex
(和
string
)解决方案:

#include <iostream>
#include <string>
#include <regex>

int main(){

    std::regex expression{ "[\\d]+([-|+][\\d]+)+" };
    //Explanation:
    // [\\d]+  > at least one digit
    // ([-|+]  > - OR + character
    // [\\d]+  > at least one digit
    // )+      > at least once "- Or +" and "at least one digit" (grouping!)
    bool done = false;

    do {
        std::cout << "Type in an expression (\"+\" and/or \"-\" operator!): ";
        std::string str;
        std::cin >> str;

        if (std::regex_match(str, expression)){ //does match the rules!
            std::cout << "It's a valid expression!" << std::endl;
            done = true; //exit
        }
        else{ //it doesn't . . . type in again.
            std::cout << "That's not a valid expression . . . \n" << std::endl;
        }
    } while (!done);

    return 0;
}

“不可能有to+或2-彼此靠近”-我会说
“10--2”
确实是一个有效的表达式…否则您将从程序中排除负数。不,当然-,等于+,但不,我需要的只是不同的符号,请帮助他人了解您的答案,但似乎所有这些都不是使用递归的,而且我非常希望用我编写的代码继续解决这个问题请,谢谢不管怎样,这是非常好的解释
Type in an expression ("+" and/or "-" operator!): Hello
That's not a valid expression . . .

Type in an expression ("+" and/or "-" operator!): 100+A
That's not a valid expression . . .

Type in an expression ("+" and/or "-" operator!): 100+
That's not a valid expression . . .

Type in an expression ("+" and/or "-" operator!): 100++42
That's not a valid expression . . .

Type in an expression ("+" and/or "-" operator!): +100+42
That's not a valid expression . . .

Type in an expression ("+" and/or "-" operator!): 100+42-50
It's a valid expression!