2021-11-05

2021-11-05,第1张

2021-11-05

识别(a|b)*b的C语言和C++代码实现
#include"stdio.h"
#include"string.h"

int fun()
{
char s[100];
scanf("%s",s);
int len=strlen(s);
for(int i=0;i {
if(len-1==i)
{
switch(s[i]) {
case’b’:
break;
default:
return false;
}
}
else{
switch(s[i]) {
case ‘a’:
break;
case’b’:
break;
default:
return false;
}
}
}
return 1;
}
int main()
{
while(1){
if(fun()) printf(“YESn”);
else printf(“NOn”);
}
return 0;
}


//(a|b)*b
//C++代码
#include
#include
using namespace std;string str;
int judge(int x,int now){
if(now1 && x>=str.length())
return 1;
if(x>=str.length()) return 0;
if(str[x]
‘a’) return judge(x+1,0); if(str[x]==‘b’)
return judge(x+1,1);
}
int main(){
while(1)
{
cin>>str;
if(judge(0,0))
cout<<“合法n”;
else
cout<<“不合法n”; }}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5098749.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-17
下一篇2022-11-16

发表评论

登录后才能评论

评论列表(0条)

    保存