为什么我的代码可以在 Dev C++ 中运行,但不能在 VS Code 中运行?

问题描述 投票:0回答:0

这是一段使用栈来平衡括号的代码。但它在 VS 代码中不起作用。请原谅拼写错误 :).

#include<stdio.h>
#include<string.h>
#define MAX_SIZE 100

struct stack{
char a[MAX_SIZE];
int top;
};

typedef struct stack stk;

void push(stk *s, char n)
{
  if(s->top == MAX_SIZE-1)
    printf("Stack Overflow\n");
  else
    s->a[++s->top] = n;
}

int pop(stk *s)
{
  if(s->top < 0)
  {
    printf("Stack Underflow");
    return -1;
  }
  else
  {
    return s->a[s->top--];
  }
}

int top(stk *s)
{
  if(s->top < 0)
  {
    printf("Stack Underflow\n");
    return -1;
  }
  else
  {
    return s->a[s->top];
  }
}

int isEmptyStack(stk *s)
{
  if(s->top < 0)
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

int isFullStack(stk *s)
{
  if(s->top == MAX_SIZE - 1)
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

int size(stk *s)
{
  return s->top;
}

int main()
{
  stk s;
  char a[10];
  int flag = 0;
  
  printf("Enter an expression to check for paranthesis balance: ");
  scanf("%s", a);

  for(int i=0;i<strlen(a);i++)
    {
      if(a[i] == '(' || a[i] == '{' || a[i] == '[')
        push(&s, a[i]);
      else if(a[i] == ')' || a[i] == '}' || a[i] == ']')
      {
        if((isEmptyStack(&s) == 1) || ((a[i] == ')' && s.a[s.top] == '(') || (a[i] == '}' && s.a[s.top] == '{') || (a[i] == ']' && s.a[s.top] == '[')))
        {
          flag = 1;
          pop(&s);
        }
      }
    }
  if(flag == 0)
    printf("The parantheses are not balanced.\n");
  else
    printf("The parantheses are balanced.\n");printf("%d", flag);
  return 0;
}

输出(开发 C++):

Enter an expression to check for paranthesis balance: {[]}
The parantheses are balanced.

--------------------------------
Process exited after 12.89 seconds with return value 0
Press any key to continue . . .

输出(VS代码):

PS C:\Users\chink\Downloads\Documents\C in Dev++> cd "c:\Users\chink\Downloads\Documents\C in Dev++\" ; if ($?) { gcc -std=c11 par.c -o par } ; if ($?) { .\par }
Enter an expression to check for paranthesis balance: {[]}
PS C:\Users\chink\Downloads\Documents\C in Dev++> 

我在 VS 代码上运行了代码,希望它能运行,但它只是打印扫描数组的提示,仅此而已。它没有显示任何其他内容。

c
© www.soinside.com 2019 - 2024. All rights reserved.