Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用getchar()读取不带尾随换行符的文本_C_String_Stack_Newline_Getchar - Fatal编程技术网

使用getchar()读取不带尾随换行符的文本

使用getchar()读取不带尾随换行符的文本,c,string,stack,newline,getchar,C,String,Stack,Newline,Getchar,我正在编写一个程序,目标是阅读括号/方括号,并确定它们是否在每一侧都平衡,但在阅读最后一行文本时遇到了麻烦。这可能有点混乱,但我将附加以下输入: 3 <-- The number of lines I'll scan ([]) <-- Problem (([{}]))) ([()[]()])() 因此,我的程序输出yes或no,但在最后一个问题上冻结,等待我按newline完成循环。我假设问题出在while循环的某个地方,因为粘贴的文本没有最后的换行符

我正在编写一个程序,目标是阅读括号/方括号,并确定它们是否在每一侧都平衡,但在阅读最后一行文本时遇到了麻烦。这可能有点混乱,但我将附加以下输入:

3          <-- The number of lines I'll scan
([])       <-- Problem
(([{}])))
([()[]()])()

因此,我的程序输出yes或no,但在最后一个问题上冻结,等待我按newline完成循环。我假设问题出在while循环的某个地方,因为粘贴的文本没有最后的换行符,循环永远不会结束,但我不确定如何解决这个问题。谢谢您的帮助。

您是否在最后一行输入之后复制和粘贴回车符?此外,这不是您当前的问题,但只要您有
while(getchar()!=“x')
循环-对于x的任何值-您也需要检查
EOF
。@zwol据我所知,没有一个。。。至于EOF,是的,我知道,但我不认为它对这个特殊的程序有帮助,尽管我可能无论如何都应该包括它。我遇到的问题是,一个网站检查器给了我一个字符串,而它似乎没有尾随的换行符。当我提交问题时,我得到的反馈是我的平衡代码是正确的,但输出有一个“表示错误”,我假设这是指您需要手动按enter键才能得到最终答案。@auriskv--您应该经常检查
EOF
的一个原因是
getchar()
在发生错误时返回
EOF
,您希望避免在这种情况下出现无限循环。为了进一步帮助您,我需要查看整个程序,并且需要访问此“网站检查器”。这是家庭作业吗?如果是这样,你应该和你的老师谈谈。通常的习惯用法是:
intc;而((c=getchar())!='\n'&&c!=EOF){}
。您不想像在注释中的示例中那样调用两次
getchar()
#include <stdio.h>
#include <stdlib.h>


#ifndef status_h
#define status_h
enum status { SUCCESS, FAILURE };
typedef enum status Status;

enum boolean { TRUE, FALSE };
typedef enum boolean Boolean;

#endif

typedef void* NODE;

struct node {
    char data;
    struct node* next;
} *head;
typedef struct node Node;

void stack_init_default(Node* hStack);
int stack_empty(void);
char stack_top(Node* hStack);
NODE stack_pop(Node* hStack);
NODE stack_push(Node* hStack, char item);
void stack_destroy(void);

int main(int charc, char* argv[])
{
    char x;
    int num, error = 0, i;
    Node* stack;
    stack_init_default(stack);
    scanf("%d ", &num);
    for (i = 0; i < num; i++)
    {
        stack_destroy();
        error = 0;

    while ((x = getchar()) != '\n' )
    {
        if (x == ' ')
            continue;
        if ((x == '(')||(x == '[')||(x == '{'))
        {
            stack_push(stack, x);
        }
        else if ((x == ')')&&(stack_top(stack)=='('))
        {
            stack_pop(stack);
        }
        else if ((x == ']')&&(stack_top(stack)=='['))
        {
            stack_pop(stack);
        }
        else if ((x == '}')&&(stack_top(stack)=='{'))
        {
            stack_pop(stack);
        }
        else
        {
            error = 1;
        }
    }
            putchar('\n');
            if (stack_empty() == 0 || error == 1)
                {
                    printf("No");
                }
              else
            {
                printf("Yes");
            }
    }
    printf("\n");

    return 0;
}

void stack_init_default(Node* hStack)
{
     head = NULL;
}

NODE stack_push(Node* hStack, char item)
{
    Node* tmp = (Node*)malloc(sizeof(Node));
    if(tmp == NULL)
    {
        exit(0);
    }
    tmp->data = item;
    tmp->next = head;
    head = tmp;
    return head;
}

NODE stack_pop(Node* hStack)
{
    Node* tmp = head;
    head = head->next;
    free(tmp);
    return head;
}

char stack_top(Node* hStack)
{
    if (head == NULL)
    {
        return '\0';
    }
    return head->data;
}

int stack_empty(void)
{
    return (head == NULL);
}

 void stack_destroy(void)
{

    Node* phStack = head;

    while (head != NULL)
    {
        phStack = head->next;
        free(head);
        head = phStack;
    }
    free(phStack);
    head = NULL;

}
3
([])
(([{}])))
([()[]()])()
Yes
No

Yes