栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【无标题】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【无标题】

icoding 实验四 栈
#include
#include
#include

#define STACK_SIZE 100


char contents[STACK_SIZE];
int top = 0;

void stack_overflow(void)
{
printf(“Stack overflown”);
exit(EXIT_FAILURE);
}

void stack_underflow(void)
{
printf(“Stack underflown”);
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 ‘’;
}

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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/656881.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号