[英]Trying to write a program that ask the user to input parentheses and brackets. The program will then tell the users whether they are properly nested
正如標題所述,我正在嘗試編寫一個程序來告訴用戶輸入的括號和/或括號是否正確嵌套。 這是我的代碼。
#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): undefined reference to `stack_overflow'
C:\Users\g\AppData\Local\Temp\ccy6YhqI.o:brackets.c:(.text+0x76): undefined reference to `stack_underflow'
collect2.exe:錯誤:ld 返回 1 退出狀態
我似乎無法找到我收到此錯誤的原因。 任何幫助,將不勝感激。
顯然你還沒有實現 stack_overflow() 和 stack_underflow()。
無效的富(); 不是一個足夠的 function 定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.