C++ 接受C+中的语法+;
这是我一直坚持的实验任务 我需要接受这个语法C++ 接受C+中的语法+;,c++,compiler-construction,grammar,C++,Compiler Construction,Grammar,这是我一直坚持的实验任务 我需要接受这个语法(ab)*b,它基本上意味着任意数量的“ab”和以b结尾 我写了这段代码,但不知怎么的,它只检查前两个字母 #include <iostream.h> #include <conio.h> #include <string.h> enum track {true, false}; void main() { clrscr(); char*str; enum track track_pos
(ab)*b
,它基本上意味着任意数量的“ab”和以b结尾
我写了这段代码,但不知怎么的,它只检查前两个字母
#include <iostream.h>
#include <conio.h>
#include <string.h>
enum track {true, false};
void main()
{
clrscr();
char*str;
enum track track_pos, track_pos_2;
cout<<"enter the string: ";
cin>>str;
int len=strlen(str);
cout<<"length of the string is "<<len;
getch();
int i;
for(i=0;i<len; i++)
{
++str;
cout<<"loop"<<i;
if(*str=='a' && i%2==0)
{
cout<<"\nchecking a...";
track_pos=true;
cout<<"\na.check";
++str;
if (*str=='b')
{
cout<<"\nchecking b...";
track_pos=true;
cout<<"\nb.check";
}
else{
track_pos=false;
cout<<"\nb.uncheck";
}
}
}
if(*str=='b')
track_pos_2=true;
else
track_pos_2=false;
if(track_pos==true && track_pos_2==true)
cout<<"\nThe string is accpeted.";
else
cout<<"\nThe string is rejected.";
getch();
cout<<"\n\nDo you want to continue (Y/N)? ";
char ch;
cin>>ch;
if(ch=='y' || ch=='Y')
main();
}
#包括
#包括
#包括
枚举轨迹{true,false};
void main()
{
clrsc();
char*str;
枚举跟踪跟踪位置,跟踪位置2;
库斯特;
int len=strlen(str);
无法实现一个简单的状态机。它具有以下状态:
- 0=开始
- 1='收到了一份(ab)'
- 2='收到的b/b(ab)'
- 3='收到最终b'
- -1=错误,语法无效
那么您只需要这样一个函数:
int nextState(int currentState, char inputChar) {
if (currentState == 0 && inputChar == 'a') return 1; // handled string is "a"
if (currentState == 0 && inputChar == 'b') return 3; // handled string is "b"
if (currentState == 1 && inputChar == 'b') return 2; // handled string is "ab", or "abab", or ...
if (currentState == 2 && inputChar == 'a') return 1; // handled string is "aba", or "ababa", or ...
if (currentState == 2 && inputChar == 'b') return 3; // handled string is "abb", or "ababb", or ...
return -1;
}
在输入字符上迭代这个“状态机”,从状态0开始,如果最终处于状态3,则输入是有效的
int isValid(char* inputString) {
int state = 0;
for(int i=0; i<str_len(inputString); i++) {
state = nextState(state, inputString[i]);
}
return (state == 3);
}
int是有效的(char*inputString){
int state=0;
对于(inti=0;i不要这样做
enum track {true, false};
这里的true等于0,false等于1!当您以后分配track_pos时,您可能会得到错误的值!(因为将bool转换为int时,true将转换为1,false将转换为0。)
不过这只是一个猜测。可能还有其他问题。您的代码有问题:
#include <iostream.h>
<>在C和C++中,main必须返回int:
void main()
非标准功能:
clrscr();
没有为此指针分配内存:
char*str;
然后在此处使用-结果未定义行为:
cin>>str;
对主服务器的非法调用:
main();
<>我怀疑你在使用一个非常陈旧过时的C++编译器。你应该用MinGW之类的东西来代替它。 < P>我会后悔的,但是每次我看这个问题,我都会发现你的代码有别的问题。这是一行一行。我可能错过很多。
这个标题的正确名称是“IOFSUBE”,而不是“IOStras.h”——“.h”版本被弃用。同样,在现代C++中使用“string”,而不是“String .h”,使用现代STL字符串类。
#include <iostream.h>
#include <conio.h>
#include <string.h>
main
函数的返回值是int
,而不是void
void main()
{
clrscr();
你知道什么是缓冲区溢出吗?你已经在这里定义了str
作为一个指针,没有分配内存,你稍后会写入未定义的内存位。这是未定义的行为,你几乎肯定会崩溃。我建议,你应该将str
定义为std::string
-这将是错误的icely可以避免缓冲区溢出,它有许多有用的方法可以在程序中使用
char*str;
enum track track_pos, track_pos_2;
cout<<"enter the string: ";
如果str
是一个std::string
,您将执行size\t leng=str.length()
在循环体中声明i
,因为您不再使用它。如下所示:
for (int i=0; i<len; i++) etc...
int i;
for(i=0;i<len; i++)
{
您应该将此更改为:
if (str[i]=='a' && i%2==0)
(顺便说一句,与指针算术版本不同,即使str
是std::string
,这也可以工作)
你真的应该在某个时候退出,如果你发现字符串不匹配,那么字符串的结尾就没有意义了
cout<<"\nchecking a...";
请注意,因为我们正在处理前面提到的缓冲区溢出,所以您正在迭代未定义的内存。还请注意,您没有在此处递增i
++str;
if (*str=='b')
{
cout<<"\nchecking b...";
track_pos=true;
cout<<"\nb.check";
}
else{
track_pos=false;
cout<<"\nb.uncheck";
}
}
}
我应该提到拼写错误吗
cout<<"\nThe string is accpeted.";
else
cout<<"\nThe string is rejected.";
getch();
cout<<"\n\nDo you want to continue (Y/N)? ";
char ch;
cin>>ch;
这是我第一次看到递归的main
:-)这是最容易的出路……:在混淆C代码竞争的情况下,这是很常见的。我怀疑这是使用一个非常古老的C++编译器。EnUM类给出了它。是的,像<代码> <代码>,没有<代码> STD::/Cord>前缀。等等,你能做吗?因为它是一个完整的类似于C++的程序,它所贴的是什么,编译它。在他的机器上用他的疯狂编译器!所以我只是想猜测他的疯狂编译器能用它做什么。对不同的状态使用枚举,这将使代码更好地可读性和可维护性。在开始编写代码之前,在纸上设计状态机,以确保涵盖所有不同的状态转换。作为一种快捷方式,您可以可能需要先检查最后的b。您可以将其转换为可能的输入值和结果状态的多维数组,并使其更干净。然后它将是:返回输出[currentState][inputChar];如果您有很多输入,这将变得不太有用,但在这里它会很好,并且可以让您轻松添加更多状态,而无需更改代码。我喜欢1800INFORMATION的答案,但这个解决方案是我最喜欢的。感谢您的评论。我想给出一个尽可能简单的答案。当然,我会在真正的应用程序中使用枚举你的状态机是错误的,它接受(ab)+b
而不是(ab)*b
。删除状态0并将2作为初始状态。“将导致最终堆栈溢出的漏洞”-他的程序已经将他引导到stack overflow.com。我不得不佩服发布此答案的家伙的坚持。
int len=strlen(str);
cout<<"length of the string is "<<len;
getch();
for (int i=0; i<len; i++) etc...
int i;
for(i=0;i<len; i++)
{
++str;
cout<<"loop"<<i;
if (str[i]=='a' && i%2==0)
if(*str=='a' && i%2==0)
{
cout<<"\nchecking a...";
track_pos=true;
cout<<"\na.check";
++str;
if (*str=='b')
{
cout<<"\nchecking b...";
track_pos=true;
cout<<"\nb.check";
}
else{
track_pos=false;
cout<<"\nb.uncheck";
}
}
}
if(*str=='b')
track_pos_2=true;
else
track_pos_2=false;
if(track_pos==true && track_pos_2==true)
cout<<"\nThe string is accpeted.";
else
cout<<"\nThe string is rejected.";
getch();
cout<<"\n\nDo you want to continue (Y/N)? ";
char ch;
cin>>ch;
if(ch=='y' || ch=='Y')
main();
}