我认为我缺乏结构知识。我收到如下错误:
pda.c:33:26:错误:未声明“顶部”(此功能首次使用)
if(pda.stack [top] =='\ 0')
pda.c:54:7:错误:未声明“已接受”(此功能首次使用)
如果(接受== 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;
}
有任何想法吗?谢谢。
最佳答案
就像结构中的其他变量一样,必须使用点符号将top
作为pda.top
访问。并且accepted
必须为pda.accepted
。
某些语言具有名称范围操作符,例如with
或using
,但是C对此没有捷径。