我认为我的结构知识缺乏。我收到的错误如下:
- pda.c:33:26:错误:'top'未声明(首次使用此功能)
if(pda.stack [top] =='\ 0')- pda.c:54:7:错误:'接受'未声明(首次使用此功能)
if(接受== 0)- pda.c:在函数'qX'中:
醇>
pda.c:93:17:错误:'top'未声明(首次使用此功能)
pda.stack [top] =输入;
基本上我的全局结构中的变量没有得到识别,我不知道为什么。这是我记录的代码:
//This project is a PDA which accepts alphabetical, lower-case palindromes written out and divided by a #.
//Example: "hello world#dlrow olleh" is an accepted string.
#include <stdio.h>
//Pushdown automata structure includes the string to validate with an index, the stack
//with an index, the current state, the running status, and the result.
struct PDA {
char string[200];
char stack[200];
int current;
int top;
int qOrp;
int finished;
int accepted;
};
//Initialization of struct and state methods.
struct PDA pda = {.current = 0, .top = 0, .qOrp = 0, .finished = 0, .accepted = 0};
int validateInput();
int qE(char input);
int qX(char input);
int pX(char input);
int pE();
int main() {
//Prompt for string to validate.
printf("Enter the string to be verified: ");
scanf("%s\n", &pda.string);
//Validates the alphabet of the string.
pda.finished = validateInput();
//While the PDA is rinning, do...
while(pda.finished == 0) {
//Different cases for different states.
switch(pda.qOrp) {
case 0:
//State for the initial, empty stack.
if(pda.stack[top] == '\0')
pda.finished = qE(pda.string[current]);
//State for non-empty stack before the #.
else {
pda.finished = qE(pda.string[current]);
}
break;
case 1:
//State for empty stack after #.
if(pda.stack[top] == '\0')
pda.finished = pE();
//State for non-empty stack after #.
else
pda.finished = pX(pda.string[current]);
break;
//Reject if not in one of the states above.
default:
pda.finished = 1;
}
}
//Print for acceptance/rejection.
if(accepted == 0)
printf("The string \"%s\" is rejected.", pda.string);
else
printf("The string \"%s\" is accepted.", pda.string);
return 0;
}
//Loops through the string to validate and makes sure the alphabet is correct. Otherwise, reject.
int validateInput() {
int i = 0;
while(pda.string[i] != '\0') {
if(pda.string[i] < 97 || pda.string[i] > 122 || pda.string[i] != '#' || pda.string[i] != ' ')
return 1;
}
return 0;
}
//Initial State. Checks for illegal characters. Otherwise, push current character.
int qE(char input) {
if(input == '#')
return 1;
else {
pda.stack[top] = input;
pda.top++;
pda.current++;
return 0;
}
}
//Second State. Switches to next state on #. Otherwise, push current character.
int qX(char input) {
if(input == '#') {
pda.qOrp++;
pda.current--;
pda.top--;
}
else {
pda.stack[top] = input;
pda.top++;
pda.current++;
}
}
//Third state. Checks for illegal characters. Otherwise, pop one element from stack.
int pX(char input) {
if(input == '#')
return 1;
else {
if(pda.string[current] == pda.stack[top]) {
pda.stack[top] = '\0';
pda.top--;
pda.current--;
}
else
return 1;
}
}
//Final state. Declares the string "accepted."
int pE() {
pda.accepted = 1;
return 1;
}
任何想法?感谢。
答案 0 :(得分:0)
就像结构中的其他变量一样,top
必须使用点表示法作为pda.top
进行访问。 accepted
必须为pda.accepted
。
某些语言有with
或using
这样的名称范围操作符,但C没有这方面的快捷方式。