C语言!!!!

C语言!!!!刚了解到栈不知道咋做有点懵题目是修改下面的栈示例使它存储字符而不是整数。接下来,增加main函数,用来要求用户输入一串圆括号或花括号,然后指出它们之间的嵌套... C语言!!!!刚了解到栈 不知道咋做 有点懵
题目是修改下面的栈示例使它存储字符而不是整数。接下来,增加main函数,用来要求用户输入一串圆括号或花括号,然后指出它们之间的嵌套是否正确:
Enter parenteses and/or braces:((){}{()})
Parenteses/braces are nested properly
提示:读入左圆括号或左花括号时,把它们像字符一样压入栈中。当读入右圆括号或右花括号时,把栈顶的项弹出,并且检查弹出项是否是匹配的圆括号或花括号。(如果不是,那么圆括号或花括号嵌套不正确。)当程序读入换行符时,检查栈是否为空。如果为空,那么圆括号或花括号匹配;如果栈不为空(或者如果曾经调用过stack_underflow函数),那么圆括号或花括号不匹配。如果调用stack_underflow函数,程序显示信息Stack overflow,并且立刻终止。
下面是栈示例:
#include <stdbool.h>
#define STACK_SIZE 100

int contents[STACK_SIZE];
int top = 0;

void make_empty (void)
{
top = 0;
}
bool is_empty (void)
{
return top == 0;
}
bool is_full (void)
{
return top == STACK_SIZE;
}
void push (int i)
{
is (is_full))
stack_overflow();
else
contents[top++] = i;
}
int pop (void)
{
if (is_empty ())
stack_underflow ();
else
return contents[--top];
}
展开
 我来答
闽忠饶俏
2020-04-15 · TA获得超过3.7万个赞
知道大有可为答主
回答量:1.2万
采纳率:32%
帮助的人:756万
展开全部
一楼正确。
第二道题的A
如果操作的数是常量比如1,就不能用++,--对其进行操作了。
如果定义了一些不能变的数也就不能用了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wenqing8585
2018-04-10 · TA获得超过8252个赞
知道大有可为答主
回答量:8078
采纳率:70%
帮助的人:3789万
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define STACK_SIZE 100
char contents[STACK_SIZE];
int top = 0;
void make_empty(void)
{
top = 0;
}
bool is_empty(void)
{
return top == 0;
}
bool is_full(void)
{
return top == STACK_SIZE;
}
void stack_overflow(void)
{
printf("Stack overflow");
exit(1);
}
char stack_underflow(void)
{
printf("Stack underflow");
return 0;
}
void push(char i)
{
if(is_full())
stack_overflow();
else
contents[top++] = i;
}
char pop(void)
{
if (is_empty())
return stack_underflow();
else
return contents[--top];
}
int main()
{
char ch;
while (true) {
ch = getchar();
if (ch == '(' || ch == '{') {
push(ch);
}
else if (ch == ')') {
if (pop() != '(') {
printf("Parenteses/braces are not nested properly! ");
break;
}
}
else if (ch == '}') {
if (pop() != '{') {
printf("Parenteses/braces are not nested properly! ");
break;
}
}
else if (ch == '\n') {
if (is_empty()) {
printf("Parenteses/braces are nested properly! ");
}
else{
printf("Parenteses/braces are not nested properly! ");

}
break;
}
}
return 0;
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
一变班爱麻啊堡0J
高粉答主

2020-01-13 · 醉心答题,欢迎关注
知道答主
回答量:11.4万
采纳率:2%
帮助的人:5452万
展开全部
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式