goto问题和for循环问题
goto problems and for loops problems
我有一个项目,我需要写一个代码来模拟DFA来接受url。我已经弄清楚的DFA部分是完全可以的,但是我在代码方面遇到了一些问题。第一个问题是,当我运行代码时,它似乎没有通过cout<lt;"输入y/n部分。第二个可能是其中的一部分,但我得到了很多
27逗号运算符的左操作数无效[-Wunused值]
27逗号运算符的右操作数无效[-Wunused值]
27错误:应为";"在")"标记之前
31错误:"转到"之前应为主表达式
31错误:在"转到"之前应为")"
由于某种原因,在每个for循环处
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main() {
char asdf='y';
string str;
int stringlength,state;
while (asdf=='y'){
start:
cout << "Do you want to enter a string?(y/n)n";
cin >> asdf;
cout << "Enter the string:n";
cin >> str;
stringlength=str.size();
state=0;
//transition states q0 start
//w means goto q1,increment state by 1
if(str[0]=='w'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q1;
}
//. means trap state,loop the rest of the char's and output trap state and output "rejected", then return to start
if(str[0]=='.'){
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trapn";
}
cout << "rejectedn";
goto start;
}
//not w and not . means goto q7
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q7;
q1:
//w means goto q2
if (str[1]=='w'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q2;
}
//. means go to q6
if (str[1]=='.'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q6;
}
//not w and not . means goto q7
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q7;
q2:
//w means goto q3
if (str[2]=='w'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q3;
}
//. means go to q6
if (str[2]=='.'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q6;
}
//not w and not . means goto q7
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q7;
q3:
//w means goto q3
if (str[3]=='.'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q4;
}
//not . means goto q7
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q7;
q4:
//. mean trap state
if (str[4]=='.'){
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trapn";
}
cout << "rejectedn";
goto start;
}
//everything else goes to q5
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q5;
q5:
//. means goto q6
if (str[state]=='.'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q6;
}
//everything else goes back to q5
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q5;
q6:
//c means goto q8
if (str[state]=='c'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q8;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trapn";
}
cout << "rejectedn";
goto start;
q7:
//. means goto q6
if (str[state]=='.'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q6;
}
//everything else goes back to q7
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q7;
q8:
//o means goto q9
if (str[state]=='o'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q9;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trapn";
}
cout << "rejectedn";
goto start;
q9:
//m means goto q10
if (str[state]=='m'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q10;
}
//. means goto q11
if (str[state]=='.'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q11;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trapn";
}
cout << "rejectedn";
goto start;
q10:
//compare length of string to state,if it matches there are no more char in string and is accepted
if (stringlength==state){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
cout << "acceptedn";
goto start;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trapn";
}
cout << "rejectedn";
goto start;
q11:
//j means goto q12
if (str[state]=='j'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q12;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trapn";
}
cout << "rejectedn";
goto start;
q12:
//p means goto q13
if (str[state]=='p'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
state++;
goto q13;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trapn";
}
cout << "rejectedn";
goto start;
q13:
//compare length of string to state,if it matches there are no more char in string and is accepted
if (stringlength==state){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"n";
cout << "acceptedn";
goto start;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trapn";
}
cout << "rejectedn";
goto start;
}
return 0;
}
您需要使用分号而不是逗号来分隔for
循环中的三个表达式:
for(state,state>0,state--){
^-------^ Change these to ;
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 在java中解决这段代码时面临循环中的问题
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- 退出简单while循环时出现问题
- 循环无限运行C++解决骑士之旅问题
- Visual Studio 2017循环自动向量化问题
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 嵌套While循环不起作用(C++问题)
- 使用 GMP 的 while 循环出现问题
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- OpenMP for 循环并行性问题
- 这个返回元素位置的基于循环的函数有什么问题?
- 为什么循环会导致指针出现问题?
- 使用基于范围的循环迭代矢量时出现问题
- 在 c++ 中循环遍历数组时遇到问题
- 运行无限循环的最小二叉树问题
- 在C++中循环访问自定义结构列表的小问题
- 在 for 循环中更新两个变量时遇到问题C++