gpt4 book ai didi

c - 如何用c语言编写正则表达式?我正在使用 c 中的正则表达式概念对 Prime No. 进行测试用例,以下是我的源代码

转载 作者:行者123 更新时间:2023-11-30 19:35:20 24 4
gpt4 key购买 nike

如何用c语言编写正则表达式?我正在使用 c 中的正则表达式概念对 Prime No. 进行测试用例,以下是我的源代码。此代码不应接受任何负数,不应接受 0 和 1,不应接受十进制数(浮点值),也不应接受任何字母或字符或特殊符号,我对正则表达式接受的概念有疑问!

#include <stdio.h>
#include <string.h>
#include <regex.h>


int main() {
int i, n, prime, flag=0;

char ch[10],*p;

printf("Enter the Number to check Whether its prime or not\n");

scanf("%s",&ch);

if(p= strstr(ch,".")){

printf("Decimal Values are not allowed in Prime Number!\n");

goto error;

}else if (ch==[A-Za-z]){

printf("Please Enter the Numeric Value\n");

}

n=atoi(ch);

printf("The Integer Type Cast Value is:%d\n",n );

if(n<=0){

printf("%d is not a Valid number, prime number is always positive & greater than & is not in decimal 0\n",n );

goto error;

}else{

for(i=2; i<=n/2; ++i){

if(n%i==0){

flag=1;

break;

}
}
}

if (flag==0)
printf("%d is a prime number.",n);
else if (flag==1)

printf("%d is not a prime number.",n);

else

error:printf("%s is not a Valid number, prime number is always positive & greater than & is not in decimal 0\n",ch);

return 0;
}

最佳答案

您不能简单地将字符串与正则表达式进行比较,如下所示:ch==[A-Za-z]。您必须使用 regcomp() 准备(编译)正则表达式,然后使用 regexec() 将正则表达式与您的输入字符串进行匹配。

可以找到使用 POSIX 正则表达式函数的旧示例 here .

关于c - 如何用c语言编写正则表达式?我正在使用 c 中的正则表达式概念对 Prime No. 进行测试用例,以下是我的源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42811106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com