K&R练习1-24

huangapple go评论65阅读模式
英文:

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;
    }
}

当我给它输入{}( ) () {} []时,它可以正常工作,但如果输入(()),它会给出以下输出:

K&R练习1-24

英文:

> 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 &lt;stdio.h&gt;
#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 &amp;&amp; keep == YES)
{
if (c == &#39;[&#39; || c == &#39;(&#39; || c == &#39;{&#39;)
push(stack, c);
else if (c == &#39;]&#39; || c == &#39;)&#39; || c == &#39;}&#39;)
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 &lt; a; ++i)
stack[i] = stack[i+1];
stack[a] = &#39;\0&#39;;
}
int check(int stack[], int c)
{
if ((c == &#39;]&#39; &amp;&amp; stack[0] == &#39;[&#39;) || (c == &#39;)&#39; &amp;&amp; stack[0] == &#39;(&#39;) || (c == &#39;}&#39; &amp;&amp; stack[0] == &#39;{&#39;))
{
pull(stack);
return YES;
}
else
{
printf(&quot;Mismatched character: &#39;%c&#39;\n&quot;, c);
return NO;
}
}

when I give it an output like {} or ( ) () {} [] it works but with something like (()) it gives me this output:

K&R练习1-24

答案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 &gt;= 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.”)

huangapple
  • 本文由 发表于 2023年8月4日 23:18:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定