【问题标题】:How to write a string and see if it is an expression? [closed]如何写一个字符串,看看它是否是一个表达式? [关闭]
【发布时间】:2017-03-05 06:51:29
【问题描述】:

我现在正在使用递归解决算法问题。所以在这个问题中你需要弄清楚的是,如果你输入的字符串是一个表达式。例如,您有字符串“256+300-500” - 这是一个表达式。所以表达式是一个包含数字和符号“+”和“-”的字符串。标志 + 和 - 不能彼此靠近,也不能有 + 或 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;
}

这个程序的输出是0,因为它不是一个数字,如果是输出应该是1。请帮我制作我需要继续这个程序的程序。

【问题讨论】:

  • “不可能有 + 或 2 - 彼此靠近” - 我想说"10--2" 确实是一个有效的表达。 . .否则你会从你的程序中排除负数。
  • 不,当然 -- 等于 + 但不,我只需要不同的符号,请帮助某人

标签: c++ algorithm c++11 recursion c++14


【解决方案1】:

我认为您的问题需要一个现代的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;
}

代码运行示例:

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!

【讨论】:

    【解决方案2】:

    使用带有小检查的循环

    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
    

    【讨论】:

    • 感谢您的回答,但似乎这些都没有使用 recursive ,而且我非常希望继续解决我所做的代码的问题,谢谢无论如何它很好解释
    猜你喜欢
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 2012-01-29
    • 1970-01-01
    • 2018-02-02
    • 2015-12-30
    • 1970-01-01
    相关资源
    最近更新 更多