【无标题】

该程序实现了一个简单的括号匹配检查器,用于检测输入的括号和大括号是否正确嵌套。当用户输入一系列的左括号和右括号时,程序会检查它们是否按正确的顺序出现。如果括号匹配无误,程序将输出提示;否则,它会指出括号匹配错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

icoding 实验四 栈
#include <stdbool.h> /* C99 only */
#include <stdio.h>
#include <stdlib.h>

#define STACK_SIZE 100

/* external variables */
char contents[STACK_SIZE];
int top = 0;

void stack_overflow(void)
{
printf(“Stack overflow\n”);
exit(EXIT_FAILURE);
}

void stack_underflow(void)
{
printf(“Stack underflow\n”);
exit(EXIT_FAILURE);
}

void make_empty(void)
{
top = 0;
}

bool is_empty(void)
{
return top == 0;
}

bool is_full(void)
{
return top == STACK_SIZE;
}

void push(char ch)
{
if (is_full())
stack_overflow();
else
contents[top++] = ch;
}

char pop(void)
{
if (is_empty())
stack_underflow();
else
return contents[–top];

return ‘\0’; /* prevents compiler warning due to stack_underflow() call */
}

int main(void)
{
char ch,elem;
printf(“Enter parentheses and/or braces:”);
while((ch=getchar())!=’\n’){
if(ch==’(’||ch==’{’){
push(ch);//左右括号入栈
}
else if(ch==’)’||ch==’}’){
if(!is_empty()){
elem=pop();//出栈赋值
if(elem==’(’&&ch==’)’){
continue;
}
else if(ch==’}’&&elem==’{’){
continue;
}
else{
printf(“Parentheses/braces are NOT nested properly”);
break;
}
}
else{
printf(“Parentheses/braces are NOT nested properly”);
break;
}
}
}
if(is_empty()){
printf(“Parentheses/braces are nested properly”);
}
else{
printf(“Parentheses/braces are NOT nested properly”);
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值