如何写一个字符串,看看它是否是一个表达式
How to write a string and see if it is an expression?
我正在用递归解决一个算法问题。在这个问题中你需要找出的是如果你输入的字符串是一个表达式。例如,你有一个字符串"256+300-500"——这是一个表达式。因此,表达式是包含数字和符号"+"answers"-"的字符串。符号"+"answers"-"不能离得太近,也不能离得太近。所以"256++300"answers"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。
我认为你的问题呼唤一个现代的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!
use loop with small check
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
相关文章:
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 如何在一个表达式中生成并返回结果?
- 为什么一个表达式中的 std::string 连接给出的结果与逐个字符不同的结果?
- CUDA C++:文件中 kernel.cu 应有一个表达式
- 如何在一个表达式中将 char[] 转换为向量<char>(可移植)
- 无限数组C++在一个表达式中使用两个新值调整数组大小
- 期待一个表达式
- 在不使用lambda的情况下,我如何整齐地创建一个表达式的函数
- C++11-返回一个表达式以确定类型
- 用一个表达式包装变元宏中的每个元素
- 我有 3 个错误:期望一个")",期望一个表达式,long 类型的参数与 U32 类型的参数不兼容
- 计算一个表达式来计算循环
- 为什么 gdb 不能在 !=/== 和 &&/||组合在一个表达式中?
- 在 C++11 中的一个表达式中对同一变量进行双重赋值
- when是lambda函数中的一个表达式
- 如何写一个字符串,看看它是否是一个表达式
- c++标准算法,用最低优先级的运算符分隔一个表达式为两个表达式
- 我如何写一个表达式,在GDB的指针
- 我如何解析一个表达式嵌套括号与boost.Spirit