问题描述
正如标题所述,我正在尝试编写一个程序,该程序会告诉用户输入的括号和/或圆括号是否正确嵌套。这是我的代码。
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 100
char contents[STACK_SIZE];
int top = 0;
void stack_overflow();
void stack_underflow();
void make_empty(void)
{
top = 0;
}
bool is_empty(void)
{
return top == 0;
}
bool is_full (void)
{
return top == STACK_SIZE;
}
void push(char i)
{
if (is_full())
stack_overflow();
else
contents[top++] = i;
}
char pop(void)
{
if (is_empty())
stack_underflow();
else
return contents[--top];
}
int main(void)
{
char input;
bool is_nested=true;
printf("Enter parentheses and/or braces: ");
while ((input = getchar()) != '\n')
{
if(input =='(' || input == '{')
push(input);
if(input ==')' && pop() != '(')
is_nested = false;
if(input =='}' && pop() != '{')
is_nested = false;
}
if (is_empty() == false) is_nested = false;
if (is_nested)
printf("Parentheses/braces are nested properly");
else
printf("Parentheses/braces are not nested properly");
return 0;
}
当我编译代码时,我不断收到错误:
C:\Users\g\AppData\Local\Temp\ccy6YhqI.o:brackets.c:(.text+0x45):对`stack_overflow'的未定义引用
C:\Users\g\AppData\Local\Temp\ccy6YhqI.o:brackets.c:(.text+0x76):对`stack_underflow'的未定义引用
collect2.exe:错误:ld 返回 1 个退出状态
我似乎无法找到我收到此错误的原因。任何帮助将不胜感激。
解决方法
显然你还没有实现 stack_overflow() 和 stack_underflow()。
void foo();不是一个充分的函数定义。