英文:
K&R exercise 1-24
问题
这是我为该程序编写的部分翻译(它还不完整,我稍后会继续完善):
#include <stdio.h>
#define YES 1
#define NO 0
int a = 0;
void push(int stack[], int c);
void pull(int stack[]);
int check(int stack[], int c);
int main()
{
int stack[1000], c, keep = 1;
extern int a;
while ((c = getchar()) != EOF && keep == YES)
{
if (c == '[' || c == '(' || c == '{')
push(stack, c);
else if (c == ']' || c == ')' || c == '}')
keep = check(stack, c);
}
return 0;
}
void push(int stack[], int c)
{
int i;
for (i = a; i; --i)
stack[i+1] = stack[i];
++a;
stack[0] = c;
}
void pull(int stack[])
{
int i;
--a;
for (i = 0; i < a; ++i)
stack[i] = stack[i+1];
stack[a] = '\0';
}
int check(int stack[], int c)
{
if ((c == ']' && stack[0] == '[') || (c == ')' && stack[0] == '(') || (c == '}' && stack[0] == '{'))
{
pull(stack);
return YES;
}
else
{
printf("不匹配的字符:%c\n", c);
return NO;
}
}
当我给它输入{}
或( ) () {} []
时,它可以正常工作,但如果输入(())
,它会给出以下输出:
英文:
> Exercise 1-24. Write a program to check a C program for rudimentary
> syntax errors like unmatched parentheses, brackets and braces. Don't
> forget about quotes, both single and double, escape sequences, and
> comments. (This program is hard if you do it in full generality.)
This is the code I wrote for the program(It's not complete yet, I'll work on it more later on):
#include <stdio.h>
#define YES 1
#define NO 0
int a = 0;
void push(int stack[], int c);
void pull(int stack[]);
int check(int stack[], int c);
int main()
{
int stack[1000], c, keep = 1;
extern int a;
while ((c = getchar()) != EOF && keep == YES)
{
if (c == '[' || c == '(' || c == '{')
push(stack, c);
else if (c == ']' || c == ')' || c == '}')
keep = check(stack, c);
}
return 0;
}
void push(int stack[], int c)
{
int i;
for (i = a; i; --i)
stack[i+1] = stack[i];
++a;
stack[0] = c;
}
void pull(int stack[])
{
int i;
--a;
for (i = 0; i < a; ++i)
stack[i] = stack[i+1];
stack[a] = '\0';
}
int check(int stack[], int c)
{
if ((c == ']' && stack[0] == '[') || (c == ')' && stack[0] == '(') || (c == '}' && stack[0] == '{'))
{
pull(stack);
return YES;
}
else
{
printf("Mismatched character: '%c'\n", c);
return NO;
}
}
when I give it an output like {}
or ( ) () {} []
it works but with something like (())
it gives me this output:
答案1
得分: 4
Your push
routine is broken. The loop for (i = a; i; --i)
never executes the body when i
is zero, so it never executes stack[1] = stack[0]
. You need the test to be i >= 0
. (However, there is a better way to implement a stack without moving all the elements every time there is a push or pop. Also, we call removing an item from the top of a stack a “pop,” not a “pull.”)
英文:
Your push
routine is broken. The loop for (i = a; i; --i)
never executes the body when i
is zero, so it never executes stack[1] = stack[0]
. You need the test to be i >= 0
. (However, there is a better way to implement a stack without moving all the elements every time there is a push or pop. Also, we call removing an item from the top of a stack a “pop,” not a “pull.”)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论